From edca0e5334c42d8af9c4ddcba38df2bb653d0b6c Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 31 Jan 2024 15:55:33 +0200 Subject: [PATCH] Partially migrate to new collections --- .../kotlin/org/koitharu/kotatsu/core/model/Manga.kt | 9 ++++++--- .../kotatsu/core/parser/RemoteMangaRepository.kt | 3 ++- .../koitharu/kotatsu/core/ui/widgets/CubicSlider.kt | 4 ++-- .../kotatsu/core/ui/widgets/SegmentedBarView.kt | 3 ++- .../kotatsu/core/util/progress/TimeLeftEstimator.kt | 13 ++++++++++++- .../ui/worker/DownloadSlowdownDispatcher.kt | 5 +++-- .../org/koitharu/kotatsu/tracker/domain/Tracker.kt | 3 ++- .../kotatsu/tracker/domain/TrackingRepository.kt | 3 ++- 8 files changed, 31 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/model/Manga.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/model/Manga.kt index f98d055ae..8cf08b944 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/model/Manga.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/model/Manga.kt @@ -3,6 +3,7 @@ package org.koitharu.kotatsu.core.model import android.net.Uri import androidx.annotation.DrawableRes import androidx.annotation.StringRes +import androidx.collection.MutableObjectIntMap import androidx.core.os.LocaleListCompat import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.util.ext.iterator @@ -31,12 +32,14 @@ fun Collection.countChaptersByBranch(): Int { if (size <= 1) { return size } - val acc = HashMap() + val acc = MutableObjectIntMap() for (item in this) { val branch = item.chapter.branch - acc[branch] = (acc[branch] ?: 0) + 1 + acc[branch] = acc.getOrDefault(branch, 0) + 1 } - return acc.values.max() + var max = 0 + acc.forEachValue { x -> if (x > max) max = x } + return max } @get:StringRes diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/RemoteMangaRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/RemoteMangaRepository.kt index 217d38572..09c9c3114 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/RemoteMangaRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/RemoteMangaRepository.kt @@ -1,6 +1,7 @@ package org.koitharu.kotatsu.core.parser import android.util.Log +import androidx.collection.MutableLongSet import coil.request.CachePolicy import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope @@ -189,7 +190,7 @@ class RemoteMangaRepository( return emptyList() } val result = ArrayList(size) - val set = HashSet(size) + val set = MutableLongSet(size) for (page in this) { if (set.add(page.id)) { result.add(page) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/widgets/CubicSlider.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/widgets/CubicSlider.kt index a75693982..57c7bbb57 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/widgets/CubicSlider.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/widgets/CubicSlider.kt @@ -1,8 +1,8 @@ package org.koitharu.kotatsu.core.ui.widgets import android.content.Context -import android.util.ArrayMap import android.util.AttributeSet +import androidx.collection.MutableScatterMap import com.google.android.material.slider.Slider import kotlin.math.cbrt import kotlin.math.pow @@ -12,7 +12,7 @@ class CubicSlider @JvmOverloads constructor( attrs: AttributeSet? = null, ) : Slider(context, attrs) { - private val changeListeners = ArrayMap(1) + private val changeListeners = MutableScatterMap(1) override fun setValue(value: Float) { super.setValue(value.unmap()) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/widgets/SegmentedBarView.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/widgets/SegmentedBarView.kt index 78ca6ad48..6b1e345bf 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/widgets/SegmentedBarView.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/widgets/SegmentedBarView.kt @@ -11,6 +11,7 @@ import android.view.View import android.view.ViewOutlineProvider import androidx.annotation.ColorInt import androidx.annotation.FloatRange +import androidx.collection.MutableFloatList import androidx.interpolator.view.animation.FastOutSlowInInterpolator import org.koitharu.kotatsu.core.util.ext.getAnimationDuration import org.koitharu.kotatsu.core.util.ext.isAnimationsEnabled @@ -25,7 +26,7 @@ class SegmentedBarView @JvmOverloads constructor( private val paint = Paint(Paint.ANTI_ALIAS_FLAG) private val segmentsData = ArrayList() - private val segmentsSizes = ArrayList() + private val segmentsSizes = MutableFloatList() private var cornerSize = 0f private var scaleFactor = 1f private var scaleAnimator: ValueAnimator? = null diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/progress/TimeLeftEstimator.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/progress/TimeLeftEstimator.kt index e83507ef1..2aa9ef8a9 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/progress/TimeLeftEstimator.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/progress/TimeLeftEstimator.kt @@ -1,6 +1,8 @@ package org.koitharu.kotatsu.core.util.progress import android.os.SystemClock +import androidx.collection.IntList +import androidx.collection.MutableIntList import java.util.concurrent.TimeUnit import kotlin.math.roundToInt import kotlin.math.roundToLong @@ -10,7 +12,7 @@ private const val NO_TIME = -1L class TimeLeftEstimator { - private var times = ArrayList() + private var times = MutableIntList() private var lastTick: Tick? = null private val tooLargeTime = TimeUnit.DAYS.toMillis(1) @@ -50,6 +52,15 @@ class TimeLeftEstimator { return if (etl == NO_TIME) NO_TIME else System.currentTimeMillis() + etl } + private fun IntList.average(): Double { + if (isEmpty()) { + return 0.0 + } + var acc = 0L + forEach { acc += it } + return acc / size.toDouble() + } + private class Tick( @JvmField val value: Int, @JvmField val total: Int, diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadSlowdownDispatcher.kt b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadSlowdownDispatcher.kt index e59722045..e6a512f95 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadSlowdownDispatcher.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadSlowdownDispatcher.kt @@ -1,5 +1,6 @@ package org.koitharu.kotatsu.download.ui.worker +import androidx.collection.MutableObjectLongMap import kotlinx.coroutines.delay import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.RemoteMangaRepository @@ -9,7 +10,7 @@ class DownloadSlowdownDispatcher( private val mangaRepositoryFactory: MangaRepository.Factory, private val defaultDelay: Long, ) { - private val timeMap = HashMap() + private val timeMap = MutableObjectLongMap() suspend fun delay(source: MangaSource) { val repo = mangaRepositoryFactory.create(source) as? RemoteMangaRepository ?: return @@ -17,7 +18,7 @@ class DownloadSlowdownDispatcher( return } val lastRequest = synchronized(timeMap) { - val res = timeMap[source] ?: 0L + val res = timeMap.getOrDefault(source, 0L) timeMap[source] = System.currentTimeMillis() res } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/tracker/domain/Tracker.kt b/app/src/main/kotlin/org/koitharu/kotatsu/tracker/domain/Tracker.kt index 115fc4dfb..689bd2ee4 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/tracker/domain/Tracker.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/tracker/domain/Tracker.kt @@ -1,6 +1,7 @@ package org.koitharu.kotatsu.tracker.domain import androidx.annotation.VisibleForTesting +import androidx.collection.MutableLongSet import coil.request.CachePolicy import org.koitharu.kotatsu.core.model.getPreferredBranch import org.koitharu.kotatsu.core.parser.MangaRepository @@ -30,7 +31,7 @@ class Tracker @Inject constructor( if (sources.isEmpty()) { return emptyList() } - val knownManga = HashSet() + val knownManga = MutableLongSet() val result = ArrayList() // Favourites if (AppSettings.TRACK_FAVOURITES in sources) { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/tracker/domain/TrackingRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/tracker/domain/TrackingRepository.kt index 406735fb9..f9d67f097 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/tracker/domain/TrackingRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/tracker/domain/TrackingRepository.kt @@ -1,6 +1,7 @@ package org.koitharu.kotatsu.tracker.domain import androidx.annotation.VisibleForTesting +import androidx.collection.MutableLongSet import androidx.room.withTransaction import dagger.Reusable import kotlinx.coroutines.flow.Flow @@ -74,7 +75,7 @@ class TrackingRepository @Inject constructor( ids.windowed(MAX_QUERY_IDS, MAX_QUERY_IDS, true) .flatMap { dao.findAll(it) } }.groupBy { it.mangaId } - val idSet = HashSet() + val idSet = MutableLongSet(mangaList.size) val result = ArrayList(mangaList.size) for (item in mangaList) { val manga = if (item.isLocal) {