Created
June 1, 2018 22:34
-
-
Save ivary43/48b8c7be5fb77068bc58f5642675d143 to your computer and use it in GitHub Desktop.
code Snippet
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
package org.mifos.mobile.cn.ui.mifos.login | |
import android.content.Context | |
import org.mifos.mobile.cn.R | |
import org.mifos.mobile.cn.data.local.PreferencesHelper | |
import org.mifos.mobile.cn.injection.ApplicationContext | |
import org.mifos.mobile.cn.ui.base.BasePresenter | |
import javax.inject.Inject | |
class LoginPresenter @Inject | |
constructor(@ApplicationContext context: Context) : BasePresenter<LoginContract.View>(context), LoginContract.Presenter { | |
@Inject | |
internal lateinit var preferencesHelper: PreferencesHelper | |
//TODO: edit this while implementing API | |
override fun login(username: String, password: String) { | |
checkViewAttached() | |
getMvpView.showProgressDialog() | |
if (isCredentialValid(username, password)) { | |
getMvpView.hideProgressDialog() | |
if (preferencesHelper.password.equals(password) | |
&& preferencesHelper.username.equals(username)) { | |
getMvpView.showUserLoginSuccessfully() | |
} else { | |
getMvpView.hideProgressDialog() | |
getMvpView.showError("Error Logging in") | |
} | |
} | |
} | |
override fun attachView(mvpView: LoginContract.View) { | |
super.attachView(mvpView) | |
} | |
override fun detachView() { | |
super.detachView() | |
} | |
fun isCredentialValid(username: String, password: String): Boolean { | |
val resources = context.resources | |
val correctUsername = username.trim() | |
if (username.isEmpty()) { | |
getMvpView.showError(context.getString(R.string.error_validation_blank, | |
context.getString(R.string.username))) | |
return false | |
} else if (username.length < 5) { | |
getMvpView.showError(context.getString(R.string.error_validation_minimum_chars, | |
resources.getString(R.string.username), | |
resources.getInteger(R.integer.username_minimum_length))) | |
return false | |
} else if (correctUsername.contains(" ")) { | |
getMvpView.showError(context.getString( | |
R.string.error_validation_cannot_contain_spaces, | |
resources.getString(R.string.username))) | |
return false | |
} else if (password.isEmpty()) { | |
getMvpView.showError(context.getString(R.string.error_validation_blank, | |
context.getString(R.string.password))) | |
return false | |
} else if (password.length < 6) { | |
getMvpView.showError(context.getString(R.string.error_validation_minimum_chars, | |
resources.getString(R.string.password), | |
resources.getInteger(R.integer.password_minimum_length))) | |
return false | |
} | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment