ui backbone
This commit is contained in:
parent
3e374d24f6
commit
b7045fc242
24 changed files with 918 additions and 73 deletions
103
lib/features/core/services/app_lifecycle_service.dart
Normal file
103
lib/features/core/services/app_lifecycle_service.dart
Normal file
|
@ -0,0 +1,103 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mc_gallery/locator.dart';
|
||||
|
||||
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;
|
||||
|
||||
late final StreamController<AppLifecycleState> _streamController = StreamController.broadcast();
|
||||
final Map<String, StreamSubscription> _appLifecycleSubscriptions = {};
|
||||
|
||||
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;
|
||||
_streamController.add(state);
|
||||
} catch (error, stackTrace) {
|
||||
_loggingService.error(
|
||||
'Something went wrong logging ${state.name} inside the didChangeAppLifeCycleState method',
|
||||
error,
|
||||
stackTrace,
|
||||
);
|
||||
}
|
||||
super.didChangeAppLifecycleState(state);
|
||||
}
|
||||
|
||||
void addListener({
|
||||
required void Function(AppLifecycleState appLifecycleState) listener,
|
||||
required String tag,
|
||||
bool tryCallListenerOnAdd = true,
|
||||
}) {
|
||||
try {
|
||||
if (_appLifecycleSubscriptions.containsKey(tag)) {
|
||||
_loggingService.warning('Tag already active, returning!');
|
||||
} else {
|
||||
final message = 'Adding $tag appLifecycleState listener';
|
||||
_loggingService.info('$message..');
|
||||
if (_appLifeCycleState != null && tryCallListenerOnAdd) listener(_appLifeCycleState!);
|
||||
_appLifecycleSubscriptions[tag] = _streamController.stream.listen(listener);
|
||||
_loggingService.good('$message success!');
|
||||
}
|
||||
} catch (error, stackTrace) {
|
||||
_loggingService.error(
|
||||
'Something went wrong adding $tag appLifecycleState listener!',
|
||||
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 {
|
||||
_loggingService.warning('Subscription was not found!');
|
||||
}
|
||||
} catch (error, stackTrace) {
|
||||
_loggingService.error(
|
||||
'Something went wrong removing $tag appLifecycleState listener!',
|
||||
error,
|
||||
stackTrace,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static AppLifecycleService get locate => Locator.locate();
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
import 'package:flutter/widgets.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:mc_gallery/features/home/views/image_carousel/image_carousel_view.dart';
|
||||
import 'package:mc_gallery/locator.dart';
|
||||
|
||||
import '../abstracts/router/app_router.dart';
|
||||
import '../abstracts/router/routes.dart';
|
||||
|
@ -11,11 +13,19 @@ class NavigationService {
|
|||
|
||||
final McgRouter _mcgRouter;
|
||||
|
||||
void pushImageCarouselView(BuildContext context) =>
|
||||
context.pushNamed(Routes.imageCarousel.routeName);
|
||||
void pushImageCarouselView(
|
||||
BuildContext context, {
|
||||
required ImageCarouselViewArguments imageCarouselViewArguments,
|
||||
}) =>
|
||||
context.pushNamed(
|
||||
Routes.imageCarousel.routeName,
|
||||
extra: imageCarouselViewArguments,
|
||||
);
|
||||
|
||||
void backToGallery(BuildContext context) => context.pop();
|
||||
|
||||
void previous() {}
|
||||
void next() {}
|
||||
|
||||
static NavigationService get locate => Locator.locate();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue