Skip to content

Instantly share code, notes, and snippets.

@sheerazam
Created April 11, 2016 08:46
Show Gist options
  • Save sheerazam/212f559a19232bd97d3bc9c6e1d01d44 to your computer and use it in GitHub Desktop.
Save sheerazam/212f559a19232bd97d3bc9c6e1d01d44 to your computer and use it in GitHub Desktop.
Login MVP
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:orientation="vertical"
android:layout_marginTop="16dp"
>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="@string/user_name"
android:drawableLeft="@drawable/ic_action_person"
android:drawablePadding="8dp"
/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="@string/password"
android:inputType="textPassword"
android:drawableLeft="@drawable/ic_action_accounts"
android:drawablePadding="8dp"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/log_in"
android:drawableRight="@drawable/ic_action_accept"
/>
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:visibility="gone"
/>
</LinearLayout>
public class LoginActivity extends Activity implements LoginView, View.OnClickListener {
private ProgressBar progressBar;
private EditText username;
private EditText password;
private LoginPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
progressBar = (ProgressBar) findViewById(R.id.progress);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
findViewById(R.id.button).setOnClickListener(this);
presenter = new LoginPresenterImpl(this);
}
@Override public void showProgress() {
progressBar.setVisibility(View.VISIBLE);
}
@Override public void hideProgress() {
progressBar.setVisibility(View.GONE);
}
@Override public void setUsernameError() {
username.setError(getString(R.string.username_error));
}
@Override public void setPasswordError() {
password.setError(getString(R.string.password_error));
}
@Override public void navigateToHome() {
startActivity(new Intent(this, MainActivity.class));
finish();
}
@Override public void onClick(View v) {
presenter.validateCredentials(username.getText().toString(), password.getText().toString());
}
}
public interface LoginInteractor {
public void login(String username, String password, OnLoginFinishedListener listener);
}
public class LoginInteractorImpl implements LoginInteractor {
@Override
public void login(final String username, final String password, final OnLoginFinishedListener listener) {
// Mock login. I'm creating a handler to delay the answer a couple of seconds
new Handler().postDelayed(new Runnable() {
@Override public void run() {
boolean error = false;
if (TextUtils.isEmpty(username)){
listener.onUsernameError();
error = true;
}
if (TextUtils.isEmpty(password)){
listener.onPasswordError();
error = true;
}
if (!error){
listener.onSuccess();
}
}
}, 2000);
}
}
public interface LoginPresenter {
public void validateCredentials(String username, String password);
}
public class LoginPresenterImpl implements LoginPresenter, OnLoginFinishedListener {
private LoginView loginView;
private LoginInteractor loginInteractor;
public LoginPresenterImpl(LoginView loginView) {
this.loginView = loginView;
this.loginInteractor = new LoginInteractorImpl();
}
@Override public void validateCredentials(String username, String password) {
loginView.showProgress();
loginInteractor.login(username, password, this);
}
@Override public void onUsernameError() {
loginView.setUsernameError();
loginView.hideProgress();
}
@Override public void onPasswordError() {
loginView.setPasswordError();
loginView.hideProgress();
}
@Override public void onSuccess() {
loginView.navigateToHome();
}
}
public interface LoginView {
public void showProgress();
public void hideProgress();
public void setUsernameError();
public void setPasswordError();
public void navigateToHome();
}
public interface OnLoginFinishedListener {
public void onUsernameError();
public void onPasswordError();
public void onSuccess();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment