-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the logic to pop-up biometric sheet
- Loading branch information
1 parent
0913e54
commit 645cd41
Showing
6 changed files
with
188 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/aritradas/uncrack/presentation/settings/BiometricAuthListener.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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.aritradas.uncrack.presentation.settings | ||
|
||
interface BiometricAuthListener { | ||
fun onBiometricAuthSuccess() | ||
fun onUserCancelled() | ||
fun onErrorOccurred() | ||
} |
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
77 changes: 77 additions & 0 deletions
77
app/src/main/java/com/aritradas/uncrack/sharedViewModel/SharedViewModel.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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.aritradas.uncrack.sharedViewModel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.aritradas.uncrack.MainActivity | ||
import com.aritradas.uncrack.data.datastore.DataStoreUtil | ||
import com.aritradas.uncrack.presentation.settings.BiometricAuthListener | ||
import com.aritradas.uncrack.util.AppBioMetricManager | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class SharedViewModel @Inject constructor( | ||
private val appBioMetricManager: AppBioMetricManager, | ||
dataStoreUtil: DataStoreUtil, | ||
) : ViewModel() { | ||
|
||
private val dataStore = dataStoreUtil.dataStore | ||
|
||
private val _loading = MutableStateFlow(true) | ||
val loading: StateFlow<Boolean> = _loading.asStateFlow() | ||
|
||
private val _initAuth = MutableStateFlow(false) | ||
val initAuth: StateFlow<Boolean> = _initAuth.asStateFlow() | ||
|
||
private val _finishActivity = MutableStateFlow(false) | ||
val finishActivity: StateFlow<Boolean> = _finishActivity.asStateFlow() | ||
|
||
init { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
dataStore.data.map { preferences -> | ||
preferences[DataStoreUtil.IS_BIOMETRIC_AUTH_SET_KEY] ?: false | ||
}.collect { biometricAuthState -> | ||
if (biometricAuthState && appBioMetricManager.canAuthenticate()) { | ||
_initAuth.emit(true) | ||
} else { | ||
delay(1_000L) | ||
_loading.emit(false) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun showBiometricPrompt(mainActivity: MainActivity) { | ||
appBioMetricManager.initBiometricPrompt( | ||
activity = mainActivity, | ||
listener = object : BiometricAuthListener { | ||
override fun onBiometricAuthSuccess() { | ||
viewModelScope.launch { | ||
_loading.emit(false) | ||
} | ||
} | ||
|
||
override fun onUserCancelled() { | ||
finishActivity() | ||
} | ||
|
||
override fun onErrorOccurred() { | ||
finishActivity() | ||
} | ||
} | ||
) | ||
} | ||
|
||
private fun finishActivity() { | ||
viewModelScope.launch { | ||
_finishActivity.emit(true) | ||
} | ||
} | ||
} |