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 {
BehaviorSubject<String> get content;
BehaviorSubject<String> get title;
BehaviorSubject<String> get category;
Stream<Exception> get errors;
@ -67,10 +63,10 @@ class NotesNoteBloc extends $NotesNoteBloc {
);
});
_contentSubject.add(note.content);
_titleSubject.add(note.title);
_emitNote(note);
id = note.id;
initialContent = note.content;
initialTitle = note.title;
}
void _emitNote(final NotesNote note) {
@ -96,30 +92,22 @@ class NotesNoteBloc extends $NotesNoteBloc {
final _updateQueue = Queue();
late final int id;
late final String initialContent;
late final String initialTitle;
late String _etag;
final _contentSubject = BehaviorSubject<String>();
final _titleSubject = BehaviorSubject<String>();
final _categorySubject = BehaviorSubject<String>();
final _errorsStreamController = StreamController<Exception>();
@override
void dispose() {
unawaited(_contentSubject.close());
unawaited(_titleSubject.close());
unawaited(_categorySubject.close());
unawaited(_errorsStreamController.close());
super.dispose();
}
@override
Stream<Exception> _mapToErrorsState() => _errorsStreamController.stream.asBroadcastStream();
@override
BehaviorSubject<String> _mapToContentState() => _contentSubject;
@override
BehaviorSubject<String> _mapToTitleState() => _titleSubject;
BehaviorSubject<String> _mapToCategoryState() => _categorySubject;
@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]
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]
late final BehaviorSubject<String> _categoryState = _mapToCategoryState();
@ -49,22 +43,12 @@ abstract class $NotesNoteBloc extends RxBlocBase
@override
void updateCategory(String category) => _$updateCategoryEvent.add(category);
@override
BehaviorSubject<String> get content => _contentState;
@override
BehaviorSubject<String> get title => _titleState;
@override
BehaviorSubject<String> get category => _categoryState;
@override
Stream<Exception> get errors => _errorsState;
BehaviorSubject<String> _mapToContentState();
BehaviorSubject<String> _mapToTitleState();
BehaviorSubject<String> _mapToCategoryState();
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> {
late final _contentController = TextEditingController();
late final _titleController = TextEditingController();
late final _contentController = TextEditingController()..text = widget.bloc.initialContent;
late final _titleController = TextEditingController()..text = widget.bloc.initialTitle;
final _contentFocusNode = FocusNode();
final _titleFocusNode = FocusNode();
bool _showEditor = false;
@ -34,41 +34,15 @@ class _NotesNotePageState extends State<NotesNotePage> {
handleNotesException(context, error);
});
widget.bloc.content.listen((final content) {
if (_contentController.text != content) {
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);
}
});
_contentController.addListener(() => widget.bloc.updateContent(_contentController.text));
_titleController.addListener(() => widget.bloc.updateTitle(_titleController.text));
WidgetsBinding.instance.addPostFrameCallback((final _) async {
if (Provider.of<NeonPlatform>(context, listen: false).canUseWakelock) {
await Wakelock.enable();
}
if (widget.bloc.options.defaultNoteViewTypeOption.value == DefaultNoteViewType.edit ||
(await widget.bloc.content.first).isEmpty) {
widget.bloc.initialContent.isEmpty) {
setState(() {
_showEditor = true;
});
@ -171,13 +145,8 @@ class _NotesNotePageState extends State<NotesNotePage> {
border: InputBorder.none,
),
)
: RxBlocBuilder(
bloc: widget.bloc,
state: (final bloc) => bloc.content,
builder: (final context, final contentSnapshot, final _) {
final content = contentSnapshot.data ?? '';
return MarkdownBody(
data: content,
: MarkdownBody(
data: _contentController.text,
onTapLink: (final text, final href, final title) async {
if (href != null) {
await launchUrlString(
@ -186,8 +155,6 @@ class _NotesNotePageState extends State<NotesNotePage> {
);
}
},
);
},
),
),
),

Loading…
Cancel
Save