This commit is contained in:
Mohammed Al-Samarraie
2026-01-13 13:07:31 +03:00
parent ac8a769ff0
commit fa4bee4771
15 changed files with 578 additions and 45 deletions

View File

@@ -2,6 +2,11 @@ import 'package:dio/dio.dart';
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../network/api_client.dart';
import '../../data/datasources/auth_remote_data_source.dart';
import '../../data/datasources/user_local_data_source.dart';
import '../../data/repositories/auth_repository_impl.dart';
import '../../domain/repositories/auth_repository.dart';
import '../../domain/usecases/login_usecase.dart';
final sl = GetIt.instance;
@@ -14,26 +19,29 @@ Future<void> initializeDependencies() async {
sl.registerLazySingleton<SharedPreferences>(() => sharedPreferences);
// Core
sl.registerLazySingleton<ApiClient>(() => ApiClient(dio: sl()));
sl.registerLazySingleton<ApiClient>(
() => ApiClient(dio: sl(), sharedPreferences: sl()),
);
// Data sources will be registered here
// Example:
// sl.registerLazySingleton<AuthRemoteDataSource>(
// () => AuthRemoteDataSourceImpl(apiClient: sl()),
// );
// Data sources
sl.registerLazySingleton<AuthRemoteDataSource>(
() => AuthRemoteDataSourceImpl(apiClient: sl()),
);
sl.registerLazySingleton<UserLocalDataSource>(
() => UserLocalDataSourceImpl(sharedPreferences: sl()),
);
// Repositories will be registered here
// Example:
// sl.registerLazySingleton<AuthRepository>(
// () => AuthRepositoryImpl(
// remoteDataSource: sl(),
// localDataSource: sl(),
// ),
// );
// Repositories
sl.registerLazySingleton<AuthRepository>(
() => AuthRepositoryImpl(
remoteDataSource: sl(),
localDataSource: sl(),
),
);
// Use cases will be registered here
// Example:
// sl.registerLazySingleton(() => LoginUseCase(repository: sl()));
// Use cases
sl.registerLazySingleton(() => LoginUseCase(repository: sl()));
// Blocs will be registered here
// Example:

View File

@@ -1,10 +1,13 @@
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';
class ApiClient {
final Dio dio;
static const String baseUrl = 'YOUR_API_BASE_URL_HERE';
final SharedPreferences? sharedPreferences;
static const String baseUrl = 'https://hrm.go.iq/api';
static const String _tokenKey = 'user_token';
ApiClient({required this.dio}) {
ApiClient({required this.dio, this.sharedPreferences}) {
dio.options = BaseOptions(
baseUrl: baseUrl,
connectTimeout: const Duration(seconds: 30),
@@ -15,6 +18,20 @@ class ApiClient {
},
);
// Add interceptor to add token to requests
dio.interceptors.add(
InterceptorsWrapper(
onRequest: (options, handler) async {
// Get token from SharedPreferences
final token = sharedPreferences?.getString(_tokenKey);
if (token != null && token.isNotEmpty) {
options.headers['Authorization'] = 'Bearer $token';
}
return handler.next(options);
},
),
);
// Add interceptors for logging and error handling
dio.interceptors.add(
LogInterceptor(