Ensure isEditMode is false when calling done

This commit is contained in:
Ming Ming 2021-07-09 03:26:14 +08:00
parent a07fb0d9a4
commit 8eef2a2084
3 changed files with 54 additions and 51 deletions

View file

@ -104,40 +104,39 @@ class _AlbumViewerState extends State<AlbumViewer>
} }
} }
@override
validateEditMode() => _editFormKey?.currentState?.validate() == true;
@override @override
doneEditMode() { doneEditMode() {
if (_editFormKey?.currentState?.validate() == true) { try {
try { // persist the changes
// persist the changes _editFormKey.currentState.save();
_editFormKey.currentState.save(); final newAlbum = makeEdited(_editAlbum);
final newAlbum = makeEdited(_editAlbum); if (newAlbum.copyWith(lastUpdated: _album.lastUpdated) != _album) {
if (newAlbum.copyWith(lastUpdated: _album.lastUpdated) != _album) { _log.info("[doneEditMode] Album modified: $newAlbum");
_log.info("[doneEditMode] Album modified: $newAlbum"); final albumRepo = AlbumRepo(AlbumCachedDataSource());
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 {
setState(() { setState(() {
// reset edits _album = newAlbum;
_editAlbum = null;
// update the list to show the real album
_transformItems();
}); });
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() { void _initAlbum() {

View file

@ -168,10 +168,11 @@ mixin AlbumViewerMixin<T extends StatefulWidget>
color: Theme.of(context).colorScheme.primary, color: Theme.of(context).colorScheme.primary,
tooltip: AppLocalizations.of(context).doneButtonTooltip, tooltip: AppLocalizations.of(context).doneButtonTooltip,
onPressed: () { onPressed: () {
if (doneEditMode()) { if (validateEditMode()) {
setState(() { setState(() {
_isEditMode = false; _isEditMode = false;
}); });
doneEditMode();
} }
}, },
), ),
@ -186,8 +187,12 @@ mixin AlbumViewerMixin<T extends StatefulWidget>
@mustCallSuper @mustCallSuper
void enterEditMode() {} void enterEditMode() {}
/// Validates the pending modifications
@protected @protected
bool doneEditMode(); bool validateEditMode();
@protected
void doneEditMode();
/// Return a new album with the edits /// Return a new album with the edits
@protected @protected

View file

@ -90,30 +90,29 @@ class _DynamicAlbumViewerState extends State<DynamicAlbumViewer>
); );
} }
@override
validateEditMode() => _editFormKey?.currentState?.validate() == true;
@override @override
doneEditMode() { doneEditMode() {
if (_editFormKey?.currentState?.validate() == true) { _editFormKey.currentState.save();
_editFormKey.currentState.save(); final newAlbum = makeEdited(_album);
final newAlbum = makeEdited(_album); if (newAlbum.copyWith(lastUpdated: _album.lastUpdated) != _album) {
if (newAlbum.copyWith(lastUpdated: _album.lastUpdated) != _album) { _log.info("[doneEditMode] Album modified: $newAlbum");
_log.info("[doneEditMode] Album modified: $newAlbum"); final albumRepo = AlbumRepo(AlbumCachedDataSource());
final albumRepo = AlbumRepo(AlbumCachedDataSource()); setState(() {
setState(() { _album = newAlbum;
_album = newAlbum; });
}); UpdateAlbum(albumRepo)(widget.account, newAlbum)
UpdateAlbum(albumRepo)(widget.account, newAlbum) .catchError((e, stacktrace) {
.catchError((e, stacktrace) { SnackBarManager().showSnackBar(SnackBar(
SnackBarManager().showSnackBar(SnackBar( content: Text(exception_util.toUserString(e, context)),
content: Text(exception_util.toUserString(e, context)), duration: k.snackBarDurationNormal,
duration: k.snackBarDurationNormal, ));
)); });
}); } else {
} else { _log.fine("[doneEditMode] Album not modified");
_log.fine("[doneEditMode] Album not modified");
}
return true;
} }
return false;
} }
void _initAlbum() { void _initAlbum() {