|
|
|
@ -70,9 +70,7 @@ class ProfilePictureViewModel extends ChangeNotifier {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
_cachedImageBytes = base64Decode(imageData);
|
|
|
|
_cachedImageBytes = base64Decode(imageData);
|
|
|
|
_cachedImageDataHash = '${imageData.length}_${imageData.hashCode}';
|
|
|
|
_cachedImageDataHash = '${imageData.length}_${imageData.hashCode}';
|
|
|
|
print('✅ Cached existing profile image');
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
print('❌ Error caching existing image: $e');
|
|
|
|
|
|
|
|
_cachedImageBytes = null;
|
|
|
|
_cachedImageBytes = null;
|
|
|
|
_cachedImageDataHash = null;
|
|
|
|
_cachedImageDataHash = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -85,7 +83,6 @@ class ProfilePictureViewModel extends ChangeNotifier {
|
|
|
|
final currentPatientId = _appState.getAuthenticatedUser()?.patientId;
|
|
|
|
final currentPatientId = _appState.getAuthenticatedUser()?.patientId;
|
|
|
|
|
|
|
|
|
|
|
|
if (currentPatientId != null && currentPatientId != _currentPatientId) {
|
|
|
|
if (currentPatientId != null && currentPatientId != _currentPatientId) {
|
|
|
|
print('🔄 User switched detected: $_currentPatientId -> $currentPatientId');
|
|
|
|
|
|
|
|
_handleUserSwitch(currentPatientId);
|
|
|
|
_handleUserSwitch(currentPatientId);
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -97,8 +94,6 @@ class ProfilePictureViewModel extends ChangeNotifier {
|
|
|
|
final oldPatientId = _currentPatientId;
|
|
|
|
final oldPatientId = _currentPatientId;
|
|
|
|
_currentPatientId = newPatientId;
|
|
|
|
_currentPatientId = newPatientId;
|
|
|
|
|
|
|
|
|
|
|
|
print('🧹 Clearing cache for old user: $oldPatientId');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Clear AppState cache
|
|
|
|
// Clear AppState cache
|
|
|
|
_appState.clearProfileImageCache();
|
|
|
|
_appState.clearProfileImageCache();
|
|
|
|
|
|
|
|
|
|
|
|
@ -113,17 +108,14 @@ class ProfilePictureViewModel extends ChangeNotifier {
|
|
|
|
notifyListeners();
|
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
|
|
|
|
// Load new user's profile image
|
|
|
|
// Load new user's profile image
|
|
|
|
print('📥 Loading profile image for new user: $newPatientId');
|
|
|
|
|
|
|
|
_profileSettingsViewModel.getProfileImage(
|
|
|
|
_profileSettingsViewModel.getProfileImage(
|
|
|
|
patientID: newPatientId,
|
|
|
|
patientID: newPatientId,
|
|
|
|
forceRefresh: true,
|
|
|
|
forceRefresh: true,
|
|
|
|
onSuccess: (data) {
|
|
|
|
onSuccess: (data) {
|
|
|
|
print('✅ Profile image loaded successfully for user: $newPatientId');
|
|
|
|
|
|
|
|
_tryCacheExistingImage();
|
|
|
|
_tryCacheExistingImage();
|
|
|
|
notifyListeners();
|
|
|
|
notifyListeners();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
onError: (error) {
|
|
|
|
onError: (error) {
|
|
|
|
print('❌ Error loading profile image: $error');
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
notifyListeners();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
);
|
|
|
|
@ -133,22 +125,18 @@ class ProfilePictureViewModel extends ChangeNotifier {
|
|
|
|
void loadProfileImage({bool forceRefresh = false}) {
|
|
|
|
void loadProfileImage({bool forceRefresh = false}) {
|
|
|
|
// Check if profile image is already loaded in AppState (skip if forcing refresh)
|
|
|
|
// Check if profile image is already loaded in AppState (skip if forcing refresh)
|
|
|
|
if (!forceRefresh && _appState.getProfileImageData != null && _appState.getProfileImageData!.isNotEmpty) {
|
|
|
|
if (!forceRefresh && _appState.getProfileImageData != null && _appState.getProfileImageData!.isNotEmpty) {
|
|
|
|
print('✅ Profile image already cached in AppState');
|
|
|
|
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
final patientID = _appState.getAuthenticatedUser()?.patientId;
|
|
|
|
final patientID = _appState.getAuthenticatedUser()?.patientId;
|
|
|
|
if (patientID == null) {
|
|
|
|
if (patientID == null) {
|
|
|
|
print('⚠️ Cannot load profile image - no authenticated user');
|
|
|
|
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
print('📥 Loading profile image for patient: $patientID (forceRefresh: $forceRefresh)');
|
|
|
|
|
|
|
|
_profileSettingsViewModel.getProfileImage(
|
|
|
|
_profileSettingsViewModel.getProfileImage(
|
|
|
|
patientID: patientID,
|
|
|
|
patientID: patientID,
|
|
|
|
forceRefresh: forceRefresh,
|
|
|
|
forceRefresh: forceRefresh,
|
|
|
|
onSuccess: (data) {
|
|
|
|
onSuccess: (data) {
|
|
|
|
print('✅ Profile image loaded successfully');
|
|
|
|
|
|
|
|
_tryCacheExistingImage();
|
|
|
|
_tryCacheExistingImage();
|
|
|
|
notifyListeners();
|
|
|
|
notifyListeners();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
@ -188,10 +176,6 @@ class ProfilePictureViewModel extends ChangeNotifier {
|
|
|
|
false, // Don't show files option, only camera and gallery
|
|
|
|
false, // Don't show files option, only camera and gallery
|
|
|
|
(base64String, file) async {
|
|
|
|
(base64String, file) async {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
print('=== Starting image processing ===');
|
|
|
|
|
|
|
|
print('File path: ${file.path}');
|
|
|
|
|
|
|
|
print('File exists: ${await file.exists()}');
|
|
|
|
|
|
|
|
print('Original file size: ${await file.length() / 1024} KB');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Compress and resize the image
|
|
|
|
// Compress and resize the image
|
|
|
|
print('Calling compressAndResizeImage...');
|
|
|
|
print('Calling compressAndResizeImage...');
|
|
|
|
@ -266,14 +250,10 @@ class ProfilePictureViewModel extends ChangeNotifier {
|
|
|
|
onError('No authenticated user found');
|
|
|
|
onError('No authenticated user found');
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
print('📤 Uploading profile image for patient: $patientID');
|
|
|
|
|
|
|
|
_profileSettingsViewModel.uploadProfileImage(
|
|
|
|
_profileSettingsViewModel.uploadProfileImage(
|
|
|
|
patientID: patientID,
|
|
|
|
patientID: patientID,
|
|
|
|
imageData: base64String,
|
|
|
|
imageData: base64String,
|
|
|
|
onSuccess: (data) async {
|
|
|
|
onSuccess: (data) async {
|
|
|
|
print('✅ Profile image uploaded successfully');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Clear old cache first to ensure fresh data
|
|
|
|
// Clear old cache first to ensure fresh data
|
|
|
|
_cachedImageBytes = null;
|
|
|
|
_cachedImageBytes = null;
|
|
|
|
_cachedImageDataHash = null;
|
|
|
|
_cachedImageDataHash = null;
|
|
|
|
@ -287,9 +267,13 @@ class ProfilePictureViewModel extends ChangeNotifier {
|
|
|
|
|
|
|
|
|
|
|
|
// Increment version to trigger targeted rebuild (no full screen refresh)
|
|
|
|
// Increment version to trigger targeted rebuild (no full screen refresh)
|
|
|
|
_profileImageVersion.value++;
|
|
|
|
_profileImageVersion.value++;
|
|
|
|
print('🔄 Profile image version updated to ${_profileImageVersion.value} (targeted rebuild)');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onSuccess(data);
|
|
|
|
final String successMessage = data is String
|
|
|
|
|
|
|
|
? data
|
|
|
|
|
|
|
|
: (data is Map && data['message'] != null
|
|
|
|
|
|
|
|
? data['message'].toString()
|
|
|
|
|
|
|
|
: data?.toString() ?? 'Success');
|
|
|
|
|
|
|
|
onSuccess(successMessage);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
onError: (error) {
|
|
|
|
onError: (error) {
|
|
|
|
print('❌ Error uploading profile image: $error');
|
|
|
|
print('❌ Error uploading profile image: $error');
|
|
|
|
@ -308,7 +292,6 @@ class ProfilePictureViewModel extends ChangeNotifier {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
_cachedImageBytes = base64Decode(imageData!);
|
|
|
|
_cachedImageBytes = base64Decode(imageData!);
|
|
|
|
_cachedImageDataHash = currentHash;
|
|
|
|
_cachedImageDataHash = currentHash;
|
|
|
|
print('🔄 Updated cached image bytes');
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
print('❌ Error decoding profile image: $e');
|
|
|
|
print('❌ Error decoding profile image: $e');
|
|
|
|
_cachedImageBytes = null;
|
|
|
|
_cachedImageBytes = null;
|
|
|
|
@ -326,7 +309,6 @@ class ProfilePictureViewModel extends ChangeNotifier {
|
|
|
|
_cachedImageDataHash = null;
|
|
|
|
_cachedImageDataHash = null;
|
|
|
|
_selectedImage = null;
|
|
|
|
_selectedImage = null;
|
|
|
|
notifyListeners();
|
|
|
|
notifyListeners();
|
|
|
|
print('🧹 Cleared all profile picture cache');
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Check if we should show shimmer loading
|
|
|
|
/// Check if we should show shimmer loading
|
|
|
|
|