Skip to content

Commit

Permalink
Added Password Checker while creating Master Password
Browse files Browse the repository at this point in the history
  • Loading branch information
aritra-tech committed May 18, 2024
1 parent 1e4d0f1 commit 77b8a20
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.geekymusketeers.uncrack.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material.icons.filled.Done
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.unit.dp
import com.geekymusketeers.uncrack.ui.theme.normal14

@Composable
fun PasswordStrengthIndicator(
text: String,
isMet: Boolean,
modifier: Modifier = Modifier
) {

val color = if (isMet) Color.Green else Color.Red
val icon = if (isMet) Icons.Default.Done else Icons.Default.Clear
val iconColor = if (isMet) Color.Green else Color.Red

Row(modifier = modifier.fillMaxWidth()) {
Image(
imageVector = icon,
colorFilter = ColorFilter.tint(iconColor),
contentDescription = null
)

Spacer(modifier = Modifier.width(5.dp))

Text(
text = text,
color = color,
style = normal14
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.geekymusketeers.uncrack.MainActivity
import com.geekymusketeers.uncrack.R
import com.geekymusketeers.uncrack.components.PasswordStrengthIndicator
import com.geekymusketeers.uncrack.components.UCButton
import com.geekymusketeers.uncrack.components.UCTextField
import com.geekymusketeers.uncrack.domain.model.Key
import com.geekymusketeers.uncrack.ui.theme.OnPrimaryContainerLight
import com.geekymusketeers.uncrack.ui.theme.SurfaceTintLight
import com.geekymusketeers.uncrack.ui.theme.SurfaceVariantLight
import com.geekymusketeers.uncrack.ui.theme.UnCrackTheme
import com.geekymusketeers.uncrack.ui.theme.bold30
import com.geekymusketeers.uncrack.ui.theme.normal20
import com.geekymusketeers.uncrack.ui.theme.medium14
import com.geekymusketeers.uncrack.ui.theme.normal16
import com.geekymusketeers.uncrack.util.UtilsKt.findActivity
import dagger.hilt.android.AndroidEntryPoint

Expand Down Expand Up @@ -84,6 +87,8 @@ fun CreateMasterKeyContent(
val context = LocalContext.current
val masterKeyObserver by masterKeyViewModel.masterKeyLiveData.observeAsState("")
val confirmMasterKeyObserver by masterKeyViewModel.confirmMasterKeyLiveData.observeAsState("")
val hasMinLengthObserver by masterKeyViewModel.hasMinLength.observeAsState(false)
val hasSymbolObserver by masterKeyViewModel.hasSymbol.observeAsState(false)
val enableButtonObserver by masterKeyViewModel.enableButtonLiveData.observeAsState(false)
var passwordVisibility by remember { mutableStateOf(false) }
var confirmPasswordVisibility by remember { mutableStateOf(false) }
Expand All @@ -108,7 +113,7 @@ fun CreateMasterKeyContent(

Text(
text = stringResource(R.string.you_will_be_using_master_password_as_a_key_to_unlock_your_passwords),
style = normal20.copy(color = SurfaceTintLight)
style = normal16.copy(color = SurfaceTintLight)
)

Spacer(modifier = Modifier.height(50.dp))
Expand Down Expand Up @@ -141,6 +146,27 @@ fun CreateMasterKeyContent(
}
)

Spacer(modifier = Modifier.height(10.dp))

Column {
Text(
text = stringResource(R.string.master_password_must_include),
style = medium14.copy(OnPrimaryContainerLight)
)
Spacer(modifier = Modifier.height(10.dp))

PasswordStrengthIndicator(
modifier = Modifier.padding(start = 4.dp),
text = stringResource(R.string._9_or_more_characters),
isMet = hasMinLengthObserver
)
PasswordStrengthIndicator(
modifier = Modifier.padding(start = 4.dp),
text = stringResource(R.string.at_least_1_symbol),
isMet = hasSymbolObserver
)
}

Spacer(modifier = Modifier.height(30.dp))

UCTextField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ class KeyViewModel @Inject constructor(
private val _enableButtonLiveData = MutableLiveData<Boolean>()
val enableButtonLiveData: LiveData<Boolean> = _enableButtonLiveData

private val _hasMinLength = MutableLiveData(false)
val hasMinLength: LiveData<Boolean> = _hasMinLength

private val _hasSymbol = MutableLiveData(false)
val hasSymbol: LiveData<Boolean> = _hasSymbol

fun setMasterKey(masterKey: String) {
_masterKeyLiveData.value = masterKey
validatePassword(masterKey)
checkMasterKey()
}

Expand All @@ -43,6 +50,16 @@ class KeyViewModel @Inject constructor(
_masterKeyLiveData.value.isNullOrBlank()
.not() && _confirmMasterKeyLiveData.value.isNullOrBlank().not() &&
_masterKeyLiveData.value == _confirmMasterKeyLiveData.value
isPasswordValid()
}

private fun validatePassword(password: String) {
_hasMinLength.value = password.length >= 9
_hasSymbol.value = password.any { !it.isLetterOrDigit() }
}

private fun isPasswordValid(): Boolean {
return _hasMinLength.value == true && _hasSymbol.value == true
}

fun saveMasterKey(key: Key) = runIO {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@
<string name="theme">Theme</string>
<string name="old_master_password">Old Master Password\n</string>
<string name="new_master_password">New Master Password</string>
<string name="at_least_1_symbol"><![CDATA[At least 1 symbol (*#/&%....)]]></string>
<string name="_9_or_more_characters">9 or more characters</string>
<string name="master_password_must_include">Master Password must include:</string>
<string-array name="accounts">
<item>Others</item>
<item>PayPal</item>
Expand Down

0 comments on commit 77b8a20

Please sign in to comment.