Skip to content

Commit

Permalink
Prevent presenters from calling their views if they were destroyed (c…
Browse files Browse the repository at this point in the history
…loses #9)
  • Loading branch information
antoniolg committed Dec 31, 2015
1 parent 74d4a2a commit dacca3c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
Expand Up @@ -48,6 +48,11 @@ protected void onCreate(Bundle savedInstanceState) {
presenter = new LoginPresenterImpl(this);
}

@Override protected void onDestroy() {
presenter.onDestroy();
super.onDestroy();
}

@Override public void showProgress() {
progressBar.setVisibility(View.VISIBLE);
}
Expand Down
Expand Up @@ -20,4 +20,6 @@

public interface LoginPresenter {
void validateCredentials(String username, String password);

void onDestroy();
}
Expand Up @@ -29,21 +29,34 @@ public LoginPresenterImpl(LoginView loginView) {
}

@Override public void validateCredentials(String username, String password) {
loginView.showProgress();
if (loginView != null) {
loginView.showProgress();
}

loginInteractor.login(username, password, this);
}

@Override public void onDestroy() {
loginView = null;
}

@Override public void onUsernameError() {
loginView.setUsernameError();
loginView.hideProgress();
if (loginView != null) {
loginView.setUsernameError();
loginView.hideProgress();
}
}

@Override public void onPasswordError() {
loginView.setPasswordError();
loginView.hideProgress();
if (loginView != null) {
loginView.setPasswordError();
loginView.hideProgress();
}
}

@Override public void onSuccess() {
loginView.navigateToHome();
if (loginView != null) {
loginView.navigateToHome();
}
}
}
Expand Up @@ -71,6 +71,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
}

@Override protected void onDestroy() {
presenter.onDestroy();
super.onDestroy();
}

@Override public void showProgress() {
progressBar.setVisibility(View.VISIBLE);
listView.setVisibility(View.INVISIBLE);
Expand Down
Expand Up @@ -23,4 +23,6 @@ public interface MainPresenter {
void onResume();

void onItemClicked(int position);

void onDestroy();
}
Expand Up @@ -31,16 +31,27 @@ public MainPresenterImpl(MainView mainView) {
}

@Override public void onResume() {
mainView.showProgress();
if (mainView != null) {
mainView.showProgress();
}

findItemsInteractor.findItems(this);
}

@Override public void onItemClicked(int position) {
mainView.showMessage(String.format("Position %d clicked", position + 1));
if (mainView != null) {
mainView.showMessage(String.format("Position %d clicked", position + 1));
}
}

@Override public void onDestroy() {
mainView = null;
}

@Override public void onFinished(List<String> items) {
mainView.setItems(items);
mainView.hideProgress();
if (mainView != null) {
mainView.setItems(items);
mainView.hideProgress();
}
}
}

0 comments on commit dacca3c

Please sign in to comment.