mc_gallery/lib/locator.dart

120 lines
3.5 KiB
Dart
Raw Permalink Normal View History

2022-12-20 20:52:24 +00:00
import 'dart:async';
2022-12-20 08:47:07 +00:00
import 'package:connectivity_plus/connectivity_plus.dart';
2022-12-19 13:03:38 +00:00
import 'package:get_it/get_it.dart';
2022-12-20 08:47:07 +00:00
import 'package:internet_connection_checker/internet_connection_checker.dart';
2022-12-25 21:17:59 +00:00
import 'package:mc_gallery/env/env.dart';
2022-12-19 13:03:38 +00:00
2022-12-22 21:29:37 +00:00
import 'features/core/abstracts/router/app_router.dart';
2022-12-20 20:52:24 +00:00
import 'features/core/services/app_lifecycle_service.dart';
2022-12-22 21:29:37 +00:00
import 'features/core/services/connections_service.dart';
2022-12-25 00:55:53 +00:00
import 'features/core/services/local_storage_service.dart';
2022-12-22 21:29:37 +00:00
import 'features/core/services/logging_service.dart';
import 'features/core/services/navigation_service.dart';
2022-12-20 08:47:07 +00:00
import 'features/core/services/overlay_service.dart';
2022-12-22 21:29:37 +00:00
import 'features/home/api/unsplash_images_api.dart';
import 'features/home/services/image_cache_manager_service.dart';
import 'features/home/services/images_service.dart';
import 'features/home/views/gallery/gallery_view_model.dart';
import 'features/home/views/image_carousel/image_carousel_view_model.dart';
2022-12-20 08:47:07 +00:00
2022-12-19 13:03:38 +00:00
GetIt get locate => Locator.instance();
class Locator {
static GetIt instance() => GetIt.instance;
static T locate<T extends Object>() => instance().get<T>();
static Future<void> setup() async {
final locator = instance();
_registerAPIs();
_registerViewModels();
await _registerServices(locator);
await _registerRepos(locator);
2022-12-20 20:52:24 +00:00
2022-12-19 13:03:38 +00:00
_registerSingletons();
}
2022-12-20 19:29:46 +00:00
static void _registerAPIs() {
instance().registerFactory(
2022-12-25 21:17:59 +00:00
() => UnsplashImagesApi(token: Env.unsplashApiKey),
2022-12-20 19:29:46 +00:00
);
}
2022-12-19 13:03:38 +00:00
2022-12-20 20:52:24 +00:00
static void _registerViewModels() {
instance().registerFactory(
() => GalleryViewModel(
imagesService: ImagesService.locate,
navigationService: NavigationService.locate,
2022-12-21 22:40:39 +00:00
imageCacheManagerService: ImageCacheManagerService.locate,
2022-12-20 20:52:24 +00:00
),
);
instance().registerFactory(
() => ImageCarouselViewModel(
imagesService: ImagesService.locate,
),
);
}
2022-12-19 13:03:38 +00:00
2022-12-22 21:29:37 +00:00
static FutureOr<void> _registerServices(GetIt it) async {
2023-01-01 12:04:22 +00:00
it
..registerLazySingleton(
() => NavigationService(
mcgRouter: McgRouter.locate,
),
)
..registerFactory(
LoggingService.new,
)
..registerSingleton<ConnectionsService>(
ConnectionsService(
connectivity: Connectivity(),
internetConnectionChecker: InternetConnectionChecker(),
loggingService: LoggingService.locate,
),
signalsReady: true,
dispose: (final param) async => param.dispose(),
);
2022-12-22 21:29:37 +00:00
await it.isReady<ConnectionsService>();
2023-01-01 12:04:22 +00:00
it
..registerLazySingleton(
() => OverlayService(
loggingService: LoggingService.locate,
),
dispose: (param) => param.dispose(),
)
..registerSingleton<AppLifecycleService>(
AppLifecycleService(
loggingService: LoggingService.locate,
),
dispose: (final param) async => param.dispose(),
)
..registerSingleton<LocalStorageService>(
LocalStorageService(),
signalsReady: true,
);
2022-12-25 00:55:53 +00:00
await it.isReady<LocalStorageService>();
2023-01-01 12:04:22 +00:00
it
..registerSingleton<ImagesService>(
ImagesService(
imagesApi: UnsplashImagesApi.locate,
localStorageService: LocalStorageService.locate,
loggingService: LoggingService.locate,
),
)
..registerSingleton(
ImageCacheManagerService(
appLifecycleService: AppLifecycleService.locate,
localStorageService: LocalStorageService.locate,
),
);
2022-12-19 13:03:38 +00:00
}
2022-12-20 20:52:24 +00:00
static FutureOr<void> _registerRepos(GetIt locator) {}
2022-12-19 13:03:38 +00:00
static void _registerSingletons() {}
}