-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
navigation above ChangePasswordDialog
- Loading branch information
babichev.a
committed
Oct 11, 2024
1 parent
ec3c499
commit 8726fc0
Showing
8 changed files
with
62 additions
and
40 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
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
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
13 changes: 6 additions & 7 deletions
13
...n/com/softartdev/notedelight/shared/presentation/settings/security/change/ChangeResult.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 |
---|---|---|
@@ -1,12 +1,11 @@ | ||
package com.softartdev.notedelight.shared.presentation.settings.security.change | ||
|
||
sealed class ChangeResult { | ||
object InitState: ChangeResult() | ||
object Loading: ChangeResult() | ||
object Success: ChangeResult() | ||
object OldEmptyPasswordError: ChangeResult() | ||
object NewEmptyPasswordError: ChangeResult() | ||
object PasswordsNoMatchError: ChangeResult() | ||
object IncorrectPasswordError: ChangeResult() | ||
data object InitState: ChangeResult() | ||
data object Loading: ChangeResult() | ||
data object OldEmptyPasswordError: ChangeResult() | ||
data object NewEmptyPasswordError: ChangeResult() | ||
data object PasswordsNoMatchError: ChangeResult() | ||
data object IncorrectPasswordError: ChangeResult() | ||
data class Error(val message: String?): ChangeResult() | ||
} |
57 changes: 40 additions & 17 deletions
57
...om/softartdev/notedelight/shared/presentation/settings/security/change/ChangeViewModel.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 |
---|---|---|
@@ -1,36 +1,59 @@ | ||
package com.softartdev.notedelight.shared.presentation.settings.security.change | ||
|
||
import com.softartdev.notedelight.shared.base.BaseStateViewModel | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.softartdev.notedelight.shared.navigation.Router | ||
import com.softartdev.notedelight.shared.usecase.crypt.ChangePasswordUseCase | ||
import com.softartdev.notedelight.shared.usecase.crypt.CheckPasswordUseCase | ||
import io.github.aakira.napier.Napier | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.IO | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
|
||
class ChangeViewModel( | ||
private val checkPasswordUseCase: CheckPasswordUseCase, | ||
private val changePasswordUseCase: ChangePasswordUseCase | ||
) : BaseStateViewModel<ChangeResult>() { | ||
|
||
override var initResult: ChangeResult? = ChangeResult.InitState | ||
override val loadingResult: ChangeResult = ChangeResult.Loading | ||
private val changePasswordUseCase: ChangePasswordUseCase, | ||
private val router: Router, | ||
) : ViewModel() { | ||
private val mutableStateFlow: MutableStateFlow<ChangeResult> = MutableStateFlow( | ||
value = ChangeResult.InitState | ||
) | ||
val resultStateFlow: StateFlow<ChangeResult> = mutableStateFlow | ||
|
||
fun checkChange( | ||
oldPassword: CharSequence, | ||
newPassword: CharSequence, | ||
repeatNewPassword: CharSequence | ||
) = launch { | ||
when { | ||
oldPassword.isEmpty() -> ChangeResult.OldEmptyPasswordError | ||
newPassword.isEmpty() -> ChangeResult.NewEmptyPasswordError | ||
newPassword.toString() != repeatNewPassword.toString() -> ChangeResult.PasswordsNoMatchError | ||
else -> when (checkPasswordUseCase(oldPassword)) { | ||
true -> { | ||
) = viewModelScope.launch(context = Dispatchers.IO) { | ||
mutableStateFlow.value = ChangeResult.Loading | ||
try { | ||
when { | ||
oldPassword.isEmpty() -> { | ||
mutableStateFlow.value = ChangeResult.OldEmptyPasswordError | ||
} | ||
newPassword.isEmpty() -> { | ||
mutableStateFlow.value = ChangeResult.NewEmptyPasswordError | ||
} | ||
newPassword.toString() != repeatNewPassword.toString() -> { | ||
mutableStateFlow.value = ChangeResult.PasswordsNoMatchError | ||
} | ||
checkPasswordUseCase(oldPassword) -> { | ||
changePasswordUseCase(oldPassword, newPassword) | ||
ChangeResult.Success | ||
navigateUp() | ||
} | ||
else -> { | ||
mutableStateFlow.value = ChangeResult.IncorrectPasswordError | ||
} | ||
false -> ChangeResult.IncorrectPasswordError | ||
} | ||
} catch (e: Throwable) { | ||
Napier.e("❌", e) | ||
mutableStateFlow.value = ChangeResult.Error(e.message) | ||
} | ||
} | ||
|
||
override fun errorResult(throwable: Throwable): ChangeResult = | ||
ChangeResult.Error(throwable.message) | ||
fun navigateUp() = viewModelScope.launch(context = Dispatchers.Main) { | ||
router.popBackStack() | ||
} | ||
} |