diff --git a/app/src/main/java/com/canopas/catchme/ui/flow/auth/methods/SignInMethodViewModel.kt b/app/src/main/java/com/canopas/catchme/ui/flow/auth/methods/SignInMethodViewModel.kt index 8372b097..1b07739d 100644 --- a/app/src/main/java/com/canopas/catchme/ui/flow/auth/methods/SignInMethodViewModel.kt +++ b/app/src/main/java/com/canopas/catchme/ui/flow/auth/methods/SignInMethodViewModel.kt @@ -38,7 +38,9 @@ class SignInMethodViewModel @Inject constructor( try { val firebaseToken = firebaseAuth.signInWithGoogleAuthCredential(account.idToken) val isNewUSer = authService.verifiedGoogleLogin( - firebaseAuth.currentUserUid, firebaseToken, account + firebaseAuth.currentUserUid, + firebaseToken, + account ) onSignUp(isNewUSer) _state.emit(_state.value.copy(showGoogleLoading = false)) diff --git a/app/src/main/java/com/canopas/catchme/ui/flow/auth/verification/PhoneVerificationViewModel.kt b/app/src/main/java/com/canopas/catchme/ui/flow/auth/verification/PhoneVerificationViewModel.kt index ef16d48c..9f891f18 100644 --- a/app/src/main/java/com/canopas/catchme/ui/flow/auth/verification/PhoneVerificationViewModel.kt +++ b/app/src/main/java/com/canopas/catchme/ui/flow/auth/verification/PhoneVerificationViewModel.kt @@ -68,7 +68,8 @@ class PhoneVerificationViewModel @Inject constructor( val isNewUser = authService.verifiedPhoneLogin( firebaseAuth.currentUserUid, - firebaseIdToken, _state.value.phone + firebaseIdToken, + _state.value.phone ) appNavigator.navigateBack( route = AppDestinations.signIn.path, diff --git a/app/src/main/java/com/canopas/catchme/ui/flow/home/map/component/MapMarker.kt b/app/src/main/java/com/canopas/catchme/ui/flow/home/map/component/MapMarker.kt index a21b3738..9d66e205 100644 --- a/app/src/main/java/com/canopas/catchme/ui/flow/home/map/component/MapMarker.kt +++ b/app/src/main/java/com/canopas/catchme/ui/flow/home/map/component/MapMarker.kt @@ -39,7 +39,7 @@ fun MapMarker( keys = arrayOf(user.id, isSelected), state = markerState, title = user.fullName, - zIndex = if(isSelected) 1f else 0f, + zIndex = if (isSelected) 1f else 0f, anchor = Offset(0.0f, 1f), onClick = { onClick() diff --git a/data/src/main/java/com/canopas/catchme/data/service/auth/AuthService.kt b/data/src/main/java/com/canopas/catchme/data/service/auth/AuthService.kt index 7aaadb0d..437ed7e3 100644 --- a/data/src/main/java/com/canopas/catchme/data/service/auth/AuthService.kt +++ b/data/src/main/java/com/canopas/catchme/data/service/auth/AuthService.kt @@ -11,7 +11,7 @@ import javax.inject.Singleton @Singleton class AuthService @Inject constructor( private val userPreferences: UserPreferences, - private val apiUserService: ApiUserService, + private val apiUserService: ApiUserService ) { suspend fun verifiedPhoneLogin( uid: String?, @@ -33,9 +33,8 @@ class AuthService @Inject constructor( uid: String?, firebaseToken: String?, account: GoogleSignInAccount? = null, - phoneNumber: String? = null, + phoneNumber: String? = null ): Boolean { - val (isNewUser, user, session) = apiUserService.saveUser(uid, firebaseToken, account, phoneNumber) saveUser(user, session) diff --git a/data/src/main/java/com/canopas/catchme/data/service/location/ApiLocationService.kt b/data/src/main/java/com/canopas/catchme/data/service/location/ApiLocationService.kt index c0c1cf32..0f1ad3b2 100644 --- a/data/src/main/java/com/canopas/catchme/data/service/location/ApiLocationService.kt +++ b/data/src/main/java/com/canopas/catchme/data/service/location/ApiLocationService.kt @@ -11,16 +11,17 @@ import javax.inject.Singleton @Singleton class ApiLocationService @Inject constructor( - private val db: FirebaseFirestore, + db: FirebaseFirestore, private val locationManager: LocationManager ) { - private val locationRef = db.collection(FirestoreConst.FIRESTORE_COLLECTION_USER_LOCATIONS) + private val userRef = db.collection(FirestoreConst.FIRESTORE_COLLECTION_USERS) + private fun locationRef(userId: String) = userRef.document(userId).collection(FirestoreConst.FIRESTORE_COLLECTION_USER_LOCATIONS) suspend fun saveLastKnownLocation( - userId: String, + userId: String ) { val lastLocation = locationManager.getLastLocation() ?: return - val docRef = locationRef.document() + val docRef = locationRef(userId).document() val location = ApiLocation( id = docRef.id, @@ -30,7 +31,7 @@ class ApiLocationService @Inject constructor( created_at = System.currentTimeMillis() ) - locationRef.document(location.id).set(location).await() + docRef.set(location).await() } suspend fun saveCurrentLocation( @@ -39,7 +40,7 @@ class ApiLocationService @Inject constructor( longitude: Double, recordedAt: Long ) { - val docRef = locationRef.document() + val docRef = locationRef(userId).document() val location = ApiLocation( id = docRef.id, @@ -49,22 +50,22 @@ class ApiLocationService @Inject constructor( created_at = recordedAt ) - locationRef.document(location.id).set(location).await() + docRef.set(location).await() } suspend fun getCurrentLocation(userId: String) = - locationRef.whereEqualTo("user_id", userId) + locationRef(userId).whereEqualTo("user_id", userId) .orderBy("created_at", Query.Direction.DESCENDING).limit(1) .snapshotFlow(ApiLocation::class.java) fun getLocationHistoryQuery(userId: String, from: Long, to: Long) = - locationRef.whereEqualTo("user_id", userId) + locationRef(userId).whereEqualTo("user_id", userId) .whereGreaterThanOrEqualTo("created_at", from) .whereLessThan("created_at", to) .orderBy("created_at", Query.Direction.DESCENDING).limit(8) suspend fun deleteLocations(userId: String) { - locationRef.whereEqualTo("user_id", userId).get().await().documents.forEach { + locationRef(userId).whereEqualTo("user_id", userId).get().await().documents.forEach { it.reference.delete().await() } } diff --git a/data/src/main/java/com/canopas/catchme/data/service/space/ApiSpaceService.kt b/data/src/main/java/com/canopas/catchme/data/service/space/ApiSpaceService.kt index 52b76a10..8b088ede 100644 --- a/data/src/main/java/com/canopas/catchme/data/service/space/ApiSpaceService.kt +++ b/data/src/main/java/com/canopas/catchme/data/service/space/ApiSpaceService.kt @@ -23,7 +23,6 @@ class ApiSpaceService @Inject constructor( private fun spaceMemberRef(spaceId: String) = spaceRef.document(spaceId).collection(FirestoreConst.FIRESTORE_COLLECTION_SPACE_MEMBERS) - suspend fun createSpace(spaceName: String): String { val docRef = spaceRef.document() val spaceId = docRef.id diff --git a/data/src/main/java/com/canopas/catchme/data/service/user/ApiUserService.kt b/data/src/main/java/com/canopas/catchme/data/service/user/ApiUserService.kt index 0ed7f414..30781062 100644 --- a/data/src/main/java/com/canopas/catchme/data/service/user/ApiUserService.kt +++ b/data/src/main/java/com/canopas/catchme/data/service/user/ApiUserService.kt @@ -21,7 +21,8 @@ class ApiUserService @Inject constructor( private val locationService: ApiLocationService ) { private val userRef = db.collection(FIRESTORE_COLLECTION_USERS) - private val sessionRef = db.collection(FirestoreConst.FIRESTORE_COLLECTION_USER_SESSIONS) + private fun sessionRef(userId: String) = + userRef.document(userId).collection(FirestoreConst.FIRESTORE_COLLECTION_USER_SESSIONS) suspend fun getUser(userId: String): ApiUser? { return userRef.document(userId).get().await().toObject(ApiUser::class.java) @@ -31,17 +32,16 @@ class ApiUserService @Inject constructor( uid: String?, firebaseToken: String?, account: GoogleSignInAccount? = null, - phoneNumber: String? = null, + phoneNumber: String? = null ): Triple { - val savedUser = if (uid.isNullOrEmpty()) null else getUser(uid) val isExists = savedUser != null if (isExists) { - val sessionDocRef = sessionRef.document() + val sessionDocRef = sessionRef(savedUser!!.id).document() val session = ApiUserSession( id = sessionDocRef.id, - user_id = savedUser!!.id, + user_id = savedUser.id, device_id = device.getId(), device_name = device.deviceName(), session_active = true, @@ -61,7 +61,9 @@ class ApiUserService @Inject constructor( provider_firebase_id_token = firebaseToken, profile_image = account?.photoUrl?.toString() ) - val sessionDocRef = sessionRef.document() + userRef.document(uid).set(user).await() + + val sessionDocRef = sessionRef(user.id).document() val session = ApiUserSession( id = sessionDocRef.id, user_id = user.id, @@ -72,7 +74,6 @@ class ApiUserService @Inject constructor( battery_status = null ) - userRef.document().set(user).await() sessionDocRef.set(session).await() locationService.saveLastKnownLocation(user.id) return Triple(true, user, session) @@ -80,7 +81,7 @@ class ApiUserService @Inject constructor( } suspend fun deleteUser(userId: String) { - sessionRef.whereEqualTo("user_id", userId).get().await().documents.forEach { + sessionRef(userId).whereEqualTo("user_id", userId).get().await().documents.forEach { it.reference.delete().await() } userRef.document(userId).delete().await() @@ -89,5 +90,4 @@ class ApiUserService @Inject constructor( suspend fun updateUser(user: ApiUser) { userRef.document(user.id).set(user).await() } - }