package cn.flightfeather.thirdappmodule.module.common
|
|
import android.arch.lifecycle.ViewModelProviders
|
import android.os.Bundle
|
import android.view.View
|
import cn.flightfeather.thirdappmodule.R
|
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
|
import cn.flightfeather.thirdappmodule.module.base.BaseFragment
|
import cn.flightfeather.thirdappmodule.module.base.BaseFragmentActivity
|
import cn.flightfeather.thirdappmodule.util.file.FileUtils
|
import com.liulishuo.okdownload.DownloadTask
|
import com.liulishuo.okdownload.StatusUtil
|
import com.liulishuo.okdownload.core.cause.EndCause
|
import com.liulishuo.okdownload.kotlin.execute1
|
import kotlinx.android.synthetic.main.fragment_file_download.*
|
import org.jetbrains.anko.doAsync
|
import java.io.File
|
|
/**
|
* @author riku
|
* Date: 2019/12/31
|
*/
|
class FileDownloadFragment : BaseFragment() {
|
|
companion object {
|
fun newInstance(filePos: Int, filePath: String?, fileName: String?): FileDownloadFragment {
|
val bundle = Bundle().apply {
|
putInt("filePos", filePos)
|
putString("filePath", filePath)
|
putString("fileName", fileName)
|
}
|
return FileDownloadFragment().apply { arguments = bundle }
|
}
|
}
|
|
override fun getTitleText(): String? = "文件下载"
|
|
override fun toolbarEnabled(): Boolean = true
|
|
private var filePos = 0
|
|
private var filePath: String? = ""
|
|
private var fileName: String? = ""
|
|
private var task: DownloadTask? = null
|
|
private var downloadFilePath = ""
|
|
private lateinit var fileBrowseViewModel: FileBrowseViewModel
|
|
override fun getLayoutId(): Int = R.layout.fragment_file_download
|
|
override fun onCreateView() {
|
super.onCreateView()
|
fileBrowseViewModel = ViewModelProviders.of(this).get(FileBrowseViewModel::class.java)
|
|
arguments?.let {
|
filePos = it.getInt("filePos")
|
filePath = it.getString("filePath")
|
fileName = it.getString("fileName")
|
}
|
}
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
super.onViewCreated(view, savedInstanceState)
|
// img_file_type.setImageResource(FileIcons.bigIcon(fileName))
|
txt_file_name.text = fileName
|
showLoadingText(false)
|
|
initTask()
|
initStatus()
|
|
filePath?.let {url ->
|
fileBrowseViewModel.getFileCache(url, object : ResultCallBack<String> {
|
override fun onSuccess(result: String?) {
|
result?.let {
|
downloadFilePath = it
|
navigateToNextFragment()
|
}
|
}
|
|
override fun onFailure() {
|
startTask()
|
}
|
})
|
}
|
}
|
|
override fun onDestroy() {
|
super.onDestroy()
|
task?.cancel()
|
}
|
|
private fun initTask() {
|
context?.let {
|
val parentFile = FileUtils.getCacheDir(it)
|
downloadFilePath = parentFile.path + File.separator + fileName
|
filePath?.let {p ->
|
task = DownloadTask.Builder(p, parentFile)
|
.setFilename(fileName)
|
.setMinIntervalMillisCallbackProcess(16)
|
.setPassIfAlreadyCompleted(false)
|
.build()
|
}
|
}
|
}
|
|
private fun initStatus() =task?.let {
|
val status = StatusUtil.getStatus(it)
|
if (status == StatusUtil.Status.COMPLETED) {
|
|
}
|
|
}
|
|
private fun startTask() {
|
txt_progress.postDelayed({
|
if (activity != null && !activity!!.isFinishing && !activity!!.isDestroyed) {
|
showLoadingText(true)
|
}
|
}, 3000)
|
doAsync {
|
task?.execute1(
|
connected = {task, blockCount, currentOffset, totalLength ->
|
// todo: 2019/12/31
|
},
|
progress = {task, currentOffset, totalLength ->
|
circularProgressBar?.apply {
|
progress = currentOffset.toFloat()
|
progressMax = totalLength.toFloat()
|
}
|
val t = "${FileUtils.parseToFileSize(currentOffset)}/${FileUtils.parseToFileSize(totalLength)}"
|
txt_progress?.text = t
|
}
|
) { task, cause, realCause, model ->
|
task.tag = null
|
if (cause == EndCause.COMPLETED) {
|
filePath?.let {
|
fileBrowseViewModel.saveFileCache(it, downloadFilePath)
|
}
|
navigateToNextFragment()
|
} else if (cause == EndCause.ERROR) {
|
// todo: 2019/12/31
|
}
|
}
|
}
|
}
|
|
private fun showLoadingText(visible: Boolean) {
|
if (visible) {
|
circularProgressBar.visibility = View.VISIBLE
|
txt_progress.visibility = View.VISIBLE
|
progress_circular.visibility = View.GONE
|
} else {
|
circularProgressBar.visibility = View.GONE
|
txt_progress.visibility = View.GONE
|
progress_circular.visibility = View.VISIBLE
|
}
|
}
|
|
private fun navigateToNextFragment() {
|
val a = activity
|
if (a is BaseFragmentActivity) {
|
a.navigateToNext(OfficeFileReadFragment.newInstance(filePos, downloadFilePath, fileName))
|
a.deleteFragmentFromNavigation(this@FileDownloadFragment)
|
}
|
}
|
}
|