Преглед на файлове

[공통][New] API 모듈 작업 중

hyodong.min преди 7 години
родител
ревизия
cb739ecf77

+ 12 - 16
app/src/main/java/kr/co/zumo/app/lifeplus/model/FAQModel.java

@@ -3,16 +3,14 @@
  */
 package kr.co.zumo.app.lifeplus.model;
 
-import android.util.Log;
-
 import java.util.List;
 
-import io.reactivex.android.schedulers.AndroidSchedulers;
 import io.reactivex.disposables.CompositeDisposable;
-import io.reactivex.schedulers.Schedulers;
 import kr.co.zumo.app.lifeplus.bean.api.FAQBean;
+import kr.co.zumo.app.lifeplus.bean.api.FAQResultBean;
 import kr.co.zumo.app.lifeplus.bean.api.RequestBean;
-import kr.co.zumo.app.lifeplus.network.api.LifeplusAPIRepository;
+import kr.co.zumo.app.lifeplus.model.module.APIFAQModule;
+import kr.co.zumo.app.lifeplus.model.module.IAPIModuleListener;
 import kr.co.zumo.app.lifeplus.view.Event;
 
 /**
@@ -63,21 +61,19 @@ public class FAQModel extends Model {
    */
   public void loadFaq() {
     disposable.add(
-      new LifeplusAPIRepository().getFaqList(new RequestBean())
-        .subscribeOn(Schedulers.io())
-        .observeOn(AndroidSchedulers.mainThread())
-        .subscribe(resultBean -> {
-          Log.i("APP# FAQModel | loadFaq", "| " + resultBean.toPrettyJson());
-          if (resultBean.isSuccess()) {
+      new APIFAQModule().call(new RequestBean(), new IAPIModuleListener<FAQResultBean>() {
+          @Override
+          public void onApiResult(FAQResultBean resultBean) {
             faqBeans = resultBean.getData();
             onResult(new Event.Builder(Event.RESULT).build());
           }
-          else {
-            onResult(new Event.Builder(Event.ERROR).string(resultBean.getReturnMessage()).build());
+
+          @Override
+          public void onApiError(String errorMessage) {
+            onResult(new Event.Builder(Event.ERROR).string(errorMessage).build());
           }
-        }, e -> {
-          onResult(new Event.Builder(Event.ERROR).string(e.getLocalizedMessage()).build());
-        })
+        }
+      )
     );
   }
 

+ 31 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/model/module/APIFAQModule.java

@@ -0,0 +1,31 @@
+/*
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
+ */
+package kr.co.zumo.app.lifeplus.model.module;
+
+import io.reactivex.Single;
+import io.reactivex.android.schedulers.AndroidSchedulers;
+import io.reactivex.schedulers.Schedulers;
+import kr.co.zumo.app.lifeplus.bean.api.FAQResultBean;
+import kr.co.zumo.app.lifeplus.bean.api.RequestBean;
+import kr.co.zumo.app.lifeplus.network.api.LifeplusAPIRepository;
+
+/**
+ * APIFAQModule
+ * <pre>
+ * </pre>
+ *
+ * @author 민효동
+ * @version 1.0
+ * @history 민효동   [2018. 10. 12.]   [최초 작성]
+ * @since 2018. 10. 12.
+ */
+public class APIFAQModule extends APIModule<RequestBean, FAQResultBean> {
+
+  @Override
+  protected Single<FAQResultBean> getAPI(RequestBean requestBean) {
+    return new LifeplusAPIRepository().getFaqList(requestBean)
+      .subscribeOn(Schedulers.io())
+      .observeOn(AndroidSchedulers.mainThread());
+  }
+}

+ 94 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/model/module/APIModule.java

@@ -0,0 +1,94 @@
+/*
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
+ */
+package kr.co.zumo.app.lifeplus.model.module;
+
+import android.util.Log;
+
+import io.reactivex.Single;
+import io.reactivex.disposables.Disposable;
+import io.reactivex.functions.Consumer;
+import kr.co.zumo.app.lifeplus.bean.api.LifeplusAPIBean;
+import kr.co.zumo.app.lifeplus.bean.api.RequestBean;
+
+/**
+ * APIFAQModule
+ * <pre>
+ * </pre>
+ *
+ * @author 민효동
+ * @version 1.0
+ * @history 민효동   [2018. 10. 12.]   [최초 작성]
+ * @since 2018. 10. 12.
+ */
+public abstract class APIModule<T extends RequestBean, R extends LifeplusAPIBean> implements Disposable {
+
+  protected Disposable disposable;
+
+  // result
+  public class APIConsumer implements Consumer<LifeplusAPIBean> {
+    private IAPIModuleListener listener;
+
+    public APIConsumer(IAPIModuleListener listener) {
+      this.listener = listener;
+    }
+
+    @Override
+    public void accept(LifeplusAPIBean resultBean) throws Exception {
+      Log.i("APP# APIModule | accept", "| " + resultBean.toPrettyJson());
+      if (resultBean.isSuccess()) {
+        listener.onApiResult(resultBean);
+      }
+      else {
+        listener.onApiError(resultBean.getReturnMessage());
+      }
+    }
+  }
+
+  // error
+  public class APIErrorConsumer implements Consumer<Throwable> {
+    private IAPIModuleListener listener;
+
+    public APIErrorConsumer(IAPIModuleListener listener) {
+      this.listener = listener;
+    }
+
+    @Override
+    public void accept(Throwable e) throws Exception {
+      listener.onApiError(e.getLocalizedMessage());
+    }
+  }
+
+  @Override
+  public void dispose() {
+    if (null != disposable) {
+      disposable.dispose();
+      disposable = null;
+    }
+  }
+
+  @Override
+  public boolean isDisposed() {
+    if (null != disposable) {
+      return disposable.isDisposed();
+    }
+    return true;
+  }
+
+  /***********************************
+   * abstract
+   ***********************************/
+
+  protected abstract Single<R> getAPI(T requestBean);
+  
+
+  /***********************************
+   * public
+   ***********************************/
+
+  public Disposable call(T requestBean, IAPIModuleListener listener) {
+    disposable = getAPI(requestBean).subscribe(new APIConsumer(listener), new APIErrorConsumer(listener));
+    return this;
+  }
+
+}

+ 32 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/model/module/IAPIModuleListener.java

@@ -0,0 +1,32 @@
+/*
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
+ */
+package kr.co.zumo.app.lifeplus.model.module;
+
+import kr.co.zumo.app.lifeplus.bean.api.LifeplusAPIBean;
+
+/**
+ * IAPIModuleListener
+ * <pre>
+ * </pre>
+ *
+ * @author 민효동
+ * @version 1.0
+ * @history 민효동   [2018. 10. 12.]   [최초 작성]
+ * @since 2018. 10. 12.
+ */
+public interface IAPIModuleListener<T extends LifeplusAPIBean> {
+  /**
+   * API 정상 완료, 결과 T Bean 전달
+   *
+   * @param resultBean T extends LifeplusAPIBean
+   */
+  void onApiResult(T resultBean);
+
+  /**
+   * API 통신 에러/결과 에러, 결과 T Bean 전달
+   *
+   * @param errorMessage String
+   */
+  void onApiError(String errorMessage);
+}