diff --git a/app/src/main/java/com/lsp/view/activity/model/BaseModel.kt b/app/src/main/java/com/lsp/view/activity/model/BaseModel.kt new file mode 100644 index 0000000..66f0292 --- /dev/null +++ b/app/src/main/java/com/lsp/view/activity/model/BaseModel.kt @@ -0,0 +1,59 @@ +package com.lsp.view.activity.model + +import android.os.Handler +import android.os.Message +import com.lsp.view.bean.Post +import com.lsp.view.util.CallBackStatus +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response + +open class BaseModel { + /** + * @param service ArrayList接收边界为Post的泛型,若直接使用Post作为泛型参数,需要强制类型转换。 + */ + fun request(service: Call>, safeMode: Boolean, handler: Handler){ + + service.enqueue(object : Callback> { + override fun onResponse(call: Call>, response: Response>) { + val getValue:ArrayList? = response.body() + + if (getValue!=null) { + if (getValue.size==0){ + callBack(handler, CallBackStatus.DATAISNULL) + return + } + if (safeMode) { + val iter = getValue.iterator() + while (iter.hasNext()) { + val post = iter.next() + if (post.rating != "s") + iter.remove() + } + } + callBack(handler, CallBackStatus.OK,getValue) + + } + + } + + override fun onFailure(call: Call>, t: Throwable) { + callBack(handler, CallBackStatus.NETWORKERROR) + } + + }) + } + + fun callBack(handler: Handler,status: CallBackStatus,value:ArrayList){ + val msg = Message.obtain() + msg.what = status.ordinal + msg.obj = value + handler.sendMessage(msg) + } + + fun callBack(handler: Handler,status: CallBackStatus){ + val msg = Message.obtain() + msg.what = status.ordinal + handler.sendMessage(msg) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/lsp/view/activity/model/MainActivityModelImpl.kt b/app/src/main/java/com/lsp/view/activity/model/MainActivityModelImpl.kt index 81eb710..de5480f 100644 --- a/app/src/main/java/com/lsp/view/activity/model/MainActivityModelImpl.kt +++ b/app/src/main/java/com/lsp/view/activity/model/MainActivityModelImpl.kt @@ -1,18 +1,13 @@ package com.lsp.view.activity.model import android.os.Handler -import android.os.Message import com.lsp.view.retrofit.PostService import com.lsp.view.retrofit.ServiceCreator -import com.lsp.view.bean.Post import com.lsp.view.bean.Post_yand -import com.lsp.view.util.CallBackStatus import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response import kotlin.collections.ArrayList -class MainActivityModelImpl :MainActivityModel { +class MainActivityModelImpl :MainActivityModel, BaseModel() { private val TAG = this::class.java.simpleName override fun requestPostList(handler: Handler, source: String, tage: String?, page: Int,safeMode: Boolean){ val postService: PostService = ServiceCreator.create(source) @@ -20,51 +15,4 @@ class MainActivityModelImpl :MainActivityModel { request(service,safeMode, handler) } - fun callBack(handler: Handler,status: CallBackStatus,value:ArrayList){ - val msg = Message.obtain() - msg.what = status.ordinal - msg.obj = value - handler.sendMessage(msg) - } - - fun callBack(handler: Handler,status: CallBackStatus){ - val msg = Message.obtain() - msg.what = status.ordinal - handler.sendMessage(msg) - } - - /** - * @param service ArrayList接收边界为Post的泛型,若直接使用Post作为泛型参数,需要强制类型转换。 - */ - fun request(service:Call>,safeMode: Boolean,handler: Handler){ - - service.enqueue(object : Callback>{ - override fun onResponse(call: Call>, response: Response>) { - val getValue:ArrayList? = response.body() - - if (getValue!=null) { - if (getValue.size==0){ - callBack(handler,CallBackStatus.DATAISNULL) - return - } - if (safeMode) { - val iter = getValue.iterator() - while (iter.hasNext()) { - val post = iter.next() - if (post.rating != "s") - iter.remove() - } - } - callBack(handler,CallBackStatus.OK,getValue) - - } - - } - - override fun onFailure(call: Call>, t: Throwable) { - callBack(handler,CallBackStatus.NETWORKERROR) - } - - }) - } } \ No newline at end of file