diff --git a/app/src/main/java/com/example/sprout/refactor/activity/BaseActivity.java b/app/src/main/java/com/example/sprout/refactor/activity/BaseActivity.java new file mode 100644 index 0000000..0edae67 --- /dev/null +++ b/app/src/main/java/com/example/sprout/refactor/activity/BaseActivity.java @@ -0,0 +1,78 @@ +package com.example.sprout.refactor.activity; + +import android.content.Intent; +import android.support.v7.app.AppCompatActivity; +import android.widget.Toast; + +import com.example.sprout.refactor.messengers.NavigationMessage; +import com.example.sprout.refactor.messengers.ToastMessage; +import com.example.sprout.refactor.notification.Observer; +import com.example.sprout.refactor.viewmodel.BaseViewModel; + +/** + * Activityの基底クラス + * Created by matsumoto_k on 2017/12/22. + */ +public abstract class BaseActivity extends AppCompatActivity { + + /** + * 画面遷移のObserver + */ + protected Observer navigationObserver = new Observer() { + @Override + public void notify(NavigationMessage navigationMessage) { + switch (navigationMessage.getType()) { + case Start: + startActivity(new Intent(BaseActivity.this, navigationMessage.getClazz())); + break; + case Finish: + finish(); + break; + } + } + + @Override + public void error(Throwable throwable) { + + } + }; + /** + * トーストのObserver + */ + protected Observer toastObserver = new Observer() { + @Override + public void notify(ToastMessage toastMessage) { + switch (toastMessage.getLength()) { + case SHORT: + Toast.makeText(BaseActivity.this, toastMessage.getMessage(), Toast.LENGTH_SHORT).show(); + break; + case LONG: + Toast.makeText(BaseActivity.this, toastMessage.getMessage(), Toast.LENGTH_LONG).show(); + break; + } + } + + @Override + public void error(Throwable throwable) { + + } + }; + + /** + * 引数のObservableに対してobserverを登録する + * + * @param baseViewModel BaseViewModelを継承したクラスを引数に渡す + */ + protected void addUiObserver(BaseViewModel baseViewModel) { + baseViewModel.addObserver(navigationObserver, toastObserver); + } + + /** + * 引数のObservableに対してobserverを削除する + * + * @param baseViewModel BaseViewModelを継承したクラスを引数に渡す + */ + protected void removeUiObserver(BaseViewModel baseViewModel) { + baseViewModel.removeObserver(navigationObserver, toastObserver); + } +}