Small cleanup and optimizations, add a coroutine version of insertFlatMetadata
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
package exh.util
|
||||
|
||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||
import uy.kohesive.injekt.Injekt
|
||||
import uy.kohesive.injekt.api.get
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
|
||||
class DataSaver {
|
||||
private val preferences: PreferencesHelper = Injekt.get()
|
||||
object DataSaver {
|
||||
private val preferences: PreferencesHelper by injectLazy()
|
||||
|
||||
fun compress(imageUrl: String): String {
|
||||
return if (preferences.dataSaver().get() && preferences.dataSaverServer().get().isNotBlank() && !imageUrl.contains(preferences.dataSaverServer().get() + "/?")) {
|
||||
@@ -20,10 +19,10 @@ class DataSaver {
|
||||
private fun getUrl(imageUrl: String): String {
|
||||
val server = preferences.dataSaverServer().get() + "/?"
|
||||
val format = "jpg=${if (preferences.dataSaverImageFormatJpeg().get()) "1" else "0"}"
|
||||
val quality = "&l=${preferences.dataSaverImageQuality().get()}"
|
||||
val colorBW = "&bw=${if (preferences.dataSaverColorBW().get()) "1" else "0"}"
|
||||
val url = "$server$format$quality$colorBW&url="
|
||||
val quality = "l=${preferences.dataSaverImageQuality().get()}"
|
||||
val colorBW = "bw=${if (preferences.dataSaverColorBW().get()) "1" else "0"}"
|
||||
val url = "url=$imageUrl"
|
||||
|
||||
return url + imageUrl
|
||||
return "$server&$format&$quality&$colorBW&$url"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package exh.util
|
||||
|
||||
import com.pushtorefresh.storio.operations.PreparedOperation
|
||||
import com.pushtorefresh.storio.sqlite.operations.get.PreparedGetListOfObjects
|
||||
import com.pushtorefresh.storio.sqlite.operations.get.PreparedGetObject
|
||||
import com.pushtorefresh.storio.sqlite.operations.put.PreparedPutCollectionOfObjects
|
||||
@@ -16,3 +17,5 @@ suspend fun <T> PreparedGetObject<T>.executeOnIO(): T? = withContext(Dispatchers
|
||||
suspend fun <T> PreparedPutObject<T>.executeOnIO(): PutResult = withContext(Dispatchers.IO) { executeAsBlocking() }
|
||||
|
||||
suspend fun <T> PreparedPutCollectionOfObjects<T>.executeOnIO(): PutResults<T> = withContext(Dispatchers.IO) { executeAsBlocking() }
|
||||
|
||||
suspend fun <T> PreparedOperation<T>.executeOnIO(): T? = withContext(Dispatchers.IO) { executeAsBlocking() }
|
||||
|
||||
@@ -1,176 +0,0 @@
|
||||
package exh.util
|
||||
|
||||
// Zero-allocation-overhead mutable collection shims
|
||||
|
||||
private inline class CollectionShim<E>(private val coll: Collection<E>) : FakeMutableCollection<E> {
|
||||
override val size: Int get() = coll.size
|
||||
|
||||
override fun contains(element: E) = coll.contains(element)
|
||||
|
||||
override fun containsAll(elements: Collection<E>) = coll.containsAll(elements)
|
||||
|
||||
override fun isEmpty() = coll.isEmpty()
|
||||
|
||||
override fun fakeIterator() = coll.iterator()
|
||||
}
|
||||
|
||||
interface FakeMutableCollection<E> : MutableCollection<E>, FakeMutableIterable<E> {
|
||||
override fun add(element: E): Boolean {
|
||||
throw UnsupportedOperationException("This collection is immutable!")
|
||||
}
|
||||
|
||||
override fun addAll(elements: Collection<E>): Boolean {
|
||||
throw UnsupportedOperationException("This collection is immutable!")
|
||||
}
|
||||
|
||||
override fun clear() {
|
||||
throw UnsupportedOperationException("This collection is immutable!")
|
||||
}
|
||||
|
||||
override fun remove(element: E): Boolean {
|
||||
throw UnsupportedOperationException("This collection is immutable!")
|
||||
}
|
||||
|
||||
override fun removeAll(elements: Collection<E>): Boolean {
|
||||
throw UnsupportedOperationException("This collection is immutable!")
|
||||
}
|
||||
|
||||
override fun retainAll(elements: Collection<E>): Boolean {
|
||||
throw UnsupportedOperationException("This collection is immutable!")
|
||||
}
|
||||
|
||||
override fun iterator(): MutableIterator<E> = super.iterator()
|
||||
|
||||
companion object {
|
||||
fun <E> fromCollection(coll: Collection<E>): FakeMutableCollection<E> = CollectionShim(coll)
|
||||
}
|
||||
}
|
||||
|
||||
private inline class SetShim<E>(private val set: Set<E>) : FakeMutableSet<E> {
|
||||
override val size: Int get() = set.size
|
||||
|
||||
override fun contains(element: E) = set.contains(element)
|
||||
|
||||
override fun containsAll(elements: Collection<E>) = set.containsAll(elements)
|
||||
|
||||
override fun isEmpty() = set.isEmpty()
|
||||
|
||||
override fun fakeIterator() = set.iterator()
|
||||
}
|
||||
|
||||
interface FakeMutableSet<E> : MutableSet<E>, FakeMutableCollection<E> {
|
||||
/**
|
||||
* Adds the specified element to the set.
|
||||
*
|
||||
* @return `true` if the element has been added, `false` if the element is already contained in the set.
|
||||
*/
|
||||
override fun add(element: E): Boolean = super.add(element)
|
||||
|
||||
override fun addAll(elements: Collection<E>): Boolean = super.addAll(elements)
|
||||
|
||||
override fun clear() = super.clear()
|
||||
|
||||
override fun remove(element: E): Boolean = super.remove(element)
|
||||
|
||||
override fun removeAll(elements: Collection<E>): Boolean = super.removeAll(elements)
|
||||
|
||||
override fun retainAll(elements: Collection<E>): Boolean = super.retainAll(elements)
|
||||
|
||||
override fun iterator(): MutableIterator<E> = super.iterator()
|
||||
|
||||
companion object {
|
||||
fun <E> fromSet(set: Set<E>): FakeMutableSet<E> = SetShim(set)
|
||||
}
|
||||
}
|
||||
|
||||
private inline class IterableShim<E>(private val iterable: Iterable<E>) : FakeMutableIterable<E> {
|
||||
override fun fakeIterator() = iterable.iterator()
|
||||
}
|
||||
|
||||
interface FakeMutableIterable<E> : MutableIterable<E> {
|
||||
/**
|
||||
* Returns an iterator over the elements of this sequence that supports removing elements during iteration.
|
||||
*/
|
||||
override fun iterator(): MutableIterator<E> = FakeMutableIterator.fromIterator(fakeIterator())
|
||||
|
||||
fun fakeIterator(): Iterator<E>
|
||||
|
||||
companion object {
|
||||
fun <E> fromIterable(iterable: Iterable<E>): FakeMutableIterable<E> = IterableShim(iterable)
|
||||
}
|
||||
}
|
||||
|
||||
private inline class IteratorShim<E>(private val iterator: Iterator<E>) : FakeMutableIterator<E> {
|
||||
/**
|
||||
* Returns `true` if the iteration has more elements.
|
||||
*/
|
||||
override fun hasNext() = iterator.hasNext()
|
||||
|
||||
/**
|
||||
* Returns the next element in the iteration.
|
||||
*/
|
||||
override fun next() = iterator.next()
|
||||
}
|
||||
|
||||
interface FakeMutableIterator<E> : MutableIterator<E> {
|
||||
/**
|
||||
* Removes from the underlying collection the last element returned by this iterator.
|
||||
*/
|
||||
override fun remove() {
|
||||
throw UnsupportedOperationException("This set is immutable!")
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun <E> fromIterator(iterator: Iterator<E>): FakeMutableIterator<E> = IteratorShim(iterator)
|
||||
}
|
||||
}
|
||||
|
||||
private inline class EntryShim<K, V>(private val entry: Map.Entry<K, V>) : FakeMutableEntry<K, V> {
|
||||
/**
|
||||
* Returns the key of this key/value pair.
|
||||
*/
|
||||
override val key: K
|
||||
get() = entry.key
|
||||
|
||||
/**
|
||||
* Returns the value of this key/value pair.
|
||||
*/
|
||||
override val value: V
|
||||
get() = entry.value
|
||||
}
|
||||
|
||||
private inline class PairShim<K, V>(private val pair: Pair<K, V>) : FakeMutableEntry<K, V> {
|
||||
/**
|
||||
* Returns the key of this key/value pair.
|
||||
*/
|
||||
override val key: K get() = pair.first
|
||||
|
||||
/**
|
||||
* Returns the value of this key/value pair.
|
||||
*/
|
||||
override val value: V get() = pair.second
|
||||
}
|
||||
|
||||
interface FakeMutableEntry<K, V> : MutableMap.MutableEntry<K, V> {
|
||||
override fun setValue(newValue: V): V {
|
||||
throw UnsupportedOperationException("This entry is immutable!")
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun <K, V> fromEntry(entry: Map.Entry<K, V>): FakeMutableEntry<K, V> = EntryShim(entry)
|
||||
|
||||
fun <K, V> fromPair(pair: Pair<K, V>): FakeMutableEntry<K, V> = PairShim(pair)
|
||||
|
||||
fun <K, V> fromPair(key: K, value: V) = object : FakeMutableEntry<K, V> {
|
||||
/**
|
||||
* Returns the key of this key/value pair.
|
||||
*/
|
||||
override val key: K = key
|
||||
|
||||
/**
|
||||
* Returns the value of this key/value pair.
|
||||
*/
|
||||
override val value: V = value
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,21 +24,27 @@ fun Manga.mangaType(context: Context): String {
|
||||
/**
|
||||
* The type of comic the manga is (ie. manga, manhwa, manhua)
|
||||
*/
|
||||
fun Manga.mangaType(): MangaType {
|
||||
val sourceName = Injekt.get<SourceManager>().getOrStub(source).name
|
||||
fun Manga.mangaType(sourceName: String = Injekt.get<SourceManager>().getOrStub(source).name): MangaType {
|
||||
val currentTags = getGenres().orEmpty()
|
||||
return if (currentTags.any { tag -> isMangaTag(tag) }) {
|
||||
MangaType.TYPE_MANGA
|
||||
} else if (currentTags.any { tag -> isWebtoonTag(tag) } || isWebtoonSource(sourceName)) {
|
||||
MangaType.TYPE_WEBTOON
|
||||
} else if (currentTags.any { tag -> isComicTag(tag) } || isComicSource(sourceName)) {
|
||||
MangaType.TYPE_COMIC
|
||||
} else if (currentTags.any { tag -> isManhuaTag(tag) } || isManhuaSource(sourceName)) {
|
||||
MangaType.TYPE_MANHUA
|
||||
} else if (currentTags.any { tag -> isManhwaTag(tag) } || isManhwaSource(sourceName)) {
|
||||
MangaType.TYPE_MANHWA
|
||||
} else {
|
||||
MangaType.TYPE_MANGA
|
||||
return when {
|
||||
currentTags.any { tag -> isMangaTag(tag) } -> {
|
||||
MangaType.TYPE_MANGA
|
||||
}
|
||||
currentTags.any { tag -> isWebtoonTag(tag) } || isWebtoonSource(sourceName) -> {
|
||||
MangaType.TYPE_WEBTOON
|
||||
}
|
||||
currentTags.any { tag -> isComicTag(tag) } || isComicSource(sourceName) -> {
|
||||
MangaType.TYPE_COMIC
|
||||
}
|
||||
currentTags.any { tag -> isManhuaTag(tag) } || isManhuaSource(sourceName) -> {
|
||||
MangaType.TYPE_MANHUA
|
||||
}
|
||||
currentTags.any { tag -> isManhwaTag(tag) } || isManhwaSource(sourceName) -> {
|
||||
MangaType.TYPE_MANHWA
|
||||
}
|
||||
else -> {
|
||||
MangaType.TYPE_MANGA
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,8 +52,7 @@ fun Manga.mangaType(): MangaType {
|
||||
* The type the reader should use. Different from manga type as certain manga has different
|
||||
* read types
|
||||
*/
|
||||
fun Manga.defaultReaderType(): Int? {
|
||||
val type = mangaType()
|
||||
fun Manga.defaultReaderType(type: MangaType = mangaType()): Int? {
|
||||
return if (type == MangaType.TYPE_MANHWA || type == MangaType.TYPE_WEBTOON) {
|
||||
ReaderActivity.WEBTOON
|
||||
} else null
|
||||
|
||||
@@ -1,32 +1,8 @@
|
||||
package exh.util
|
||||
|
||||
import com.pushtorefresh.storio.operations.PreparedOperation
|
||||
import com.pushtorefresh.storio.sqlite.operations.get.PreparedGetObject
|
||||
import kotlinx.coroutines.CancellableContinuation
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineStart
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.InternalCoroutinesApi
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import rx.Completable
|
||||
import rx.CompletableSubscriber
|
||||
import rx.Emitter
|
||||
import rx.Observable
|
||||
import rx.Observer
|
||||
import rx.Scheduler
|
||||
import rx.Single
|
||||
import rx.SingleSubscriber
|
||||
import rx.Subscriber
|
||||
import rx.Subscription
|
||||
import rx.subjects.ReplaySubject
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.resumeWithException
|
||||
|
||||
/**
|
||||
* Transform a cold single to a hot single
|
||||
@@ -49,7 +25,7 @@ fun <T> Observable<T>.melt(): Observable<T> {
|
||||
subscribe(rs)
|
||||
return rs
|
||||
}
|
||||
|
||||
/*
|
||||
suspend fun <T> Single<T>.await(subscribeOn: Scheduler? = null): T {
|
||||
return suspendCancellableCoroutine { continuation ->
|
||||
val self = if (subscribeOn != null) subscribeOn(subscribeOn) else this
|
||||
@@ -181,11 +157,11 @@ private suspend fun <T> Observable<T>.awaitOne(): T = suspendCancellableCoroutin
|
||||
}
|
||||
|
||||
override fun onError(e: Throwable) {
|
||||
/*
|
||||
*//*
|
||||
* Rx1 observable throws NoSuchElementException if cancellation happened before
|
||||
* element emission. To mitigate this we try to atomically resume continuation with exception:
|
||||
* if resume failed, then we know that continuation successfully cancelled itself
|
||||
*/
|
||||
*//*
|
||||
val token = cont.tryResumeWithException(e)
|
||||
if (token != null) {
|
||||
cont.completeResume(token)
|
||||
@@ -220,10 +196,10 @@ fun <T : Any> Observable<T>.asFlow(): Flow<T> = callbackFlow {
|
||||
fun <T : Any> Flow<T>.asObservable(backpressureMode: Emitter.BackpressureMode = Emitter.BackpressureMode.NONE): Observable<T> {
|
||||
return Observable.create(
|
||||
{ emitter ->
|
||||
/*
|
||||
*//*
|
||||
* ATOMIC is used here to provide stable behaviour of subscribe+dispose pair even if
|
||||
* asObservable is already invoked from unconfined
|
||||
*/
|
||||
*//*
|
||||
val job = GlobalScope.launch(Dispatchers.Unconfined, start = CoroutineStart.ATOMIC) {
|
||||
try {
|
||||
collect { emitter.onNext(it) }
|
||||
@@ -241,4 +217,4 @@ fun <T : Any> Flow<T>.asObservable(backpressureMode: Emitter.BackpressureMode =
|
||||
},
|
||||
backpressureMode
|
||||
)
|
||||
}
|
||||
}*/
|
||||
|
||||
@@ -23,13 +23,11 @@ fun UrlImportableSource.urlImportFetchSearchManga(context: Context, query: Strin
|
||||
})
|
||||
.map { res ->
|
||||
MangasPage(
|
||||
(
|
||||
if (res is GalleryAddEvent.Success) {
|
||||
listOf(res.manga)
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
),
|
||||
if (res is GalleryAddEvent.Success) {
|
||||
listOf(res.manga)
|
||||
} else {
|
||||
emptyList()
|
||||
},
|
||||
false
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user