init static method

void init({
  1. required Tracker tracker,
  2. FlutterSecureStorage? secureStorage,
  3. CacheConfig? cacheConfig,
})

Implementation

static void init({
  required Tracker tracker,
  FlutterSecureStorage? secureStorage,
  CacheConfig? cacheConfig,
}) {
  Injector.registerLazySingleton<Tracker>(() => tracker);

  Injector.registerLazySingleton<ConnectivityInfo>(
    () {
      return ConnectivityInfoImpl(
        connectivity: Connectivity(),
      );
    },
  );
  Injector.registerLazySingleton<Network>(
    () {
      IntegrationInterceptor? integrationInterceptor;
      if (Injector.isRegistered<IntegrationInterceptor>()) {
        integrationInterceptor = Injector.get<IntegrationInterceptor>();
      }

      var saimobileappsInterceptor = SaiMobileAppsInterceptor(
        localizationService: Injector.get(),
        authStorage: Injector.get(),
      );

      var baseOptions = BaseOptions(
        baseUrl: Injector.get<ConfigRepository>().baseUrl,
        headers: Map<String, dynamic>.fromEntries(
          [
            const MapEntry(
              HttpHeaders.acceptHeader,
              "application/json",
            ),
          ],
        ),
      );

      var refreshTokenDio = Dio(
        BaseOptions(
          baseUrl: Injector.get<ConfigRepository>().baseUrl,
        ),
      )..interceptors.add(
          saimobileappsInterceptor,
        );
      var saiMobileAppsDio = Dio(
        baseOptions,
      )..interceptors.addAll([
          saimobileappsInterceptor,
          if (integrationInterceptor != null) integrationInterceptor,
        ]);

      var authenticatedInterceptor = AuthenticatedInterceptor(
        authRepository: Injector.get(),
        refreshTokenClient: NetworkClientImpl(
          dio: refreshTokenDio,
          connectivity: Injector.get(),
          errorTracker: Injector.get<Tracker>().error,
        ),
      );

      var authenticatedSaimobileappsDio = Dio(baseOptions)
        ..interceptors.addAll([
          saimobileappsInterceptor,
          authenticatedInterceptor,
          if (integrationInterceptor != null) integrationInterceptor,
        ]);

      var googleMapsDio = Dio(
        BaseOptions(
          baseUrl: "https://maps.googleapis.com/maps/api",
        ),
      )..interceptors.add(
          GoogleApiInterceptor(
            configRepository: Injector.get<ConfigRepository>(),
          ),
        );

      if (cacheConfig != null) {
        var cacheOptions = CacheOptions(
          store: FileCacheStore(
            cacheConfig.cacheDirectory,
          ),
          policy: CachePolicy.noCache,
        );
        var cacheInterceptor = CacheInterceptor(
          cacheOptions: cacheOptions,
          cacheConfig: cacheConfig,
        );
        saiMobileAppsDio.interceptors.add(cacheInterceptor);
        authenticatedSaimobileappsDio.interceptors.add(cacheInterceptor);
      }

      if (kDebugMode) {
        var curlLoggerInterceptor = CurlLoggerDioInterceptor(printOnSuccess: true);
        refreshTokenDio.interceptors.add(curlLoggerInterceptor);
        saiMobileAppsDio.interceptors.add(curlLoggerInterceptor);
        authenticatedSaimobileappsDio.interceptors.add(curlLoggerInterceptor);
        googleMapsDio.interceptors.add(curlLoggerInterceptor);
      }

      return NetworkImpl(
        connectivityInfo: Injector.get(),
        client: NetworkClientImpl(
          dio: saiMobileAppsDio,
          connectivity: Injector.get(),
          errorTracker: Injector.get<Tracker>().error,
        ),
        authenticatedClient: NetworkClientImpl(
          dio: authenticatedSaimobileappsDio,
          connectivity: Injector.get(),
          errorTracker: Injector.get<Tracker>().error,
        ),
        googleApiClient: NetworkClientImpl(
          dio: googleMapsDio,
          connectivity: Injector.get(),
          errorTracker: Injector.get<Tracker>().error,
        ),
      );
    },
  );
  Injector.registerLazySingleton<LocationService>(
    () => LocationServiceImpl(),
  );
  Injector.registerLazySingleton<AuthStorage>(
    () => AuthStorageImpl(
      secureStorage: secureStorage ?? const FlutterSecureStorage(),
    ),
  );

  Injector.registerLazySingleton(
    () => AuthRepository(
      authStorage: Injector.get(),
    ),
  );

  Injector.registerLazySingleton(
    () => UrlLauncherService(),
  );

  Injector.registerLazySingleton(
    () => LocalizationService(),
  );

  Injector.registerLazySingleton<PermissionService>(
    () => PermissionServiceImpl(),
  );

  Injector.registerLazySingleton<MapService>(
    () => MapServiceImpl(),
  );

  Injector.registerLazySingleton<ImageService>(
    () => ImageServiceImpl(
      permissionService: Injector.get(),
      picker: ImagePicker(),
      configRepository: Injector.get(),
      deviceInfoPlugin: DeviceInfoPlugin(),
      platform: DevicePlatform(
        isAndroid: Platform.isAndroid,
        isIOS: Platform.isIOS,
      ),
      tracker: Injector.get(),
    ),
  );

  PlatformService platform = PlatformService();
  Injector.registerLazySingleton(
    () => platform,
  );
}