Skip to content

Commit

Permalink
Manage api calls as per free and premium user
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-megh-l committed Jan 24, 2025
1 parent 1b9a66c commit 78541b5
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ class SignInMethodViewModel @Inject constructor(
_state.emit(_state.value.copy(showGoogleLoading = true))
try {
val firebaseToken = firebaseAuth.signInWithGoogleAuthCredential(account.idToken)
Timber.e("XXXXXX: Current User UID: ${firebaseAuth.currentUserUid}")
authService.verifiedGoogleLogin(
val isNewUser = authService.verifiedGoogleLogin(
firebaseAuth.currentUserUid,
firebaseToken,
account
)
onSignUp()
onSignUp(isNewUser)
_state.emit(_state.value.copy(showGoogleLoading = false))
} catch (e: Exception) {
Timber.e(e, "Failed to sign in with google")
Expand All @@ -67,7 +66,7 @@ class SignInMethodViewModel @Inject constructor(
_state.emit(_state.value.copy(showAppleLoading = true))
try {
val firebaseToken = authResult.user?.getIdToken(true)?.await()
authService.verifiedAppleLogin(
val isNewUser = authService.verifiedAppleLogin(
firebaseAuth.currentUserUid,
firebaseToken?.token ?: "",
authResult.user ?: run {
Expand All @@ -80,7 +79,7 @@ class SignInMethodViewModel @Inject constructor(
return@launch
}
)
onSignUp()
onSignUp(isNewUser)
_state.emit(_state.value.copy(showAppleLoading = false))
} catch (e: Exception) {
Timber.e(e, "Failed to sign in with Apple")
Expand All @@ -97,25 +96,38 @@ class SignInMethodViewModel @Inject constructor(
_state.value = _state.value.copy(error = null)
}

private fun onSignUp() = viewModelScope.launch(appDispatcher.MAIN) {
private fun onSignUp(isNewUSer: Boolean) = viewModelScope.launch(appDispatcher.MAIN) {
val currentUser = authService.currentUser ?: return@launch
val showSetPinScreen = currentUser.identity_key_public?.toBytes()
.contentEquals(currentUser.identity_key_private?.toBytes())
val showEnterPinScreen = !showSetPinScreen && userPreferences.getPasskey()
.isNullOrEmpty()

if (showSetPinScreen) {
if (showSetPinScreen && currentUser.isPremiumUser) {
navigator.navigateTo(
AppDestinations.setPin.path,
popUpToRoute = AppDestinations.signIn.path,
inclusive = true
)
} else if (showEnterPinScreen) {
} else if (showEnterPinScreen && currentUser.isPremiumUser) {
navigator.navigateTo(
AppDestinations.enterPin.path,
popUpToRoute = AppDestinations.signIn.path,
inclusive = true
)
} else if (isNewUSer) {
navigator.navigateTo(
AppDestinations.onboard.path,
popUpToRoute = AppDestinations.signIn.path,
inclusive = true
)
} else {
userPreferences.setOnboardShown(true)
navigator.navigateTo(
AppDestinations.home.path,
popUpToRoute = AppDestinations.signIn.path,
inclusive = true
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const val LOGIN_TYPE_GOOGLE = 1
const val LOGIN_TYPE_APPLE = 2
const val LOGIN_DEVICE_TYPE_ANDROID = 1

const val FREE_USER = 0
const val PREMIUM_USER = 1
private const val FREE_USER = 0
private const val PREMIUM_USER = 1

@Keep
data class ApiUser(
Expand All @@ -28,7 +28,7 @@ data class ApiUser(
val battery_pct: Float? = 0f,
val created_at: Long? = System.currentTimeMillis(),
val updated_at: Long? = System.currentTimeMillis(),
val user_type: Int = PREMIUM_USER,
val user_type: Int = FREE_USER,
val identity_key_public: String? = null,
val identity_key_private: String? = null,
val identity_key_salt: String? = null // Salt for key derivation
Expand Down
Loading

0 comments on commit 78541b5

Please sign in to comment.