Skip to content

Commit 8ad43b8

Browse files
authored
feat: improve biometric login and update build gradle
feat: improve biometric login and update build gradle
2 parents 424a0a7 + 73af3fb commit 8ad43b8

4 files changed

Lines changed: 28 additions & 24 deletions

File tree

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ android {
1313
applicationId "com.opennotes"
1414
minSdk 26
1515
targetSdk 36
16-
versionCode 9
17-
versionName "1.3.5"
16+
versionCode 10
17+
versionName "1.3.6"
1818

1919
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2020
vectorDrawables {

app/src/main/java/com/opennotes/feature_node/presentation/MainActivity.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package com.opennotes.feature_node.presentation
2121
import android.os.Bundle
2222
import androidx.activity.compose.setContent
2323
import androidx.activity.viewModels
24+
import androidx.biometric.BiometricManager
2425
import androidx.biometric.BiometricPrompt
2526
import androidx.compose.animation.EnterTransition
2627
import androidx.compose.animation.ExitTransition
@@ -159,6 +160,16 @@ class MainActivity : FragmentActivity() {
159160
onSuccess: () -> Unit,
160161
onError: (Int, CharSequence) -> Unit
161162
) {
163+
val promptInfo = BiometricPrompt.PromptInfo.Builder()
164+
.setTitle("Unlock OpenNotes")
165+
.setSubtitle("Confirm your identity to access your notes")
166+
.setAllowedAuthenticators(
167+
BiometricManager.Authenticators.BIOMETRIC_STRONG or
168+
BiometricManager.Authenticators.BIOMETRIC_WEAK or
169+
BiometricManager.Authenticators.DEVICE_CREDENTIAL
170+
)
171+
.build()
172+
162173
BiometricPrompt(
163174
this,
164175
ContextCompat.getMainExecutor(this),
@@ -171,13 +182,7 @@ class MainActivity : FragmentActivity() {
171182
}
172183
override fun onAuthenticationFailed() = Unit
173184
}
174-
).authenticate(
175-
BiometricPrompt.PromptInfo.Builder()
176-
.setTitle("Unlock OpenNotes")
177-
.setSubtitle("Confirm your fingerprint to access your notes")
178-
.setNegativeButtonText("Cancel")
179-
.build()
180-
)
185+
).authenticate(promptInfo)
181186
}
182187

183188
private fun handleBiometricError(

app/src/main/java/com/opennotes/feature_node/presentation/settings/SettingsScreen.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ fun SettingsScreen(
117117
val biometricManager = BiometricManager.from(context)
118118
val canAuthenticate = biometricManager.canAuthenticate(
119119
BiometricManager.Authenticators.BIOMETRIC_STRONG or
120-
BiometricManager.Authenticators.BIOMETRIC_WEAK
120+
BiometricManager.Authenticators.BIOMETRIC_WEAK or
121+
BiometricManager.Authenticators.DEVICE_CREDENTIAL
121122
)
122123

123124
if (canAuthenticate != BiometricManager.BIOMETRIC_SUCCESS) {
@@ -153,8 +154,12 @@ fun SettingsScreen(
153154

154155
val promptInfo = BiometricPrompt.PromptInfo.Builder()
155156
.setTitle("Enable biometric lock")
156-
.setSubtitle("Confirm your fingerprint to enable app lock")
157-
.setNegativeButtonText("Cancel")
157+
.setSubtitle("Confirm your identity to enable app lock")
158+
.setAllowedAuthenticators(
159+
BiometricManager.Authenticators.BIOMETRIC_STRONG or
160+
BiometricManager.Authenticators.BIOMETRIC_WEAK or
161+
BiometricManager.Authenticators.DEVICE_CREDENTIAL
162+
)
158163
.build()
159164

160165
prompt.authenticate(promptInfo)

app/src/main/java/com/opennotes/feature_node/presentation/settings/SettingsViewModel.kt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import kotlinx.coroutines.flow.SharingStarted
3636
import kotlinx.coroutines.flow.StateFlow
3737
import kotlinx.coroutines.flow.asStateFlow
3838
import kotlinx.coroutines.flow.first
39+
import kotlinx.coroutines.flow.onEach
3940
import kotlinx.coroutines.flow.receiveAsFlow
4041
import kotlinx.coroutines.flow.stateIn
4142
import kotlinx.coroutines.launch
@@ -48,10 +49,13 @@ class SettingsViewModel @Inject constructor(
4849
) : ViewModel() {
4950

5051
val settings: StateFlow<Settings> = dataStoreRepository.getSettingsFlow()
52+
.onEach {
53+
if (!_isLoaded.value) _isLoaded.value = true
54+
}
5155
.stateIn(
5256
scope = viewModelScope,
53-
started = SharingStarted.WhileSubscribed(5000),
54-
initialValue = Settings() // Default settings while loading
57+
started = SharingStarted.Eagerly, // starts collecting immediately, not waiting for subscribers
58+
initialValue = Settings()
5559
)
5660

5761
private val _isAppUnlocked = MutableStateFlow(false)
@@ -75,16 +79,6 @@ class SettingsViewModel @Inject constructor(
7579
object RequestBiometricAuthForEnable : UiEvent()
7680
}
7781

78-
init {
79-
80-
viewModelScope.launch {
81-
82-
settings.first()
83-
_isLoaded.value = true
84-
}
85-
}
86-
87-
8882
fun updateThemeMode(themeMode: ThemeMode) {
8983
val newSettings = settings.value.copy(themeMode = themeMode)
9084
viewModelScope.launch {

0 commit comments

Comments
 (0)