From be6d33e51bc5a97a08cda63dbf0ee4342f99b890 Mon Sep 17 00:00:00 2001 From: aamir-csol Date: Tue, 7 Apr 2026 14:50:52 +0300 Subject: [PATCH] family switch parent child and child 2 --- lib/core/api_consts.dart | 2 +- lib/core/app_state.dart | 43 ++++++++++++--- .../authentication/authentication_repo.dart | 52 ++++++++----------- .../authentication_view_model.dart | 11 +++- 4 files changed, 69 insertions(+), 39 deletions(-) diff --git a/lib/core/api_consts.dart b/lib/core/api_consts.dart index 63d36252..5fb73a37 100644 --- a/lib/core/api_consts.dart +++ b/lib/core/api_consts.dart @@ -4,7 +4,7 @@ import 'package:hmg_patient_app_new/core/enums.dart'; class ApiConsts { static const maxSmallScreen = 660; - static AppEnvironmentTypeEnum appEnvironmentType = AppEnvironmentTypeEnum.uat; + static AppEnvironmentTypeEnum appEnvironmentType = AppEnvironmentTypeEnum.prod; // static String baseUrl = 'https://uat.hmgwebservices.com/'; // HIS API URL UAT diff --git a/lib/core/app_state.dart b/lib/core/app_state.dart index babc7b45..a5a8d72a 100644 --- a/lib/core/app_state.dart +++ b/lib/core/app_state.dart @@ -176,22 +176,53 @@ class AppState { isRatedVisible = value; } - // Dynamic object to track user switch payloads Map _userSwitchPayloads = {}; int _childSwitchCounter = 0; Map get getUserSwitchPayloads => _userSwitchPayloads; void addUserSwitchPayload(dynamic payload, {bool isSuperUser = false}) { + var patientID = payload is Map ? payload['patientid'] : null; + + if (patientID == null) { + if (isSuperUser) { + _userSwitchPayloads['superUser'] = payload; + _childSwitchCounter = 0; + } else { + _childSwitchCounter++; + if (_childSwitchCounter == 1) { + _userSwitchPayloads['child'] = payload; + } else { + _userSwitchPayloads['child$_childSwitchCounter'] = payload; + } + } + return; + } + + String? existingKey; + for (var entry in _userSwitchPayloads.entries) { + if (entry.value is Map && entry.value['PatientID'] == patientID) { + existingKey = entry.key; + break; + } + } + if (isSuperUser) { + if (existingKey != null && existingKey != 'superUser') { + _userSwitchPayloads.remove(existingKey); + } _userSwitchPayloads['superUser'] = payload; - _childSwitchCounter = 0; // Reset counter when super user is set + _childSwitchCounter = 0; } else { - _childSwitchCounter++; - if (_childSwitchCounter == 1) { - _userSwitchPayloads['child'] = payload; + if (existingKey != null) { + _userSwitchPayloads[existingKey] = payload; } else { - _userSwitchPayloads['child$_childSwitchCounter'] = payload; + _childSwitchCounter++; + if (_childSwitchCounter == 1) { + _userSwitchPayloads['child'] = payload; + } else { + _userSwitchPayloads['child$_childSwitchCounter'] = payload; + } } } } diff --git a/lib/features/authentication/authentication_repo.dart b/lib/features/authentication/authentication_repo.dart index 65f15c91..de4bec62 100644 --- a/lib/features/authentication/authentication_repo.dart +++ b/lib/features/authentication/authentication_repo.dart @@ -6,6 +6,7 @@ import 'package:hmg_patient_app_new/core/api_consts.dart'; import 'package:hmg_patient_app_new/core/app_state.dart'; import 'package:hmg_patient_app_new/core/common_models/generic_api_model.dart'; import 'package:hmg_patient_app_new/core/dependencies.dart'; +import 'package:hmg_patient_app_new/core/enums.dart'; import 'package:hmg_patient_app_new/core/exceptions/api_failure.dart'; import 'package:hmg_patient_app_new/features/authentication/models/resp_models/select_device_by_imei.dart'; import 'package:hmg_patient_app_new/services/logger_service.dart'; @@ -271,7 +272,6 @@ class AuthenticationRepoImp implements AuthenticationRepo { } } - // Determine which API to use based on zipCode final zipCode = _getZipCodeFromRequest(newRequest); final isOthersCountry = zipCode == '0'; final endpoint = isFormFamilyFile @@ -282,14 +282,12 @@ class AuthenticationRepoImp implements AuthenticationRepo { ? ApiConsts.checkActivationCodeOthers : ApiConsts.checkActivationCode; - // Store the payload in AppState - try { GenericApiModel? apiResponse; Failure? failure; // Determine the payload to send - final requestBody = isFormFamilyFile + var requestBody = isFormFamilyFile ? familyRequest : isRegister ? newRequest @@ -297,32 +295,24 @@ class AuthenticationRepoImp implements AuthenticationRepo { ? switchRequest : newRequest.toJson(); - if (!isSwitchUser) { - print("Initial Super User Payload"); - appState.addUserSwitchPayload(requestBody, isSuperUser: true); - print(requestBody); - } else if (isSwitchUser) { - // User is switching profiles - if (appState.getSuperUserID == responseID) { - // Switching back to the super user account - print("Switching Back to Super User"); - appState.addUserSwitchPayload(requestBody, isSuperUser: true); - print(requestBody); - } else { - var superUserPaylod = appState.getUserSwitchPayloadByIndex(isSuperUser: true); - if (appState.getSuperUserID == null && superUserPaylod != null) { - if (superUserPaylod != null) { - // requestBody['LoginType'] = loginType; - // requestBody['PatientIdentificationID'] = ""; - // requestBody['DeviceToken'] = newRequest.deviceToken; - // requestBody.removeWhere((key, value) => ['NationalID', 'isDentalAllowedBackend', 'ProjectOutSA', 'ForRegisteration', 'PatientID'].contains(key)); - - } - print("Switch to Family Where Super User Is ${appState.getSuperUserID}"); - appState.addUserSwitchPayload(requestBody, isSuperUser: false); - print(requestBody); - } else { - print("Switching Into Family File Member"); + if (isSwitchUser) { + Map allUsersPayload; + var switchingPatientID; + var superUserPatientID; + bool isSwitchingToSuperUser = false; + + allUsersPayload = appState.getUserSwitchPayloads; + switchingPatientID = switchRequest["PatientID"]; + superUserPatientID = allUsersPayload['superUser']['PatientID']; + isSwitchingToSuperUser = (appState.getSuperUserID == responseID) || (allUsersPayload.containsKey('superUser') && superUserPatientID == switchingPatientID); + + if (isSwitchingToSuperUser) { + var superUserPayload = appState.getUserSwitchPayloadByIndex(isSuperUser: true); + if (superUserPayload != null) { + requestBody['DeviceToken'] = newRequest.deviceToken; + requestBody['ProjectOutSA'] = 0; + requestBody['PatientIdentificationID'] = ""; + requestBody.removeWhere((key, value) => ['NationalID', 'isDentalAllowedBackend', 'ForRegisteration'].contains(key)); } } } @@ -596,7 +586,7 @@ class AuthenticationRepoImp implements AuthenticationRepo { } @override - Future>> getServicePrivilege() { + Future> getServicePrivilege() { Map mapDevice = {}; try { GenericApiModel? apiResponse; diff --git a/lib/features/authentication/authentication_view_model.dart b/lib/features/authentication/authentication_view_model.dart index 8d313774..c9917d8d 100644 --- a/lib/features/authentication/authentication_view_model.dart +++ b/lib/features/authentication/authentication_view_model.dart @@ -385,7 +385,7 @@ class AuthenticationViewModel extends ChangeNotifier { mobileNumber: phoneNumber, selectedLoginType: otpTypeEnum.toInt(), zipCode: selectedCountrySignup.countryCode, - nationalId:nationalIdOrFileNumber, + nationalId: nationalIdOrFileNumber, isFileNo: isForRegister ? isPatientHasFile(request: payload) : false, patientId: isFormFamilyFile ? _appState.getAuthenticatedUser()!.patientId : 0, isForRegister: isForRegister, @@ -615,13 +615,21 @@ class AuthenticationViewModel extends ChangeNotifier { _appState.setSuperUserID = null; _appState.setIsChildLoggedIn = false; activation.list!.first.isParentUser = true; + var allUsersPayload = _appState.getUserSwitchPayloads; + if ((allUsersPayload.containsKey('superUser') && allUsersPayload['superUser'] != null)) { + _appState.addUserSwitchPayload(activation.list!.first.toJson(), isSuperUser: false); + } else { + _appState.addUserSwitchPayload(activation.list!.first.toJson(), isSuperUser: true); + } } else { _appState.setSuperUserID = _appState.getAuthenticatedUser()?.patientId; _appState.setIsChildLoggedIn = true; activation.list!.first.isParentUser = false; + _appState.addUserSwitchPayload(activation.list!.first.toJson(), isSuperUser: false); } } else { activation.list!.first.isParentUser = true; + _appState.addUserSwitchPayload(activation.list!.first.toJson(), isSuperUser: true); } activation.list!.first.bloodGroup = activation.patientBloodType; activation.list!.first.zipCode = selectedCountrySignup == CountryEnum.others ? '0' : selectedCountrySignup.countryCode; @@ -1152,6 +1160,7 @@ class AuthenticationViewModel extends ChangeNotifier { _appState.setVidaPlusProjectList([]); _appState.setHMCProjectList([]); _appState.setProjectsDetailList([]); + _appState.clearUserSwitchPayloads(); await clearDefaultInputValues(); _navigationService.pushAndReplace(AppRoutes.landingScreen); } catch (e) {