Skip to content

Instantly share code, notes, and snippets.

@Augusent
Last active May 17, 2017 18:27
Show Gist options
  • Save Augusent/3be6894204dfc907592c5e7e1641529b to your computer and use it in GitHub Desktop.
Save Augusent/3be6894204dfc907592c5e7e1641529b to your computer and use it in GitHub Desktop.
Presenter (Moxy) Example
@InjectViewState
public class EditProfilePresenter extends BasePresenter<EditProfileView> {
@Inject EditProfileInteractor profileInteractor;
private EditProfileViewModel model;
public EditProfilePresenter() {
App.get().getAppComponent().inject(this);
}
@Override protected void onFirstViewAttach() {
super.onFirstViewAttach();
loadViewModel();
}
public Subscription observeApplies(Observable<EditProfileViewModel> applies) {
return applies.map(EditProfileViewModel::getProfile)
.subscribe(this::updateProfile, getViewState()::showError);
}
public Subscription observePhotoPath(Observable<String> imageChanges) {
return imageChanges.subscribe(this::uploadPhoto, getViewState()::showError);
}
private void uploadPhoto(String path) {
addSubscription(profileInteractor.uploadPhoto(path)
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(getViewState()::showPhotoProgress)
.doOnUnsubscribe(getViewState()::hidePhotoProgress)
.subscribe(model::setImage, getViewState()::showError)
);
}
private void updateProfile(Profile profile) {
addSubscription(profileInteractor.updateProfile(profile)
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(subscription -> getViewState().showProgress())
.doOnUnsubscribe(getViewState()::hideProgress)
.subscribe(getViewState()::showUpdateComplete, getViewState()::showError)
);
}
private void loadViewModel() {
addSubscription(profileInteractor.getEditProfileViewModel()
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(getViewState()::showProgress)
.doOnUnsubscribe(getViewState()::hideProgress)
.subscribe(this::handleProfileModel, getViewState()::showError)
);
}
private void handleProfileModel(EditProfileViewModel model) {
this.model = model;
getViewState().showProfile(this.model);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment