Refactor callback
This commit is contained in:
@@ -29,7 +29,7 @@ import com.lsp.view.activity.favtag.FavTagActivity
|
||||
import com.lsp.view.activity.model.MainActivityModelImpl
|
||||
import com.lsp.view.activity.setting.SettingsActivity
|
||||
import com.lsp.view.bean.Post_yand
|
||||
import com.lsp.view.util.Code
|
||||
import com.lsp.view.util.CallBackStatus
|
||||
|
||||
|
||||
class MainActivity : BaseActivity() {
|
||||
@@ -331,11 +331,11 @@ class MainActivity : BaseActivity() {
|
||||
override fun handleMessage(msg: Message) {
|
||||
super.handleMessage(msg)
|
||||
when (msg.what){
|
||||
Code.DATAISNULL ->{
|
||||
CallBackStatus.DATAISNULL.ordinal ->{
|
||||
Snackbar.make(swipeRefreshLayout,"未找到内容",Snackbar.LENGTH_SHORT).show()
|
||||
search.setText("")
|
||||
}
|
||||
Code.OK -> {
|
||||
CallBackStatus.OK.ordinal -> {
|
||||
if (type == ISREFRESH )
|
||||
//刷新数据
|
||||
adapter.refreshData(msg.obj as ArrayList<Post_yand>)
|
||||
@@ -344,7 +344,7 @@ class MainActivity : BaseActivity() {
|
||||
adapter.addData(msg.obj as ArrayList<Post_yand>)
|
||||
|
||||
}
|
||||
Code.NETWORKERROR -> {
|
||||
CallBackStatus.NETWORKERROR.ordinal -> {
|
||||
Snackbar.make(swipeRefreshLayout,"网络连接失败",Snackbar.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ import com.hentai.yandeview.Retrofit.PostService
|
||||
import com.hentai.yandeview.Retrofit.ServiceCreator
|
||||
import com.lsp.view.bean.Post
|
||||
import com.lsp.view.bean.Post_yand
|
||||
import com.lsp.view.util.Code
|
||||
import com.lsp.view.util.CallBackStatus
|
||||
import retrofit2.Call
|
||||
import retrofit2.Callback
|
||||
import retrofit2.Response
|
||||
import java.util.ArrayList
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
class MainActivityModelImpl :MainActivityModel {
|
||||
private val TAG = this::class.java.simpleName
|
||||
@@ -20,22 +20,16 @@ class MainActivityModelImpl :MainActivityModel {
|
||||
request(service,safeMode, handler)
|
||||
}
|
||||
|
||||
fun <T:Post>sendData(value: ArrayList<T>,handler: Handler){
|
||||
fun <T:Post> callBack(handler: Handler,status: CallBackStatus,value:ArrayList<T>){
|
||||
val msg = Message.obtain()
|
||||
msg.what = Code.OK
|
||||
msg.what = status.ordinal
|
||||
msg.obj = value
|
||||
handler.sendMessage(msg)
|
||||
}
|
||||
|
||||
fun sendNullMsg(handler: Handler){
|
||||
fun callBack(handler: Handler,status: CallBackStatus){
|
||||
val msg = Message.obtain()
|
||||
msg.what = Code.DATAISNULL
|
||||
handler.sendMessage(msg)
|
||||
}
|
||||
|
||||
fun sendConnErrorMsg(handler: Handler){
|
||||
val msg = Message.obtain()
|
||||
msg.what = Code.NETWORKERROR
|
||||
msg.what = status.ordinal
|
||||
handler.sendMessage(msg)
|
||||
}
|
||||
|
||||
@@ -50,7 +44,7 @@ class MainActivityModelImpl :MainActivityModel {
|
||||
|
||||
if (getValue!=null) {
|
||||
if (getValue.size==0){
|
||||
sendNullMsg(handler)
|
||||
callBack(handler,CallBackStatus.DATAISNULL)
|
||||
return
|
||||
}
|
||||
if (safeMode) {
|
||||
@@ -61,14 +55,14 @@ class MainActivityModelImpl :MainActivityModel {
|
||||
iter.remove()
|
||||
}
|
||||
}
|
||||
callBack(handler,CallBackStatus.OK,getValue)
|
||||
|
||||
sendData(getValue,handler)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<ArrayList<T>>, t: Throwable) {
|
||||
sendConnErrorMsg(handler)
|
||||
callBack(handler,CallBackStatus.NETWORKERROR)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ import android.content.Intent
|
||||
import android.media.MediaScannerConnection
|
||||
import android.os.*
|
||||
import android.util.Log
|
||||
import com.lsp.view.util.Code
|
||||
import com.lsp.view.util.CallBackStatus
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import java.io.File
|
||||
@@ -19,19 +19,15 @@ class DownloadService : Service() {
|
||||
private val mBinder = DownloadBinder(this)
|
||||
|
||||
class DownloadBinder(val context: Context) : Binder() {
|
||||
fun sendOK(file:File, handler: Handler){
|
||||
val msg = Message.obtain()
|
||||
msg.what = Code.OK
|
||||
msg.obj = file
|
||||
handler.sendMessage(msg)
|
||||
}
|
||||
|
||||
|
||||
fun sendError(handler: Handler){
|
||||
fun callBack(handler: Handler,status:CallBackStatus,obj:Any?=null){
|
||||
val msg = Message.obtain()
|
||||
msg.what = Code.DOWNLOADERROR
|
||||
msg.obj = null
|
||||
msg.what = status.ordinal
|
||||
if (obj!=null){
|
||||
msg.obj = obj
|
||||
}
|
||||
handler.sendMessage(msg)
|
||||
|
||||
}
|
||||
fun downloadPic(file_url: String, end: String,handler: Handler,md5 : String?) {
|
||||
|
||||
@@ -58,11 +54,11 @@ class DownloadService : Service() {
|
||||
context, arrayOf(file.path),
|
||||
null, null
|
||||
)
|
||||
if (md5==getFileMD5(file.path)){
|
||||
sendOK(file, handler)
|
||||
}else{
|
||||
if (md5 == getFileMD5(file.path)) {
|
||||
callBack(handler, CallBackStatus.OK,file)
|
||||
} else {
|
||||
file.delete()
|
||||
sendMD5Error(handler)
|
||||
callBack(handler,CallBackStatus.MD5COMPAREERROR)
|
||||
}
|
||||
|
||||
return@thread
|
||||
@@ -70,14 +66,14 @@ class DownloadService : Service() {
|
||||
} else {
|
||||
Log.e("Test", "errorNet")
|
||||
file.delete()
|
||||
sendError(handler)
|
||||
callBack(handler, CallBackStatus.NETWORKERROR)
|
||||
return@thread
|
||||
}
|
||||
|
||||
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
sendError(handler)
|
||||
callBack(handler, CallBackStatus.DOWNLOADERROR)
|
||||
return@thread
|
||||
} finally {
|
||||
fos.close()
|
||||
@@ -85,9 +81,9 @@ class DownloadService : Service() {
|
||||
|
||||
} else {
|
||||
FileD.mkdirs()
|
||||
downloadPic(file_url, end, handler,md5)
|
||||
downloadPic(file_url, end, handler, md5)
|
||||
}
|
||||
sendError(handler)
|
||||
callBack(handler,CallBackStatus.DOWNLOADERROR)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,12 +135,6 @@ class DownloadService : Service() {
|
||||
}
|
||||
return result.toString()
|
||||
}
|
||||
fun sendMD5Error(handler: Handler){
|
||||
val msg = Message.obtain()
|
||||
msg.what = Code.MD5ERROR
|
||||
msg.obj = null
|
||||
handler.sendMessage(msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.lsp.view.util;
|
||||
|
||||
public enum CallBackStatus {
|
||||
OK,NETWORKERROR,DATAISNULL,DOWNLOADERROR,SETWP,MD5COMPAREERROR,FILEEXISTS
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.lsp.view.util;
|
||||
|
||||
public class Code {
|
||||
public static final int OK = 0;
|
||||
public static final int NETWORKERROR = -2;
|
||||
public static final int DATAISNULL = -1;
|
||||
public static final int DOWNLOADERROR = -3;
|
||||
public static final int SETWP = 100;
|
||||
public static final int MD5ERROR = -4;
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.lsp.view.util
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.os.Message
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import com.lsp.view.MyApplication
|
||||
|
||||
@@ -22,15 +23,15 @@ object DownloadUtil {
|
||||
override fun handleMessage(msg: Message) {
|
||||
super.handleMessage(msg)
|
||||
when(msg.what){
|
||||
Code.OK ->{
|
||||
CallBackStatus.OK.ordinal ->{
|
||||
Toast.makeText(MyApplication.getContext(), "保存成功", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
|
||||
Code.DOWNLOADERROR -> {
|
||||
CallBackStatus.DOWNLOADERROR.ordinal -> {
|
||||
Toast.makeText(MyApplication.getContext(), "下载异常", Toast.LENGTH_SHORT).show()
|
||||
|
||||
}
|
||||
Code.MD5ERROR -> {
|
||||
CallBackStatus.MD5COMPAREERROR.ordinal -> {
|
||||
Toast.makeText(MyApplication.getContext(), "文件下载异常,MD5对比失败", Toast.LENGTH_SHORT).show()
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user