-
-
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.
Created Password Generator ViewModel
- Loading branch information
1 parent
1218bb3
commit b90bffa
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
.../main/java/com/geekymusketeers/uncrack/presentation/account/PasswordGeneratorViewModel.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,85 @@ | ||
package com.geekymusketeers.uncrack.presentation.account | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
|
||
@HiltViewModel | ||
class PasswordGeneratorViewModel : ViewModel() { | ||
|
||
private val _password = MutableLiveData<String>() | ||
val password: LiveData<String> = _password | ||
|
||
private val _passwordLength = MutableLiveData<Int>() | ||
val passwordLength: LiveData<Int> = _passwordLength | ||
|
||
private val _includeUppercase = MutableLiveData<Boolean>() | ||
val includeUppercase: LiveData<Boolean> = _includeUppercase | ||
|
||
private val _includeLowercase = MutableLiveData<Boolean>() | ||
val includeLowercase: LiveData<Boolean> = _includeLowercase | ||
|
||
private val _includeNumbers = MutableLiveData<Boolean>() | ||
val includeNumbers: LiveData<Boolean> = _includeNumbers | ||
|
||
private val _includeSpecialChars = MutableLiveData<Boolean>() | ||
val includeSpecialChars: LiveData<Boolean> = _includeSpecialChars | ||
|
||
fun generatePassword() { | ||
_password.value = generatePassword( | ||
_passwordLength.value, | ||
_includeUppercase.value, | ||
_includeLowercase.value, | ||
_includeNumbers.value, | ||
_includeSpecialChars.value | ||
) | ||
} | ||
|
||
fun updatePasswordLength(newLength: Int) { | ||
_passwordLength.value = newLength | ||
} | ||
|
||
fun updateIncludeUppercase(include: Boolean) { | ||
_includeUppercase.value = include | ||
} | ||
|
||
fun updateIncludeLowercase(include: Boolean) { | ||
_includeLowercase.value = include | ||
} | ||
|
||
fun updateIncludeNumbers(include: Boolean) { | ||
_includeNumbers.value = include | ||
} | ||
|
||
fun updateIncludeSpecialChars(include: Boolean) { | ||
_includeSpecialChars.value = include | ||
} | ||
|
||
private fun generatePassword( | ||
length: Int?, | ||
includeUppercase: Boolean?, | ||
includeLowercase: Boolean?, | ||
includeNumbers: Boolean?, | ||
includeSpecialChars: Boolean? | ||
): String { | ||
val charPool = buildString { | ||
if (includeUppercase == true) { | ||
append("ABCDEFGHIJKLMNOPQRSTUVWXYZ") | ||
} | ||
if (includeLowercase == true) { | ||
append("abcdefghijklmnopqrstuvwxyz") | ||
} | ||
if (includeNumbers == true) { | ||
append("0123456789") | ||
} | ||
if (includeSpecialChars == true) { | ||
append("!@#$%^&*()_+") | ||
} | ||
} | ||
|
||
return (1..length!!) | ||
.map { charPool.random() } | ||
.joinToString("") | ||
} | ||
} |