mc_gallery/lib/features/core/services/app_lifecycle_service.dart

106 lines
3.4 KiB
Dart
Raw Normal View History

2022-12-20 20:52:24 +00:00
import 'dart:async';
2022-12-25 21:17:59 +00:00
import 'package:flutter/widgets.dart';
2022-12-20 20:52:24 +00:00
2022-12-25 21:17:59 +00:00
import '/locator.dart';
2022-12-20 20:52:24 +00:00
import 'logging_service.dart';
typedef AddLifeCycleListener = void Function({
required void Function(AppLifecycleState appLifecycleState) listener,
required String tag,
bool tryCallListenerOnAdd,
});
typedef RemoveLifeCycleListener = Future<void> Function({required String tag});
/// Used to observe the current app lifecycle state.
class AppLifecycleService with WidgetsBindingObserver {
AppLifecycleService({
required LoggingService loggingService,
}) : _loggingService = loggingService {
WidgetsBinding.instance.addObserver(this);
_appLifeCycleState = WidgetsBinding.instance.lifecycleState;
}
final LoggingService _loggingService;
2022-12-25 21:17:59 +00:00
late final StreamController<AppLifecycleState> _lifecycleStateStreamController =
StreamController.broadcast();
2023-01-01 12:04:22 +00:00
final Map<String, StreamSubscription<dynamic>> _appLifecycleSubscriptions = {};
2022-12-20 20:52:24 +00:00
AppLifecycleState? _appLifeCycleState;
AppLifecycleState? get appLifeCycleState => _appLifeCycleState;
Future<void> dispose() async {
_loggingService.info('Disposing app lifecycle service..');
WidgetsBinding.instance.removeObserver(this);
for (final subscription in _appLifecycleSubscriptions.values) {
await subscription.cancel();
}
_appLifecycleSubscriptions.clear();
_loggingService.good('App lifecycle service disposed!');
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
try {
_appLifeCycleState = state;
2022-12-25 21:17:59 +00:00
_lifecycleStateStreamController.add(state);
2022-12-20 20:52:24 +00:00
} catch (error, stackTrace) {
_loggingService.error(
2022-12-25 21:17:59 +00:00
'Something went wrong with ${state.name} inside the didChangeAppLifeCycleState method',
2022-12-20 20:52:24 +00:00
error,
stackTrace,
);
}
2022-12-25 21:17:59 +00:00
2022-12-20 20:52:24 +00:00
super.didChangeAppLifecycleState(state);
}
void addListener({
required void Function(AppLifecycleState appLifecycleState) listener,
required String tag,
bool tryCallListenerOnAdd = true,
}) {
try {
if (_appLifecycleSubscriptions.containsKey(tag)) {
2022-12-25 21:17:59 +00:00
_loggingService.warning('Tag already active, returning');
2022-12-20 20:52:24 +00:00
} else {
final message = 'Adding $tag appLifecycleState listener';
_loggingService.info('$message..');
if (_appLifeCycleState != null && tryCallListenerOnAdd) listener(_appLifeCycleState!);
2022-12-25 21:17:59 +00:00
_appLifecycleSubscriptions[tag] = _lifecycleStateStreamController.stream.listen(listener);
2022-12-20 20:52:24 +00:00
_loggingService.good('$message success!');
}
} catch (error, stackTrace) {
_loggingService.error(
2022-12-25 21:17:59 +00:00
'Something went wrong adding $tag appLifecycleState listener',
2022-12-20 20:52:24 +00:00
error,
stackTrace,
);
}
}
Future<void> removeListener({required String tag}) async {
try {
final message = 'Removing $tag appLifecycleState listener';
_loggingService.info('$message..');
final subscription = _appLifecycleSubscriptions[tag];
if (subscription != null) {
await subscription.cancel();
_appLifecycleSubscriptions.remove(tag);
_loggingService.good('$message success!');
} else {
2022-12-25 21:17:59 +00:00
_loggingService.warning('Subscription was not found');
2022-12-20 20:52:24 +00:00
}
} catch (error, stackTrace) {
_loggingService.error(
2022-12-25 21:17:59 +00:00
'Something went wrong removing $tag appLifecycleState listener',
2022-12-20 20:52:24 +00:00
error,
stackTrace,
);
}
}
static AppLifecycleService get locate => Locator.locate();
}