add comment drafts
This commit is contained in:
parent
eacbda0b5d
commit
5f23176e6e
|
@ -1,6 +1,7 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hive_flutter/adapters.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
@ -19,6 +20,7 @@ Future<void> mainCommon(AppConfig appConfig) async {
|
|||
|
||||
final logConsoleStore = LogConsolePageStore();
|
||||
final sharedPrefs = await SharedPreferences.getInstance();
|
||||
await Hive.initFlutter();
|
||||
|
||||
_setupLogger(appConfig, logConsoleStore);
|
||||
_setupTimeago();
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
import 'package:hive/hive.dart';
|
||||
|
||||
class CommentDraftStore {
|
||||
static const _boxKey = 'comment_drafts';
|
||||
|
||||
static Future<String?> loadDraft(String apId) async {
|
||||
final box = await Hive.openBox<String>(_boxKey);
|
||||
final text = box.get(apId);
|
||||
await box.close();
|
||||
return text;
|
||||
}
|
||||
|
||||
static Future<void> saveDraft(String apId, String text) async {
|
||||
final box = await Hive.openBox<String>(_boxKey);
|
||||
await box.put(apId, text);
|
||||
await box.close();
|
||||
}
|
||||
|
||||
static Future<void> removeDraft(String apId) async {
|
||||
final box = await Hive.openBox<String>(_boxKey);
|
||||
await box.delete(apId);
|
||||
await box.close();
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ import 'package:lemmy_api_client/v3.dart';
|
|||
import '../hooks/delayed_loading.dart';
|
||||
import '../hooks/logged_in_action.dart';
|
||||
import '../l10n/l10n.dart';
|
||||
import '../stores/comment_drafts_store.dart';
|
||||
import 'editor/editor.dart';
|
||||
import 'markdown_mode_icon.dart';
|
||||
import 'markdown_text.dart';
|
||||
|
@ -40,6 +41,20 @@ class WriteComment extends HookWidget {
|
|||
text: _isEdit ? comment?.content : null,
|
||||
);
|
||||
|
||||
// load draft if exists
|
||||
useEffect(() {
|
||||
() async {
|
||||
if (_isEdit) return;
|
||||
|
||||
final previousDraft =
|
||||
await CommentDraftStore.loadDraft(comment?.apId ?? post.apId);
|
||||
if (previousDraft != null) {
|
||||
editorController.textEditingController.text = previousDraft;
|
||||
}
|
||||
}();
|
||||
return null;
|
||||
}, []);
|
||||
|
||||
final preview = () {
|
||||
final body = () {
|
||||
final text = comment?.content ?? post.body;
|
||||
|
@ -84,6 +99,10 @@ class WriteComment extends HookWidget {
|
|||
));
|
||||
}
|
||||
}();
|
||||
|
||||
// remove draft because it's not needed anymore
|
||||
await CommentDraftStore.removeDraft(comment?.apId ?? post.apId);
|
||||
|
||||
Navigator.of(context).pop(res.commentView);
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
|
@ -94,7 +113,17 @@ class WriteComment extends HookWidget {
|
|||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: const CloseButton(),
|
||||
leading: CloseButton(
|
||||
onPressed: () async {
|
||||
// save draft before closing
|
||||
if (!_isEdit &&
|
||||
editorController.textEditingController.text.trim().isNotEmpty) {
|
||||
await CommentDraftStore.saveDraft(comment?.apId ?? post.apId,
|
||||
editorController.textEditingController.text);
|
||||
}
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: markdownModeIcon(fancy: showFancy.value),
|
||||
|
|
21
pubspec.lock
21
pubspec.lock
|
@ -350,6 +350,27 @@ packages:
|
|||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.0"
|
||||
hive:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: hive
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.2.3"
|
||||
hive_flutter:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: hive_flutter
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.0"
|
||||
hive_generator:
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
name: hive_generator
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.3"
|
||||
http:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
|
@ -43,7 +43,8 @@ dependencies:
|
|||
provider: ^6.0.0
|
||||
mobx: ^2.0.4
|
||||
flutter_mobx: ^2.0.2
|
||||
|
||||
hive: ^2.2.3
|
||||
hive_flutter: ^1.1.0
|
||||
# utils
|
||||
timeago: ^3.0.2
|
||||
fuzzy: ^0.4.0-nullsafety.0
|
||||
|
@ -71,6 +72,7 @@ dev_dependencies:
|
|||
build_runner: ^2.1.2
|
||||
mobx_codegen: ^2.0.2
|
||||
freezed: ^2.0.2
|
||||
hive_generator: ^1.1.3
|
||||
|
||||
flutter_icons:
|
||||
android: true
|
||||
|
|
Loading…
Reference in New Issue