|
|
|
@ -34,13 +34,12 @@ class _ProfilePictureWidgetState extends State<ProfilePictureWidget> {
|
|
|
|
void initState() {
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
super.initState();
|
|
|
|
_currentPatientId = _appState.getAuthenticatedUser()?.patientId;
|
|
|
|
_currentPatientId = _appState.getAuthenticatedUser()?.patientId;
|
|
|
|
print('🎬 ProfilePictureWidget initState - patient: $_currentPatientId');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Use addPostFrameCallback to ensure widget is built before loading
|
|
|
|
// Use addPostFrameCallback to ensure widget is built before loading
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
if (!mounted) return;
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
|
|
|
|
|
|
final profileVm = context.read<ProfileSettingsViewModel>();
|
|
|
|
|
|
|
|
final patientID = _appState.getAuthenticatedUser()?.patientId;
|
|
|
|
final patientID = _appState.getAuthenticatedUser()?.patientId;
|
|
|
|
|
|
|
|
|
|
|
|
if (patientID == null) {
|
|
|
|
if (patientID == null) {
|
|
|
|
@ -48,20 +47,9 @@ class _ProfilePictureWidgetState extends State<ProfilePictureWidget> {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check if we have data in AppState that matches current user
|
|
|
|
// Always load fresh data from API - do NOT use AppState cache
|
|
|
|
final appStateImageData = _appState.getProfileImageData;
|
|
|
|
print('📥 Loading fresh profile image from API for patient: $patientID');
|
|
|
|
|
|
|
|
_loadProfileImage(forceRefresh: true);
|
|
|
|
if (appStateImageData != null && appStateImageData.isNotEmpty) {
|
|
|
|
|
|
|
|
// Sync to ViewModel if it doesn't have data
|
|
|
|
|
|
|
|
if (profileVm.profileImageData == null || profileVm.profileImageData!.isEmpty) {
|
|
|
|
|
|
|
|
print('🔄 Syncing AppState data to ViewModel');
|
|
|
|
|
|
|
|
profileVm.syncProfileImageFromAppState();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// No cached data - load from API
|
|
|
|
|
|
|
|
print('📥 No cached data - loading from API for patient: $patientID');
|
|
|
|
|
|
|
|
_loadProfileImage(forceRefresh: false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -88,13 +76,25 @@ class _ProfilePictureWidgetState extends State<ProfilePictureWidget> {
|
|
|
|
final oldPatientId = _currentPatientId;
|
|
|
|
final oldPatientId = _currentPatientId;
|
|
|
|
_currentPatientId = currentPatientId;
|
|
|
|
_currentPatientId = currentPatientId;
|
|
|
|
|
|
|
|
|
|
|
|
// Clear the old profile image data
|
|
|
|
// Clear the old profile image data from BOTH AppState and ViewModel
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
final profileVm = context.read<ProfileSettingsViewModel>();
|
|
|
|
final profileVm = context.read<ProfileSettingsViewModel>();
|
|
|
|
|
|
|
|
|
|
|
|
print('🧹 Clearing cache for old user: $oldPatientId');
|
|
|
|
print('🧹 Clearing cache for old user: $oldPatientId');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Clear AppState cache first
|
|
|
|
|
|
|
|
_appState.clearProfileImageCache();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Then clear ViewModel cache
|
|
|
|
profileVm.clearProfileImageCache();
|
|
|
|
profileVm.clearProfileImageCache();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Force rebuild to show default avatar immediately
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
|
|
|
|
_selectedImage = null; // Clear any selected image
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
print('📥 Loading profile image for new user: $currentPatientId');
|
|
|
|
print('📥 Loading profile image for new user: $currentPatientId');
|
|
|
|
// Load the new user's profile image immediately
|
|
|
|
// Load the new user's profile image immediately
|
|
|
|
profileVm.getProfileImage(
|
|
|
|
profileVm.getProfileImage(
|
|
|
|
@ -455,6 +455,7 @@ class _ProfilePictureWidgetState extends State<ProfilePictureWidget> {
|
|
|
|
Widget _buildProfileImage(ProfileSettingsViewModel profileVm) {
|
|
|
|
Widget _buildProfileImage(ProfileSettingsViewModel profileVm) {
|
|
|
|
// Always get fresh user data
|
|
|
|
// Always get fresh user data
|
|
|
|
final currentUser = _appState.getAuthenticatedUser();
|
|
|
|
final currentUser = _appState.getAuthenticatedUser();
|
|
|
|
|
|
|
|
final currentPatientId = currentUser?.patientId;
|
|
|
|
final gender = currentUser?.gender ?? 1;
|
|
|
|
final gender = currentUser?.gender ?? 1;
|
|
|
|
final age = currentUser?.age ?? 0;
|
|
|
|
final age = currentUser?.age ?? 0;
|
|
|
|
|
|
|
|
|
|
|
|
@ -480,11 +481,22 @@ class _ProfilePictureWidgetState extends State<ProfilePictureWidget> {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Use ViewModel data if available, otherwise fall back to AppState
|
|
|
|
// // IMPORTANT: Verify the cached image belongs to the current user
|
|
|
|
// This ensures we show the current logged-in user's image (same as homepage profile icon)
|
|
|
|
// // If _currentPatientId doesn't match currentPatientId, don't show cached image
|
|
|
|
final String? imageData = profileVm.profileImageData ?? _appState.getProfileImageData;
|
|
|
|
// if (currentPatientId != _currentPatientId) {
|
|
|
|
|
|
|
|
// print('⚠️ Patient ID mismatch - showing default avatar (current: $currentPatientId, cached: $_currentPatientId)');
|
|
|
|
|
|
|
|
// return Image.asset(
|
|
|
|
|
|
|
|
// defaultImage,
|
|
|
|
|
|
|
|
// width: 136.w,
|
|
|
|
|
|
|
|
// height: 136.h,
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// IMPORTANT: Use ONLY ViewModel data, DO NOT fallback to AppState
|
|
|
|
|
|
|
|
// This prevents showing stale cached data when switching users
|
|
|
|
|
|
|
|
final String? imageData = profileVm.profileImageData;
|
|
|
|
|
|
|
|
|
|
|
|
print('🖼️ Building profile image - has data: ${imageData != null && imageData.isNotEmpty}, patient: ${currentUser?.patientId}');
|
|
|
|
print('🖼️ Building profile image - has data: ${imageData != null && imageData.isNotEmpty}, patient: $currentPatientId (ViewModel ONLY)');
|
|
|
|
|
|
|
|
|
|
|
|
// Show uploaded image if available
|
|
|
|
// Show uploaded image if available
|
|
|
|
if (imageData != null && imageData.isNotEmpty) {
|
|
|
|
if (imageData != null && imageData.isNotEmpty) {
|
|
|
|
@ -510,7 +522,7 @@ class _ProfilePictureWidgetState extends State<ProfilePictureWidget> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Show default image (no image data or user has no uploaded image)
|
|
|
|
// Show default image (no image data or user has no uploaded image)
|
|
|
|
print('📷 Showing default avatar for user ${currentUser?.patientId}');
|
|
|
|
print('📷 Showing default avatar for user $currentPatientId');
|
|
|
|
return Image.asset(
|
|
|
|
return Image.asset(
|
|
|
|
defaultImage,
|
|
|
|
defaultImage,
|
|
|
|
width: 136.w,
|
|
|
|
width: 136.w,
|
|
|
|
|