WIP
This commit is contained in:
@@ -83,7 +83,7 @@ class DownloadManager constructor(context: Context) : ContextWrapper(context), D
|
||||
|
||||
@Synchronized
|
||||
fun download(source: String, itemID: String) = CoroutineScope(Dispatchers.IO).launch {
|
||||
val source: AnySource by instance(tag = source)
|
||||
val source: AnySource by source(source)
|
||||
val info = async { source.info(itemID) }
|
||||
val images = async { source.images(itemID) }
|
||||
|
||||
|
||||
@@ -23,7 +23,9 @@ import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.builtins.MapSerializer
|
||||
import kotlinx.serialization.builtins.SetSerializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.Json.Default.decodeFromString
|
||||
import kotlinx.serialization.serializer
|
||||
import java.io.File
|
||||
|
||||
@@ -44,7 +46,7 @@ class SavedSet <T: Any> (private val file: File, any: T, private val set: Mutabl
|
||||
fun load() {
|
||||
set.clear()
|
||||
kotlin.runCatching {
|
||||
Json.decodeFromString(serializer, file.readText())
|
||||
decodeFromString(serializer, file.readText())
|
||||
}.onSuccess {
|
||||
set.addAll(it)
|
||||
}
|
||||
@@ -111,7 +113,7 @@ class SavedMap <K: Any, V: Any> (private val file: File, anyKey: K, anyValue: V,
|
||||
fun load() {
|
||||
map.clear()
|
||||
kotlin.runCatching {
|
||||
Json.decodeFromString(serializer, file.readText())
|
||||
decodeFromString(serializer, file.readText())
|
||||
}.onSuccess {
|
||||
map.putAll(it)
|
||||
}
|
||||
@@ -167,4 +169,79 @@ class SavedMap <K: Any, V: Any> (private val file: File, anyKey: K, anyValue: V,
|
||||
save()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class SavedSourceSet(private val file: File) {
|
||||
|
||||
private val _map = mutableMapOf<String, MutableSet<String>>()
|
||||
val map: Map<String, Set<String>> = _map
|
||||
|
||||
private val serializer = MapSerializer(String.serializer(), SetSerializer(String.serializer()))
|
||||
|
||||
@Synchronized
|
||||
fun load() {
|
||||
_map.clear()
|
||||
kotlin.runCatching {
|
||||
decodeFromString(serializer, file.readText())
|
||||
}.onSuccess {
|
||||
it.forEach { (k, v) ->
|
||||
_map[k] = v.toMutableSet()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun save() {
|
||||
file.parentFile?.mkdirs()
|
||||
if (!file.exists())
|
||||
file.createNewFile()
|
||||
|
||||
file.writeText(Json.encodeToString(serializer, _map))
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun add(source: String, value: String) {
|
||||
load()
|
||||
|
||||
_map[source]?.remove(value)
|
||||
|
||||
if (!_map.containsKey(source))
|
||||
_map[source] = mutableSetOf()
|
||||
else
|
||||
_map[source]!!.add(value)
|
||||
|
||||
save()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun addAll(from: Map<String, Set<String>>) {
|
||||
load()
|
||||
|
||||
for (source in from.keys) {
|
||||
if (_map.containsKey(source)) {
|
||||
_map[source]!!.removeAll(from[source]!!)
|
||||
_map[source]!!.addAll(from[source]!!)
|
||||
} else {
|
||||
_map[source] = from[source]!!.toMutableSet()
|
||||
}
|
||||
}
|
||||
|
||||
save()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun remove(source: String, value: String): Boolean {
|
||||
load()
|
||||
|
||||
return (_map[source]?.remove(value) ?: false).also {
|
||||
save()
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun clear() {
|
||||
_map.clear()
|
||||
save()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,14 +20,17 @@ package xyz.quaver.pupil.util
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import kotlinx.serialization.json.*
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import org.kodein.di.*
|
||||
import xyz.quaver.hitomi.GalleryInfo
|
||||
import xyz.quaver.hitomi.getReferer
|
||||
import xyz.quaver.hitomi.imageUrlFromImage
|
||||
import xyz.quaver.pupil.sources.ItemInfo
|
||||
import xyz.quaver.pupil.sources.SourceEntries
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.util.*
|
||||
@@ -128,4 +131,15 @@ fun InputStream.copyTo(out: OutputStream, onCopy: (totalBytesCopied: Long, bytes
|
||||
bytes = read(buffer)
|
||||
}
|
||||
return bytesCopied
|
||||
}
|
||||
|
||||
fun DIAware.source(source: String) = lazy { direct.source(source) }
|
||||
fun DirectDIAware.source(source: String) = instance<SourceEntries>().toMap()[source]!!
|
||||
|
||||
fun View.hide() {
|
||||
visibility = View.INVISIBLE
|
||||
}
|
||||
|
||||
fun View.show() {
|
||||
visibility = View.VISIBLE
|
||||
}
|
||||
@@ -18,7 +18,6 @@
|
||||
|
||||
package xyz.quaver.pupil.util
|
||||
|
||||
import android.content.Context
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
|
||||
@@ -18,37 +18,33 @@
|
||||
|
||||
package xyz.quaver.pupil.util
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.DownloadManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.net.Uri
|
||||
import android.util.Base64
|
||||
import android.webkit.URLUtil
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.google.firebase.crashlytics.FirebaseCrashlytics
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.*
|
||||
import okhttp3.Call
|
||||
import okhttp3.Callback
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
import org.kodein.di.DI
|
||||
import org.kodein.di.DIAware
|
||||
import org.kodein.di.android.di
|
||||
import org.kodein.di.instance
|
||||
import ru.noties.markwon.Markwon
|
||||
import xyz.quaver.pupil.BuildConfig
|
||||
import xyz.quaver.pupil.R
|
||||
import xyz.quaver.pupil.client
|
||||
import xyz.quaver.pupil.favorites
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.net.URL
|
||||
@@ -184,7 +180,7 @@ fun checkUpdate(context: Context, force: Boolean = false) {
|
||||
}
|
||||
}
|
||||
|
||||
fun restore(url: String, onFailure: ((Throwable) -> Unit)? = null, onSuccess: ((List<String>) -> Unit)? = null) {
|
||||
fun restore(context: Context, url: String, onFailure: ((Throwable) -> Unit)? = null, onSuccess: ((Set<String>) -> Unit)? = null) {
|
||||
if (!URLUtil.isValidUrl(url)) {
|
||||
onFailure?.invoke(IllegalArgumentException())
|
||||
return
|
||||
@@ -201,9 +197,10 @@ fun restore(url: String, onFailure: ((Throwable) -> Unit)? = null, onSuccess: ((
|
||||
}
|
||||
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
val favorites = object: DIAware { override val di by di(context); val favorites: SavedSourceSet by instance(tag = "favorites") }
|
||||
kotlin.runCatching {
|
||||
Json.decodeFromString<List<String>>(response.also { if (it.code() != 200) throw IOException() }.body().use { it?.string() } ?: "[]").let {
|
||||
favorites.addAll(it)
|
||||
Json.decodeFromString<Set<String>>(response.also { if (it.code() != 200) throw IOException() }.body().use { it?.string() } ?: "[]").let {
|
||||
favorites.favorites.addAll(mapOf("hitomi.la" to it))
|
||||
onSuccess?.invoke(it)
|
||||
}
|
||||
}.onFailure { onFailure?.invoke(it) }
|
||||
|
||||
Reference in New Issue
Block a user