-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connected firestore to add and edit products
- Loading branch information
1 parent
f21d396
commit ed25706
Showing
22 changed files
with
565 additions
and
612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
-if class androidx.credentials.CredentialManager | ||
-keep class androidx.credentials.playservices.** { | ||
*; | ||
} | ||
} | ||
-keepclassmembers class com.freshkeeper.screens.home.viewmodel.FoodItem { | ||
public <init>(); | ||
} | ||
-keep class com.google.android.gms.** { *; } | ||
-dontwarn com.google.android.gms.** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 103 additions & 13 deletions
116
app/src/main/java/com/freshkeeper/screens/authentication/GoogleViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,132 @@ | ||
package com.freshkeeper.screens.authentication | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.biometric.BiometricPrompt | ||
import androidx.core.content.ContextCompat | ||
import androidx.credentials.Credential | ||
import androidx.credentials.CustomCredential | ||
import androidx.fragment.app.FragmentActivity | ||
import androidx.navigation.NavController | ||
import com.freshkeeper.ERROR_TAG | ||
import com.freshkeeper.R | ||
import com.freshkeeper.UNEXPECTED_CREDENTIAL | ||
import com.freshkeeper.model.service.AccountService | ||
import com.freshkeeper.screens.AppViewModel | ||
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential | ||
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential.Companion.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import javax.inject.Inject | ||
import kotlin.coroutines.resume | ||
|
||
@HiltViewModel | ||
class GoogleViewModel | ||
@Inject | ||
constructor( | ||
private val accountService: AccountService, | ||
) : AppViewModel() { | ||
private val _errorMessage = MutableStateFlow<Int?>(null) | ||
|
||
fun onSignInWithGoogle( | ||
credential: Credential, | ||
navController: NavController, | ||
context: Context, | ||
activity: FragmentActivity, | ||
) { | ||
launchCatching { | ||
if (credential is CustomCredential && | ||
credential.type == | ||
TYPE_GOOGLE_ID_TOKEN_CREDENTIAL | ||
) { | ||
val googleIdTokenCredential = | ||
GoogleIdTokenCredential.createFrom( | ||
credential.data, | ||
) | ||
accountService.signInWithGoogle(googleIdTokenCredential.idToken) | ||
navController.navigate("home") { launchSingleTop = true } | ||
} else { | ||
Log.e(ERROR_TAG, UNEXPECTED_CREDENTIAL) | ||
if (isBiometricEnabled(context)) { | ||
launchCatching { | ||
val authenticated = authenticateBiometric(context, activity, credential) | ||
if (authenticated) { | ||
navController.navigate("home") { launchSingleTop = true } | ||
} else { | ||
_errorMessage.value = R.string.biometric_auth_failed | ||
} | ||
} | ||
} else { | ||
launchCatching { | ||
if (credential is CustomCredential && | ||
credential.type == | ||
TYPE_GOOGLE_ID_TOKEN_CREDENTIAL | ||
) { | ||
val googleIdTokenCredential = | ||
GoogleIdTokenCredential.createFrom( | ||
credential.data, | ||
) | ||
accountService.signInWithGoogle(googleIdTokenCredential.idToken) | ||
navController.navigate("home") { launchSingleTop = true } | ||
} else { | ||
Log.e(ERROR_TAG, UNEXPECTED_CREDENTIAL) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun isBiometricEnabled(context: Context): Boolean { | ||
val sharedPreferences = | ||
context.getSharedPreferences( | ||
"user_preferences", | ||
Context.MODE_PRIVATE, | ||
) | ||
return sharedPreferences.getBoolean("biometric_enabled", false) | ||
} | ||
|
||
private suspend fun authenticateBiometric( | ||
context: Context, | ||
activity: FragmentActivity, | ||
credential: Credential?, | ||
): Boolean = | ||
suspendCancellableCoroutine { continuation -> | ||
val executor = ContextCompat.getMainExecutor(context) | ||
val biometricPrompt = | ||
BiometricPrompt( | ||
activity, | ||
executor, | ||
object : BiometricPrompt.AuthenticationCallback() { | ||
override fun onAuthenticationSucceeded( | ||
result: BiometricPrompt | ||
.AuthenticationResult, | ||
) { | ||
if (credential is CustomCredential && | ||
credential.type | ||
== TYPE_GOOGLE_ID_TOKEN_CREDENTIAL | ||
) { | ||
val googleIdTokenCredential = | ||
GoogleIdTokenCredential.createFrom(credential.data) | ||
launchCatching { | ||
accountService.signInWithGoogle( | ||
googleIdTokenCredential | ||
.idToken, | ||
) | ||
continuation.resume(true) | ||
} | ||
} else { | ||
continuation.resume(false) | ||
} | ||
} | ||
|
||
override fun onAuthenticationFailed() { | ||
continuation.resume(false) | ||
} | ||
|
||
override fun onAuthenticationError( | ||
errorCode: Int, | ||
errString: CharSequence, | ||
) { | ||
continuation.resume(false) | ||
} | ||
}, | ||
) | ||
|
||
val promptInfo = | ||
BiometricPrompt.PromptInfo | ||
.Builder() | ||
.setTitle("Biometrische Authentifizierung") | ||
.setSubtitle("Bitte authentifizieren Sie sich, um fortzufahren") | ||
.setNegativeButtonText("Abbrechen") | ||
.build() | ||
|
||
biometricPrompt.authenticate(promptInfo) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.