Refactor extensions

pull/195/head
Koitharu 4 years ago
parent 6934daecff
commit be67b36b6a
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -3,14 +3,13 @@ package org.koitharu.kotatsu.core.model.parcelable
import android.os.Parcel import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import org.koitharu.kotatsu.parsers.model.MangaChapter import org.koitharu.kotatsu.parsers.model.MangaChapter
import org.koitharu.kotatsu.utils.ext.createList
class ParcelableMangaChapters( class ParcelableMangaChapters(
val chapters: List<MangaChapter>, val chapters: List<MangaChapter>,
) : Parcelable { ) : Parcelable {
constructor(parcel: Parcel) : this( constructor(parcel: Parcel) : this(
createList(parcel.readInt()) { parcel.readMangaChapter() } List(parcel.readInt()) { parcel.readMangaChapter() }
) )
override fun writeToParcel(parcel: Parcel, flags: Int) { override fun writeToParcel(parcel: Parcel, flags: Int) {

@ -3,14 +3,13 @@ package org.koitharu.kotatsu.core.model.parcelable
import android.os.Parcel import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import org.koitharu.kotatsu.parsers.model.MangaPage import org.koitharu.kotatsu.parsers.model.MangaPage
import org.koitharu.kotatsu.utils.ext.createList
class ParcelableMangaPages( class ParcelableMangaPages(
val pages: List<MangaPage>, val pages: List<MangaPage>,
) : Parcelable { ) : Parcelable {
constructor(parcel: Parcel) : this( constructor(parcel: Parcel) : this(
createList(parcel.readInt()) { parcel.readMangaPage() } List(parcel.readInt()) { parcel.readMangaPage() }
) )
override fun writeToParcel(parcel: Parcel, flags: Int) { override fun writeToParcel(parcel: Parcel, flags: Int) {

@ -3,14 +3,14 @@ package org.koitharu.kotatsu.core.model.parcelable
import android.os.Parcel import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import org.koitharu.kotatsu.parsers.model.MangaTag import org.koitharu.kotatsu.parsers.model.MangaTag
import org.koitharu.kotatsu.utils.ext.createSet import org.koitharu.kotatsu.utils.ext.Set
class ParcelableMangaTags( class ParcelableMangaTags(
val tags: Set<MangaTag>, val tags: Set<MangaTag>,
) : Parcelable { ) : Parcelable {
constructor(parcel: Parcel) : this( constructor(parcel: Parcel) : this(
createSet(parcel.readInt()) { parcel.readMangaTag() } Set(parcel.readInt()) { parcel.readMangaTag() }
) )
override fun writeToParcel(parcel: Parcel, flags: Int) { override fun writeToParcel(parcel: Parcel, flags: Int) {

@ -10,7 +10,7 @@ import org.koitharu.kotatsu.core.db.MangaDatabase
import org.koitharu.kotatsu.parsers.model.MangaChapter import org.koitharu.kotatsu.parsers.model.MangaChapter
import org.koitharu.kotatsu.scrobbling.data.ScrobblingEntity import org.koitharu.kotatsu.scrobbling.data.ScrobblingEntity
import org.koitharu.kotatsu.scrobbling.domain.model.* import org.koitharu.kotatsu.scrobbling.domain.model.*
import org.koitharu.kotatsu.utils.ext.findKey import org.koitharu.kotatsu.utils.ext.findKeyByValue
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
abstract class Scrobbler( abstract class Scrobbler(
@ -59,7 +59,7 @@ abstract class Scrobbler(
scrobbler = scrobblerService, scrobbler = scrobblerService,
mangaId = mangaId, mangaId = mangaId,
targetId = targetId, targetId = targetId,
status = statuses.findKey(status), status = statuses.findKeyByValue(status),
chapter = chapter, chapter = chapter,
comment = comment, comment = comment,
rating = rating, rating = rating,

@ -18,25 +18,20 @@ inline fun <T> MutableSet(size: Int, init: (index: Int) -> T): MutableSet<T> {
return set return set
} }
inline fun <T> createSet(size: Int, init: (index: Int) -> T): Set<T> = when (size) { @Suppress("FunctionName")
inline fun <T> Set(size: Int, init: (index: Int) -> T): Set<T> = when (size) {
0 -> emptySet() 0 -> emptySet()
1 -> Collections.singleton(init(0)) 1 -> Collections.singleton(init(0))
else -> MutableSet(size, init) else -> MutableSet(size, init)
} }
inline fun <T> createList(size: Int, init: (index: Int) -> T): List<T> = when (size) {
0 -> emptyList()
1 -> Collections.singletonList(init(0))
else -> MutableList(size, init)
}
fun <T> List<T>.asArrayList(): ArrayList<T> = if (this is ArrayList<*>) { fun <T> List<T>.asArrayList(): ArrayList<T> = if (this is ArrayList<*>) {
this as ArrayList<T> this as ArrayList<T>
} else { } else {
ArrayList(this) ArrayList(this)
} }
fun <K, V> Map<K, V>.findKey(value: V): K? { fun <K, V> Map<K, V>.findKeyByValue(value: V): K? {
for ((k, v) in entries) { for ((k, v) in entries) {
if (v == value) { if (v == value) {
return k return k

@ -1,34 +0,0 @@
package org.koitharu.kotatsu.utils.ext
import androidx.core.os.LocaleListCompat
import java.util.*
fun LocaleListCompat.getOrThrow(index: Int) = get(index) ?: throw kotlin.NoSuchElementException()
fun LocaleListCompat.toList(): List<Locale> = createList(size()) { i -> getOrThrow(i) }
operator fun LocaleListCompat.iterator() = object : Iterator<Locale> {
private var index = 0
override fun hasNext(): Boolean = index < size()
override fun next(): Locale = getOrThrow(index++)
}
inline fun <R, C : MutableCollection<in R>> LocaleListCompat.mapTo(
destination: C,
block: (Locale) -> R,
): C {
val len = size()
for (i in 0 until len) {
val item = get(i) ?: continue
destination.add(block(item))
}
return destination
}
inline fun <T> LocaleListCompat.map(block: (Locale) -> T): List<T> {
return mapTo(ArrayList(size()), block)
}
inline fun <T> LocaleListCompat.mapToSet(block: (Locale) -> T): Set<T> {
return mapTo(LinkedHashSet(size()), block)
}

@ -0,0 +1,35 @@
package org.koitharu.kotatsu.utils.ext
import androidx.core.os.LocaleListCompat
import java.util.*
operator fun LocaleListCompat.iterator(): ListIterator<Locale> = LocaleListCompatIterator(this)
fun LocaleListCompat.toList(): List<Locale> = List(size()) { i -> getOrThrow(i) }
inline fun <T> LocaleListCompat.map(block: (Locale) -> T): List<T> {
return List(size()) { i -> block(getOrThrow(i)) }
}
inline fun <T> LocaleListCompat.mapToSet(block: (Locale) -> T): Set<T> {
return Set(size()) { i -> block(getOrThrow(i)) }
}
fun LocaleListCompat.getOrThrow(index: Int) = get(index) ?: throw NoSuchElementException()
private class LocaleListCompatIterator(private val list: LocaleListCompat) : ListIterator<Locale> {
private var index = 0
override fun hasNext() = index < list.size()
override fun hasPrevious() = index > 0
override fun next() = list.get(index++) ?: throw NoSuchElementException()
override fun nextIndex() = index
override fun previous() = list.get(--index) ?: throw NoSuchElementException()
override fun previousIndex() = index - 1
}
Loading…
Cancel
Save