Fixed missing loading spinner due to seeded streams in note repository.

This commit is contained in:
Dennis Nemec
2026-01-08 15:59:45 +01:00
parent 6eafb0fab0
commit b481a45afc
4 changed files with 63 additions and 58 deletions

View File

@ -7,22 +7,24 @@ import 'package:rxdart/rxdart.dart';
class NoteRepository {
final NoteService service;
final _notesStream = BehaviorSubject<List<Note>>.seeded([]);
final _imageNoteStream = BehaviorSubject<List<ImageNote>>.seeded([]);
final _noteTemplateStream = BehaviorSubject<List<NoteTemplate>>.seeded([]);
final _notesStream = BehaviorSubject<List<Note>?>.seeded(null);
final _imageNoteStream = BehaviorSubject<List<ImageNote>?>.seeded(null);
final _noteTemplateStream = BehaviorSubject<List<NoteTemplate>?>.seeded(null);
Stream<List<Note>> get notes => _notesStream.stream;
Stream<List<ImageNote>> get images => _imageNoteStream.stream;
Stream<List<NoteTemplate>> get templates => _noteTemplateStream.stream;
Stream<List<Note>?> get notes => _notesStream.stream;
Stream<List<ImageNote>?> get images => _imageNoteStream.stream;
Stream<List<NoteTemplate>?> get templates => _noteTemplateStream.stream;
List<Note> get _currentNotes => _notesStream.value ?? [];
List<ImageNote> get _currentImages => _imageNoteStream.value ?? [];
NoteRepository({required this.service});
Future<void> addNote(String deliveryId, String content) async {
final note = await service.addNote(content, int.parse(deliveryId));
final currentNotes = _notesStream.value;
currentNotes.add(note);
_currentNotes.add(note);
_notesStream.add(currentNotes);
_notesStream.add(_currentNotes);
}
Future<void> editNote(String noteId, String content) async {
@ -30,17 +32,19 @@ class NoteRepository {
await service.editNote(newNote);
final currentNotes = _notesStream.value;
final index = currentNotes.indexWhere((note) => note.id == int.parse(noteId));
currentNotes[index] = newNote;
final index = _currentNotes.indexWhere((note) => note.id == int.parse(noteId));
_notesStream.add(currentNotes);
if (index != -1) {
_currentNotes[index] = newNote;
_notesStream.add(currentNotes);
}
}
Future<void> deleteNote(String noteId) async {
await service.deleteNote(int.parse(noteId));
final currentNotes = _notesStream.value;
final index = currentNotes.indexWhere((note) => note.id == int.parse(noteId));
currentNotes.removeAt(index);
final index = _currentNotes.indexWhere((note) => note.id == int.parse(noteId));
_currentNotes.removeAt(index);
_notesStream.add(currentNotes);
}
@ -66,9 +70,8 @@ class NoteRepository {
bytes,
"image/png",
);
final imageNotes = _imageNoteStream.value;
imageNotes.add(ImageNote.make(objectId, fileName, bytes));
_imageNoteStream.add(imageNotes);
_currentImages.add(ImageNote.make(objectId, fileName, bytes));
_imageNoteStream.add(_currentImages);
}
Future<void> addNamedImage(String deliveryId, Uint8List bytes, String filename) async {
@ -79,18 +82,15 @@ class NoteRepository {
"image/png",
);
final imageNotes = _imageNoteStream.value;
imageNotes.add(ImageNote.make(objectId, filename, bytes));
_imageNoteStream.add(imageNotes);
_currentImages.add(ImageNote.make(objectId, filename, bytes));
_imageNoteStream.add(_currentImages);
}
Future<void> deleteImage(String deliveryId, String objectId) async {
await service.removeImage(objectId);
final images = _imageNoteStream.value;
final index = images.indexWhere((imageNote) => imageNote.objectId == objectId);
images.removeAt(index);
_imageNoteStream.add(images);
final index = _currentImages.indexWhere((imageNote) => imageNote.objectId == objectId);
_currentImages.removeAt(index);
_imageNoteStream.add(_currentImages);
}
}