Skip to content

Commit

Permalink
Refactor user location collection
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-radhika-s committed Feb 7, 2024
1 parent 8b713a4 commit 8901b1e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -31,17 +32,16 @@ class ApiUserService @Inject constructor(
uid: String?,
firebaseToken: String?,
account: GoogleSignInAccount? = null,
phoneNumber: String? = null,
phoneNumber: String? = null
): Triple<Boolean, ApiUser, ApiUserSession> {

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,
Expand All @@ -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,
Expand All @@ -72,15 +74,14 @@ 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)
}
}

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()
Expand All @@ -89,5 +90,4 @@ class ApiUserService @Inject constructor(
suspend fun updateUser(user: ApiUser) {
userRef.document(user.id).set(user).await()
}

}

0 comments on commit 8901b1e

Please sign in to comment.