ente/mobile/lib/services/app_lifecycle_service.dart

47 lines
1.3 KiB
Dart
Raw Normal View History

import 'package:logging/logging.dart';
import 'package:media_extension/media_extension_action_types.dart';
2023-06-25 08:52:33 +00:00
import "package:shared_preferences/shared_preferences.dart";
class AppLifecycleService {
2023-06-25 08:52:33 +00:00
static const String keyLastAppOpenTime = "last_app_open_time";
final _logger = Logger("AppLifecycleService");
bool isForeground = false;
MediaExtentionAction mediaExtensionAction =
MediaExtentionAction(action: IntentAction.main);
2023-06-25 08:52:33 +00:00
late SharedPreferences _preferences;
static final AppLifecycleService instance =
AppLifecycleService._privateConstructor();
AppLifecycleService._privateConstructor();
2023-06-26 07:51:31 +00:00
void init(SharedPreferences preferences) {
_preferences = preferences;
2023-06-25 08:52:33 +00:00
}
void setMediaExtensionAction(MediaExtentionAction mediaExtensionAction) {
_logger.info("App invoked via ${mediaExtensionAction.action}");
this.mediaExtensionAction = mediaExtensionAction;
}
2022-03-07 20:18:50 +00:00
void onAppInForeground(String reason) {
_logger.info("App in foreground via $reason");
isForeground = true;
}
2022-03-07 20:18:50 +00:00
void onAppInBackground(String reason) {
_logger.info("App in background $reason");
_preferences.setInt(
keyLastAppOpenTime,
DateTime.now().microsecondsSinceEpoch,
);
isForeground = false;
}
2023-06-25 08:52:33 +00:00
int getLastAppOpenTime() {
return _preferences.getInt(keyLastAppOpenTime) ?? 0;
}
}