2025-08-03 11:55:57 +03:30
|
|
|
import 'package:rasadyar_core/core.dart';
|
|
|
|
|
import 'package:rasadyar_livestock/data/common/dio_exception_handeler.dart';
|
|
|
|
|
import 'package:rasadyar_livestock/data/data_source/remote/auth/auth_remote.dart';
|
|
|
|
|
import 'package:rasadyar_livestock/data/data_source/remote/auth/auth_remote_imp.dart';
|
2025-08-05 14:48:47 +03:30
|
|
|
import 'package:rasadyar_livestock/data/data_source/remote/livestock/livestock_remote.dart';
|
|
|
|
|
import 'package:rasadyar_livestock/data/data_source/remote/livestock/livestock_remote_imp.dart';
|
2025-08-03 14:17:08 +03:30
|
|
|
import 'package:rasadyar_livestock/data/repository/auth/auth_repository.dart';
|
2025-08-03 11:55:57 +03:30
|
|
|
import 'package:rasadyar_livestock/data/repository/auth/auth_repository_imp.dart';
|
2025-08-05 14:48:47 +03:30
|
|
|
import 'package:rasadyar_livestock/data/repository/livestock/livestock_repository.dart';
|
|
|
|
|
import 'package:rasadyar_livestock/data/repository/livestock/livestock_repository_imp.dart';
|
2025-09-30 12:50:05 +03:30
|
|
|
import 'package:rasadyar_livestock/data/service/live_stock_storage_service.dart';
|
2025-08-11 12:56:26 +03:30
|
|
|
import 'package:rasadyar_livestock/hive_registrar.g.dart';
|
2025-08-03 11:55:57 +03:30
|
|
|
import 'package:rasadyar_livestock/presentation/routes/app_pages.dart';
|
|
|
|
|
|
2025-08-19 11:56:04 +03:30
|
|
|
import '../data/data_source/local/tmp/tmp_local_data-source.dart';
|
|
|
|
|
|
2025-09-30 12:50:05 +03:30
|
|
|
GetIt diLiveStock = GetIt.asNewInstance();
|
2025-08-03 11:55:57 +03:30
|
|
|
|
2025-08-03 14:17:08 +03:30
|
|
|
Future<void> setupLiveStockDI() async {
|
2025-08-03 11:55:57 +03:30
|
|
|
diLiveStock.registerSingleton(DioErrorHandler());
|
2025-09-03 11:43:52 +03:30
|
|
|
|
2025-08-11 12:56:26 +03:30
|
|
|
await IsolatedHive.initFlutter();
|
|
|
|
|
IsolatedHive.registerAdapters();
|
2025-08-03 14:17:08 +03:30
|
|
|
final tokenService = Get.find<TokenStorageService>();
|
2025-09-30 12:50:05 +03:30
|
|
|
Get.lazyPut(() => LiveStockStorageService());
|
2025-08-19 11:22:34 +03:30
|
|
|
|
2025-08-03 11:55:57 +03:30
|
|
|
if (tokenService.baseurl.value == null) {
|
2025-09-30 12:50:05 +03:30
|
|
|
await tokenService.saveBaseUrl(Module.inspection, 'https://api.dam.rasadyar.net/');
|
2025-08-03 11:55:57 +03:30
|
|
|
}
|
2025-08-03 14:17:08 +03:30
|
|
|
|
2025-08-05 14:48:47 +03:30
|
|
|
// First register AppInterceptor with lazy callbacks
|
2025-08-03 11:55:57 +03:30
|
|
|
diLiveStock.registerLazySingleton<AppInterceptor>(
|
2025-08-04 15:31:34 +03:30
|
|
|
() => AppInterceptor(
|
2025-08-03 11:55:57 +03:30
|
|
|
refreshTokenCallback: () async {
|
2025-09-03 11:43:52 +03:30
|
|
|
// Use lazy access to avoid circular dependency
|
2025-08-03 14:17:08 +03:30
|
|
|
final authRepository = diLiveStock.get<AuthRepository>();
|
|
|
|
|
final hasAuthenticated = await authRepository.hasAuthenticated();
|
2025-08-03 11:55:57 +03:30
|
|
|
if (hasAuthenticated) {
|
2025-08-03 14:17:08 +03:30
|
|
|
final newToken = await authRepository.loginWithRefreshToken(
|
2025-08-03 11:55:57 +03:30
|
|
|
authRequest: {'refresh': tokenService.refreshToken.value},
|
|
|
|
|
);
|
|
|
|
|
return newToken?.access;
|
2025-08-20 11:05:31 +03:30
|
|
|
}
|
2025-08-03 11:55:57 +03:30
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
saveTokenCallback: (String newToken) async {
|
2025-09-30 12:50:05 +03:30
|
|
|
// await tokenService.saveAccessToken(newToken);
|
2025-08-03 11:55:57 +03:30
|
|
|
},
|
|
|
|
|
clearTokenCallback: () async {
|
2025-09-13 13:52:25 +03:30
|
|
|
await tokenService.deleteModuleTokens(Module.liveStocks);
|
2025-08-03 11:55:57 +03:30
|
|
|
Get.offAllNamed(LiveStockRoutes.auth, arguments: Module.liveStocks);
|
|
|
|
|
},
|
|
|
|
|
authArguments: Module.liveStocks,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
2025-08-04 15:31:34 +03:30
|
|
|
// Register DioRemote with the interceptor
|
2025-08-03 11:55:57 +03:30
|
|
|
diLiveStock.registerLazySingleton<DioRemote>(
|
2025-08-04 15:31:34 +03:30
|
|
|
() => DioRemote(
|
2025-08-03 11:55:57 +03:30
|
|
|
baseUrl: tokenService.baseurl.value,
|
2025-08-04 15:31:34 +03:30
|
|
|
// interceptors: diLiveStock.get<AppInterceptor>(),
|
2025-08-03 11:55:57 +03:30
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
2025-08-04 15:31:34 +03:30
|
|
|
// Initialize DioRemote
|
2025-08-03 14:17:08 +03:30
|
|
|
await diLiveStock.get<DioRemote>().init();
|
2025-08-04 15:31:34 +03:30
|
|
|
|
|
|
|
|
// Now register the data source and repository
|
2025-08-05 14:48:47 +03:30
|
|
|
|
|
|
|
|
//region Auth
|
2025-08-04 15:31:34 +03:30
|
|
|
diLiveStock.registerLazySingleton<AuthRemoteDataSource>(
|
|
|
|
|
() => AuthRemoteDataSourceImp(diLiveStock.get<DioRemote>()),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
diLiveStock.registerLazySingleton<AuthRepository>(
|
2025-08-05 14:48:47 +03:30
|
|
|
() => AuthRepositoryImp(authRemote: diLiveStock.get<AuthRemoteDataSource>()),
|
|
|
|
|
);
|
|
|
|
|
//endregion
|
|
|
|
|
|
|
|
|
|
//region Livestock
|
|
|
|
|
diLiveStock.registerLazySingleton<LivestockRemoteDataSource>(
|
|
|
|
|
() => LivestockRemoteDataSourceImp(),
|
|
|
|
|
);
|
2025-08-19 11:56:04 +03:30
|
|
|
|
|
|
|
|
diLiveStock.registerLazySingleton<TmpLocalDataSource>(() => TmpLocalDataSource());
|
|
|
|
|
|
2025-08-05 14:48:47 +03:30
|
|
|
diLiveStock.registerLazySingleton<LivestockRepository>(
|
2025-08-19 11:56:04 +03:30
|
|
|
() => LivestockRepositoryImp(
|
|
|
|
|
livestockRemote: diLiveStock.get<LivestockRemoteDataSource>(),
|
|
|
|
|
tmpLocalDataSource: diLiveStock.get<TmpLocalDataSource>(),
|
|
|
|
|
),
|
2025-08-04 15:31:34 +03:30
|
|
|
);
|
2025-08-05 14:48:47 +03:30
|
|
|
//endregion
|
2025-08-04 15:31:34 +03:30
|
|
|
|
|
|
|
|
diLiveStock.registerLazySingleton<ImagePicker>(() => ImagePicker());
|
|
|
|
|
await diLiveStock.allReady();
|
2025-08-03 11:55:57 +03:30
|
|
|
}
|
2025-09-01 10:50:05 +03:30
|
|
|
|
|
|
|
|
Future<void> removeLiveStockDI() async {
|
2025-09-03 11:43:52 +03:30
|
|
|
eLog("removeLiveStockDI");
|
|
|
|
|
await diLiveStock.resetScope();
|
2025-09-01 10:50:05 +03:30
|
|
|
}
|