ListAdapter Network调用从服务器下载文件并使用WorkManager显示进度

时间:2019-04-28 11:17:13

标签: android listadapter android-livedata android-workmanager android-listadapter

我很想知道使用改造下载文件的设计并在Item适配器上显示进度。

我正在使用工作管理器来进行工作,并通过翻新来下载文件,但是目前我找不到任何方法来显示项目进度。

请找到我的代码如下。

适配器代码

class PamphletListAdapter(
        appExecutors: AppExecutors,
        private val callback: ClickCallback)
    : DataBoundListAdapter<PamphletModel, PamphletCardViewBinding>(appExecutors,
        PamphletListItemDiff()) {

    override fun createBinding(parent: ViewGroup): PamphletCardViewBinding {
        return PamphletCardViewBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    }

    override fun bind(binding: PamphletCardViewBinding, item: PamphletModel, position: Int, isLast: Boolean) {
        binding.pamphletModel = item
        binding.pamphletItemView.setOnClickListener {
            binding.pamphletModel?.let { data ->
                callback.onClick(data, position)
            }
        }
    }

    interface ClickCallback {
        fun onClick(data: PamphletModel, position: Int)
    }
}

片段上的OnClick处理程序。

private fun addToDownload(position: Int, data: PamphletModel) {
    history.push(position)
    val workData: Data = Data.Builder()
            .putString(Constants.WORK_MANAGER_DATA, PamphletModel.getStringFromPamphlets(data))
            .putInt(Constants.WORK_MANAGER_POSITION, position)
            .build()

    val fileManager = OneTimeWorkRequestBuilder<FileDownLoadManagerWorker>()
            .setInputData(workData).build()

    WorkManager.getInstance().getWorkInfoByIdLiveData(fileManager.id).observe(this@PamphletListFragment, Observer {
        handlePDFDownloadResponse(fileManager, it)
    })

    WorkManager.getInstance().enqueue(fileManager)
}

工作管理器代码。

override fun doWork(): Result {
    val pamphletModel = PamphletModel.getPamphletsFromString(inputData.getString(Constants.WORK_MANAGER_DATA))


    if (PatwFileUtils.checkCacheFilesExist(pamphletModel.pdfPath, pamphletModel.thumbnailPath)) {
        return Result.success()
    }

    val rootDirectory = PatwFileUtils.getPamphletDirectory(context)

    //download pdf
    pamphletModel.pdfPath = DownloadManager.downloadFile(pamphletModel.downloadUrl, PatwFileUtils.getPamphletPDFFilePath(rootDirectory, pamphletModel.pamphletId))?.path

    //Download image -> save on thumbnail Path
    pamphletModel.thumbnailPath = getPamphletThumbnailImage(context, pamphletModel)

    pamphletModel.isDownloaded = Constants.PDF_STATUS_DOWNLOADED

    pamphletModel.createAt = Date()

    //Insert data to database
    pamphletDao.insert(pamphletModel)

    val outputData = createOutputData(pamphletModel, inputData.getInt(Constants.WORK_MANAGER_POSITION, 0))
    Timber.i("Work successfully completed...$pamphletModel")
    return Result.success(outputData)
}

完成工作管理器的观察员代码。

private fun handlePDFDownloadResponse(fileManager: OneTimeWorkRequest, workInfo: WorkInfo) {
    Timber.i("Work id = ${fileManager.id}  State =  ${workInfo.state} ")
    if (workInfo.state == WorkInfo.State.SUCCEEDED) {
        val result = workInfo.outputData

        val resultPosition: Int = result.getInt(Constants.WORK_MANAGER_POSITION, 0)
        val resultData = PamphletModel.getPamphletsFromString(result.getString(Constants.WORK_MANAGER_DATA))
        resultData.isDownloaded = Constants.PDF_STATUS_DOWNLOADED
        pamphletListAdapter.notifyItemChanged(resultPosition)

        if (!history.empty() && history.peek() == resultPosition) {
            history.clear()
            UtilIntent.launchPamphletFragment(context!!, configuration, resultData)
        }
    }
}

请帮助我显示项目进度,请告诉我是否需要其他任何班级代码。

1 个答案:

答案 0 :(得分:1)

您将需要根据进度更新其他数据源。 WorkManager不提供此功能。例如,您可以使用Room更新某个边数据库中的进度,并使用LiveData观察值。