Partially migrate to new collections

This commit is contained in:
Koitharu
2024-01-31 15:55:33 +02:00
parent a4e2675d61
commit edca0e5334
8 changed files with 31 additions and 12 deletions

View File

@@ -3,6 +3,7 @@ package org.koitharu.kotatsu.core.model
import android.net.Uri import android.net.Uri
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.collection.MutableObjectIntMap
import androidx.core.os.LocaleListCompat import androidx.core.os.LocaleListCompat
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.util.ext.iterator import org.koitharu.kotatsu.core.util.ext.iterator
@@ -31,12 +32,14 @@ fun Collection<ChapterListItem>.countChaptersByBranch(): Int {
if (size <= 1) { if (size <= 1) {
return size return size
} }
val acc = HashMap<String?, Int>() val acc = MutableObjectIntMap<String?>()
for (item in this) { for (item in this) {
val branch = item.chapter.branch 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 @get:StringRes

View File

@@ -1,6 +1,7 @@
package org.koitharu.kotatsu.core.parser package org.koitharu.kotatsu.core.parser
import android.util.Log import android.util.Log
import androidx.collection.MutableLongSet
import coil.request.CachePolicy import coil.request.CachePolicy
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@@ -189,7 +190,7 @@ class RemoteMangaRepository(
return emptyList() return emptyList()
} }
val result = ArrayList<MangaPage>(size) val result = ArrayList<MangaPage>(size)
val set = HashSet<Long>(size) val set = MutableLongSet(size)
for (page in this) { for (page in this) {
if (set.add(page.id)) { if (set.add(page.id)) {
result.add(page) result.add(page)

View File

@@ -1,8 +1,8 @@
package org.koitharu.kotatsu.core.ui.widgets package org.koitharu.kotatsu.core.ui.widgets
import android.content.Context import android.content.Context
import android.util.ArrayMap
import android.util.AttributeSet import android.util.AttributeSet
import androidx.collection.MutableScatterMap
import com.google.android.material.slider.Slider import com.google.android.material.slider.Slider
import kotlin.math.cbrt import kotlin.math.cbrt
import kotlin.math.pow import kotlin.math.pow
@@ -12,7 +12,7 @@ class CubicSlider @JvmOverloads constructor(
attrs: AttributeSet? = null, attrs: AttributeSet? = null,
) : Slider(context, attrs) { ) : Slider(context, attrs) {
private val changeListeners = ArrayMap<OnChangeListener, OnChangeListenerMapper>(1) private val changeListeners = MutableScatterMap<OnChangeListener, OnChangeListenerMapper>(1)
override fun setValue(value: Float) { override fun setValue(value: Float) {
super.setValue(value.unmap()) super.setValue(value.unmap())

View File

@@ -11,6 +11,7 @@ import android.view.View
import android.view.ViewOutlineProvider import android.view.ViewOutlineProvider
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import androidx.annotation.FloatRange import androidx.annotation.FloatRange
import androidx.collection.MutableFloatList
import androidx.interpolator.view.animation.FastOutSlowInInterpolator import androidx.interpolator.view.animation.FastOutSlowInInterpolator
import org.koitharu.kotatsu.core.util.ext.getAnimationDuration import org.koitharu.kotatsu.core.util.ext.getAnimationDuration
import org.koitharu.kotatsu.core.util.ext.isAnimationsEnabled 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 paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val segmentsData = ArrayList<Segment>() private val segmentsData = ArrayList<Segment>()
private val segmentsSizes = ArrayList<Float>() private val segmentsSizes = MutableFloatList()
private var cornerSize = 0f private var cornerSize = 0f
private var scaleFactor = 1f private var scaleFactor = 1f
private var scaleAnimator: ValueAnimator? = null private var scaleAnimator: ValueAnimator? = null

View File

@@ -1,6 +1,8 @@
package org.koitharu.kotatsu.core.util.progress package org.koitharu.kotatsu.core.util.progress
import android.os.SystemClock import android.os.SystemClock
import androidx.collection.IntList
import androidx.collection.MutableIntList
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlin.math.roundToInt import kotlin.math.roundToInt
import kotlin.math.roundToLong import kotlin.math.roundToLong
@@ -10,7 +12,7 @@ private const val NO_TIME = -1L
class TimeLeftEstimator { class TimeLeftEstimator {
private var times = ArrayList<Int>() private var times = MutableIntList()
private var lastTick: Tick? = null private var lastTick: Tick? = null
private val tooLargeTime = TimeUnit.DAYS.toMillis(1) private val tooLargeTime = TimeUnit.DAYS.toMillis(1)
@@ -50,6 +52,15 @@ class TimeLeftEstimator {
return if (etl == NO_TIME) NO_TIME else System.currentTimeMillis() + etl 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( private class Tick(
@JvmField val value: Int, @JvmField val value: Int,
@JvmField val total: Int, @JvmField val total: Int,

View File

@@ -1,5 +1,6 @@
package org.koitharu.kotatsu.download.ui.worker package org.koitharu.kotatsu.download.ui.worker
import androidx.collection.MutableObjectLongMap
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
@@ -9,7 +10,7 @@ class DownloadSlowdownDispatcher(
private val mangaRepositoryFactory: MangaRepository.Factory, private val mangaRepositoryFactory: MangaRepository.Factory,
private val defaultDelay: Long, private val defaultDelay: Long,
) { ) {
private val timeMap = HashMap<MangaSource, Long>() private val timeMap = MutableObjectLongMap<MangaSource>()
suspend fun delay(source: MangaSource) { suspend fun delay(source: MangaSource) {
val repo = mangaRepositoryFactory.create(source) as? RemoteMangaRepository ?: return val repo = mangaRepositoryFactory.create(source) as? RemoteMangaRepository ?: return
@@ -17,7 +18,7 @@ class DownloadSlowdownDispatcher(
return return
} }
val lastRequest = synchronized(timeMap) { val lastRequest = synchronized(timeMap) {
val res = timeMap[source] ?: 0L val res = timeMap.getOrDefault(source, 0L)
timeMap[source] = System.currentTimeMillis() timeMap[source] = System.currentTimeMillis()
res res
} }

View File

@@ -1,6 +1,7 @@
package org.koitharu.kotatsu.tracker.domain package org.koitharu.kotatsu.tracker.domain
import androidx.annotation.VisibleForTesting import androidx.annotation.VisibleForTesting
import androidx.collection.MutableLongSet
import coil.request.CachePolicy import coil.request.CachePolicy
import org.koitharu.kotatsu.core.model.getPreferredBranch import org.koitharu.kotatsu.core.model.getPreferredBranch
import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository
@@ -30,7 +31,7 @@ class Tracker @Inject constructor(
if (sources.isEmpty()) { if (sources.isEmpty()) {
return emptyList() return emptyList()
} }
val knownManga = HashSet<Long>() val knownManga = MutableLongSet()
val result = ArrayList<TrackingItem>() val result = ArrayList<TrackingItem>()
// Favourites // Favourites
if (AppSettings.TRACK_FAVOURITES in sources) { if (AppSettings.TRACK_FAVOURITES in sources) {

View File

@@ -1,6 +1,7 @@
package org.koitharu.kotatsu.tracker.domain package org.koitharu.kotatsu.tracker.domain
import androidx.annotation.VisibleForTesting import androidx.annotation.VisibleForTesting
import androidx.collection.MutableLongSet
import androidx.room.withTransaction import androidx.room.withTransaction
import dagger.Reusable import dagger.Reusable
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
@@ -74,7 +75,7 @@ class TrackingRepository @Inject constructor(
ids.windowed(MAX_QUERY_IDS, MAX_QUERY_IDS, true) ids.windowed(MAX_QUERY_IDS, MAX_QUERY_IDS, true)
.flatMap { dao.findAll(it) } .flatMap { dao.findAll(it) }
}.groupBy { it.mangaId } }.groupBy { it.mangaId }
val idSet = HashSet<Long>() val idSet = MutableLongSet(mangaList.size)
val result = ArrayList<MangaTracking>(mangaList.size) val result = ArrayList<MangaTracking>(mangaList.size)
for (item in mangaList) { for (item in mangaList) {
val manga = if (item.isLocal) { val manga = if (item.isLocal) {