Commit f54fd033 authored by Antonia Wüst's avatar Antonia Wüst

fix #64

parent f66f6925
......@@ -10,6 +10,7 @@ import de.akamu.tudarmstadt.data.user.UserDataSourceImpl
import de.akamu.tudarmstadt.interactors.UserInteractor
import de.akamu.tudarmstadt.model.User
import de.akamu.tudarmstadt.util.AppUserUtil
import de.akamu.tudarmstadt.util.Constants
import kotlinx.android.synthetic.main.activity_change_password.*
class ChangePasswordActivity :
......@@ -19,7 +20,7 @@ class ChangePasswordActivity :
override lateinit var presenter: ChangePasswordContract.Presenter
private var user : User? = null
private var user: User? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -38,7 +39,8 @@ class ChangePasswordActivity :
}
override fun onClick(v: View?) {
if(v!!.id == changepw_confirm_btn.id) {
if (v!!.id == changepw_confirm_btn.id) {
enableChangePasswordButton(false)
val currentPw = changepw_current.text.toString()
val newPw = changepw_new.text.toString()
val newPw2 = changepw_confirm_new.text.toString()
......@@ -47,13 +49,17 @@ class ChangePasswordActivity :
}
override fun showChangePasswordSuccess() {
Snackbar.make(findViewById(R.id.coordinator_change_password),
R.string.password_changed, Snackbar.LENGTH_LONG).show()
Snackbar.make(
findViewById(R.id.coordinator_change_password),
R.string.password_changed, Snackbar.LENGTH_LONG
).show()
}
override fun showChangePasswordFailed(reason: String) {
Snackbar.make(findViewById(R.id.coordinator_change_password),
getString(R.string.failed_to_change_password) + reason, Snackbar.LENGTH_LONG).show()
Snackbar.make(
findViewById(R.id.coordinator_change_password),
getString(R.string.failed_to_change_password) + reason, Snackbar.LENGTH_LONG
).show()
}
override fun hideProgress() {
......@@ -66,19 +72,28 @@ class ChangePasswordActivity :
}
override fun showPasswordIncorrect() {
Snackbar.make(findViewById(R.id.coordinator_change_password),
getString(R.string.failed_to_change_password) + getString(R.string.error_invalid_password), Snackbar.LENGTH_LONG).show()
Snackbar.make(
findViewById(R.id.coordinator_change_password),
getString(R.string.failed_to_change_password) + getString(R.string.error_invalid_password),
Snackbar.LENGTH_LONG
).show()
changepw_current.error = getString(R.string.error_invalid_password)
}
override fun showPasswordsMismatch() {
Snackbar.make(findViewById(R.id.coordinator_change_password),
getString(R.string.failed_to_change_password) + getString(R.string.error_invalid_password_notequal), Snackbar.LENGTH_LONG).show()
Snackbar.make(
findViewById(R.id.coordinator_change_password),
getString(R.string.failed_to_change_password) + getString(R.string.error_invalid_password_notequal),
Snackbar.LENGTH_LONG
).show()
}
override fun showPasswordTooShort() {
Snackbar.make(findViewById(R.id.coordinator_change_password),
getString(R.string.failed_to_change_password) + getString(R.string.error_invalid_password_short), Snackbar.LENGTH_LONG).show()
Snackbar.make(
findViewById(R.id.coordinator_change_password),
getString(R.string.failed_to_change_password) + getString(R.string.error_invalid_password_short),
Snackbar.LENGTH_LONG
).show()
}
override fun showOldPasswordEmpty() {
......@@ -93,4 +108,8 @@ class ChangePasswordActivity :
override fun handleUnauthorizedEvent() {
//Don't handle unauthorized event
}
override fun enableChangePasswordButton(isEnabled: Boolean) {
changepw_confirm_btn.isEnabled = isEnabled
}
}
......@@ -9,8 +9,10 @@ interface ChangePasswordContract {
interface View : BaseView<Presenter> {
/** Let the user know that changing his/her password was successful **/
fun showChangePasswordSuccess()
/** Let the user know that changing his/her password has failed due to [reason] **/
fun showChangePasswordFailed(reason: String)
/** Show an indeterminate progress bar to the user while processing the changed password **/
fun showPasswordGetsVerified()
fun showPasswordIncorrect()
......@@ -18,8 +20,12 @@ interface ChangePasswordContract {
fun showPasswordTooShort()
fun showUpdatingPassword()
fun showOldPasswordEmpty()
/** Hide the indeterminate progress bar, that is shown to the user while processing the changed password **/
fun hideProgress()
/** Enables / Disables change password button **/
fun enableChangePasswordButton(isEnabled: Boolean)
}
interface Presenter : BasePresenter {
......
......@@ -7,10 +7,11 @@ import de.akamu.tudarmstadt.interactors.UserInteractor
import de.akamu.tudarmstadt.model.User
import de.akamu.tudarmstadt.util.Constants
class ChangePasswordPresenter(private val interactor: PasswordInteractor,
private val userInteractor: UserInteractor,
private var view: ChangePasswordContract.View?)
: ChangePasswordContract.Presenter {
class ChangePasswordPresenter(
private val interactor: PasswordInteractor,
private val userInteractor: UserInteractor,
private var view: ChangePasswordContract.View?
) : ChangePasswordContract.Presenter {
init {
view?.presenter = this
......@@ -20,11 +21,13 @@ class ChangePasswordPresenter(private val interactor: PasswordInteractor,
if (currentPw.isEmpty()) {
view?.showOldPasswordEmpty()
view?.enableChangePasswordButton(true)
return
}
// Check if new passwords are valid
if(!isPasswordValid(newPw, newPw2)) {
if (!isPasswordValid(newPw, newPw2)) {
view?.enableChangePasswordButton(true)
return
}
......@@ -33,22 +36,28 @@ class ChangePasswordPresenter(private val interactor: PasswordInteractor,
AkamuFirebase.loadFirebaseToken(object : AkamuFirebase.GetFirebaseTokenCallback {
override fun onGetFirebaseTokenSuccess(firebaseToken: String) {
interactor.verifyPassword(user, currentPw, firebaseToken, object: PasswordDataSource.VerifyPasswordCallback {
override fun passwordCorrect() {
user.password = newPw
updateUserWithNewPW(newPw)
}
override fun passwordInCorrect() {
view?.showPasswordIncorrect()
view?.hideProgress()
}
override fun passwordError(msg: String) {
view?.showChangePasswordFailed(msg)
view?.hideProgress()
}
})
interactor.verifyPassword(
user,
currentPw,
firebaseToken,
object : PasswordDataSource.VerifyPasswordCallback {
override fun passwordCorrect() {
user.password = newPw
updateUserWithNewPW(newPw)
}
override fun passwordInCorrect() {
view?.showPasswordIncorrect()
view?.hideProgress()
view?.enableChangePasswordButton(true)
}
override fun passwordError(msg: String) {
view?.showChangePasswordFailed(msg)
view?.hideProgress()
view?.enableChangePasswordButton(true)
}
})
}
override fun onGetFirebaseTokenFail(error: String) {
......@@ -62,14 +71,14 @@ class ChangePasswordPresenter(private val interactor: PasswordInteractor,
view = null
}
private fun isPasswordValid(newPw: String, newPw2: String) : Boolean {
private fun isPasswordValid(newPw: String, newPw2: String): Boolean {
if(newPw.isEmpty() || newPw2.isEmpty() || newPw.length < Constants.MIN_PASSWORD_LENGTH) {
if (newPw.isEmpty() || newPw2.isEmpty() || newPw.length < Constants.MIN_PASSWORD_LENGTH) {
view?.showPasswordTooShort()
return false
}
if(newPw != newPw2) {
if (newPw != newPw2) {
view?.showPasswordsMismatch()
return false
}
......@@ -81,17 +90,21 @@ class ChangePasswordPresenter(private val interactor: PasswordInteractor,
private fun updateUserWithNewPW(newPassword: String) {
view?.showUpdatingPassword()
userInteractor.updateUserPasswordInServer(newPassword, object : UserDataSource.UpdateUserCallback {
override fun onUpdateUserSuccess() {
view?.showChangePasswordSuccess()
view?.hideProgress()
}
override fun onUpdateUserFail(reason: String) {
view?.showChangePasswordFailed(reason)
view?.hideProgress()
}
})
userInteractor.updateUserPasswordInServer(
newPassword,
object : UserDataSource.UpdateUserCallback {
override fun onUpdateUserSuccess() {
view?.showChangePasswordSuccess()
view?.hideProgress()
view?.enableChangePasswordButton(true)
}
override fun onUpdateUserFail(reason: String) {
view?.showChangePasswordFailed(reason)
view?.hideProgress()
view?.enableChangePasswordButton(true)
}
})
}
}
\ No newline at end of file
......@@ -40,8 +40,10 @@ class PasswordFragment : Fragment(), PasswordContract.View, Step {
val v = inflater.inflate(R.layout.fragment_password, container, false)
presenter = PasswordPresenter(this)
errorStringTooShort = getString(R.string.error_invalid_password_short)
errorStringTooLong = getString(R.string.error_invalid_password_long)
errorStringTooShort =
getString(R.string.error_invalid_password_short, Constants.MIN_PASSWORD_LENGTH)
errorStringTooLong =
getString(R.string.error_invalid_password_long, Constants.MAX_PASSWORD_LENGTH)
errorStringNotEqual = getString(R.string.error_invalid_password_notequal)
errorStringNotUpperLower = getString(R.string.error_invalid_password_notupperlower)
return v
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment