105 lines
4.3 KiB
Dart
105 lines
4.3 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:get_it/get_it.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:maibu_satabot_v2/features/devices/data/datasources/impl/device_http_datasource_impl.dart';
|
|
import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_repository.dart';
|
|
import 'package:maibu_satabot_v2/features/devices/domain/usecases/get_user_device_usecase.dart';
|
|
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_cubit.dart';
|
|
import 'package:maibu_satabot_v2/features/remote_control/data/repositories/remote_control_repository_impl.dart';
|
|
import 'package:maibu_satabot_v2/features/remote_control/domain/repositories/remote_control_repository.dart';
|
|
import 'package:maibu_satabot_v2/features/remote_control/domain/usecase/diff_steer_usecase.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../../features/auth/data/datasources/auth_http_datasource.dart';
|
|
import '../../features/auth/data/datasources/impl/auth_http_datasource_impl.dart';
|
|
import '../../features/auth/data/repositories/auth_repository_impl.dart';
|
|
import '../../features/auth/domain/repositories/auth_repository.dart';
|
|
import '../../features/auth/domain/usecases/login_usecase.dart';
|
|
import '../../features/auth/presentation/bloc/auth_cubit.dart';
|
|
import '../../features/auth/presentation/bloc/login_bloc.dart';
|
|
import '../../features/auth/presentation/bloc/login_cubit.dart';
|
|
import '../../features/devices/data/datasources/device_http_datasource.dart';
|
|
import '../../features/devices/data/repositories/device_repository_impl.dart';
|
|
import '../../features/remote_control/presentation/bloc/remote_control_cubit.dart';
|
|
import '../app/app_user_cubit.dart';
|
|
import '../network/dio_client.dart';
|
|
import '../network/net_message_dispatcher.dart';
|
|
import '../network/tcp/tcp_client.dart';
|
|
import '../router/app_router.dart';
|
|
import '../storage/impl/user_storage_impl.dart';
|
|
import '../storage/user_storage.dart';
|
|
|
|
final sl = GetIt.instance;
|
|
|
|
Future<void> init() async {
|
|
/// 1. 底层工具与存储 (最底层)
|
|
/// 1.1 --- Network ---
|
|
/// 1.1.1 Dio:Http客户端
|
|
sl.registerLazySingleton<Dio>(() => DioClient.create());
|
|
|
|
/// 1.1.2 TcpClient:TCP客户端
|
|
sl.registerLazySingleton(() => TcpClient());
|
|
|
|
/// 1.1.3 NetMessageDispatcher:消息调度器,并将 TcpClient 注入给它
|
|
sl.registerLazySingleton(() => NetMessageDispatcher(sl()));
|
|
|
|
/// 1.2 --- 本地存储 (LocalStorage) ---
|
|
/// 1.2.1 SharedPreferences:本地存储库
|
|
final sharedPrefs = await SharedPreferences.getInstance();
|
|
sl.registerLazySingleton<SharedPreferences>(() => sharedPrefs);
|
|
|
|
/// 1.2.2 UserStorage:用户存储库
|
|
sl.registerLazySingleton<UserStorage>(
|
|
// 必须先注册上面本地存储库的实例
|
|
() => UserStorageImpl(sl<SharedPreferences>()),
|
|
);
|
|
|
|
/// 2. 数据源 (DataSource)
|
|
sl.registerLazySingleton<AuthHttpDataSource>(
|
|
() => AuthHttpDataSourceImpl(sl()),
|
|
);
|
|
sl.registerLazySingleton<DeviceHttpDatasource>(
|
|
() => DeviceHttpDatasourceImpl(sl()),
|
|
);
|
|
|
|
/// 3. 仓库 (Repository)
|
|
sl.registerLazySingleton<AuthRepository>(
|
|
() => AuthRepositoryImpl(
|
|
httpRemote: sl(), // 自动寻找已注册的 LoginRemoteDataSource
|
|
localTokenStorage: sl(), // 自动寻找已注册的 TokenStorage
|
|
),
|
|
);
|
|
sl.registerLazySingleton<DeviceRepository>(() => DeviceRepositoryImpl(sl()));
|
|
sl.registerLazySingleton<RemoteControlRepository>(
|
|
() => RemoteControlRepositoryImpl(sl(), sl()),
|
|
);
|
|
|
|
/// 4. 用例 (UseCase)
|
|
sl.registerLazySingleton(() => LoginUseCase(sl()));
|
|
sl.registerLazySingleton(() => GetUserDeviceUseCase(sl()));
|
|
sl.registerLazySingleton(() => DiffSteerUseCase());
|
|
|
|
/// 5. 状态管理 (Cubit/Bloc)
|
|
sl.registerLazySingleton(() => AppUserCubit()); // AuthCubit 依赖它,必须先注册
|
|
sl.registerLazySingleton(() => DevicesCubit(sl(), sl()));
|
|
sl.registerFactory(() => RemoteControlCubit(sl()));
|
|
|
|
/// 6. 认证 (Auth)
|
|
// --- 关键修改点 1: AuthCubit 必须在 GoRouter 之前注册,并传入参数 ---
|
|
sl.registerLazySingleton(
|
|
() => AuthCubit(
|
|
sl<UserStorage>(),
|
|
sl<TcpClient>(),
|
|
sl<AppUserCubit>(),
|
|
sl<NetMessageDispatcher>(),
|
|
),
|
|
);
|
|
|
|
/// 7. 路由 (Router)
|
|
sl.registerSingleton<GoRouter>(createRouter(sl<AuthCubit>()));
|
|
|
|
/// 8. 页面级 Bloc/Cubit (Factory)
|
|
sl.registerFactory(() => LoginBloc(sl()));
|
|
sl.registerFactory(() => LoginCubit(sl(), sl()));
|
|
}
|