diff --git a/lib/feature/delivery/detail/bloc/note_bloc.dart b/lib/feature/delivery/detail/bloc/note_bloc.dart index 7aacfbf..1c479d0 100644 --- a/lib/feature/delivery/detail/bloc/note_bloc.dart +++ b/lib/feature/delivery/detail/bloc/note_bloc.dart @@ -26,19 +26,27 @@ class NoteBloc extends Bloc { required this.deliveryId, }) : super(NoteInitial()) { _combinedSubscription = CombineLatestStream.combine3( - repository.notes, - repository.images, - repository.templates, - (note, image, templates) => {"note": note, "image": image, "templates": templates}, - ).listen( - (data) => add( - DataUpdated( - images: data["image"] as List, - notes: data["note"] as List, - templates: data["templates"] as List - ), - ), - ); + repository.notes, + repository.images, + repository.templates, + (note, image, templates) { + if (note == null || image == null || templates == null) { + return null; + } + + return {"note": note, "image": image, "templates": templates}; + }, + ) + .where((data) => data != null) + .listen( + (data) => add( + DataUpdated( + images: data!["image"] as List, + notes: data["note"] as List, + templates: data["templates"] as List, + ), + ), + ); on(_load); on(_add); @@ -58,7 +66,13 @@ class NoteBloc extends Bloc { } Future _dataUpdated(DataUpdated event, Emitter emit) async { - emit(NoteLoaded(notes: event.notes, images: event.images, templates: event.templates)); + emit( + NoteLoaded( + notes: event.notes, + images: event.images, + templates: event.templates, + ), + ); } Future _reset(ResetNotes event, Emitter emit) async { @@ -102,11 +116,6 @@ class NoteBloc extends Bloc { emit.call(NoteLoading()); try { - List urls = - event.delivery.images.map((image) => image.url).toList(); - - debugPrint("IMAGE URLS : $urls"); - await repository.loadNotes(event.delivery.id); await repository.loadTemplates(); diff --git a/lib/feature/delivery/detail/presentation/delivery_detail_page.dart b/lib/feature/delivery/detail/presentation/delivery_detail_page.dart index c0b9298..b5332d8 100644 --- a/lib/feature/delivery/detail/presentation/delivery_detail_page.dart +++ b/lib/feature/delivery/detail/presentation/delivery_detail_page.dart @@ -121,14 +121,9 @@ class _DeliveryDetailState extends State { void _openSignatureView(Delivery delivery) { Navigator.of(context).push( MaterialPageRoute( - builder: (context) { - return BlocProvider( - create: - (context) => NoteBloc( - repository: NoteRepository(service: NoteService()), - opBloc: context.read(), - deliveryId: delivery.id, - ), + builder: (_) { + return BlocProvider.value( + value: context.read(), child: SignatureView(onSigned: _onSign, delivery: delivery), ); }, @@ -160,11 +155,13 @@ class _DeliveryDetailState extends State { Padding( padding: const EdgeInsets.only(left: 20), child: FilledButton( - onPressed: - () => - _step == _steps.length - 1 - ? _openSignatureView(delivery) - : _clickForward, + onPressed: () { + if (_step == _steps.length - 1) { + _openSignatureView(delivery); + } else { + _clickForward(); + } + }, child: _step == _steps.length - 1 ? const Text("Unterschreiben") diff --git a/lib/feature/delivery/detail/repository/note_repository.dart b/lib/feature/delivery/detail/repository/note_repository.dart index 2a281a1..5361bd4 100644 --- a/lib/feature/delivery/detail/repository/note_repository.dart +++ b/lib/feature/delivery/detail/repository/note_repository.dart @@ -7,22 +7,24 @@ import 'package:rxdart/rxdart.dart'; class NoteRepository { final NoteService service; - final _notesStream = BehaviorSubject>.seeded([]); - final _imageNoteStream = BehaviorSubject>.seeded([]); - final _noteTemplateStream = BehaviorSubject>.seeded([]); + final _notesStream = BehaviorSubject?>.seeded(null); + final _imageNoteStream = BehaviorSubject?>.seeded(null); + final _noteTemplateStream = BehaviorSubject?>.seeded(null); - Stream> get notes => _notesStream.stream; - Stream> get images => _imageNoteStream.stream; - Stream> get templates => _noteTemplateStream.stream; + Stream?> get notes => _notesStream.stream; + Stream?> get images => _imageNoteStream.stream; + Stream?> get templates => _noteTemplateStream.stream; + + List get _currentNotes => _notesStream.value ?? []; + List get _currentImages => _imageNoteStream.value ?? []; NoteRepository({required this.service}); Future 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 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 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 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 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); } } diff --git a/lib/feature/delivery/detail/service/notes_service.dart b/lib/feature/delivery/detail/service/notes_service.dart index 5c4498d..b55ca5a 100644 --- a/lib/feature/delivery/detail/service/notes_service.dart +++ b/lib/feature/delivery/detail/service/notes_service.dart @@ -121,7 +121,6 @@ class NoteService { } Map responseJson = jsonDecode(response.body); - debugPrint(responseJson.toString()); NoteGetResponseDTO responseDto = NoteGetResponseDTO.fromJson( responseJson, );