156 lines
5.6 KiB
Dart
156 lines
5.6 KiB
Dart
import 'package:device_info_plus/device_info_plus.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
import 'package:get_it/get_it.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../features/auth/cubit/login_cubit.dart';
|
|
|
|
import '../features/auth/data/auth_repository.dart';
|
|
import '../features/profile/data/profile_repository.dart';
|
|
|
|
import '../features/rating/cubit/rating_cubit.dart';
|
|
|
|
import '../features/rating/data/rating_repository.dart';
|
|
|
|
import '../features/settings/cubit/settings_cubit.dart';
|
|
|
|
import '../features/trip/cubit/history_cubit.dart';
|
|
|
|
import '../features/chat/cubit/chat_cubit.dart';
|
|
import '../features/chat/data/chat_repository.dart';
|
|
import '../features/onboarding/cubit/onboarding_cubit.dart';
|
|
import '../features/onboarding/data/driver_repository.dart';
|
|
import '../features/trip/cubit/duty_cubit.dart';
|
|
|
|
import '../features/trip/data/history_repository.dart';
|
|
|
|
import '../features/trip/data/maps_repository.dart';
|
|
|
|
import '../features/trip/data/trip_repository.dart';
|
|
|
|
import '../features/wallet/cubit/payout_cubit.dart';
|
|
import '../features/wallet/cubit/wallet_cubit.dart';
|
|
|
|
import '../features/wallet/data/wallet_repository.dart';
|
|
|
|
import 'api/antlaq_api.dart';
|
|
|
|
import 'api/api_client.dart';
|
|
|
|
import 'build_config.dart';
|
|
|
|
import 'location/location_service.dart';
|
|
|
|
import 'notifications/push_service.dart';
|
|
|
|
import 'realtime/realtime_service.dart';
|
|
|
|
import 'session/session_cubit.dart';
|
|
import 'session/session_source.dart';
|
|
|
|
import 'storage/token_store.dart';
|
|
|
|
import 'tenant/tenant_cubit.dart';
|
|
|
|
final sl = GetIt.instance;
|
|
|
|
/// نقطة الإقفال الوحيدة (docs/23 §5): الميزة المطفأة **لا تُسجَّل**، بدل
|
|
/// `if` مبعثرة في الشاشات.
|
|
Future<void> setupInjector() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
sl.registerSingleton<SharedPreferences>(prefs);
|
|
|
|
const secure = FlutterSecureStorage(
|
|
iOptions: IOSOptions(accessibility: KeychainAccessibility.first_unlock),
|
|
);
|
|
|
|
final tokens = TokenStore(secure);
|
|
await tokens.load();
|
|
await _ensureDeviceId(tokens);
|
|
sl.registerSingleton<TokenStore>(tokens);
|
|
|
|
sl.registerSingleton<SettingsCubit>(SettingsCubit(prefs));
|
|
|
|
// انتهاء الجلسة يصعد من طبقة الشبكة إلى `SessionCubit` مباشرة — الراوتر
|
|
// يستمع إليه فيخرج المستخدم بلا `BuildContext` في الـinterceptor.
|
|
sl.registerSingleton<ApiClient>(
|
|
ApiClient.create(
|
|
tokens: tokens,
|
|
onSessionExpired: () async => sl<SessionCubit>().onSessionExpired(),
|
|
),
|
|
);
|
|
|
|
final authRepo = AuthRepository(sl(), sl());
|
|
sl.registerSingleton<AuthRepository>(authRepo);
|
|
sl.registerSingleton<SessionSource>(authRepo);
|
|
sl.registerSingleton<ProfileRepository>(ProfileRepository(sl()));
|
|
sl.registerSingleton<SessionCubit>(
|
|
SessionCubit(authRepo, onAuthenticated: () => sl<PushService>().register()),
|
|
);
|
|
|
|
// تدفّق الدخول قصير العمر: نسخة جديدة لكل دخول، لا نسخة واحدة أبدية.
|
|
sl.registerFactory<LoginCubit>(() => LoginCubit(sl(), sl()));
|
|
|
|
// الخرائط عميل منفصل بلا توكن تريبز — map-saas لا شأن له بجلستنا.
|
|
sl.registerSingleton<AntlaqApi>(AntlaqApi.create());
|
|
sl.registerSingleton<MapsRepository>(MapsRepository(sl()));
|
|
sl.registerSingleton<TripRepository>(TripRepository(sl()));
|
|
sl.registerSingleton<LocationService>(LocationService());
|
|
sl.registerSingleton<RealtimeService>(RealtimeService(tokens));
|
|
|
|
sl.registerSingleton<TenantCubit>(TenantCubit(sl(), prefs));
|
|
sl.registerSingleton<PushService>(PushService(sl()));
|
|
|
|
sl.registerSingleton<DriverRepository>(DriverRepository(sl()));
|
|
sl.registerFactory<OnboardingCubit>(() => OnboardingCubit(sl()));
|
|
|
|
sl.registerSingleton<HistoryRepository>(HistoryRepository(sl()));
|
|
sl.registerFactory<HistoryCubit>(() => HistoryCubit(sl()));
|
|
|
|
sl.registerSingleton<RatingRepository>(RatingRepository(sl()));
|
|
sl.registerFactory<RatingCubit>(() => RatingCubit(sl()));
|
|
|
|
// الدردشة خلف علمها: إطفاؤه يحذف كودها من الـbinary (docs/23 §5).
|
|
if (Features.chat) {
|
|
sl.registerSingleton<ChatRepository>(ChatRepository(sl()));
|
|
sl.registerFactory<ChatCubit>(() => ChatCubit(sl()));
|
|
}
|
|
|
|
// المحفظة خلف علم الاستحقاق: الميزة المطفأة لا تُسجَّل أصلاً (docs/23 §5).
|
|
if (Features.wallet) {
|
|
sl.registerSingleton<WalletRepository>(WalletRepository(sl()));
|
|
sl.registerFactory<WalletCubit>(() => WalletCubit(sl()));
|
|
sl.registerFactory<PayoutCubit>(() => PayoutCubit(sl()));
|
|
}
|
|
|
|
sl.registerFactory<DutyCubit>(() => DutyCubit(
|
|
trips: sl(),
|
|
location: sl(),
|
|
realtime: sl(),
|
|
));
|
|
}
|
|
|
|
/// بصمة الجهاز (`x-device-id`) — يفرضها الخادم عند تفعيل
|
|
/// `AUTH_REQUIRE_DEVICE_BINDING` (docs/38 §8). تُرسَل من الآن كي يُفعَّل
|
|
/// العَلَم لاحقاً بلا تعديل التطبيق: **أرسل أولاً ثم فعّل الخادم**.
|
|
Future<void> _ensureDeviceId(TokenStore tokens) async {
|
|
if (await tokens.deviceId() != null) return;
|
|
final info = DeviceInfoPlugin();
|
|
String? id;
|
|
try {
|
|
if (defaultTargetPlatform == TargetPlatform.android) {
|
|
id = (await info.androidInfo).id;
|
|
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
|
|
id = (await info.iosInfo).identifierForVendor;
|
|
}
|
|
} catch (_) {
|
|
// منصّة لا تجيب — المعرّف الفارغ أفضل من إقلاع فاشل.
|
|
}
|
|
if (id != null && id.isNotEmpty) await tokens.setDeviceId(id);
|
|
}
|