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..07c6539 --- /dev/null +++ b/app/src/main/java/com/example/sprout/refactor/activity/BaseActivity.java @@ -0,0 +1,68 @@ +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.viewmodel.BaseViewModel; + +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; + +/** + * Activityの基底クラス + * Created by matsumoto_k on 2017/12/22. + */ +public abstract class BaseActivity extends AppCompatActivity { + private CompositeDisposable subscriptions = null; + + @Override + protected void onResume() { + super.onResume(); + subscriptions = new CompositeDisposable(); + } + + /** + * 引数のObservableに対してsubscribeする + * + * @param baseViewModel BaseViewModelを継承したクラスを引数に渡す + */ + protected void addUiObserver(BaseViewModel baseViewModel) { + subscriptions.add( + baseViewModel.getToastMessanger() + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(toastMessage -> { + switch (toastMessage.getLength()) { + case SHORT: + Toast.makeText(this, toastMessage.getMessage(), Toast.LENGTH_SHORT).show(); + break; + case LONG: + Toast.makeText(this, toastMessage.getMessage(), Toast.LENGTH_LONG).show(); + break; + } + }) + ); + // 画面遷移の購読 + subscriptions.add( + baseViewModel.getNavigationMessenger() + .observeOn(Schedulers.computation()) + .subscribe(navigationMessage -> { + switch (navigationMessage.getType()) { + case Start: + startActivity(new Intent(BaseActivity.this, navigationMessage.getClazz())); + break; + case Finsih: + finish(); + } + }) + ); + } + + /** + * disposeする + */ + protected void removeUiObserver() { + subscriptions.dispose(); + } +}