Skip to content

Commit

Permalink
Add AppLifecycleListener to save data model on app hide (#197)
Browse files Browse the repository at this point in the history
* Add AppLifecycleListener to save data model on app hide
Fixes #187

* Add condition to check for test environment in AppLifecycleListener
  • Loading branch information
LarsRefsgaard authored Dec 11, 2023
1 parent bc225a8 commit b4d96e4
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/view_models/view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ abstract class SerializableViewModel<D extends DataModel> extends ViewModel {

// save the data model on a regular basis.
Timer.periodic(const Duration(minutes: 3), (_) => save(model.toJson()));

/// Check if we are running in a test environment.
/// If so, do not listen to app lifecycle events.
/// [AppLifecycleListener] is not supported in a test environment.
if (!Platform.environment.containsKey('FLUTTER_TEST')) {
AppLifecycleListener(
onStateChange: (value) {
info('AppLifecycleState changed to $value');
},
onHide: () async {
await save(model.toJson());
},
);
}
}

/// Current path and filename of the data.
Expand Down

0 comments on commit b4d96e4

Please sign in to comment.