From 8eef2a20849210bfbedf1b1532930d22883731d2 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Fri, 9 Jul 2021 03:26:14 +0800 Subject: [PATCH] Ensure isEditMode is false when calling done --- lib/widget/album_viewer.dart | 55 ++++++++++++++-------------- lib/widget/album_viewer_mixin.dart | 9 ++++- lib/widget/dynamic_album_viewer.dart | 41 ++++++++++----------- 3 files changed, 54 insertions(+), 51 deletions(-) diff --git a/lib/widget/album_viewer.dart b/lib/widget/album_viewer.dart index dbc62336..adda74e0 100644 --- a/lib/widget/album_viewer.dart +++ b/lib/widget/album_viewer.dart @@ -104,40 +104,39 @@ class _AlbumViewerState extends State } } + @override + validateEditMode() => _editFormKey?.currentState?.validate() == true; + @override doneEditMode() { - if (_editFormKey?.currentState?.validate() == true) { - try { - // persist the changes - _editFormKey.currentState.save(); - final newAlbum = makeEdited(_editAlbum); - if (newAlbum.copyWith(lastUpdated: _album.lastUpdated) != _album) { - _log.info("[doneEditMode] Album modified: $newAlbum"); - final albumRepo = AlbumRepo(AlbumCachedDataSource()); - setState(() { - _album = newAlbum; - }); - UpdateAlbum(albumRepo)(widget.account, newAlbum) - .catchError((e, stacktrace) { - SnackBarManager().showSnackBar(SnackBar( - content: Text(exception_util.toUserString(e, context)), - duration: k.snackBarDurationNormal, - )); - }); - } else { - _log.fine("[doneEditMode] Album not modified"); - } - return true; - } finally { + try { + // persist the changes + _editFormKey.currentState.save(); + final newAlbum = makeEdited(_editAlbum); + if (newAlbum.copyWith(lastUpdated: _album.lastUpdated) != _album) { + _log.info("[doneEditMode] Album modified: $newAlbum"); + final albumRepo = AlbumRepo(AlbumCachedDataSource()); setState(() { - // reset edits - _editAlbum = null; - // update the list to show the real album - _transformItems(); + _album = newAlbum; }); + UpdateAlbum(albumRepo)(widget.account, newAlbum) + .catchError((e, stacktrace) { + SnackBarManager().showSnackBar(SnackBar( + content: Text(exception_util.toUserString(e, context)), + duration: k.snackBarDurationNormal, + )); + }); + } else { + _log.fine("[doneEditMode] Album not modified"); } + } finally { + setState(() { + // reset edits + _editAlbum = null; + // update the list to show the real album + _transformItems(); + }); } - return false; } void _initAlbum() { diff --git a/lib/widget/album_viewer_mixin.dart b/lib/widget/album_viewer_mixin.dart index 40d15bcd..c8599663 100644 --- a/lib/widget/album_viewer_mixin.dart +++ b/lib/widget/album_viewer_mixin.dart @@ -168,10 +168,11 @@ mixin AlbumViewerMixin color: Theme.of(context).colorScheme.primary, tooltip: AppLocalizations.of(context).doneButtonTooltip, onPressed: () { - if (doneEditMode()) { + if (validateEditMode()) { setState(() { _isEditMode = false; }); + doneEditMode(); } }, ), @@ -186,8 +187,12 @@ mixin AlbumViewerMixin @mustCallSuper void enterEditMode() {} + /// Validates the pending modifications @protected - bool doneEditMode(); + bool validateEditMode(); + + @protected + void doneEditMode(); /// Return a new album with the edits @protected diff --git a/lib/widget/dynamic_album_viewer.dart b/lib/widget/dynamic_album_viewer.dart index b0462be8..42ea2ce7 100644 --- a/lib/widget/dynamic_album_viewer.dart +++ b/lib/widget/dynamic_album_viewer.dart @@ -90,30 +90,29 @@ class _DynamicAlbumViewerState extends State ); } + @override + validateEditMode() => _editFormKey?.currentState?.validate() == true; + @override doneEditMode() { - if (_editFormKey?.currentState?.validate() == true) { - _editFormKey.currentState.save(); - final newAlbum = makeEdited(_album); - if (newAlbum.copyWith(lastUpdated: _album.lastUpdated) != _album) { - _log.info("[doneEditMode] Album modified: $newAlbum"); - final albumRepo = AlbumRepo(AlbumCachedDataSource()); - setState(() { - _album = newAlbum; - }); - UpdateAlbum(albumRepo)(widget.account, newAlbum) - .catchError((e, stacktrace) { - SnackBarManager().showSnackBar(SnackBar( - content: Text(exception_util.toUserString(e, context)), - duration: k.snackBarDurationNormal, - )); - }); - } else { - _log.fine("[doneEditMode] Album not modified"); - } - return true; + _editFormKey.currentState.save(); + final newAlbum = makeEdited(_album); + if (newAlbum.copyWith(lastUpdated: _album.lastUpdated) != _album) { + _log.info("[doneEditMode] Album modified: $newAlbum"); + final albumRepo = AlbumRepo(AlbumCachedDataSource()); + setState(() { + _album = newAlbum; + }); + UpdateAlbum(albumRepo)(widget.account, newAlbum) + .catchError((e, stacktrace) { + SnackBarManager().showSnackBar(SnackBar( + content: Text(exception_util.toUserString(e, context)), + duration: k.snackBarDurationNormal, + )); + }); + } else { + _log.fine("[doneEditMode] Album not modified"); } - return false; } void _initAlbum() {