Skip to content

Commit

Permalink
feat: giảm độ phức tạp của password
Browse files Browse the repository at this point in the history
- đa ngôn ngữ cho màn thay password và một và component
  • Loading branch information
nqmgaming committed Dec 29, 2024
1 parent 228dd32 commit 2ccf149
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 54 deletions.
2 changes: 1 addition & 1 deletion app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
]
}
],
"minSdkVersionForDexing": 29
"minSdkVersionForDexing": 28
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pwhs.quickmem.presentation.app.settings.user_info.change_password

import android.widget.Toast
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
Expand Down Expand Up @@ -36,7 +37,11 @@ fun ChangePasswordSettingScreen(
viewModel.uiEvent.collect { event ->
when (event) {
is ChangePasswordSettingUiEvent.OnError -> {
Toast.makeText(context, event.errorMessage, Toast.LENGTH_SHORT).show()
Toast.makeText(
context,
context.getString(event.errorMessage),
Toast.LENGTH_SHORT
).show()
}

ChangePasswordSettingUiEvent.OnPasswordChanged -> {
Expand Down Expand Up @@ -85,9 +90,9 @@ fun ChangePasswordSetting(
currentPassword: String = "",
newPassword: String = "",
confirmPassword: String = "",
errorCurrentPassword: String = "",
errorNewPassword: String = "",
errorConfirmPassword: String = "",
@StringRes errorCurrentPassword: Int? = null,
@StringRes errorNewPassword: Int? = null,
@StringRes errorConfirmPassword: Int? = null,
isLoading: Boolean = false,
onCurrentPasswordChanged: (String) -> Unit = {},
onNewPasswordChanged: (String) -> Unit = {},
Expand Down Expand Up @@ -119,7 +124,7 @@ fun ChangePasswordSetting(
value = currentPassword,
onValueChange = onCurrentPasswordChanged,
placeholder = stringResource(R.string.txt_current_password),
errorMessage = errorCurrentPassword,
errorMessage = errorCurrentPassword?.let { stringResource(it) },
isSecure = true
)
SettingTextField(
Expand All @@ -128,7 +133,7 @@ fun ChangePasswordSetting(
value = newPassword,
onValueChange = onNewPasswordChanged,
placeholder = stringResource(R.string.txt_new_password),
errorMessage = errorNewPassword,
errorMessage = errorNewPassword?.let { stringResource(it) },
isSecure = true
)
SettingTextField(
Expand All @@ -137,7 +142,7 @@ fun ChangePasswordSetting(
value = confirmPassword,
onValueChange = onConfirmPasswordChanged,
placeholder = stringResource(R.string.txt_confirm_new_password),
errorMessage = errorConfirmPassword,
errorMessage = errorConfirmPassword?.let { stringResource(it) },
isSecure = true
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.pwhs.quickmem.presentation.app.settings.user_info.change_password

import androidx.annotation.StringRes

sealed class ChangePasswordSettingUiEvent {
data class OnError(val errorMessage: String) : ChangePasswordSettingUiEvent()
data class OnError(@StringRes val errorMessage: Int) : ChangePasswordSettingUiEvent()
data object OnPasswordChanged : ChangePasswordSettingUiEvent()
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.pwhs.quickmem.presentation.app.settings.user_info.change_password

import androidx.annotation.StringRes

data class ChangePasswordSettingUiState(
val email: String = "",
val currentPassword: String = "",
val newPassword: String = "",
val confirmPassword: String = "",
val errorCurrentPassword: String = "",
val errorNewPassword: String = "",
val errorConfirmPassword: String = "",
@StringRes val errorCurrentPassword: Int? = null,
@StringRes val errorNewPassword: Int? = null,
@StringRes val errorConfirmPassword: Int? = null,
val isLoading: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package com.pwhs.quickmem.presentation.app.settings.user_info.change_password
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.pwhs.quickmem.R
import com.pwhs.quickmem.core.datastore.TokenManager
import com.pwhs.quickmem.core.utils.Resources
import com.pwhs.quickmem.domain.model.auth.ChangePasswordRequestModel
import com.pwhs.quickmem.domain.repository.AuthRepository
import com.pwhs.quickmem.util.strongPassword
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -22,7 +22,7 @@ import javax.inject.Inject
class ChangePasswordSettingViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val tokenManager: TokenManager,
private val authRepository: AuthRepository
private val authRepository: AuthRepository,
) : ViewModel() {
private val _uiState = MutableStateFlow(ChangePasswordSettingUiState())
val uiState = _uiState.asStateFlow()
Expand All @@ -43,7 +43,7 @@ class ChangePasswordSettingViewModel @Inject constructor(
_uiState.update {
it.copy(
currentPassword = event.currentPassword,
errorCurrentPassword = ""
errorCurrentPassword = null
)
}
}
Expand All @@ -52,7 +52,7 @@ class ChangePasswordSettingViewModel @Inject constructor(
_uiState.update {
it.copy(
newPassword = event.newPassword,
errorNewPassword = ""
errorNewPassword = null
)
}
}
Expand All @@ -61,7 +61,7 @@ class ChangePasswordSettingViewModel @Inject constructor(
_uiState.update {
it.copy(
confirmPassword = event.confirmPassword,
errorConfirmPassword = ""
errorConfirmPassword = null
)
}
}
Expand All @@ -75,7 +75,7 @@ class ChangePasswordSettingViewModel @Inject constructor(
) {
changePassword()
} else {
_uiEvent.trySend(ChangePasswordSettingUiEvent.OnError("Invalid password"))
_uiEvent.trySend(ChangePasswordSettingUiEvent.OnError(R.string.txt_error_occurred))
}
}
}
Expand All @@ -100,14 +100,12 @@ class ChangePasswordSettingViewModel @Inject constructor(
is Resources.Error -> {
_uiState.update {
it.copy(
errorCurrentPassword = resource.message ?: "An error occurred",
errorCurrentPassword = R.string.txt_error_occurred,
isLoading = false
)
}
_uiEvent.send(
ChangePasswordSettingUiEvent.OnError(
resource.message ?: "An error occurred"
)
ChangePasswordSettingUiEvent.OnError(R.string.txt_error_occurred)
)
}

Expand All @@ -131,24 +129,24 @@ class ChangePasswordSettingViewModel @Inject constructor(
private fun validatePassword(
newPassword: String,
confirmPassword: String,
currentPassword: String
currentPassword: String,
): Boolean {
if (currentPassword.isEmpty() || newPassword.isEmpty() || confirmPassword.isEmpty()) {
_uiState.update {
it.copy(
errorCurrentPassword = "Please fill in all fields",
errorNewPassword = "Please fill in all fields",
errorConfirmPassword = "Please fill in all fields",
errorCurrentPassword = R.string.txt_please_fill_in_all_fields,
errorNewPassword = R.string.txt_please_fill_in_all_fields,
errorConfirmPassword = R.string.txt_please_fill_in_all_fields,
isLoading = false
)
}
return false
}

if (!newPassword.strongPassword()) {
if (newPassword.length < 6) {
_uiState.update {
it.copy(
errorNewPassword = "Password must be at least 8 characters long",
errorNewPassword = R.string.txt_password_is_too_weak_and_required,
isLoading = false
)
}
Expand All @@ -158,8 +156,8 @@ class ChangePasswordSettingViewModel @Inject constructor(
if (newPassword != confirmPassword) {
_uiState.update {
it.copy(
errorNewPassword = "Passwords do not match",
errorConfirmPassword = "Passwords do not match",
errorNewPassword = R.string.txt_passwords_do_not_match,
errorConfirmPassword = R.string.txt_passwords_do_not_match,
isLoading = false
)
}
Expand All @@ -169,7 +167,7 @@ class ChangePasswordSettingViewModel @Inject constructor(
if (currentPassword == newPassword) {
_uiState.update {
it.copy(
errorNewPassword = "New password must be different from the current password",
errorNewPassword = R.string.txt_new_password_must_be_different_from_the_current_password,
isLoading = false
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.pwhs.quickmem.domain.model.auth.LoginRequestModel
import com.pwhs.quickmem.domain.model.auth.ResendEmailRequestModel
import com.pwhs.quickmem.domain.repository.AuthRepository
import com.pwhs.quickmem.util.emailIsValid
import com.pwhs.quickmem.util.strongPassword
import com.revenuecat.purchases.CustomerInfo
import com.revenuecat.purchases.Purchases
import com.revenuecat.purchases.PurchasesError
Expand Down Expand Up @@ -163,7 +162,7 @@ class LoginWithEmailViewModel @Inject constructor(

override fun onReceived(
customerInfo: CustomerInfo,
created: Boolean
created: Boolean,
) {
Timber.d("Customer info: $customerInfo")
}
Expand Down Expand Up @@ -229,7 +228,7 @@ class LoginWithEmailViewModel @Inject constructor(
} else {
_uiState.update { it.copy(emailError = null) }
}
if (!uiState.value.password.strongPassword() || uiState.value.password.isEmpty()) {
if (uiState.value.password.length < 6 || uiState.value.password.isEmpty()) {
_uiState.update { it.copy(passwordError = R.string.txt_password_is_too_weak_and_required) }
isValid = false
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.pwhs.quickmem.domain.repository.AuthRepository
import com.pwhs.quickmem.util.emailIsValid
import com.pwhs.quickmem.util.getNameFromEmail
import com.pwhs.quickmem.util.getUsernameFromEmail
import com.pwhs.quickmem.util.strongPassword
import com.wajahatkarim3.easyvalidation.core.view_ktx.validEmail
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.channels.Channel
Expand Down Expand Up @@ -65,7 +64,7 @@ class SignupWithEmailViewModel @Inject constructor(
}

is SignUpWithEmailUiAction.PasswordChanged -> {
if (!event.password.strongPassword()) {
if (event.password.length < 6) {
_uiState.update {
it.copy(
password = event.password,
Expand Down Expand Up @@ -176,7 +175,7 @@ class SignupWithEmailViewModel @Inject constructor(
} else {
_uiState.update { it.copy(emailError = null) }
}
if (!uiState.value.password.strongPassword() || uiState.value.password.isEmpty()) {
if (uiState.value.password.isEmpty() || uiState.value.password.length < 6) {
_uiState.update { it.copy(passwordError = R.string.txt_password_is_too_weak_and_required) }
isValid = false
} else {
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/java/com/pwhs/quickmem/util/StringExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ fun String.emailIsValid(): Boolean {
return this.validEmail()
}

fun String.strongPassword(): Boolean {
val regex = "^(?=.*[A-Z])(?=.*[!@#$&*])(?=.*[0-9])(?=.*[a-z]).{8,}$".toRegex()
return this.matches(regex)
}

fun String.toTimestamp(): Long? {
if (this.isEmpty()) return null
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
Expand Down
24 changes: 13 additions & 11 deletions app/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@
<string name="txt_email_is_not_registered">Email chưa được đăng ký</string>
<string name="txt_invalid_email_or_password">Email hoặc mật khẩu không hợp lệ</string>
<string name="txt_your_account_has_been_blocked">Tài khoản của bạn đã bị khóa</string>
<string name="txt_password_is_too_weak_and_required">Mật khẩu quá yếu! Mật khẩu phải chứa ít nhất 8 ký tự, 1 chữ cái viết hoa, 1 chữ cái viết thường, 1 số và 1 ký tự đặc biệt.</string>
<string name="txt_password_is_too_weak_and_required">Mật khẩu cần có ít nhất 6 ký tự.</string>
<string name="txt_birthday_is_required">Ngày sinh là bắt buộc</string>
<string name="txt_email_is_already_registered">Email đã được đăng ký</string>
<string name="txt_something_went_wrong">Đã xảy ra lỗi</string>
Expand Down Expand Up @@ -648,14 +648,16 @@
<string name="txt_study_set_not_found">Không tìm thấy bộ học</string>
<string name="txt_flashcard_not_found">Không tìm thấy thẻ</string>
<string name="txt_study_set_copied">Tạo bản sao mới thành công!</string>
<string name="txt_title_must_be_at_least_1_characters">Title must be at least 1 characters</string>
<string name="txt_failed_to_update_folder">Failed to update folder</string>
<string name="txt_sets">%1$s sets</string>
<string name="txt_please_enter_a_search_query">Please enter a search query</string>
<string name="txt_error_create_folder">An error when create folder</string>
<string name="txt_search_recent_query_not_found">Search recent query not found.</string>
<string name="txt_failed_to_clear_search_history">Failed to clear search history</string>
<string name="txt_password_changed_successfully">Password changed successfully</string>
<string name="txt_password_is_incorrect">Password is incorrect</string>
<string name="txt_toggle_password_visibility">Toggle password visibility</string>
<string name="txt_title_must_be_at_least_1_characters">Tiêu đề phải có ít nhất 1 ký tự</string>
<string name="txt_failed_to_update_folder">Cập nhật thư mục thất bại</string>
<string name="txt_sets">%1$s bộ</string>
<string name="txt_please_enter_a_search_query">Vui lòng nhập truy vấn tìm kiếm</string>
<string name="txt_error_create_folder">Lỗi khi tạo thư mục</string>
<string name="txt_search_recent_query_not_found">Không tìm thấy truy vấn tìm kiếm gần đây</string>
<string name="txt_failed_to_clear_search_history">Xóa lịch sử tìm kiếm thất bại</string>
<string name="txt_password_changed_successfully">Đổi mật khẩu thành công</string>
<string name="txt_password_is_incorrect">Mật khẩu không chính xác</string>
<string name="txt_toggle_password_visibility">Chuyển đổi hiển thị mật khẩu</string>
<string name="txt_new_password_must_be_different_from_the_current_password">Mật khẩu mới phải khác mật khẩu hiện tại</string>
<string name="txt_please_fill_in_all_fields">Vui lòng điền vào tất cả các trường</string>
</resources>
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@
<string name="txt_email_is_not_registered">Email is not registered</string>
<string name="txt_invalid_email_or_password">Invalid email or password</string>
<string name="txt_your_account_has_been_blocked">Your account has been blocked</string>
<string name="txt_password_is_too_weak_and_required">Password is too weak! Password must contain at least 8 characters, 1 uppercase letter, 1 lowercase letter, 1 number, and 1 special character.</string>
<string name="txt_password_is_too_weak_and_required">Password length must be at least 6 characters</string>
<string name="txt_birthday_is_required">Birthday is required</string>
<string name="txt_email_is_already_registered">Email is already registered</string>
<string name="txt_something_went_wrong">Something went wrong</string>
Expand Down Expand Up @@ -661,4 +661,6 @@
<string name="txt_password_changed_successfully">Password changed successfully</string>
<string name="txt_password_is_incorrect">Password is incorrect</string>
<string name="txt_toggle_password_visibility">Toggle password visibility</string>
<string name="txt_new_password_must_be_different_from_the_current_password">New password must be different from the current password</string>
<string name="txt_please_fill_in_all_fields">Please fill in all fields</string>
</resources>

0 comments on commit 2ccf149

Please sign in to comment.