|
|
@@ -6,6 +6,11 @@ package kr.co.zumo.app.lifeplus.model;
|
|
|
import android.arch.lifecycle.ViewModel;
|
|
|
import android.util.Log;
|
|
|
|
|
|
+import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
+import io.reactivex.disposables.CompositeDisposable;
|
|
|
+import io.reactivex.schedulers.Schedulers;
|
|
|
+import kr.co.zumo.app.lifeplus.application.App;
|
|
|
+import kr.co.zumo.app.lifeplus.network.NetworkWatcher;
|
|
|
import kr.co.zumo.app.lifeplus.util.StringUtil;
|
|
|
import kr.co.zumo.app.lifeplus.view.Event;
|
|
|
import kr.co.zumo.app.lifeplus.view.ILifeCycle;
|
|
|
@@ -27,11 +32,66 @@ import kr.co.zumo.app.lifeplus.view.presenter.Presenter;
|
|
|
*/
|
|
|
public abstract class Model extends ViewModel implements ILifeCycle {
|
|
|
|
|
|
- protected IModelResult listener;
|
|
|
+ private IModelResult listener;
|
|
|
protected Presenter presenter;
|
|
|
+ protected CompositeDisposable disposable = new CompositeDisposable();
|
|
|
|
|
|
public Model() {
|
|
|
- Log.w("APP# Model | Model", "| --------> " + this.getClass().getSimpleName() + " <--------");
|
|
|
+ Log.w("APP# Model | Model", "| >>>>>>>>>>>> " + this.getClass().getSimpleName());
|
|
|
+ }
|
|
|
+
|
|
|
+ /***********************************
|
|
|
+ * own
|
|
|
+ ***********************************/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 네트워크의 연결 상태를 감시한다.
|
|
|
+ * 변동이 있을 때 presenter 로 알려준다.
|
|
|
+ */
|
|
|
+ private void watchNetwork() {
|
|
|
+ Log.w("APP# Model | watchNetwork", "| " + this.getClass().getSimpleName() + " - Start network watching............");
|
|
|
+ disposable.add(
|
|
|
+ NetworkWatcher.watch(App.getInstance().getContext())
|
|
|
+ .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(isConnectedAll -> {
|
|
|
+ Log.i("APP# Model | watchNetwork", "|" + "isConnectedAll: +++++++++" + isConnectedAll);
|
|
|
+ if (null != presenter) {
|
|
|
+ presenter.onChangedConnection(isConnectedAll, 0);
|
|
|
+ }
|
|
|
+ }, e -> {
|
|
|
+ Log.i("APP# Model | watchNetwork", "|" + "isConnectedAll: ++++++++++" + "error ");
|
|
|
+ if (null != presenter) {
|
|
|
+ presenter.onChangedConnection(false, 0);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ private void stopNetworkWatching() {
|
|
|
+ Log.w("APP# Model | stopNetworkWatching", "| " + this.getClass().getSimpleName() + " - Stop network watching___________");
|
|
|
+ disposable.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void onResult(Event event) {
|
|
|
+ try {
|
|
|
+ if (null != listener) {
|
|
|
+ listener.onResult(event);
|
|
|
+ }
|
|
|
+ } catch (Throwable throwable) {
|
|
|
+ onError(new Event.Builder(Event.ERROR).build(), throwable);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void onError(Event event, Throwable throwable) {
|
|
|
+ Log.e("APP# Model | onResult", "|" + throwable.toString());
|
|
|
+ try {
|
|
|
+ if (null != listener) {
|
|
|
+ listener.onResult(event);
|
|
|
+ }
|
|
|
+ } catch (Throwable e) {
|
|
|
+ Log.e("APP# Model | onError", "|" + e.toString());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -52,14 +112,24 @@ public abstract class Model extends ViewModel implements ILifeCycle {
|
|
|
this.listener = listener;
|
|
|
}
|
|
|
|
|
|
- public Presenter getPresenter() {
|
|
|
- return this.presenter;
|
|
|
- }
|
|
|
-
|
|
|
+ /**
|
|
|
+ * set Presenter
|
|
|
+ *
|
|
|
+ * @param presenter
|
|
|
+ */
|
|
|
public void setPresenter(Presenter presenter) {
|
|
|
this.presenter = presenter;
|
|
|
}
|
|
|
|
|
|
+ public Presenter getPresenter() {
|
|
|
+ return this.presenter;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Preferences 반환
|
|
|
+ *
|
|
|
+ * @return LifeplusPreferences
|
|
|
+ */
|
|
|
public LifeplusPreferences getPreferences() {
|
|
|
return SuperModel.getInstance().getPreferences();
|
|
|
}
|
|
|
@@ -70,30 +140,36 @@ public abstract class Model extends ViewModel implements ILifeCycle {
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean isUpdateUser() {
|
|
|
- return StringUtil.isFull(getPreferences().getEncryptionUserNo()) && getPreferences().isZumoUserUpdateCompleted() == false;
|
|
|
+ LifeplusPreferences preferences = getPreferences();
|
|
|
+ return StringUtil.isFull(preferences.getEncryptionUserNo()) && preferences.isZumoUserUpdateCompleted() == false;
|
|
|
}
|
|
|
|
|
|
+ /***********************************
|
|
|
+ * ILifeCycle
|
|
|
+ ***********************************/
|
|
|
@Override
|
|
|
public final void start() {
|
|
|
- Log.w("APP# Model | start", "|" + "| --------> " + this.getClass().getSimpleName() + " <--------");
|
|
|
+ Log.w("APP# Model | start", "| -------->> " + this.getClass().getSimpleName());
|
|
|
startInternal();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public final void pause() {
|
|
|
- Log.w("APP# Model | pause", "|" + "| --------> " + this.getClass().getSimpleName() + " <--------");
|
|
|
- pauseInternal();
|
|
|
+ public final void resume() {
|
|
|
+ Log.w("APP# Model | resume", "| -------->> " + this.getClass().getSimpleName());
|
|
|
+ watchNetwork();
|
|
|
+ resumeInternal();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public final void resume() {
|
|
|
- Log.w("APP# Model | resume", "|" + "| --------> " + this.getClass().getSimpleName() + " <--------");
|
|
|
- resumeInternal();
|
|
|
+ public final void pause() {
|
|
|
+ Log.w("APP# Model | pause", "| " + this.getClass().getSimpleName() + " <<--------");
|
|
|
+ stopNetworkWatching();
|
|
|
+ pauseInternal();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public final void stop() {
|
|
|
- Log.w("APP# Model | stop", "|" + "| --------> " + this.getClass().getSimpleName() + " <--------");
|
|
|
+ Log.w("APP# Model | stop", "| " + this.getClass().getSimpleName() + " <<--------");
|
|
|
stopInternal();
|
|
|
}
|
|
|
|
|
|
@@ -101,7 +177,7 @@ public abstract class Model extends ViewModel implements ILifeCycle {
|
|
|
* dispose
|
|
|
*/
|
|
|
private void destroy() {
|
|
|
- Log.w("APP# Model | dispose", "| >>>>>>>>>>>>>> " + this.getClass().getSimpleName());
|
|
|
+ Log.w("APP# Model | dispose", "| " + this.getClass().getSimpleName() + "<<<<<<<<<<<<");
|
|
|
destroyInternal();
|
|
|
}
|
|
|
|
|
|
@@ -115,31 +191,14 @@ public abstract class Model extends ViewModel implements ILifeCycle {
|
|
|
|
|
|
protected abstract void pauseInternal();
|
|
|
|
|
|
+
|
|
|
+ /***********************************
|
|
|
+ * ViewModel
|
|
|
+ ***********************************/
|
|
|
@Override
|
|
|
protected void onCleared() {
|
|
|
super.onCleared();
|
|
|
|
|
|
destroy();
|
|
|
}
|
|
|
-
|
|
|
- protected void onResult(Event event) {
|
|
|
- try {
|
|
|
- if (null != listener) {
|
|
|
- listener.onResult(event);
|
|
|
- }
|
|
|
- } catch (Throwable throwable) {
|
|
|
- onError(new Event.Builder(Event.ERROR).build(), throwable);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- protected void onError(Event event, Throwable throwable) {
|
|
|
- Log.e("APP# Model | onResult", "|" + throwable.toString());
|
|
|
- try {
|
|
|
- if (null != listener) {
|
|
|
- listener.onResult(event);
|
|
|
- }
|
|
|
- } catch (Throwable e) {
|
|
|
- Log.e("APP# Model | onError", "|" + e.toString());
|
|
|
- }
|
|
|
- }
|
|
|
}
|