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
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() {

View file

@ -168,10 +168,11 @@ mixin AlbumViewerMixin<T extends StatefulWidget>
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<T extends StatefulWidget>
@mustCallSuper
void enterEditMode() {}
/// Validates the pending modifications
@protected
bool doneEditMode();
bool validateEditMode();
@protected
void doneEditMode();
/// Return a new album with the edits
@protected

View file

@ -90,30 +90,29 @@ class _DynamicAlbumViewerState extends State<DynamicAlbumViewer>
);
}
@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() {