From f12093d6c7e2b60b1fbd8c29449a313fb02daef5 Mon Sep 17 00:00:00 2001 From: gurnxxrpannu Date: Mon, 5 May 2025 12:42:57 +0530 Subject: [PATCH] Fix crash on login by safely handling missing facilityID and parkingPlaceID in UserVanSpDetails - Wrapped Toast in withContext(Dispatchers.Main) to avoid UI thread issues - Added null checks for 'facilityID' and 'parkingPlaceID' - Assigned default values (-1) when fields are missing to prevent crashes - Removed redundant lines that accessed these fields unconditionally - Fix verified using demo credentials provided by maintainers --- .../cho/repositories/UserRepo.kt | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/piramalswasthya/cho/repositories/UserRepo.kt b/app/src/main/java/org/piramalswasthya/cho/repositories/UserRepo.kt index be5c57d95..8b52ed7a6 100644 --- a/app/src/main/java/org/piramalswasthya/cho/repositories/UserRepo.kt +++ b/app/src/main/java/org/piramalswasthya/cho/repositories/UserRepo.kt @@ -256,13 +256,25 @@ class UserRepo @Inject constructor( val servicePointName = vanSp.getString("servicePointName") user?.servicePointName = servicePointName if (!vanSp.has("facilityID")) { - Toast.makeText(context, "Facility ID not found", Toast.LENGTH_LONG).show() + withContext(Dispatchers.Main) { + Toast.makeText(context, "Facility ID not found", Toast.LENGTH_LONG).show() + } delay(3000) + // Set a default value instead of trying to access a non-existent field + user?.facilityID = -1 // Use a default value like -1 to indicate missing data + } else { + val facilityId = vanSp.getInt("facilityID") + user?.facilityID = facilityId } - val facilityId = vanSp.getInt("facilityID") - user?.facilityID = facilityId - user?.parkingPlaceId = vanSp.getInt("parkingPlaceID") - + if (!vanSp.has("parkingPlaceID")) { + user?.parkingPlaceId = -1 // Default value + } else { + user?.parkingPlaceId = vanSp.getInt("parkingPlaceID") + } + // Remove these redundant lines that are causing the crash + // val facilityId = vanSp.getInt("facilityID") + // user?.facilityID = facilityId + // user?.parkingPlaceId = vanSp.getInt("parkingPlaceID") } true } else {