-
-
Save nbarishok/7b11cc4abd42a22dcd40 to your computer and use it in GitHub Desktop.
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
class WeatherCommunicationBus implements WeatherView, WeatherPresenter { | |
private static final String VIEW_STATE_KEY = "VIEW_STATE"; | |
private WeatherViewState mViewState; | |
private WeatherPresenter mPresenter; | |
private WeatherView mView; | |
public WeatherCommunicationBus(WeatherPresenter presenter) { | |
mPresenter = presenter; | |
mViewState = createViewState(); | |
mPresenter.attachView(this); | |
} | |
private WeatherViewState createViewState() { | |
return new WeatherViewState(); | |
} | |
@Override | |
public void showLoading() { | |
mViewState.setStateShowLoading(); | |
mView.showLoading(); | |
} | |
@Override | |
public void hideLoading() { | |
mViewState.setStateHideLoading(); | |
mView.hideLoading(); | |
} | |
@Override | |
public void setData(WeatherViewModel data) { | |
mView.setData(data); | |
} | |
@Override | |
public void showContent() { | |
mViewState.setStateShowContent(); | |
mView.showContent(); | |
} | |
@Override | |
public void showError(WeatherError error) { | |
mViewState.setStateShowError(error); | |
mView.showError(error); | |
} | |
// presenter | |
@Override | |
public void loadWeather() { | |
mPresenter.loadWeather(); | |
} | |
@Override | |
public void attachView(WeatherView view) { | |
mView = view; | |
mViewState.apply(view); | |
} | |
@Override | |
public void detachView() { | |
mView = DetachedWeatherView.instance(); | |
} | |
@Override | |
public void onCreate(@Nullable Bundle arguments, @Nullable Bundle savedInstanceState) { | |
mPresenter.onCreate(arguments, savedInstanceState); | |
if (savedInstanceState != null) { | |
if (savedInstanceState.containsKey(VIEW_STATE_KEY)) { | |
mViewState = savedInstanceState.getParcelable(VIEW_STATE_KEY); | |
} | |
} | |
} | |
@Override | |
public void onSaveInstanceState(Bundle bundle) { | |
mPresenter.onSaveInstanceState(bundle); | |
bundle.putParcelable(VIEW_STATE_KEY, mViewState); | |
} | |
@Override | |
public void onDestroy() { | |
mPresenter.detachView(); | |
mPresenter.onDestroy(); | |
} | |
private static class DetachedWeatherView implements WeatherView { | |
private static final DetachedWeatherView view = new DetachedWeatherView(); | |
public static DetachedWeatherView instance() { | |
return view; | |
} | |
@Override | |
public void showLoading() { | |
} | |
@Override | |
public void hideLoading() { | |
} | |
@Override | |
public void setData(WeatherViewModel data) { | |
} | |
@Override | |
public void showContent() { | |
} | |
@Override | |
public void showError(WeatherError error) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment