From 643b5aaa9ca8e77e518808eb8fd97960cfa7098a Mon Sep 17 00:00:00 2001 From: Shabinder Singh Date: Wed, 13 Oct 2021 01:55:37 +0530 Subject: [PATCH] Optimised Track Progress Updation Logic --- .../list/store/SpotiFlyerListStoreProvider.kt | 23 ++++++++----------- .../FetchPlatformQueryResult.kt | 6 +++++ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/common/list/src/commonMain/kotlin/com/shabinder/common/list/store/SpotiFlyerListStoreProvider.kt b/common/list/src/commonMain/kotlin/com/shabinder/common/list/store/SpotiFlyerListStoreProvider.kt index a8667fc3..0056710c 100644 --- a/common/list/src/commonMain/kotlin/com/shabinder/common/list/store/SpotiFlyerListStoreProvider.kt +++ b/common/list/src/commonMain/kotlin/com/shabinder/common/list/store/SpotiFlyerListStoreProvider.kt @@ -175,21 +175,18 @@ internal class SpotiFlyerListStoreProvider(dependencies: SpotiFlyerList.Dependen } private fun List.updateTracksStatuses(map: Map): List { - val updatedList = ArrayList(this) - LinkedHashMap(map).forEach { newTrack -> - indexOfFirst { it.title == newTrack.key }.let { position -> - if (position != -1) { - updatedList.getOrNull(position)?.copy( - downloaded = newTrack.value, - progress = (newTrack.value as? DownloadStatus.Downloading)?.progress - ?: updatedList[position].progress - )?.also { updatedTrack -> - updatedList[position] = updatedTrack - // logger.d("$position) ${updatedTrack.downloaded} - ${updatedTrack.title}","List Store Track Update") - } + // create a copy in order not to access real referenced ever-changing collections + val trackList = ArrayList(this) + val updatedMap = HashMap(map) + + repeat(trackList.size) { index -> + trackList[index].also { oldTrack -> + updatedMap[oldTrack.title]?.also { newStatus -> + trackList[index] = oldTrack.copy(downloaded = newStatus) } } } - return updatedList + + return trackList } } diff --git a/common/providers/src/commonMain/kotlin/com.shabinder.common.providers/FetchPlatformQueryResult.kt b/common/providers/src/commonMain/kotlin/com.shabinder.common.providers/FetchPlatformQueryResult.kt index fcf097a3..a0ce7b8b 100644 --- a/common/providers/src/commonMain/kotlin/com.shabinder.common.providers/FetchPlatformQueryResult.kt +++ b/common/providers/src/commonMain/kotlin/com.shabinder.common.providers/FetchPlatformQueryResult.kt @@ -190,6 +190,12 @@ class FetchPlatformQueryResult( downloadLink = URL audioQuality = quality audioFormat = format + }.onFailure { + // Append Error To StackTrace + appendPadded( + "Fetching From YT Failed:", + it.stackTraceToString() + ) } } }