| | |
| | | package cn.flightfeather.thirdapp.repository |
| | | |
| | | import android.annotation.SuppressLint |
| | | import android.os.Environment |
| | | import cn.flightfeather.thirdapp.bean.* |
| | | import cn.flightfeather.thirdapp.common.net.ResponseBodyCallBack |
| | | import cn.flightfeather.thirdapp.common.net.ResultCallBack |
| | | import cn.flightfeather.thirdapp.common.net.ResultObserver |
| | | import cn.flightfeather.thirdapp.common.net.RetrofitFactory |
| | | import cn.flightfeather.thirdapp.bean.vo.ProblemlistVo |
| | | import cn.flightfeather.thirdapp.common.net.* |
| | | import cn.flightfeather.thirdapp.httpservice.InspectionService |
| | | import cn.flightfeather.thirdapp.httpservice.MediaFileService |
| | | import cn.flightfeather.thirdapp.httpservice.ProblemListService |
| | | import cn.flightfeather.thirdapp.repository.dao.DomainDao |
| | | import cn.flightfeather.thirdapp.repository.dao.MediaFileDao |
| | | import cn.flightfeather.thirdapp.repository.dao.ProblemTypeDao |
| | | import io.reactivex.android.schedulers.AndroidSchedulers |
| | | import io.reactivex.schedulers.Schedulers |
| | | import okhttp3.ResponseBody |
| | | import retrofit2.Call |
| | | import retrofit2.Callback |
| | | import retrofit2.Response |
| | | import java.io.BufferedInputStream |
| | | import java.io.File |
| | | import java.io.FileOutputStream |
| | | import java.io.IOException |
| | | |
| | | /** |
| | | * @author riku |
| | |
| | | class ProblemRepository { |
| | | |
| | | val retrofit = RetrofitFactory.instance.retrofit |
| | | val retrofitImage = RetrofitFactory.instance.retrofitImage |
| | | private val domainDao = DomainDao() |
| | | private val problemTypeDao = ProblemTypeDao() |
| | | private val mediaFileDao = MediaFileDao() |
| | |
| | | } |
| | | |
| | | /** |
| | | * 更新问题 |
| | | */ |
| | | fun updateProblem(problem: ProblemlistVo, resultCallBack: ResultCallBack<ResponseBody>) { |
| | | retrofit.create(ProblemListService::class.java).updateProblemList(problem) |
| | | .enqueue(ResponseBodyCallBack(resultCallBack)) |
| | | } |
| | | |
| | | /** |
| | | * 图片本地缓存 |
| | | */ |
| | | fun putMediaFileLocal(mediaFile: Mediafile, resultCallBack: ResultCallBack<Int>) { |
| | |
| | | |
| | | }) |
| | | } |
| | | |
| | | /** |
| | | * 获取本地缓存的图片 |
| | | */ |
| | | fun getMediaFileLocal(problemGuid: String, resultCallBack: ResultCallBack<List<Mediafile>>) { |
| | | val dbService = mediaFileDao.getMediaFileByProblemId(problemGuid).map { |
| | | Response.success(it) |
| | | } |
| | | |
| | | RetrofitFactory.executeResult(dbService, object : ResultObserver<List<Mediafile>>() { |
| | | override fun onSuccess(result: List<Mediafile>?) { |
| | | resultCallBack.onSuccess(result) |
| | | } |
| | | |
| | | override fun onFailure(e: Throwable, isNetWorkError: Boolean) { |
| | | resultCallBack.onFailure() |
| | | } |
| | | }) |
| | | } |
| | | |
| | | /** |
| | | * 下载问题图片 |
| | | */ |
| | | fun downloadMediaFile(mediaFile:Mediafile, resultCallBack: ResultCallBack<File>) { |
| | | val url: String = mediaFile.extension1 + mediaFile.guid + ".jpg" |
| | | retrofitImage.create(MediaFileService::class.java).downloadImage(url) |
| | | .enqueue(object : Callback<ResponseBody> { |
| | | override fun onFailure(call: Call<ResponseBody>, t: Throwable) { |
| | | resultCallBack.onFailure() |
| | | } |
| | | |
| | | override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) { |
| | | if (response.body() != null) { |
| | | val file = writeResponseBodyToDisk(mediaFile, response.body()) |
| | | if (file?.exists() == true) { |
| | | resultCallBack.onSuccess(file) |
| | | } else { |
| | | resultCallBack.onFailure() |
| | | } |
| | | } else { |
| | | resultCallBack.onFailure() |
| | | } |
| | | } |
| | | }) |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 删除问题图片 |
| | | */ |
| | | @SuppressLint("CheckResult") |
| | | fun deleteMediaFile(mediaFile: Mediafile, resultCallBack: ResultCallBack<Boolean>) { |
| | | if (mediaFile.remark == "未上传") { |
| | | mediaFileDao.deleteMediaFile(mediaFile.guid) |
| | | .subscribeOn(Schedulers.io()) |
| | | .observeOn(AndroidSchedulers.mainThread()) |
| | | .subscribe{ |
| | | if (it) { |
| | | resultCallBack.onSuccess(it) |
| | | } |
| | | } |
| | | } else if (mediaFile.remark == "已上传") { |
| | | retrofit.create(MediaFileService::class.java).deleteMediaFile(mediaFile.guid) |
| | | .enqueue(object : Callback<ResponseBody?> { |
| | | override fun onResponse(call: Call<ResponseBody?>, response: Response<ResponseBody?>) { |
| | | if (response.body() != null) { |
| | | mediaFileDao.deleteMediaFile(mediaFile.guid) |
| | | .subscribeOn(Schedulers.io()) |
| | | .observeOn(AndroidSchedulers.mainThread()) |
| | | .subscribe{ |
| | | if (it) { |
| | | resultCallBack.onSuccess(it) |
| | | } |
| | | } |
| | | } else if (response.errorBody() != null) { |
| | | resultCallBack.onFailure() |
| | | } |
| | | } |
| | | |
| | | override fun onFailure(call: Call<ResponseBody?>, t: Throwable) { |
| | | resultCallBack.onFailure() |
| | | } |
| | | }) |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 删除问题 |
| | | */ |
| | | fun deleteProblem(problemId: String, resultCallBack: ResultCallBack2<Int>) { |
| | | retrofit.create(ProblemListService::class.java).deleteProblem(problemId) |
| | | .enqueue(ResponseBodyCallBack2(resultCallBack)) |
| | | } |
| | | |
| | | //将下载的图片写入磁盘 |
| | | private fun writeResponseBodyToDisk(mediafile: Mediafile, body: ResponseBody?): File? { |
| | | if (body == null) { |
| | | return null |
| | | } |
| | | return try { |
| | | val `is` = body.byteStream() |
| | | var file = File(Environment.getExternalStorageDirectory(), mediafile.path + mediafile.description) |
| | | if (file.exists()) { |
| | | file.delete() |
| | | file = File(Environment.getExternalStorageDirectory(), mediafile.path + mediafile.description) |
| | | } |
| | | file.parentFile.mkdirs() |
| | | val fos = FileOutputStream(file) |
| | | val bis = BufferedInputStream(`is`) |
| | | val buffer = ByteArray(1024) |
| | | var len: Int |
| | | while (bis.read(buffer).also { len = it } != -1) { |
| | | fos.write(buffer, 0, len) |
| | | } |
| | | fos.flush() |
| | | fos.close() |
| | | bis.close() |
| | | `is`.close() |
| | | file |
| | | } catch (e: IOException) { |
| | | e.printStackTrace() |
| | | null |
| | | } |
| | | } |
| | | } |