diff --git a/app/src/main/java/com/canopas/yourspace/ui/flow/settings/SettingsViewModel.kt b/app/src/main/java/com/canopas/yourspace/ui/flow/settings/SettingsViewModel.kt index 84f1880a..67a8e936 100644 --- a/app/src/main/java/com/canopas/yourspace/ui/flow/settings/SettingsViewModel.kt +++ b/app/src/main/java/com/canopas/yourspace/ui/flow/settings/SettingsViewModel.kt @@ -37,7 +37,7 @@ class SettingsViewModel @Inject constructor( } private fun getUser() = viewModelScope.launch(appDispatcher.IO) { - authService.getUserFlow().collectLatest { user -> + authService.getUserFlow()?.collectLatest { user -> _state.emit(_state.value.copy(user = user)) } } diff --git a/data/src/main/java/com/canopas/yourspace/data/repository/SpaceRepository.kt b/data/src/main/java/com/canopas/yourspace/data/repository/SpaceRepository.kt index fd32ea30..28e8a7c0 100644 --- a/data/src/main/java/com/canopas/yourspace/data/repository/SpaceRepository.kt +++ b/data/src/main/java/com/canopas/yourspace/data/repository/SpaceRepository.kt @@ -115,8 +115,8 @@ class SpaceRepository @Inject constructor( val spaceId = currentSpaceId if (spaceId.isEmpty()) { - val userId = authService.currentUser?.id ?: "" - return getUserSpaces(userId).firstOrNull()?.sortedBy { it.created_at }?.firstOrNull() + val userId = authService.currentUser?.id + return userId?.let { getUserSpaces(it).firstOrNull()?.sortedBy { it.created_at }?.firstOrNull() } } return getSpace(spaceId) } @@ -185,8 +185,8 @@ class SpaceRepository @Inject constructor( } suspend fun deleteUserSpaces() { - val userId = authService.currentUser?.id ?: "" - val allSpace = getUserSpaces(userId).firstOrNull() ?: emptyList() + val userId = authService.currentUser?.id + val allSpace = userId?.let { getUserSpaces(it).firstOrNull() } ?: emptyList() val ownSpace = allSpace.filter { it.admin_id == userId } val joinedSpace = allSpace.filter { it.admin_id != userId } @@ -195,19 +195,21 @@ class SpaceRepository @Inject constructor( } joinedSpace.forEach { space -> - spaceService.removeUserFromSpace(space.id, userId) + if (userId != null) { + spaceService.removeUserFromSpace(space.id, userId) + } } } suspend fun deleteSpace(spaceId: String) { invitationService.deleteInvitations(spaceId) spaceService.deleteSpace(spaceId) - val userId = authService.currentUser?.id ?: "" + val userId = authService.currentUser?.id currentSpaceId = - getUserSpaces(userId).firstOrNull()?.sortedBy { it.created_at }?.firstOrNull()?.id + userId?.let { getUserSpaces(it).firstOrNull()?.sortedBy { it.created_at }?.firstOrNull()?.id } ?: "" - val user = userService.getUser(userId) + val user = userId?.let { userService.getUser(it) } val updatedSpaceIds = user?.space_ids?.toMutableList()?.apply { remove(spaceId) } ?: return @@ -218,9 +220,11 @@ class SpaceRepository @Inject constructor( } suspend fun leaveSpace(spaceId: String) { - val userId = authService.currentUser?.id ?: "" - spaceService.removeUserFromSpace(spaceId, userId) - val user = userService.getUser(userId) + val userId = authService.currentUser?.id + if (userId != null) { + spaceService.removeUserFromSpace(spaceId, userId) + } + val user = userId?.let { userService.getUser(it) } val updatedSpaceIds = user?.space_ids?.toMutableList()?.apply { remove(spaceId) } ?: return diff --git a/data/src/main/java/com/canopas/yourspace/data/service/auth/AuthService.kt b/data/src/main/java/com/canopas/yourspace/data/service/auth/AuthService.kt index f76e081d..5c8a9f6b 100644 --- a/data/src/main/java/com/canopas/yourspace/data/service/auth/AuthService.kt +++ b/data/src/main/java/com/canopas/yourspace/data/service/auth/AuthService.kt @@ -120,15 +120,15 @@ class AuthService @Inject constructor( } suspend fun deleteAccount() { - val currentUser = currentUser ?: return - apiUserService.deleteUser(currentUser.id) + val currentUser = currentUser + currentUser?.let { apiUserService.deleteUser(it.id) } signOut() } suspend fun generateAndSaveUserKeys(passKey: String) { - val user = currentUser ?: throw IllegalStateException("No user logged in") + val user = currentUser try { - val updatedUser = apiUserService.generateAndSaveUserKeys(user, passKey) + val updatedUser = user?.let { apiUserService.generateAndSaveUserKeys(it, passKey) } currentUser = updatedUser } catch (e: Exception) { throw SecurityException("Failed to generate user keys", e) @@ -136,8 +136,8 @@ class AuthService @Inject constructor( } suspend fun validatePasskey(passKey: String): Boolean { - val user = currentUser ?: return false - val validationResult = apiUserService.validatePasskey(user, passKey) + val user = currentUser + val validationResult = user?.let { apiUserService.validatePasskey(it, passKey) } if (validationResult != null) { userPreferences.storePasskey(passKey) userPreferences.storePrivateKey(validationResult) @@ -145,8 +145,8 @@ class AuthService @Inject constructor( return validationResult != null } - suspend fun getUser(): ApiUser? = apiUserService.getUser(currentUser?.id ?: "") - suspend fun getUserFlow() = apiUserService.getUserFlow(currentUser?.id ?: "") + suspend fun getUser(): ApiUser? = currentUser?.id?.let { apiUserService.getUser(it) } + suspend fun getUserFlow() = currentUser?.id?.let { apiUserService.getUserFlow(it) } suspend fun updateBatteryStatus(batteryPercentage: Float) { val user = currentUser ?: return @@ -157,8 +157,10 @@ class AuthService @Inject constructor( } suspend fun updateUserSessionState(state: Int) { - val currentUser = currentUser ?: return - apiUserService.updateSessionState(currentUser.id, state) + val currentUser = currentUser + if (currentUser != null) { + apiUserService.updateSessionState(currentUser.id, state) + } } } diff --git a/data/src/main/java/com/canopas/yourspace/data/service/location/ApiJourneyService.kt b/data/src/main/java/com/canopas/yourspace/data/service/location/ApiJourneyService.kt index efdf64e9..aba3e12e 100644 --- a/data/src/main/java/com/canopas/yourspace/data/service/location/ApiJourneyService.kt +++ b/data/src/main/java/com/canopas/yourspace/data/service/location/ApiJourneyService.kt @@ -38,16 +38,13 @@ class ApiJourneyService @Inject constructor( get() = userPreferences.currentSpace ?: "" private fun spaceMemberRef(spaceId: String) = - spaceRef.document(spaceId.takeIf { it.isNotBlank() } ?: "null") - .collection(FIRESTORE_COLLECTION_SPACE_MEMBERS) + spaceRef.document(spaceId).collection(FIRESTORE_COLLECTION_SPACE_MEMBERS) private fun spaceMemberJourneyRef(spaceId: String, userId: String) = - spaceMemberRef(spaceId).document(userId.takeIf { it.isNotBlank() } ?: "null") - .collection(Config.FIRESTORE_COLLECTION_USER_JOURNEYS) + spaceMemberRef(spaceId).document(userId).collection(Config.FIRESTORE_COLLECTION_USER_JOURNEYS) private fun spaceGroupKeysRef(spaceId: String) = - spaceRef.document(spaceId.takeIf { it.isNotBlank() } ?: "null") - .collection(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS) + spaceRef.document(spaceId).collection(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS) .document(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS) private suspend fun getGroupKeyDoc(spaceId: String): GroupKeysDoc? { diff --git a/data/src/main/java/com/canopas/yourspace/data/service/location/ApiLocationService.kt b/data/src/main/java/com/canopas/yourspace/data/service/location/ApiLocationService.kt index bbbee0c7..bf14c451 100644 --- a/data/src/main/java/com/canopas/yourspace/data/service/location/ApiLocationService.kt +++ b/data/src/main/java/com/canopas/yourspace/data/service/location/ApiLocationService.kt @@ -52,16 +52,14 @@ class ApiLocationService @Inject constructor( private val spaceRef by lazy { db.collection(FIRESTORE_COLLECTION_SPACES) } private fun spaceMemberRef(spaceId: String) = - spaceRef.document(spaceId.takeIf { it.isNotBlank() } ?: "null") - .collection(FIRESTORE_COLLECTION_SPACE_MEMBERS) + spaceRef.document(spaceId).collection(FIRESTORE_COLLECTION_SPACE_MEMBERS) private fun spaceMemberLocationRef(spaceId: String, userId: String) = - spaceMemberRef(spaceId.takeIf { it.isNotBlank() } ?: "null").document(userId) + spaceMemberRef(spaceId).document(userId) .collection(Config.FIRESTORE_COLLECTION_USER_LOCATIONS) private fun spaceGroupKeysRef(spaceId: String) = - spaceRef.document(spaceId.takeIf { it.isNotBlank() } ?: "null") - .collection(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS) + spaceRef.document(spaceId).collection(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS) .document(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS) suspend fun saveLastKnownLocation(userId: String) {