✨ Post content local cache
This commit is contained in:
@ -1,12 +1,19 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:get/get_rx/get_rx.dart';
|
||||
import 'package:solian/models/post.dart';
|
||||
import 'package:solian/models/realm.dart';
|
||||
import 'package:solian/widgets/attachments/attachment_publish.dart';
|
||||
import 'package:solian/widgets/posts/editor/post_editor_overview.dart';
|
||||
import 'package:textfield_tags/textfield_tags.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class PostEditorController extends GetxController {
|
||||
late final SharedPreferences _prefs;
|
||||
|
||||
final titleController = TextEditingController();
|
||||
final descriptionController = TextEditingController();
|
||||
final contentController = TextEditingController();
|
||||
@ -23,7 +30,28 @@ class PostEditorController extends GetxController {
|
||||
|
||||
RxBool isDraft = false.obs;
|
||||
|
||||
RxBool isRestoreFromLocal = false.obs;
|
||||
Rx<DateTime?> lastSaveTime = Rx(null);
|
||||
Timer? _saveTimer;
|
||||
|
||||
PostEditorController() {
|
||||
SharedPreferences.getInstance().then((inst) {
|
||||
_prefs = inst;
|
||||
localRead();
|
||||
_saveTimer = Timer.periodic(
|
||||
const Duration(seconds: 3),
|
||||
(Timer t) {
|
||||
if (isNotEmpty) {
|
||||
localSave();
|
||||
lastSaveTime.value = DateTime.now();
|
||||
lastSaveTime.refresh();
|
||||
} else if (_prefs.containsKey('post_editor_local_save')) {
|
||||
localClear();
|
||||
lastSaveTime.value = null;
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
contentController.addListener(() {
|
||||
contentLength.value = contentController.text.length;
|
||||
});
|
||||
@ -57,6 +85,46 @@ class PostEditorController extends GetxController {
|
||||
isDraft.value = !isDraft.value;
|
||||
}
|
||||
|
||||
void localSave() {
|
||||
_prefs.setString(
|
||||
'post_editor_local_save',
|
||||
jsonEncode({
|
||||
...payload,
|
||||
'reply_to': replyTo.value?.toJson(),
|
||||
'repost_to': repostTo.value?.toJson(),
|
||||
'edit_to': editTo.value?.toJson(),
|
||||
'realm': realmZone.value?.toJson(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
void localRead() {
|
||||
if (_prefs.containsKey('post_editor_local_save')) {
|
||||
isRestoreFromLocal.value = true;
|
||||
payload = jsonDecode(_prefs.getString('post_editor_local_save')!);
|
||||
}
|
||||
}
|
||||
|
||||
void localClear() {
|
||||
_prefs.remove('post_editor_local_save');
|
||||
}
|
||||
|
||||
void currentClear() {
|
||||
titleController.clear();
|
||||
descriptionController.clear();
|
||||
contentController.clear();
|
||||
tagController.clearTags();
|
||||
attachments.clear();
|
||||
isDraft.value = false;
|
||||
isRestoreFromLocal.value = false;
|
||||
lastSaveTime.value = null;
|
||||
contentLength.value = 0;
|
||||
editTo.value = null;
|
||||
replyTo.value = null;
|
||||
repostTo.value = null;
|
||||
realmZone.value = null;
|
||||
}
|
||||
|
||||
set editTarget(Post? value) {
|
||||
if (value == null) {
|
||||
editTo.value = null;
|
||||
@ -99,14 +167,46 @@ class PostEditorController extends GetxController {
|
||||
};
|
||||
}
|
||||
|
||||
set payload(Map<String, dynamic> value) {
|
||||
titleController.text = value['title'] ?? '';
|
||||
descriptionController.text = value['description'] ?? '';
|
||||
contentController.text = value['content'] ?? '';
|
||||
attachments.value = value['attachments'].cast<int>() ?? List.empty();
|
||||
attachments.refresh();
|
||||
isDraft.value = value['is_draft'];
|
||||
if (value['reply_to'] != null) {
|
||||
replyTo.value = Post.fromJson(value['reply_to']);
|
||||
}
|
||||
if (value['repost_to'] != null) {
|
||||
repostTo.value = Post.fromJson(value['repost_to']);
|
||||
}
|
||||
if (value['edit_to'] != null) {
|
||||
editTo.value = Post.fromJson(value['edit_to']);
|
||||
}
|
||||
if (value['realm'] != null) {
|
||||
realmZone.value = Realm.fromJson(value['realm']);
|
||||
}
|
||||
}
|
||||
|
||||
bool get isEmpty {
|
||||
if (contentController.text.isEmpty) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool get isNotEmpty {
|
||||
return [
|
||||
titleController.text.isNotEmpty,
|
||||
descriptionController.text.isNotEmpty,
|
||||
contentController.text.isNotEmpty,
|
||||
attachments.isNotEmpty,
|
||||
tagController.getTags?.isNotEmpty ?? false,
|
||||
].any((x) => x);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_saveTimer?.cancel();
|
||||
|
||||
contentController.dispose();
|
||||
tagController.dispose();
|
||||
super.dispose();
|
||||
|
@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_animate/flutter_animate.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:solian/controllers/post_editor_controller.dart';
|
||||
import 'package:solian/exts.dart';
|
||||
import 'package:solian/models/post.dart';
|
||||
@ -70,6 +71,7 @@ class _PostPublishScreenState extends State<PostPublishScreen> {
|
||||
if (resp.statusCode != 200) {
|
||||
context.showErrorDialog(resp.bodyString);
|
||||
} else {
|
||||
_editorController.localClear();
|
||||
AppRouter.instance.pop(resp.body);
|
||||
}
|
||||
|
||||
@ -234,11 +236,63 @@ class _PostPublishScreenState extends State<PostPublishScreen> {
|
||||
),
|
||||
),
|
||||
Material(
|
||||
elevation: 8,
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Obx(() {
|
||||
final textStyle = TextStyle(
|
||||
fontSize: 12,
|
||||
color: Theme.of(context)
|
||||
.colorScheme
|
||||
.onSurface
|
||||
.withOpacity(0.75),
|
||||
);
|
||||
final showFactors = [
|
||||
_editorController.isRestoreFromLocal.value,
|
||||
_editorController.lastSaveTime.value != null,
|
||||
];
|
||||
final doShow = showFactors.any((x) => x);
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 4,
|
||||
horizontal: 16,
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
if (showFactors[0])
|
||||
Text('postRestoreFromLocal'.tr, style: textStyle)
|
||||
.paddingOnly(right: 4),
|
||||
if (showFactors[0])
|
||||
InkWell(
|
||||
child: Text('clear'.tr, style: textStyle),
|
||||
onTap: () {
|
||||
_editorController.localClear();
|
||||
_editorController.currentClear();
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
if (showFactors.where((x) => x).length > 1)
|
||||
Text(
|
||||
'·',
|
||||
style: textStyle,
|
||||
).paddingSymmetric(horizontal: 8),
|
||||
if (showFactors[1])
|
||||
Text(
|
||||
'postAutoSaveAt'.trParams({
|
||||
'date': DateFormat('HH:mm:ss').format(
|
||||
_editorController.lastSaveTime.value ??
|
||||
DateTime.now(),
|
||||
)
|
||||
}),
|
||||
style: textStyle,
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
.animate(target: doShow ? 1 : 0)
|
||||
.fade(curve: Curves.easeInOut, duration: 300.ms);
|
||||
}),
|
||||
if (_editorController.mode.value == 0)
|
||||
Obx(
|
||||
() => TweenAnimationBuilder<double>(
|
||||
|
@ -93,8 +93,11 @@ const i18nEnglish = {
|
||||
'totalPostCount': 'Posts',
|
||||
'totalUpvote': 'Upvote',
|
||||
'totalDownvote': 'Downvote',
|
||||
'clear': 'Clear',
|
||||
'pinPost': 'Pin this post',
|
||||
'unpinPost': 'Unpin this post',
|
||||
'postRestoreFromLocal': 'Restore from local',
|
||||
'postAutoSaveAt': 'Auto saved at @date',
|
||||
'postOverview': 'Overview',
|
||||
'postPinned': 'Pinned',
|
||||
'postListNews': 'News',
|
||||
|
@ -87,8 +87,11 @@ const i18nSimplifiedChinese = {
|
||||
'totalPostCount': '总帖数',
|
||||
'totalUpvote': '获顶数',
|
||||
'totalDownvote': '获踩数',
|
||||
'clear': '清除',
|
||||
'pinPost': '置顶本帖',
|
||||
'unpinPost': '取消置顶本帖',
|
||||
'postRestoreFromLocal': '内容从本地暂存回复',
|
||||
'postAutoSaveAt': '已自动保存于 @date',
|
||||
'postOverview': '帖子概览',
|
||||
'postPinned': '已置顶',
|
||||
'postEditorModeStory': '发个帖子',
|
||||
|
Reference in New Issue
Block a user