Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sneh-s committed Jan 10, 2025
1 parent 5584ffb commit a8681e8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 }

Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,33 +120,33 @@ 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)
}
}

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)
}
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
Expand All @@ -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)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a8681e8

Please sign in to comment.