Last active
May 17, 2017 18:27
-
-
Save Augusent/3be6894204dfc907592c5e7e1641529b to your computer and use it in GitHub Desktop.
Presenter (Moxy) Example
This file contains hidden or 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
@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