You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.2 KiB
103 lines
2.2 KiB
2 years ago
|
part of '../neon_notes.dart';
|
||
2 years ago
|
|
||
|
abstract class NotesNoteBlocEvents {
|
||
2 years ago
|
void updateContent(final String content);
|
||
|
|
||
|
void updateTitle(final String title);
|
||
|
|
||
|
void updateCategory(final String category);
|
||
2 years ago
|
}
|
||
|
|
||
|
abstract class NotesNoteBlocStates {
|
||
|
BehaviorSubject<String> get category;
|
||
|
}
|
||
|
|
||
2 years ago
|
class NotesNoteBloc extends InteractiveBloc implements NotesNoteBlocEvents, NotesNoteBlocStates {
|
||
2 years ago
|
NotesNoteBloc(
|
||
|
this.options,
|
||
|
this._client,
|
||
|
this._notesBloc,
|
||
2 years ago
|
final NextcloudNotesNote note,
|
||
2 years ago
|
) {
|
||
|
_emitNote(note);
|
||
|
id = note.id;
|
||
2 years ago
|
initialContent = note.content;
|
||
|
initialTitle = note.title;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
void _emitNote(final NextcloudNotesNote note) {
|
||
2 years ago
|
category.add(note.category);
|
||
2 years ago
|
_etag = note.etag;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
// ignore: avoid_void_async
|
||
2 years ago
|
void _wrapAction(final Future<NextcloudNotesNote> Function(String etag) call) async {
|
||
2 years ago
|
await _updateQueue.add(() async {
|
||
|
try {
|
||
|
final data = await call(_etag);
|
||
|
_emitNote(data);
|
||
|
await _notesBloc.refresh();
|
||
|
} catch (e, s) {
|
||
|
debugPrint(e.toString());
|
||
|
debugPrint(s.toString());
|
||
|
addError(e);
|
||
|
}
|
||
|
});
|
||
2 years ago
|
}
|
||
|
|
||
|
final NotesAppSpecificOptions options;
|
||
|
final NextcloudClient _client;
|
||
|
final NotesBloc _notesBloc;
|
||
2 years ago
|
final _updateQueue = Queue();
|
||
2 years ago
|
|
||
|
late final int id;
|
||
2 years ago
|
late final String initialContent;
|
||
|
late final String initialTitle;
|
||
2 years ago
|
late String _etag;
|
||
2 years ago
|
|
||
|
@override
|
||
|
void dispose() {
|
||
2 years ago
|
unawaited(category.close());
|
||
2 years ago
|
super.dispose();
|
||
|
}
|
||
|
|
||
|
@override
|
||
2 years ago
|
BehaviorSubject<String> category = BehaviorSubject<String>();
|
||
|
|
||
|
@override
|
||
|
Future refresh() async {}
|
||
|
|
||
|
@override
|
||
|
void updateCategory(final String category) {
|
||
|
_wrapAction(
|
||
|
(final etag) async => _client.notes.updateNote(
|
||
|
id: id,
|
||
|
category: category,
|
||
|
ifMatch: '"$etag"',
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void updateContent(final String content) {
|
||
|
_wrapAction(
|
||
|
(final etag) async => _client.notes.updateNote(
|
||
|
id: id,
|
||
|
content: content,
|
||
|
ifMatch: '"$etag"',
|
||
|
),
|
||
|
);
|
||
|
}
|
||
2 years ago
|
|
||
|
@override
|
||
2 years ago
|
void updateTitle(final String title) {
|
||
|
_wrapAction(
|
||
|
(final etag) async => _client.notes.updateNote(
|
||
|
id: id,
|
||
|
title: title,
|
||
|
ifMatch: '"$etag"',
|
||
|
),
|
||
|
);
|
||
|
}
|
||
2 years ago
|
}
|