Add selector for password length and remove selector for multiple passwords.

This commit is contained in:
Pavel Baksy 2024-02-08 23:37:45 +01:00
parent b105f7deef
commit 1365bc509d
2 changed files with 19 additions and 12 deletions

View File

@ -29,7 +29,13 @@ class MainActivity : AppCompatActivity() {
generateButton = findViewById(R.id.generateButton) generateButton = findViewById(R.id.generateButton)
switchWithoutSpaces = findViewById(R.id.switchWithoutSpaces) switchWithoutSpaces = findViewById(R.id.switchWithoutSpaces)
switchSpecialChars = findViewById(R.id.switchSpecialChars) switchSpecialChars = findViewById(R.id.switchSpecialChars)
passwordLengthPicker = findViewById(R.id.passwordLengthPicker)
// Set the range of selectable values for password length (1 to 10)
val defaultPasswordLength = 4
passwordLengthPicker.minValue = 1
passwordLengthPicker.maxValue = 10
passwordLengthPicker.value = defaultPasswordLength
generateButton.setOnClickListener { generateButton.setOnClickListener {
generatePassword() generatePassword()
@ -45,8 +51,8 @@ class MainActivity : AppCompatActivity() {
} }
private fun generatePassword() { private fun generatePassword() {
val passwordLength = passwordLengthPicker.value
val password = generateRandomWords(6) val password = generateRandomWords(passwordLength)
passwordTextView.text = password passwordTextView.text = password
} }
@ -108,13 +114,22 @@ class MainActivity : AppCompatActivity() {
} }
private fun addSpecialCharacters(password: String): String { private fun addSpecialCharacters(password: String): String {
// Add special characters like $, comma, dot, etc. // Insert special character, uppercase character, and digit at random positions within the password
val specialChars = listOf('$', ',', '.', '#', '@', '!', '%', '&') val specialChars = listOf('$', ',', '.', '#', '@', '!', '%', '&')
val random = Random.Default val random = Random.Default
val index = random.nextInt(0, password.length + 1) val index = random.nextInt(0, password.length + 1)
val specialChar = specialChars[random.nextInt(specialChars.size)] val specialChar = specialChars[random.nextInt(specialChars.size)]
return password.substring(0, index) + specialChar + password.substring(index) val uppercaseChar = ('A'..'Z').random() // Generate random uppercase character
val digit = random.nextInt(0, 10) // Generate random digit (number)
val newPassword = StringBuilder(password)
newPassword.insert(index, specialChar)
newPassword.insert(random.nextInt(0, newPassword.length + 1), uppercaseChar)
newPassword.insert(random.nextInt(0, newPassword.length + 1), digit.toString())
//return password.substring(0, index) + specialChar + password.substring(index)
return newPassword.toString()
} }
} }

View File

@ -48,14 +48,6 @@
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"/> android:layout_marginTop="32dp"/>
<NumberPicker
android:id="@+id/numPasswordsPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/passwordLengthPicker"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>
<com.google.android.material.switchmaterial.SwitchMaterial <com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/switchWithoutSpaces" android:id="@+id/switchWithoutSpaces"
android:layout_width="wrap_content" android:layout_width="wrap_content"