Forráskód Böngészése

1. add some logic

guangjin2.xian 2 éve
szülő
commit
11f386c071

+ 49 - 37
base/src/main/java/com/ysnows/base/base/BRRepository.kt

@@ -36,7 +36,10 @@ open class BRRepository : BRepository() {
      *
      * */
 
-    private suspend fun <T : IResp<*>> reqList(pageLoading: Boolean = true, func: suspend () -> T): T {
+    private suspend fun <T : IResp<*>> reqList(
+        pageLoading: Boolean = true,
+        func: suspend () -> T
+    ): T {
 
         try {
             if (pageLoading) {
@@ -55,7 +58,7 @@ open class BRRepository : BRepository() {
                     }
                 }
                 bView.toLogin()
-               // bView.processCodeOffline()
+                // bView.processCodeOffline()
             } else {
 
                 if (page <= 1) {
@@ -89,7 +92,7 @@ open class BRRepository : BRepository() {
                 }
 
 
-                if (resp.more(page)) {
+                if (resp.more(page, 10)) {
                     view().adapter().loadMoreModule.loadMoreComplete()
                 } else {
                     //数据全部加载完毕
@@ -119,7 +122,10 @@ open class BRRepository : BRepository() {
                 page -= 1
             }
 
-            return Resp<Any>(NetCode.CODE_EXCEPTION, message = ExceptionUtils.getExceptionName(e)) as T
+            return Resp<Any>(
+                NetCode.CODE_EXCEPTION,
+                message = ExceptionUtils.getExceptionName(e)
+            ) as T
         }
     }
 
@@ -135,7 +141,10 @@ open class BRRepository : BRepository() {
     }
 
 
-    fun rxReqFirstPage(api: Observable<IResp<Collection<Any>>>, pagable: Boolean): Observable<IResp<Collection<Any>>> {
+    fun rxReqFirstPage(
+        api: Observable<IResp<Collection<Any>>>,
+        pagable: Boolean
+    ): Observable<IResp<Collection<Any>>> {
         this.api = api;
         page = 1
         return rxReqList(api, pagable)
@@ -152,48 +161,51 @@ open class BRRepository : BRepository() {
     }
 
     @SuppressLint("CheckResult")
-    fun rxReqList(observable: Observable<IResp<Collection<Any>>>, pageLoading: Boolean): Observable<IResp<Collection<Any>>> {
+    fun rxReqList(
+        observable: Observable<IResp<Collection<Any>>>,
+        pageLoading: Boolean
+    ): Observable<IResp<Collection<Any>>> {
         return Observable.create { emitter: ObservableEmitter<IResp<Collection<Any>>> ->
             rxReq(observable, pageLoading, false)
-                    .doOnError { throwable: Throwable ->
-                        throwable.printStackTrace()
-                        if (page >= 1) {
-                            view().adapter().loadMoreModule.loadMoreFail()
-                            page -= 1
-                        }
-                        emitter.onError(throwable)
+                .doOnError { throwable: Throwable ->
+                    throwable.printStackTrace()
+                    if (page >= 1) {
+                        view().adapter().loadMoreModule.loadMoreFail()
+                        page -= 1
                     }
-                    .subscribe({ res: IResp<Collection<Any>> ->
-                        view().onListReceive(res)
-                        if (page <= 1) {
-                            if (res.empty()) { //如果没有数据
-                                view().refreshing(false)
-                                view().loadService!!.showCallback(EmptyCallback::class.java)
-                            } else {
-                                view().refreshing(false)
-                                //Object data = res.data();
+                    emitter.onError(throwable)
+                }
+                .subscribe({ res: IResp<Collection<Any>> ->
+                    view().onListReceive(res)
+                    if (page <= 1) {
+                        if (res.empty()) { //如果没有数据
+                            view().refreshing(false)
+                            view().loadService!!.showCallback(EmptyCallback::class.java)
+                        } else {
+                            view().refreshing(false)
+                            //Object data = res.data();
 //                                if (data instanceof IResultList) {
 //                                    adapter.setList(((IResultList) data).list());
 //                                }
-                                view().adapter().setList(res.data() as Collection<Nothing>)
-                                view().loadService!!.showSuccess()
-                            }
-                            view().getTotal(res.total())
-                        } else {
+                            view().adapter().setList(res.data() as Collection<Nothing>)
+                            view().loadService!!.showSuccess()
+                        }
+                        view().getTotal(res.total())
+                    } else {
 //                            Object data = res.data();
 //                            if (data instanceof IResultList) {
 //                                view().getAdapter().addData((((IResultList) data).list()));
 //                            }
-                            view().adapter().addData(res.data() as Collection<Nothing>)
-                        }
-                        if (res.more(page)) {
-                            view().adapter().loadMoreModule.loadMoreComplete()
-                        } else {
-                            //数据全部加载完毕
-                            view().adapter().loadMoreModule.loadMoreEnd()
-                        }
-                        emitter.onNext(res)
-                    }, { throwable: Throwable? -> }) { emitter.onComplete() }
+                        view().adapter().addData(res.data() as Collection<Nothing>)
+                    }
+                    if (res.more(page)) {
+                        view().adapter().loadMoreModule.loadMoreComplete()
+                    } else {
+                        //数据全部加载完毕
+                        view().adapter().loadMoreModule.loadMoreEnd()
+                    }
+                    emitter.onNext(res)
+                }, { throwable: Throwable? -> }) { emitter.onComplete() }
         }
     }
 

+ 2 - 1
base/src/main/java/com/ysnows/base/net/IResp.kt

@@ -9,4 +9,5 @@ interface IResp<D> {
     fun total(): Int
     fun more(page: Int): Boolean
     fun empty(): Boolean
-}
+    fun more(page: Int, pageCount: Int): Boolean
+}

+ 4 - 4
base/src/main/java/com/ysnows/base/net/PageResp.kt

@@ -2,12 +2,12 @@ package com.ysnows.base.net
 
 class PageResp<D> : Resp<PageData<D>>() {
 
-    override fun empty(): Boolean {
-        return (data()?.count ?: 0) <= 0
+    override fun total(): Int {
+        return data()?.count ?: 0
     }
 
-    override fun more(page: Int): Boolean {
-        return page * count() < total()
+    override fun more(page: Int, pageCount: Int): Boolean {
+        return page * pageCount < total()
     }
 
 }

+ 9 - 5
base/src/main/java/com/ysnows/base/net/Resp.kt

@@ -4,11 +4,11 @@ import com.ysnows.base.base.BApp
 import com.ysnows.base.utils.Toasts
 
 open class Resp<D>(
-        val status: Int = 0,
-        val message: String? = null,
-        val total: Int = 0,
-        val count: Int = 10,
-        val datas: D? = null
+    val status: Int = 0,
+    val message: String? = null,
+    val total: Int = 0,
+    val count: Int = 10,
+    val datas: D? = null
 ) : IResp<D> {
 
     override fun ok(showError: Boolean): Boolean {
@@ -47,6 +47,10 @@ open class Resp<D>(
         return page * count() < total()
     }
 
+    override fun more(page: Int, pageCount: Int): Boolean {
+        return page * count() < total()
+    }
+
     override fun empty(): Boolean {
         return total() <= 0
     }