Skip to content

Commit

Permalink
refactor openMF#1599: edit password to compose
Browse files Browse the repository at this point in the history
  • Loading branch information
PratyushSingh07 committed Apr 1, 2024
1 parent 0246047 commit f145095
Show file tree
Hide file tree
Showing 4 changed files with 323 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ fun MfPasswordTextField(
contentDescription = "Show password"
)
}
}
},
colors = OutlinedTextFieldDefaults.colors(
focusedBorderColor = Color.Black,
focusedLabelColor = Color.Black
)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package org.mifos.mobilewallet.mifospay.password.presenter

import androidx.lifecycle.ViewModel
import com.mifos.mobilewallet.model.domain.user.UpdateUserEntityPassword
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import org.mifos.mobilewallet.core.base.UseCase
import org.mifos.mobilewallet.core.base.UseCaseHandler
import org.mifos.mobilewallet.core.domain.usecase.user.AuthenticateUser
import org.mifos.mobilewallet.core.domain.usecase.user.UpdateUser
import org.mifos.mobilewallet.mifospay.common.Constants
import org.mifos.mobilewallet.mifospay.core.datastore.PreferencesHelper
import javax.inject.Inject

@HiltViewModel
class EditPasswordViewModel @Inject constructor(
private val mUseCaseHandler: UseCaseHandler,
private val mPreferencesHelper: PreferencesHelper,
private val authenticateUserUseCase: AuthenticateUser,
private val updateUserUseCase: UpdateUser
): ViewModel() {

private val _editPasswordUiState = MutableStateFlow<EditPasswordUiState>(EditPasswordUiState.Loading)
val editPasswordUiState: StateFlow<EditPasswordUiState> = _editPasswordUiState

// fun handleSavePasswordButtonStatus(
// currentPassword: String?,
// newPassword: String?,
// newPasswordRepeat: String?
// ) {
// if (currentPassword == "" || newPassword == "" || newPasswordRepeat == "") {
// mEditPasswordView?.disableSavePasswordButton()
// } else {
// if (newPassword == newPasswordRepeat) {
// mEditPasswordView?.enableSavePasswordButton()
// } else {
// mEditPasswordView?.disableSavePasswordButton()
// }
// }
// }

fun updatePassword(
currentPassword: String?,
newPassword: String?,
newPasswordRepeat: String?
) {
_editPasswordUiState.value = EditPasswordUiState.Loading
if (isNotEmpty(currentPassword) && isNotEmpty(newPassword)
&& isNotEmpty(newPasswordRepeat)
) {
when {
currentPassword == newPassword -> {
_editPasswordUiState.value = EditPasswordUiState.Error(Constants.ERROR_PASSWORDS_CANT_BE_SAME)
}
newPassword?.let {
newPasswordRepeat?.let { it1 ->
isNewPasswordValid(
it,
it1
)
}
} == true -> {
if (currentPassword != null) {
updatePassword(currentPassword, newPassword)
}
}
else -> {
_editPasswordUiState.value = EditPasswordUiState.Error(Constants.ERROR_VALIDATING_PASSWORD)
}
}
} else {
_editPasswordUiState.value = EditPasswordUiState.Error(Constants.ERROR_FIELDS_CANNOT_BE_EMPTY)
}
}

private fun isNotEmpty(str: String?): Boolean {
return !str.isNullOrEmpty()
}

private fun isNewPasswordValid(newPassword: String, newPasswordRepeat: String): Boolean {
return newPassword == newPasswordRepeat
}

private fun updatePassword(currentPassword: String, newPassword: String) {
// authenticate and then update
mUseCaseHandler.execute(authenticateUserUseCase,
AuthenticateUser.RequestValues(
mPreferencesHelper.username,
currentPassword
),
object : UseCase.UseCaseCallback<AuthenticateUser.ResponseValue> {
override fun onSuccess(response: AuthenticateUser.ResponseValue) {
mUseCaseHandler.execute(updateUserUseCase,
UpdateUser.RequestValues(
UpdateUserEntityPassword(
newPassword
),
mPreferencesHelper.userId.toInt()
),
object : UseCase.UseCaseCallback<UpdateUser.ResponseValue?> {
override fun onSuccess(response: UpdateUser.ResponseValue?) {
_editPasswordUiState.value = EditPasswordUiState.Success
}

override fun onError(message: String) {
_editPasswordUiState.value = EditPasswordUiState.Error(message)
}
})
}

override fun onError(message: String) {
_editPasswordUiState.value = EditPasswordUiState.Error("Wrong Password")
}
})
}
}

sealed interface EditPasswordUiState {
data object Loading: EditPasswordUiState
data object Success: EditPasswordUiState
data class Error(val message: String): EditPasswordUiState
}
Original file line number Diff line number Diff line change
@@ -1,55 +1,51 @@
package org.mifos.mobilewallet.mifospay.password.ui

import android.os.Bundle
import android.view.View
import butterknife.OnFocusChange
import androidx.activity.compose.setContent
import butterknife.OnTextChanged
import dagger.hilt.android.AndroidEntryPoint
import org.mifos.mobilewallet.mifospay.R
import org.mifos.mobilewallet.mifospay.base.BaseActivity
import org.mifos.mobilewallet.mifospay.databinding.ActivityEditPasswordBinding
import org.mifos.mobilewallet.mifospay.password.EditPasswordContract
import org.mifos.mobilewallet.mifospay.password.EditPasswordContract.EditPasswordView
import org.mifos.mobilewallet.mifospay.password.presenter.EditPasswordPresenter
import org.mifos.mobilewallet.mifospay.common.Constants
import org.mifos.mobilewallet.mifospay.utils.Toaster
import org.mifos.mobilewallet.mifospay.theme.MifosTheme
import javax.inject.Inject

@AndroidEntryPoint
class EditPasswordActivity : BaseActivity(), EditPasswordView {
@JvmField
@Inject
var mPresenter: EditPasswordPresenter? = null
private var mEditPasswordPresenter: EditPasswordContract.EditPasswordPresenter? = null
class EditPasswordActivity : BaseActivity() {

private lateinit var binding: ActivityEditPasswordBinding
// private lateinit var binding: ActivityEditPasswordBinding
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityEditPasswordBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
setupUi()
mPresenter?.attachView(this)
disableSavePasswordButton()

binding.btnCancel.setOnClickListener {
onCancelClicked()
// binding = ActivityEditPasswordBinding.inflate(layoutInflater)
// val view = binding.root
// setContentView(view)
// setupUi()
// disableSavePasswordButton()

// binding.btnCancel.setOnClickListener {
// onCancelClicked()
// }

// binding.btnSave.setOnClickListener {
// onSaveClicked()
// }
setContent {
MifosTheme {
EditPasswordScreen(
onCancelChanges = { finish() }
)
}
}

binding.btnSave.setOnClickListener {
onSaveClicked()
}

}

@OnFocusChange(
R.id.et_edit_password_current,
R.id.et_edit_password_new,
R.id.et_edit_password_new_repeat
)
fun onPasswordInputFocusChanged() {
handlePasswordInputChanged()
}
// @OnFocusChange(
// R.id.et_edit_password_current,
// R.id.et_edit_password_new,
// R.id.et_edit_password_new_repeat
// )
// fun onPasswordInputFocusChanged() {
// handlePasswordInputChanged()
// }

@OnTextChanged(
R.id.et_edit_password_current,
Expand All @@ -61,53 +57,48 @@ class EditPasswordActivity : BaseActivity(), EditPasswordView {
}

private fun handlePasswordInputChanged() {
val currentPassword = binding.etEditPasswordCurrent.text.toString()
val newPassword = binding.etEditPasswordNew.text.toString()
val newPasswordRepeat = binding.etEditPasswordNewRepeat.text.toString()
mPresenter?.handleSavePasswordButtonStatus(currentPassword, newPassword, newPasswordRepeat)
// val currentPassword = binding.etEditPasswordCurrent.text.toString()
// val newPassword = binding.etEditPasswordNew.text.toString()
// val newPasswordRepeat = binding.etEditPasswordNewRepeat.text.toString()
// mPresenter?.handleSavePasswordButtonStatus(currentPassword, newPassword, newPasswordRepeat)
}

override fun enableSavePasswordButton() {
findViewById<View>(R.id.btn_save).isEnabled = true
}
// override fun enableSavePasswordButton() {
// findViewById<View>(R.id.btn_save).isEnabled = true
// }

override fun disableSavePasswordButton() {
binding.btnSave.isEnabled = false
}

private fun setupUi() {
showCloseButton()
setToolbarTitle(getString(R.string.change_password))
}
// override fun disableSavePasswordButton() {
// binding.btnSave.isEnabled = false
// }

fun onCancelClicked() {
closeActivity()
// closeActivity() -> just finish the compose UI ? same as navigating back?
}

fun onSaveClicked() {
val currentPassword = binding.etEditPasswordCurrent.text.toString()
val newPassword = binding.etEditPasswordCurrent.text.toString()
val newPasswordRepeat = binding.etEditPasswordCurrent.text.toString()
mPresenter?.updatePassword(currentPassword, newPassword, newPasswordRepeat)
// val currentPassword = binding.etEditPasswordCurrent.text.toString()
// val newPassword = binding.etEditPasswordCurrent.text.toString()
// val newPasswordRepeat = binding.etEditPasswordCurrent.text.toString()
// mPresenter?.updatePassword(currentPassword, newPassword, newPasswordRepeat)
}

override fun setPresenter(presenter: EditPasswordContract.EditPasswordPresenter?) {
mEditPasswordPresenter = presenter
}
// override fun setPresenter(presenter: EditPasswordContract.EditPasswordPresenter?) {
// mEditPasswordPresenter = presenter
// }

override fun startProgressBar() {
showProgressDialog(Constants.PLEASE_WAIT)
}
// override fun startProgressBar() {
// showProgressDialog(Constants.PLEASE_WAIT)
// }

override fun stopProgressBar() {
hideProgressDialog()
}
// override fun stopProgressBar() {
// hideProgressDialog()
// }

override fun showError(msg: String?) {
Toaster.showToast(this, msg)
}
// override fun showError(msg: String?) {
// Toaster.showToast(this, msg)
// }

override fun closeActivity() {
finish()
}
// override fun closeActivity() {
// finish()
// }
}
Loading

0 comments on commit f145095

Please sign in to comment.