-
-
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.
- Loading branch information
1 parent
b90bffa
commit 793511b
Showing
6 changed files
with
239 additions
and
3 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
186 changes: 186 additions & 0 deletions
186
app/src/main/java/com/geekymusketeers/uncrack/presentation/account/PasswordGenerator.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,186 @@ | ||
package com.geekymusketeers.uncrack.presentation.account | ||
|
||
import android.widget.Toast | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Slider | ||
import androidx.compose.material3.SliderDefaults | ||
import androidx.compose.material3.Switch | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TopAppBarDefaults | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.livedata.observeAsState | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalClipboardManager | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.AnnotatedString | ||
import androidx.compose.ui.unit.dp | ||
import androidx.navigation.NavHostController | ||
import com.geekymusketeers.uncrack.R | ||
import com.geekymusketeers.uncrack.components.UCButton | ||
import com.geekymusketeers.uncrack.components.UCTopAppBar | ||
import com.geekymusketeers.uncrack.ui.theme.BackgroundLight | ||
import com.geekymusketeers.uncrack.ui.theme.OnPrimaryContainerLight | ||
import com.geekymusketeers.uncrack.ui.theme.PrimaryLight | ||
import com.geekymusketeers.uncrack.ui.theme.bold20 | ||
import com.geekymusketeers.uncrack.ui.theme.medium20 | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun PasswordGenerator( | ||
navController: NavHostController, | ||
passwordGeneratorViewModel: PasswordGeneratorViewModel, | ||
modifier: Modifier = Modifier | ||
) { | ||
val context = LocalContext.current | ||
val clipboardManager = LocalClipboardManager.current | ||
val password by passwordGeneratorViewModel.password.observeAsState() | ||
val passwordLength by passwordGeneratorViewModel.passwordLength.observeAsState() | ||
val includeUppercase by passwordGeneratorViewModel.includeUppercase.observeAsState(true) | ||
val includeLowercase by passwordGeneratorViewModel.includeLowercase.observeAsState(true) | ||
val includeNumbers by passwordGeneratorViewModel.includeNumbers.observeAsState(true) | ||
val includeSpecialChars by passwordGeneratorViewModel.includeSpecialChars.observeAsState(true) | ||
|
||
Scaffold( | ||
topBar = { | ||
UCTopAppBar( | ||
modifier = modifier.fillMaxWidth(), | ||
"Password Generator", | ||
colors = TopAppBarDefaults.topAppBarColors(BackgroundLight), | ||
onBackPress = { navController.popBackStack() } | ||
) | ||
} | ||
) { paddingValues -> | ||
Column( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(paddingValues) | ||
.padding(16.dp) | ||
.background(BackgroundLight), | ||
verticalArrangement = Arrangement.spacedBy(16.dp) | ||
) { | ||
|
||
Text( | ||
text = password ?: "", | ||
style = bold20.copy(OnPrimaryContainerLight) | ||
) | ||
|
||
Spacer(modifier = Modifier.height(20.dp)) | ||
|
||
Text( | ||
text = stringResource(id = R.string.password_length), | ||
style = medium20.copy(OnPrimaryContainerLight) | ||
) | ||
|
||
Spacer(modifier = Modifier.height(5.dp)) | ||
|
||
passwordLength?.let { | ||
Slider( | ||
value = it.toFloat(), | ||
onValueChange = { password -> | ||
passwordGeneratorViewModel.updatePasswordLength(password.toInt()) | ||
}, | ||
valueRange = 0f..32f, | ||
colors = SliderDefaults.colors(PrimaryLight) | ||
) | ||
} | ||
|
||
Spacer(modifier = Modifier.height(20.dp)) | ||
|
||
Text( | ||
text = stringResource(R.string.include), | ||
style = medium20.copy(OnPrimaryContainerLight) | ||
) | ||
|
||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
) { | ||
SwitchItem( | ||
label = stringResource(R.string.numbers), | ||
checked = includeNumbers | ||
) { | ||
passwordGeneratorViewModel.updateIncludeNumbers(it) | ||
} | ||
SwitchItem( | ||
label = stringResource(R.string.uppercase_letters), | ||
checked = includeUppercase | ||
) { | ||
passwordGeneratorViewModel.updateIncludeUppercase(it) | ||
} | ||
SwitchItem( | ||
label = stringResource(R.string.lowercase_letters), | ||
checked = includeLowercase | ||
) { | ||
passwordGeneratorViewModel.updateIncludeLowercase(it) | ||
} | ||
SwitchItem( | ||
label = stringResource(R.string.special_symbols), | ||
checked = includeSpecialChars | ||
) { | ||
passwordGeneratorViewModel.updateIncludeSpecialChars(it) | ||
} | ||
} | ||
|
||
Spacer(modifier = Modifier.weight(1f)) | ||
|
||
Row( | ||
modifier = Modifier.fillMaxWidth(), | ||
horizontalArrangement = Arrangement.SpaceBetween | ||
) { | ||
UCButton( | ||
text = stringResource(R.string.generate), | ||
onClick = { | ||
passwordGeneratorViewModel.generatePassword() | ||
}, | ||
leadingIcon = painterResource(id = R.drawable.generate_password) | ||
) | ||
|
||
UCButton( | ||
text = stringResource(R.string.copy), | ||
onClick = { | ||
clipboardManager.setText(AnnotatedString(password.toString())) | ||
Toast.makeText(context, "Copied", Toast.LENGTH_SHORT).show() | ||
}, | ||
leadingIcon = painterResource(id = R.drawable.copy_password) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun SwitchItem( | ||
label: String, | ||
checked: Boolean, | ||
modifier: Modifier = Modifier, | ||
onCheckedChange: (Boolean) -> Unit | ||
) { | ||
Row( | ||
modifier = modifier | ||
.padding(vertical = 8.dp) | ||
.fillMaxWidth(), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Text(text = label, modifier = Modifier.weight(1f)) | ||
|
||
Switch( | ||
checked = checked, | ||
onCheckedChange = onCheckedChange, | ||
modifier = Modifier.padding(start = 8.dp) | ||
) | ||
} | ||
} |
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,20 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:pathData="M8,4V16C8,16.53 8.211,17.039 8.586,17.414C8.961,17.789 9.47,18 10,18H18C18.53,18 19.039,17.789 19.414,17.414C19.789,17.039 20,16.53 20,16V7.242C20,6.976 19.947,6.712 19.843,6.466C19.74,6.221 19.588,5.998 19.398,5.812L16.083,2.57C15.709,2.205 15.208,2 14.685,2H10C9.47,2 8.961,2.211 8.586,2.586C8.211,2.961 8,3.47 8,4Z" | ||
android:strokeLineJoin="round" | ||
android:strokeWidth="2" | ||
android:fillColor="#00000000" | ||
android:strokeColor="#000000" | ||
android:strokeLineCap="round"/> | ||
<path | ||
android:pathData="M16,18V20C16,20.53 15.789,21.039 15.414,21.414C15.039,21.789 14.53,22 14,22H6C5.47,22 4.961,21.789 4.586,21.414C4.211,21.039 4,20.53 4,20V9C4,8.47 4.211,7.961 4.586,7.586C4.961,7.211 5.47,7 6,7H8" | ||
android:strokeLineJoin="round" | ||
android:strokeWidth="2" | ||
android:fillColor="#00000000" | ||
android:strokeColor="#000000" | ||
android:strokeLineCap="round"/> | ||
</vector> |
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,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:pathData="M19,5V19H5V5H19ZM5,3C4.47,3 3.961,3.211 3.586,3.586C3.211,3.961 3,4.47 3,5V19C3,19.53 3.211,20.039 3.586,20.414C3.961,20.789 4.47,21 5,21H19C19.53,21 20.039,20.789 20.414,20.414C20.789,20.039 21,19.53 21,19V5C21,4.47 20.789,3.961 20.414,3.586C20.039,3.211 19.53,3 19,3H5ZM16.5,15C16.5,15.398 16.342,15.779 16.061,16.061C15.779,16.342 15.398,16.5 15,16.5C14.602,16.5 14.221,16.342 13.939,16.061C13.658,15.779 13.5,15.398 13.5,15C13.5,14.602 13.658,14.221 13.939,13.939C14.221,13.658 14.602,13.5 15,13.5C15.398,13.5 15.779,13.658 16.061,13.939C16.342,14.221 16.5,14.602 16.5,15ZM9,10.5C9.398,10.5 9.779,10.342 10.061,10.061C10.342,9.779 10.5,9.398 10.5,9C10.5,8.602 10.342,8.221 10.061,7.939C9.779,7.658 9.398,7.5 9,7.5C8.602,7.5 8.221,7.658 7.939,7.939C7.658,8.221 7.5,8.602 7.5,9C7.5,9.398 7.658,9.779 7.939,10.061C8.221,10.342 8.602,10.5 9,10.5Z" | ||
android:fillColor="#000000"/> | ||
</vector> |
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