Optimize imports, disallow wildcard imports because of klint, run linter
This commit is contained in:
@@ -14,7 +14,7 @@ class CachedField<T>(private val expiresAfterMs: Long) {
|
||||
|
||||
suspend fun obtain(producer: suspend () -> T): T {
|
||||
return mutex.withLock {
|
||||
if(initTime < 0 || System.currentTimeMillis() - initTime > expiresAfterMs) {
|
||||
if (initTime < 0 || System.currentTimeMillis() - initTime > expiresAfterMs) {
|
||||
content = producer()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package exh.util
|
||||
|
||||
import kotlin.coroutines.coroutineContext
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlin.coroutines.coroutineContext
|
||||
|
||||
@FlowPreview
|
||||
fun <T> Flow<T>.cancellable() = onEach {
|
||||
coroutineContext.ensureActive()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ class DeferredField<T> {
|
||||
*/
|
||||
suspend fun get(): T {
|
||||
// Check if field is initialized and return immediately if it is
|
||||
if(initialized) return content as T
|
||||
if (initialized) return content as T
|
||||
|
||||
// Wait for field to initialize
|
||||
mutex.withLock {}
|
||||
@@ -43,4 +43,4 @@ class DeferredField<T> {
|
||||
// Field is initialized, return value
|
||||
return content as T
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,11 +7,11 @@ inline fun <T> ignore(expr: () -> T): T? {
|
||||
fun <T : Throwable> T.withRootCause(cause: Throwable): T {
|
||||
val curCause = this.cause
|
||||
|
||||
if(curCause == null) {
|
||||
if (curCause == null) {
|
||||
this.initCause(cause)
|
||||
} else {
|
||||
curCause.withRootCause(cause)
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +112,6 @@ private inline class IteratorShim<E>(private val iterator: Iterator<E>) : FakeMu
|
||||
override fun next() = iterator.next()
|
||||
}
|
||||
|
||||
|
||||
interface FakeMutableIterator<E> : MutableIterator<E> {
|
||||
/**
|
||||
* Removes from the underlying collection the last element returned by this iterator.
|
||||
@@ -122,7 +121,7 @@ interface FakeMutableIterator<E> : MutableIterator<E> {
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun <E> fromIterator(iterator: Iterator<E>) : FakeMutableIterator<E> = IteratorShim(iterator)
|
||||
fun <E> fromIterator(iterator: Iterator<E>): FakeMutableIterator<E> = IteratorShim(iterator)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,4 +170,4 @@ interface FakeMutableEntry<K, V> : MutableMap.MutableEntry<K, V> {
|
||||
override val value: V = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,10 +18,10 @@ fun JsonReader.nextJsonObject(): JsonObject {
|
||||
|
||||
val obj = JsonObject()
|
||||
|
||||
while(hasNext()) {
|
||||
while (hasNext()) {
|
||||
val name = nextName()
|
||||
|
||||
when(peek()) {
|
||||
when (peek()) {
|
||||
JsonToken.BEGIN_ARRAY -> obj.add(name, nextJsonArray())
|
||||
JsonToken.BEGIN_OBJECT -> obj.add(name, nextJsonObject())
|
||||
JsonToken.NULL -> {
|
||||
@@ -45,8 +45,8 @@ fun JsonReader.nextJsonArray(): JsonArray {
|
||||
|
||||
val arr = JsonArray()
|
||||
|
||||
while(hasNext()) {
|
||||
when(peek()) {
|
||||
while (hasNext()) {
|
||||
when (peek()) {
|
||||
JsonToken.BEGIN_ARRAY -> arr.add(nextJsonArray())
|
||||
JsonToken.BEGIN_OBJECT -> arr.add(nextJsonObject())
|
||||
JsonToken.NULL -> {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package exh.util
|
||||
|
||||
import io.realm.*
|
||||
import java.util.*
|
||||
import io.realm.Case
|
||||
import io.realm.RealmModel
|
||||
import io.realm.RealmQuery
|
||||
import io.realm.RealmResults
|
||||
import java.util.Date
|
||||
|
||||
/**
|
||||
* Realm query with logging
|
||||
@@ -9,14 +12,16 @@ import java.util.*
|
||||
* @author nulldev
|
||||
*/
|
||||
|
||||
inline fun <reified E : RealmModel> RealmQuery<out E>.beginLog(clazz: Class<out E>? =
|
||||
E::class.java): LoggingRealmQuery<out E>
|
||||
= LoggingRealmQuery.fromQuery(this, clazz)
|
||||
inline fun <reified E : RealmModel> RealmQuery<out E>.beginLog(
|
||||
clazz: Class<out E>? =
|
||||
E::class.java
|
||||
): LoggingRealmQuery<out E> =
|
||||
LoggingRealmQuery.fromQuery(this, clazz)
|
||||
|
||||
class LoggingRealmQuery<E : RealmModel>(val query: RealmQuery<E>) {
|
||||
companion object {
|
||||
fun <E : RealmModel> fromQuery(q: RealmQuery<out E>, clazz: Class<out E>?)
|
||||
= LoggingRealmQuery(q).apply {
|
||||
fun <E : RealmModel> fromQuery(q: RealmQuery<out E>, clazz: Class<out E>?) =
|
||||
LoggingRealmQuery(q).apply {
|
||||
log += "SELECT * FROM ${clazz?.name ?: "???"} WHERE"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package exh.util
|
||||
|
||||
import android.util.SparseArray
|
||||
import java.util.*
|
||||
import java.util.AbstractMap
|
||||
import java.util.LinkedList
|
||||
|
||||
class NakedTrieNode<T>(val key: Int, var parent: NakedTrieNode<T>?) {
|
||||
val children = SparseArray<NakedTrieNode<T>>(1)
|
||||
@@ -12,30 +13,30 @@ class NakedTrieNode<T>(val key: Int, var parent: NakedTrieNode<T>?) {
|
||||
// Consumer should return true to continue walking, false to stop walking
|
||||
inline fun walk(prefix: String, consumer: (String, T) -> Boolean, leavesOnly: Boolean) {
|
||||
// Special case root
|
||||
if(hasData && (!leavesOnly || children.size() <= 0)) {
|
||||
if(!consumer(prefix, data as T)) return
|
||||
if (hasData && (!leavesOnly || children.size() <= 0)) {
|
||||
if (!consumer(prefix, data as T)) return
|
||||
}
|
||||
|
||||
val stack = LinkedList<Pair<String, NakedTrieNode<T>>>()
|
||||
SparseArrayValueCollection(children, true).forEach {
|
||||
stack += prefix + it.key.toChar() to it
|
||||
}
|
||||
while(!stack.isEmpty()) {
|
||||
while (!stack.isEmpty()) {
|
||||
val (key, bottom) = stack.removeLast()
|
||||
SparseArrayValueCollection(bottom.children, true).forEach {
|
||||
stack += key + it.key.toChar() to it
|
||||
}
|
||||
if(bottom.hasData && (!leavesOnly || bottom.children.size() <= 0)) {
|
||||
if(!consumer(key, bottom.data as T)) return
|
||||
if (bottom.hasData && (!leavesOnly || bottom.children.size() <= 0)) {
|
||||
if (!consumer(key, bottom.data as T)) return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getAsNode(key: String): NakedTrieNode<T>? {
|
||||
var current = this
|
||||
for(c in key) {
|
||||
for (c in key) {
|
||||
current = current.children.get(c.toInt()) ?: return null
|
||||
if(!current.hasData) return null
|
||||
if (!current.hasData) return null
|
||||
}
|
||||
return current
|
||||
}
|
||||
@@ -72,10 +73,10 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
override fun put(key: String, value: T): T? {
|
||||
// Traverse to node location in tree, making parent nodes if required
|
||||
var current = root
|
||||
for(c in key) {
|
||||
for (c in key) {
|
||||
val castedC = c.toInt()
|
||||
var node = current.children.get(castedC)
|
||||
if(node == null) {
|
||||
if (node == null) {
|
||||
node = NakedTrieNode(castedC, current)
|
||||
current.children.put(castedC, node)
|
||||
}
|
||||
@@ -83,7 +84,7 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
}
|
||||
|
||||
// Add data to node or replace existing data
|
||||
val previous = if(current.hasData) {
|
||||
val previous = if (current.hasData) {
|
||||
current.data
|
||||
} else {
|
||||
current.hasData = true
|
||||
@@ -99,7 +100,7 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
|
||||
override fun get(key: String): T? {
|
||||
val current = getAsNode(key) ?: return null
|
||||
return if(current.hasData) current.data else null
|
||||
return if (current.hasData) current.data else null
|
||||
}
|
||||
|
||||
fun getAsNode(key: String): NakedTrieNode<T>? {
|
||||
@@ -108,9 +109,9 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
|
||||
override fun containsKey(key: String): Boolean {
|
||||
var current = root
|
||||
for(c in key) {
|
||||
for (c in key) {
|
||||
current = current.children.get(c.toInt()) ?: return false
|
||||
if(!current.hasData) return false
|
||||
if (!current.hasData) return false
|
||||
}
|
||||
return current.hasData
|
||||
}
|
||||
@@ -123,10 +124,10 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
override fun remove(key: String): T? {
|
||||
// Traverse node tree while keeping track of the nodes we have visited
|
||||
val nodeStack = LinkedList<NakedTrieNode<T>>()
|
||||
for(c in key) {
|
||||
for (c in key) {
|
||||
val bottomOfStack = nodeStack.last
|
||||
val current = bottomOfStack.children.get(c.toInt()) ?: return null
|
||||
if(!current.hasData) return null
|
||||
if (!current.hasData) return null
|
||||
nodeStack.add(bottomOfStack)
|
||||
}
|
||||
|
||||
@@ -137,9 +138,9 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
bottomOfStack.data = null // Clear data field for GC
|
||||
|
||||
// Remove nodes that we visited that are useless
|
||||
for(curBottom in nodeStack.descendingIterator()) {
|
||||
for (curBottom in nodeStack.descendingIterator()) {
|
||||
val parent = curBottom.parent ?: break
|
||||
if(!curBottom.hasData && curBottom.children.size() <= 0) {
|
||||
if (!curBottom.hasData && curBottom.children.size() <= 0) {
|
||||
// No data or child nodes, this node is useless, discard
|
||||
parent.children.remove(curBottom.key)
|
||||
} else break
|
||||
@@ -176,10 +177,10 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
fun getOrPut(key: String, producer: () -> T): T {
|
||||
// Traverse to node location in tree, making parent nodes if required
|
||||
var current = root
|
||||
for(c in key) {
|
||||
for (c in key) {
|
||||
val castedC = c.toInt()
|
||||
var node = current.children.get(castedC)
|
||||
if(node == null) {
|
||||
if (node == null) {
|
||||
node = NakedTrieNode(castedC, current)
|
||||
current.children.put(castedC, node)
|
||||
}
|
||||
@@ -187,7 +188,7 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
}
|
||||
|
||||
// Add data to node or replace existing data
|
||||
if(!current.hasData) {
|
||||
if (!current.hasData) {
|
||||
current.hasData = true
|
||||
current.data = producer()
|
||||
size++
|
||||
@@ -253,7 +254,7 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
* Returns `true` if the map contains the specified [key].
|
||||
*/
|
||||
override fun containsKey(key: String): Boolean {
|
||||
if(!key.startsWith(prefix)) return false
|
||||
if (!key.startsWith(prefix)) return false
|
||||
|
||||
val childNode = node.getAsNode(key.removePrefix(prefix)) ?: return false
|
||||
return childNode.hasData && (!leavesOnly || childNode.children.size() <= 0)
|
||||
@@ -264,7 +265,7 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
*/
|
||||
override fun containsValue(value: T): Boolean {
|
||||
node.walk("", { _, v ->
|
||||
if(v == value) return true
|
||||
if (v == value) return true
|
||||
true
|
||||
}, leavesOnly)
|
||||
return false
|
||||
@@ -274,10 +275,10 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
* Returns the value corresponding to the given [key], or `null` if such a key is not present in the map.
|
||||
*/
|
||||
override fun get(key: String): T? {
|
||||
if(!key.startsWith(prefix)) return null
|
||||
if (!key.startsWith(prefix)) return null
|
||||
|
||||
val childNode = node.getAsNode(key.removePrefix(prefix)) ?: return null
|
||||
if(!childNode.hasData || (leavesOnly && childNode.children.size() > 0)) return null
|
||||
if (!childNode.hasData || (leavesOnly && childNode.children.size() > 0)) return null
|
||||
return childNode.data
|
||||
}
|
||||
|
||||
@@ -285,8 +286,8 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
* Returns `true` if the map is empty (contains no elements), `false` otherwise.
|
||||
*/
|
||||
override fun isEmpty(): Boolean {
|
||||
if(node.children.size() <= 0 && !root.hasData) return true
|
||||
if(!leavesOnly) return false
|
||||
if (node.children.size() <= 0 && !root.hasData) return true
|
||||
if (!leavesOnly) return false
|
||||
node.walk("", { _, _ -> return false }, leavesOnly)
|
||||
return true
|
||||
}
|
||||
@@ -300,7 +301,7 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
*/
|
||||
override fun containsValue(value: T): Boolean {
|
||||
walk { _, t ->
|
||||
if(t == value) {
|
||||
if (t == value) {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -342,4 +343,4 @@ class NakedTrie<T> : MutableMap<String, T> {
|
||||
true
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ import org.jsoup.nodes.Document
|
||||
|
||||
fun Response.interceptAsHtml(block: (Document) -> Unit): Response {
|
||||
val body = body
|
||||
if (body?.contentType()?.type == "text"
|
||||
&& body.contentType()?.subtype == "html") {
|
||||
if (body?.contentType()?.type == "text" &&
|
||||
body.contentType()?.subtype == "html") {
|
||||
val bodyString = body.string()
|
||||
val rebuiltResponse = newBuilder()
|
||||
.body(ResponseBody.create(body.contentType(), bodyString))
|
||||
@@ -28,4 +28,4 @@ fun Response.interceptAsHtml(block: (Document) -> Unit): Response {
|
||||
return rebuiltResponse
|
||||
}
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package exh.util
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmModel
|
||||
import io.realm.log.RealmLog
|
||||
import java.util.*
|
||||
import java.util.UUID
|
||||
|
||||
inline fun <T> realmTrans(block: (Realm) -> T): T {
|
||||
return defRealm {
|
||||
@@ -25,7 +25,7 @@ inline fun <T> Realm.trans(block: () -> T): T {
|
||||
val res = block()
|
||||
commitTransaction()
|
||||
return res
|
||||
} catch(t: Throwable) {
|
||||
} catch (t: Throwable) {
|
||||
if (isInTransaction) {
|
||||
cancelTransaction()
|
||||
} else {
|
||||
@@ -34,7 +34,7 @@ inline fun <T> Realm.trans(block: () -> T): T {
|
||||
|
||||
throw t
|
||||
} finally {
|
||||
//Just in case
|
||||
// Just in case
|
||||
if (isInTransaction) {
|
||||
cancelTransaction()
|
||||
}
|
||||
@@ -49,9 +49,8 @@ inline fun <T> Realm.useTrans(block: (Realm) -> T): T {
|
||||
}
|
||||
}
|
||||
|
||||
fun <T : RealmModel> Realm.createUUIDObj(clazz: Class<T>)
|
||||
= createObject(clazz, UUID.randomUUID().toString())!!
|
||||
|
||||
inline fun <reified T : RealmModel> Realm.createUUIDObj()
|
||||
= createUUIDObj(T::class.java)
|
||||
fun <T : RealmModel> Realm.createUUIDObj(clazz: Class<T>) =
|
||||
createObject(clazz, UUID.randomUUID().toString())!!
|
||||
|
||||
inline fun <reified T : RealmModel> Realm.createUUIDObj() =
|
||||
createUUIDObj(T::class.java)
|
||||
|
||||
@@ -2,10 +2,14 @@ package exh.util
|
||||
|
||||
import com.pushtorefresh.storio.operations.PreparedOperation
|
||||
import com.pushtorefresh.storio.sqlite.operations.get.PreparedGetObject
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import rx.*
|
||||
import rx.subjects.ReplaySubject
|
||||
import kotlin.coroutines.resumeWithException
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import rx.Completable
|
||||
import rx.Observable
|
||||
import rx.Scheduler
|
||||
import rx.Single
|
||||
import rx.Subscription
|
||||
import rx.subjects.ReplaySubject
|
||||
|
||||
/**
|
||||
* Transform a cold single to a hot single
|
||||
|
||||
@@ -18,7 +18,7 @@ fun UrlImportableSource.urlImportFetchSearchManga(query: String, fail: () -> Obs
|
||||
query.startsWith("http://") || query.startsWith("https://") -> {
|
||||
Observable.fromCallable {
|
||||
val res = galleryAdder.addGallery(query, false, this)
|
||||
MangasPage((if(res is GalleryAddEvent.Success)
|
||||
MangasPage((if (res is GalleryAddEvent.Success)
|
||||
listOf(res.manga)
|
||||
else
|
||||
emptyList()), false)
|
||||
|
||||
@@ -3,7 +3,7 @@ package exh.util
|
||||
import android.util.SparseArray
|
||||
import java.util.AbstractMap
|
||||
|
||||
class SparseArrayKeyCollection(val sparseArray: SparseArray<out Any?>, var reverse: Boolean = false): AbstractCollection<Int>() {
|
||||
class SparseArrayKeyCollection(val sparseArray: SparseArray<out Any?>, var reverse: Boolean = false) : AbstractCollection<Int>() {
|
||||
override val size get() = sparseArray.size()
|
||||
|
||||
override fun iterator() = object : Iterator<Int> {
|
||||
@@ -19,13 +19,13 @@ class SparseArrayKeyCollection(val sparseArray: SparseArray<out Any?>, var rever
|
||||
*/
|
||||
override fun next(): Int {
|
||||
var idx = index++
|
||||
if(reverse) idx = sparseArray.size() - 1 - idx
|
||||
if (reverse) idx = sparseArray.size() - 1 - idx
|
||||
return sparseArray.keyAt(idx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SparseArrayValueCollection<E>(val sparseArray: SparseArray<E>, var reverse: Boolean = false): AbstractCollection<E>() {
|
||||
class SparseArrayValueCollection<E>(val sparseArray: SparseArray<E>, var reverse: Boolean = false) : AbstractCollection<E>() {
|
||||
override val size get() = sparseArray.size()
|
||||
|
||||
override fun iterator() = object : Iterator<E> {
|
||||
@@ -41,13 +41,13 @@ class SparseArrayValueCollection<E>(val sparseArray: SparseArray<E>, var reverse
|
||||
*/
|
||||
override fun next(): E {
|
||||
var idx = index++
|
||||
if(reverse) idx = sparseArray.size() - 1 - idx
|
||||
if (reverse) idx = sparseArray.size() - 1 - idx
|
||||
return sparseArray.valueAt(idx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SparseArrayCollection<E>(val sparseArray: SparseArray<E>, var reverse: Boolean = false): AbstractCollection<Map.Entry<Int, E>>() {
|
||||
class SparseArrayCollection<E>(val sparseArray: SparseArray<E>, var reverse: Boolean = false) : AbstractCollection<Map.Entry<Int, E>>() {
|
||||
override val size get() = sparseArray.size()
|
||||
|
||||
override fun iterator() = object : Iterator<Map.Entry<Int, E>> {
|
||||
@@ -63,7 +63,7 @@ class SparseArrayCollection<E>(val sparseArray: SparseArray<E>, var reverse: Boo
|
||||
*/
|
||||
override fun next(): Map.Entry<Int, E> {
|
||||
var idx = index++
|
||||
if(reverse) idx = sparseArray.size() - 1 - idx
|
||||
if (reverse) idx = sparseArray.size() - 1 - idx
|
||||
return AbstractMap.SimpleImmutableEntry(
|
||||
sparseArray.keyAt(idx),
|
||||
sparseArray.valueAt(idx)
|
||||
|
||||
@@ -7,4 +7,4 @@ import android.net.Uri
|
||||
*/
|
||||
interface UriFilter {
|
||||
fun addToUri(builder: Uri.Builder)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import eu.kanade.tachiyomi.source.model.Filter
|
||||
open class UriGroup<V>(name: String, state: List<V>) : Filter.Group<V>(name, state), UriFilter {
|
||||
override fun addToUri(builder: Uri.Builder) {
|
||||
state.forEach {
|
||||
if(it is UriFilter) it.addToUri(builder)
|
||||
if (it is UriFilter) it.addToUri(builder)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,4 +5,4 @@ import android.content.Context
|
||||
fun dpToPx(context: Context, dp: Int): Int {
|
||||
val scale = context.resources.displayMetrics.density
|
||||
return (dp * scale + 0.5f).toInt()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user