From c47272792e566046f5b80045f95e8efb2d98bd06 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Sun, 13 Jun 2021 22:09:07 -0400 Subject: [PATCH] resolve gson crash --- .../instagrabber/workers/DownloadWorker.kt | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/workers/DownloadWorker.kt b/app/src/main/java/awais/instagrabber/workers/DownloadWorker.kt index b182c514..dd6f1352 100644 --- a/app/src/main/java/awais/instagrabber/workers/DownloadWorker.kt +++ b/app/src/main/java/awais/instagrabber/workers/DownloadWorker.kt @@ -40,9 +40,10 @@ import java.io.File import java.net.URL import java.util.* import java.util.concurrent.ExecutionException +import java.util.stream.Collectors +import kotlin.collections.Map import kotlin.math.abs - class DownloadWorker(context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) { private val notificationManager: NotificationManagerCompat = NotificationManagerCompat.from(context) @@ -88,15 +89,16 @@ class DownloadWorker(context: Context, workerParams: WorkerParameters) : Corouti return Result.success() } - private suspend fun download(urlToFilePathMap: Map) { + private suspend fun download(urlToFilePathMap: Map) { val notificationId = notificationId val entries = urlToFilePathMap.entries var count = 1 val total = urlToFilePathMap.size - for ((url, file) in entries) { + for ((url, uriString) in entries) { updateDownloadProgress(notificationId, count, total, 0f) withContext(Dispatchers.IO) { - download(notificationId, count, total, url, file) + val file = DocumentFile.fromSingleUri(applicationContext, Uri.parse(uriString)) + download(notificationId, count, total, url, file!!) } count++ } @@ -219,9 +221,9 @@ class DownloadWorker(context: Context, workerParams: WorkerParameters) : Corouti return builder.build() } - private fun showSummary(urlToFilePathMap: Map?) { + private fun showSummary(urlToFilePathMap: Map) { val context = applicationContext - val filePaths = urlToFilePathMap!!.values + val filePaths = urlToFilePathMap.mapNotNull { DocumentFile.fromSingleUri(context, Uri.parse(it.value)) } val notifications: MutableList = LinkedList() val notificationIds: MutableList = LinkedList() var count = 1 @@ -382,17 +384,19 @@ class DownloadWorker(context: Context, workerParams: WorkerParameters) : Corouti return bitmap } - class DownloadRequest private constructor(val urlToFilePathMap: Map) { + class DownloadRequest private constructor(val urlToFilePathMap: Map) { class Builder { - private var urlToFilePathMap: MutableMap = mutableMapOf() + private var urlToFilePathMap: MutableMap = mutableMapOf() fun setUrlToFilePathMap(urlToFilePathMap: MutableMap): Builder { this.urlToFilePathMap = urlToFilePathMap + .mapValues { it.value.uri.toString() } + .toMutableMap() return this } fun addUrl(url: String, filePath: DocumentFile): Builder { - urlToFilePathMap[url] = filePath + urlToFilePathMap[url] = filePath.uri.toString() return this }