Updated architecture
							parent
							
								
									d65dc4ea89
								
							
						
					
					
						commit
						357cf4392e
					
				| @ -1,9 +1,31 @@ | ||||
| import 'package:get_it/get_it.dart'; | ||||
| import 'package:hmg_patient_app_new/core/api/api_client.dart'; | ||||
| import 'package:hmg_patient_app_new/core/app_state.dart'; | ||||
| import 'package:injector/injector.dart'; | ||||
| import 'package:hmg_patient_app_new/features/authentication/authentication_repo.dart'; | ||||
| import 'package:hmg_patient_app_new/features/book_appointments/book_appointments_repo.dart'; | ||||
| import 'package:hmg_patient_app_new/features/common/common_repo.dart'; | ||||
| import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_repo.dart'; | ||||
| import 'package:hmg_patient_app_new/services/cache_service.dart'; | ||||
| import 'package:hmg_patient_app_new/services/logger_service.dart'; | ||||
| import 'package:logger/web.dart'; | ||||
| import 'package:shared_preferences/shared_preferences.dart'; | ||||
| 
 | ||||
| GetIt getIt = GetIt.instance; | ||||
| 
 | ||||
| class AppDependencies { | ||||
|   static void addDependencies() { | ||||
|     Injector injector = Injector.appInstance; | ||||
|     injector.registerSingleton<AppState>(() => AppState()); | ||||
|   static Future<void> addDependencies() async { | ||||
|     // Services | ||||
|     getIt.registerLazySingleton<LoggerService>(() => LoggerServiceImp(logger: Logger(printer: PrettyPrinter(lineLength: 0)))); | ||||
|     final sharedPreferences = await SharedPreferences.getInstance(); | ||||
|     getIt.registerLazySingleton<CacheService>(() => CacheServiceImp(sharedPreferences: sharedPreferences)); | ||||
|     getIt.registerSingleton(AppState()); | ||||
|     getIt.registerLazySingleton<ApiClient>(() => ApiClient(loggerService: getIt())); | ||||
| 
 | ||||
|     // Repositories | ||||
|     getIt.registerLazySingleton<CommonRepo>(() => CommonRepoImp(loggerService: getIt())); | ||||
|     getIt.registerLazySingleton<AuthenticationRepo>(() => AuthenticationRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt())); | ||||
|     getIt.registerLazySingleton<BookAppointmentsRepo>(() => BookAppointmentsRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt())); | ||||
|     getIt.registerLazySingleton<MyAppointmentsRepo>(() => MyAppointmentsRepoImp(loggerService: getIt<LoggerService>(), apiClient: getIt())); | ||||
| 
 | ||||
|   } | ||||
| } | ||||
|  | ||||
| @ -0,0 +1,42 @@ | ||||
| 
 | ||||
| import 'package:equatable/equatable.dart'; | ||||
| 
 | ||||
| abstract class Failure extends Equatable implements Exception { | ||||
|   final String message; | ||||
|   const Failure(this.message); | ||||
| } | ||||
| 
 | ||||
| class ServerFailure extends Failure { | ||||
|   const ServerFailure(super.message); | ||||
| 
 | ||||
|   @override | ||||
|   List<Object?> get props => [message]; | ||||
| } | ||||
| 
 | ||||
| class ConnectivityFailure extends Failure { | ||||
|   const ConnectivityFailure(super.message); | ||||
| 
 | ||||
|   @override | ||||
|   List<Object?> get props => [message]; | ||||
| } | ||||
| 
 | ||||
| class LocalStorageFailure extends Failure { | ||||
|   const LocalStorageFailure(super.message); | ||||
| 
 | ||||
|   @override | ||||
|   List<Object?> get props => [message]; | ||||
| } | ||||
| 
 | ||||
| class DuplicateUsername extends Failure { | ||||
|   const DuplicateUsername({String? message}) : super(message ?? ''); | ||||
| 
 | ||||
|   @override | ||||
|   List<Object?> get props => [message]; | ||||
| } | ||||
| 
 | ||||
| class InvalidCredentials extends Failure { | ||||
|   const InvalidCredentials({String? message}) : super(message ?? ''); | ||||
| 
 | ||||
|   @override | ||||
|   List<Object?> get props => [message]; | ||||
| } | ||||
| @ -1,4 +1,59 @@ | ||||
| class AuthenticationRepo { | ||||
| 
 | ||||
| import 'package:dartz/dartz.dart'; | ||||
| import 'package:hmg_patient_app_new/core/api/api_client.dart'; | ||||
| import 'package:hmg_patient_app_new/core/exceptions/api_exception.dart'; | ||||
| import 'package:hmg_patient_app_new/core/exceptions/api_failure.dart'; | ||||
| import 'package:hmg_patient_app_new/services/logger_service.dart'; | ||||
| 
 | ||||
| abstract class AuthenticationRepo { | ||||
|   Future<Either<Failure, dynamic>> signIn({required String phone, required String password}); | ||||
| } | ||||
| 
 | ||||
| class AuthenticationRepoImp implements AuthenticationRepo { | ||||
|   final ApiClient apiClient; | ||||
|   final LoggerService loggerService; | ||||
| 
 | ||||
|   AuthenticationRepoImp({required this.loggerService, required this.apiClient}); | ||||
| 
 | ||||
|   @override | ||||
|   Future<Either<Failure, dynamic>> signIn({required String phone, required String password}) async { | ||||
|     try { | ||||
|       // Mock API call with delayed response | ||||
|       final result = await Future.delayed( | ||||
|           const Duration(seconds: 2), | ||||
|               () => { | ||||
|             'success': true, | ||||
|             'data': [ | ||||
|               { | ||||
|                 'id': '1', | ||||
|                 'name': 'Dr. Ahmed Hassan', | ||||
|                 'specialty': 'Cardiology', | ||||
|                 'experience': '10 years', | ||||
|                 'rating': 4.8, | ||||
|                 'image': 'https://example.com/doctor1.jpg' | ||||
|               }, | ||||
|             ] | ||||
|           } | ||||
|       ); | ||||
| 
 | ||||
|       if (result != null && result is Map && result['success'] != null && result['success'] != null && result['success'] != false) { | ||||
|         return Right(result); | ||||
|       } else { | ||||
|         loggerService.errorLogs(result.toString()); | ||||
|         return Left(ServerFailure(result.toString())); | ||||
|       } | ||||
|     } on APIException catch (e) { | ||||
|       APIError? apiError; | ||||
|       if (e.error is APIError) { | ||||
|         apiError = e.error as APIError; | ||||
|       } | ||||
|       loggerService.errorLogs(e.toString()); | ||||
|       return Left(ServerFailure(apiError?.errorMessage ?? e.message)); | ||||
|     } catch (e) { | ||||
|       loggerService.errorLogs(e.toString()); | ||||
|       return Left(ServerFailure(e.toString())); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
| @ -1,5 +1,46 @@ | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:hmg_patient_app_new/core/utils/utils.dart'; | ||||
| import 'package:hmg_patient_app_new/features/authentication/authentication_repo.dart'; | ||||
| 
 | ||||
| class AuthenticationViewModel extends ChangeNotifier { | ||||
|   // Add properties and methods related to authentication here | ||||
| } | ||||
|   AuthenticationRepo authenticationRepo; | ||||
| 
 | ||||
|   AuthenticationViewModel({required this.authenticationRepo}); | ||||
| 
 | ||||
|   Future<void> signUp({ | ||||
|     required String phone, | ||||
|     required String password, | ||||
|     Function(dynamic)? onSuccess, | ||||
|     Function(String)? onError, | ||||
|   }) async { | ||||
|     Utils.showLoading(); | ||||
|     final resultEither = await authenticationRepo.signIn( | ||||
|       phone: phone, | ||||
|       password: password, | ||||
|     ); | ||||
| 
 | ||||
|     if (resultEither.isLeft()) { | ||||
| 
 | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     if (resultEither.isRight()) { | ||||
| 
 | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     resultEither.fold( | ||||
|       (failure) { | ||||
|         Utils.hideLoading(); | ||||
|         notifyListeners(); | ||||
|         if (onError != null) onError(failure.message); | ||||
|       }, | ||||
|       (data) { | ||||
|         Utils.hideLoading(); | ||||
|         notifyListeners(); | ||||
|         if (onSuccess != null) onSuccess(data); | ||||
|       }, | ||||
|     ); | ||||
|   } | ||||
| } | ||||
|  | ||||
| @ -0,0 +1,56 @@ | ||||
| import 'package:dartz/dartz.dart'; | ||||
| import 'package:hmg_patient_app_new/core/api/api_client.dart'; | ||||
| import 'package:hmg_patient_app_new/core/exceptions/api_exception.dart'; | ||||
| import 'package:hmg_patient_app_new/core/exceptions/api_failure.dart'; | ||||
| import 'package:hmg_patient_app_new/services/logger_service.dart'; | ||||
| 
 | ||||
| abstract class BookAppointmentsRepo { | ||||
|   Future<Either<Failure, dynamic>> getDoctors(); | ||||
| } | ||||
| 
 | ||||
| class BookAppointmentsRepoImp implements BookAppointmentsRepo { | ||||
|   final ApiClient apiClient; | ||||
|   final LoggerService loggerService; | ||||
| 
 | ||||
|   BookAppointmentsRepoImp({required this.loggerService, required this.apiClient}); | ||||
| 
 | ||||
|   @override | ||||
|   Future<Either<Failure, dynamic>> getDoctors() async { | ||||
|     try { | ||||
|       // Mock API call with delayed response | ||||
|       final result = await Future.delayed( | ||||
|         const Duration(seconds: 2), | ||||
|         () => { | ||||
|           'success': true, | ||||
|           'data': [ | ||||
|             { | ||||
|               'id': '1', | ||||
|               'name': 'Dr. Ahmed Hassan', | ||||
|               'specialty': 'Cardiology', | ||||
|               'experience': '10 years', | ||||
|               'rating': 4.8, | ||||
|               'image': 'https://example.com/doctor1.jpg' | ||||
|             }, | ||||
|           ] | ||||
|         } | ||||
|       ); | ||||
| 
 | ||||
|       if (result != null && result is Map && result['success'] != null && result['success'] != false) { | ||||
|         return Right(result); | ||||
|       } else { | ||||
|         loggerService.errorLogs(result.toString()); | ||||
|         return Left(ServerFailure(result.toString())); | ||||
|       } | ||||
|     } on APIException catch (e) { | ||||
|       APIError? apiError; | ||||
|       if (e.error is APIError) { | ||||
|         apiError = e.error as APIError; | ||||
|       } | ||||
|       loggerService.errorLogs(e.toString()); | ||||
|       return Left(ServerFailure(apiError?.errorMessage ?? e.message)); | ||||
|     } catch (e) { | ||||
|       loggerService.errorLogs(e.toString()); | ||||
|       return Left(ServerFailure(e.toString())); | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @ -0,0 +1,13 @@ | ||||
| import 'package:hmg_patient_app_new/services/logger_service.dart'; | ||||
| 
 | ||||
| abstract class CommonRepo { | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| class CommonRepoImp implements CommonRepo { | ||||
|   LoggerService loggerService; | ||||
| 
 | ||||
|   CommonRepoImp({required this.loggerService}); | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,78 @@ | ||||
| import 'package:dartz/dartz.dart'; | ||||
| import 'package:hmg_patient_app_new/core/api/api_client.dart'; | ||||
| import 'package:hmg_patient_app_new/core/exceptions/api_exception.dart'; | ||||
| import 'package:hmg_patient_app_new/core/exceptions/api_failure.dart'; | ||||
| import 'package:hmg_patient_app_new/services/logger_service.dart'; | ||||
| 
 | ||||
| abstract class MyAppointmentsRepo { | ||||
|   Future<Either<Failure, dynamic>> getMyAppointments(); | ||||
| } | ||||
| 
 | ||||
| class MyAppointmentsRepoImp implements MyAppointmentsRepo { | ||||
|   final ApiClient apiClient; | ||||
|   final LoggerService loggerService; | ||||
| 
 | ||||
|   MyAppointmentsRepoImp({required this.loggerService, required this.apiClient}); | ||||
| 
 | ||||
|   @override | ||||
|   Future<Either<Failure, dynamic>> getMyAppointments() async { | ||||
|     try { | ||||
|       // Mock API call with delayed response | ||||
|       final result = await Future.delayed( | ||||
|           const Duration(seconds: 2), | ||||
|               () => { | ||||
|             'success': true, | ||||
|             'data': [ | ||||
|               { | ||||
|                 'id': '1', | ||||
|                 'doctorName': 'Dr. Ahmed Hassan', | ||||
|                 'specialty': 'Cardiology', | ||||
|                 'appointmentDate': '2025-09-05', | ||||
|                 'appointmentTime': '10:00 AM', | ||||
|                 'status': 'confirmed', | ||||
|                 'clinicName': 'HMG Hospital', | ||||
|                 'patientName': 'John Doe' | ||||
|               }, | ||||
|               { | ||||
|                 'id': '2', | ||||
|                 'doctorName': 'Dr. Sarah Johnson', | ||||
|                 'specialty': 'Dermatology', | ||||
|                 'appointmentDate': '2025-09-08', | ||||
|                 'appointmentTime': '2:30 PM', | ||||
|                 'status': 'pending', | ||||
|                 'clinicName': 'HMG Medical Center', | ||||
|                 'patientName': 'John Doe' | ||||
|               }, | ||||
|               { | ||||
|                 'id': '3', | ||||
|                 'doctorName': 'Dr. Mohamed Ali', | ||||
|                 'specialty': 'Pediatrics', | ||||
|                 'appointmentDate': '2025-08-25', | ||||
|                 'appointmentTime': '11:15 AM', | ||||
|                 'status': 'completed', | ||||
|                 'clinicName': 'HMG Children\'s Clinic', | ||||
|                 'patientName': 'John Doe' | ||||
|               } | ||||
|             ] | ||||
|           } | ||||
|       ); | ||||
| 
 | ||||
|       if (result != null && result is Map && result['success'] != null && result['success'] != false) { | ||||
|         return Right(result); | ||||
|       } else { | ||||
|         loggerService.errorLogs(result.toString()); | ||||
|         return Left(ServerFailure(result.toString())); | ||||
|       } | ||||
|     } on APIException catch (e) { | ||||
|       APIError? apiError; | ||||
|       if (e.error is APIError) { | ||||
|         apiError = e.error as APIError; | ||||
|       } | ||||
|       loggerService.errorLogs(e.toString()); | ||||
|       return Left(ServerFailure(apiError?.errorMessage ?? e.message)); | ||||
|     } catch (e) { | ||||
|       loggerService.errorLogs(e.toString()); | ||||
|       return Left(ServerFailure(e.toString())); | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @ -0,0 +1,14 @@ | ||||
| 
 | ||||
| import 'package:shared_preferences/shared_preferences.dart'; | ||||
| 
 | ||||
| abstract class CacheService { | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| class CacheServiceImp implements CacheService { | ||||
|   SharedPreferences sharedPreferences; | ||||
| 
 | ||||
|   CacheServiceImp({required this.sharedPreferences}); | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
					Loading…
					
					
				
		Reference in New Issue