Browse Source

neon: Fix note editing for real

pull/122/head
jld3103 2 years ago
parent
commit
b31ebe08d0
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 24
      packages/neon/lib/src/apps/notes/blocs/note.dart
  2. 16
      packages/neon/lib/src/apps/notes/blocs/note.rxb.g.dart
  3. 47
      packages/neon/lib/src/apps/notes/pages/note.dart

24
packages/neon/lib/src/apps/notes/blocs/note.dart

@ -19,10 +19,6 @@ abstract class NotesNoteBlocEvents {
} }
abstract class NotesNoteBlocStates { abstract class NotesNoteBlocStates {
BehaviorSubject<String> get content;
BehaviorSubject<String> get title;
BehaviorSubject<String> get category; BehaviorSubject<String> get category;
Stream<Exception> get errors; Stream<Exception> get errors;
@ -67,10 +63,10 @@ class NotesNoteBloc extends $NotesNoteBloc {
); );
}); });
_contentSubject.add(note.content);
_titleSubject.add(note.title);
_emitNote(note); _emitNote(note);
id = note.id; id = note.id;
initialContent = note.content;
initialTitle = note.title;
} }
void _emitNote(final NotesNote note) { void _emitNote(final NotesNote note) {
@ -96,30 +92,22 @@ class NotesNoteBloc extends $NotesNoteBloc {
final _updateQueue = Queue(); final _updateQueue = Queue();
late final int id; late final int id;
late final String initialContent;
late final String initialTitle;
late String _etag; late String _etag;
final _contentSubject = BehaviorSubject<String>();
final _titleSubject = BehaviorSubject<String>();
final _categorySubject = BehaviorSubject<String>(); final _categorySubject = BehaviorSubject<String>();
final _errorsStreamController = StreamController<Exception>(); final _errorsStreamController = StreamController<Exception>();
@override @override
void dispose() { void dispose() {
unawaited(_contentSubject.close());
unawaited(_titleSubject.close());
unawaited(_categorySubject.close()); unawaited(_categorySubject.close());
unawaited(_errorsStreamController.close()); unawaited(_errorsStreamController.close());
super.dispose(); super.dispose();
} }
@override @override
Stream<Exception> _mapToErrorsState() => _errorsStreamController.stream.asBroadcastStream(); BehaviorSubject<String> _mapToCategoryState() => _categorySubject;
@override
BehaviorSubject<String> _mapToContentState() => _contentSubject;
@override
BehaviorSubject<String> _mapToTitleState() => _titleSubject;
@override @override
BehaviorSubject<String> _mapToCategoryState() => _categorySubject; Stream<Exception> _mapToErrorsState() => _errorsStreamController.stream.asBroadcastStream();
} }

16
packages/neon/lib/src/apps/notes/blocs/note.rxb.g.dart

@ -28,12 +28,6 @@ abstract class $NotesNoteBloc extends RxBlocBase
/// Тhe [Subject] where events sink to by calling [updateCategory] /// Тhe [Subject] where events sink to by calling [updateCategory]
final _$updateCategoryEvent = PublishSubject<String>(); final _$updateCategoryEvent = PublishSubject<String>();
/// The state of [content] implemented in [_mapToContentState]
late final BehaviorSubject<String> _contentState = _mapToContentState();
/// The state of [title] implemented in [_mapToTitleState]
late final BehaviorSubject<String> _titleState = _mapToTitleState();
/// The state of [category] implemented in [_mapToCategoryState] /// The state of [category] implemented in [_mapToCategoryState]
late final BehaviorSubject<String> _categoryState = _mapToCategoryState(); late final BehaviorSubject<String> _categoryState = _mapToCategoryState();
@ -49,22 +43,12 @@ abstract class $NotesNoteBloc extends RxBlocBase
@override @override
void updateCategory(String category) => _$updateCategoryEvent.add(category); void updateCategory(String category) => _$updateCategoryEvent.add(category);
@override
BehaviorSubject<String> get content => _contentState;
@override
BehaviorSubject<String> get title => _titleState;
@override @override
BehaviorSubject<String> get category => _categoryState; BehaviorSubject<String> get category => _categoryState;
@override @override
Stream<Exception> get errors => _errorsState; Stream<Exception> get errors => _errorsState;
BehaviorSubject<String> _mapToContentState();
BehaviorSubject<String> _mapToTitleState();
BehaviorSubject<String> _mapToCategoryState(); BehaviorSubject<String> _mapToCategoryState();
Stream<Exception> _mapToErrorsState(); Stream<Exception> _mapToErrorsState();

47
packages/neon/lib/src/apps/notes/pages/note.dart

@ -15,8 +15,8 @@ class NotesNotePage extends StatefulWidget {
} }
class _NotesNotePageState extends State<NotesNotePage> { class _NotesNotePageState extends State<NotesNotePage> {
late final _contentController = TextEditingController(); late final _contentController = TextEditingController()..text = widget.bloc.initialContent;
late final _titleController = TextEditingController(); late final _titleController = TextEditingController()..text = widget.bloc.initialTitle;
final _contentFocusNode = FocusNode(); final _contentFocusNode = FocusNode();
final _titleFocusNode = FocusNode(); final _titleFocusNode = FocusNode();
bool _showEditor = false; bool _showEditor = false;
@ -34,41 +34,15 @@ class _NotesNotePageState extends State<NotesNotePage> {
handleNotesException(context, error); handleNotesException(context, error);
}); });
widget.bloc.content.listen((final content) { _contentController.addListener(() => widget.bloc.updateContent(_contentController.text));
if (_contentController.text != content) { _titleController.addListener(() => widget.bloc.updateTitle(_titleController.text));
final selection = _contentController.selection;
_contentController
..text = content
..selection = selection;
}
});
widget.bloc.title.listen((final title) {
if (_titleController.text != title) {
final selection = _titleController.selection;
_titleController
..text = title
..selection = selection;
}
});
_contentController.addListener(() async {
if (await widget.bloc.content.first != _contentController.text) {
widget.bloc.updateContent(_contentController.text);
}
});
_titleController.addListener(() async {
if (await widget.bloc.title.first != _titleController.text) {
widget.bloc.updateTitle(_titleController.text);
}
});
WidgetsBinding.instance.addPostFrameCallback((final _) async { WidgetsBinding.instance.addPostFrameCallback((final _) async {
if (Provider.of<NeonPlatform>(context, listen: false).canUseWakelock) { if (Provider.of<NeonPlatform>(context, listen: false).canUseWakelock) {
await Wakelock.enable(); await Wakelock.enable();
} }
if (widget.bloc.options.defaultNoteViewTypeOption.value == DefaultNoteViewType.edit || if (widget.bloc.options.defaultNoteViewTypeOption.value == DefaultNoteViewType.edit ||
(await widget.bloc.content.first).isEmpty) { widget.bloc.initialContent.isEmpty) {
setState(() { setState(() {
_showEditor = true; _showEditor = true;
}); });
@ -171,13 +145,8 @@ class _NotesNotePageState extends State<NotesNotePage> {
border: InputBorder.none, border: InputBorder.none,
), ),
) )
: RxBlocBuilder( : MarkdownBody(
bloc: widget.bloc, data: _contentController.text,
state: (final bloc) => bloc.content,
builder: (final context, final contentSnapshot, final _) {
final content = contentSnapshot.data ?? '';
return MarkdownBody(
data: content,
onTapLink: (final text, final href, final title) async { onTapLink: (final text, final href, final title) async {
if (href != null) { if (href != null) {
await launchUrlString( await launchUrlString(
@ -186,8 +155,6 @@ class _NotesNotePageState extends State<NotesNotePage> {
); );
} }
}, },
);
},
), ),
), ),
), ),

Loading…
Cancel
Save