Minor cleanup

This commit is contained in:
arkon 2022-11-10 22:23:34 -05:00
parent 08d5633d81
commit 811931ccc0
19 changed files with 120 additions and 146 deletions

View file

@ -1,5 +1,7 @@
package eu.kanade.core.util package eu.kanade.core.util
import java.util.concurrent.ConcurrentHashMap
fun <T : R, R : Any> List<T>.insertSeparators( fun <T : R, R : Any> List<T>.insertSeparators(
generator: (T?, T?) -> R?, generator: (T?, T?) -> R?,
): List<R> { ): List<R> {
@ -14,3 +16,12 @@ fun <T : R, R : Any> List<T>.insertSeparators(
} }
return newList return newList
} }
/**
* Returns a new map containing only the key entries of [transform] that are not null.
*/
inline fun <K, V, R> Map<out K, V>.mapNotNullKeys(transform: (Map.Entry<K?, V>) -> R?): ConcurrentHashMap<R, V> {
val mutableMap = ConcurrentHashMap<R, V>()
forEach { element -> transform(element)?.let { mutableMap[it] = element.value } }
return mutableMap
}

View file

@ -13,7 +13,7 @@ private const val listOfStringsSeparator = ", "
val listOfStringsAdapter = object : ColumnAdapter<List<String>, String> { val listOfStringsAdapter = object : ColumnAdapter<List<String>, String> {
override fun decode(databaseValue: String) = override fun decode(databaseValue: String) =
if (databaseValue.isEmpty()) { if (databaseValue.isEmpty()) {
listOf() emptyList()
} else { } else {
databaseValue.split(listOfStringsSeparator) databaseValue.split(listOfStringsSeparator)
} }

View file

@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.data.download
import android.content.Context import android.content.Context
import androidx.core.net.toUri import androidx.core.net.toUri
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import eu.kanade.core.util.mapNotNullKeys
import eu.kanade.domain.download.service.DownloadPreferences import eu.kanade.domain.download.service.DownloadPreferences
import eu.kanade.domain.manga.model.Manga import eu.kanade.domain.manga.model.Manga
import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Chapter
@ -68,9 +69,7 @@ class DownloadCache(
downloadPreferences.downloadsDirectory().changes() downloadPreferences.downloadsDirectory().changes()
.onEach { .onEach {
rootDownloadsDir = RootDirectory(getDirectoryFromPreference()) rootDownloadsDir = RootDirectory(getDirectoryFromPreference())
invalidateCache()
// Invalidate cache
lastRenew = 0L
} }
.launchIn(scope) .launchIn(scope)
} }
@ -225,6 +224,10 @@ class DownloadCache(
notifyChanges() notifyChanges()
} }
fun invalidateCache() {
lastRenew = 0L
}
/** /**
* Returns the downloads directory from the user's preferences. * Returns the downloads directory from the user's preferences.
*/ */
@ -233,10 +236,6 @@ class DownloadCache(
return UniFile.fromUri(context, dir.toUri()) return UniFile.fromUri(context, dir.toUri())
} }
fun invalidateCache() {
lastRenew = 0L
}
/** /**
* Renews the downloads cache. * Renews the downloads cache.
*/ */
@ -315,15 +314,6 @@ class DownloadCache(
_changes.send(Unit) _changes.send(Unit)
} }
} }
/**
* Returns a new map containing only the key entries of [transform] that are not null.
*/
private inline fun <K, V, R> Map<out K, V>.mapNotNullKeys(transform: (Map.Entry<K?, V>) -> R?): ConcurrentHashMap<R, V> {
val mutableMap = ConcurrentHashMap<R, V>()
forEach { element -> transform(element)?.let { mutableMap[it] = element.value } }
return mutableMap
}
} }
/** /**

View file

@ -4,6 +4,7 @@ import eu.kanade.domain.track.service.TrackPreferences
import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.data.track.model.TrackSearch import eu.kanade.tachiyomi.data.track.model.TrackSearch
import kotlinx.serialization.Serializable
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Locale import java.util.Locale
@ -72,6 +73,16 @@ data class ALUserManga(
} }
} }
@Serializable
data class OAuth(
val access_token: String,
val token_type: String,
val expires: Long,
val expires_in: Long,
)
fun OAuth.isExpired() = System.currentTimeMillis() > expires
fun Track.toAnilistStatus() = when (status) { fun Track.toAnilistStatus() = when (status) {
Anilist.READING -> "CURRENT" Anilist.READING -> "CURRENT"
Anilist.COMPLETED -> "COMPLETED" Anilist.COMPLETED -> "COMPLETED"

View file

@ -1,14 +0,0 @@
package eu.kanade.tachiyomi.data.track.anilist
import kotlinx.serialization.Serializable
@Serializable
data class OAuth(
val access_token: String,
val token_type: String,
val expires: Long,
val expires_in: Long,
) {
fun isExpired() = System.currentTimeMillis() > expires
}

View file

@ -1,10 +0,0 @@
package eu.kanade.tachiyomi.data.track.bangumi
import kotlinx.serialization.Serializable
@Serializable
data class Avatar(
val large: String? = "",
val medium: String? = "",
val small: String? = "",
)

View file

@ -1,6 +1,58 @@
package eu.kanade.tachiyomi.data.track.bangumi package eu.kanade.tachiyomi.data.track.bangumi
import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.database.models.Track
import kotlinx.serialization.Serializable
@Serializable
data class Avatar(
val large: String? = "",
val medium: String? = "",
val small: String? = "",
)
@Serializable
data class Collection(
val `private`: Int? = 0,
val comment: String? = "",
val ep_status: Int? = 0,
val lasttouch: Int? = 0,
val rating: Float? = 0f,
val status: Status? = Status(),
val tag: List<String?>? = emptyList(),
val user: User? = User(),
val vol_status: Int? = 0,
)
@Serializable
data class Status(
val id: Int? = 0,
val name: String? = "",
val type: String? = "",
)
@Serializable
data class User(
val avatar: Avatar? = Avatar(),
val id: Int? = 0,
val nickname: String? = "",
val sign: String? = "",
val url: String? = "",
val usergroup: Int? = 0,
val username: String? = "",
)
@Serializable
data class OAuth(
val access_token: String,
val token_type: String,
val created_at: Long = System.currentTimeMillis() / 1000,
val expires_in: Long,
val refresh_token: String?,
val user_id: Long?,
)
// Access token refresh before expired
fun OAuth.isExpired() = (System.currentTimeMillis() / 1000) > (created_at + expires_in - 3600)
fun Track.toBangumiStatus() = when (status) { fun Track.toBangumiStatus() = when (status) {
Bangumi.READING -> "do" Bangumi.READING -> "do"

View file

@ -1,16 +0,0 @@
package eu.kanade.tachiyomi.data.track.bangumi
import kotlinx.serialization.Serializable
@Serializable
data class Collection(
val `private`: Int? = 0,
val comment: String? = "",
val ep_status: Int? = 0,
val lasttouch: Int? = 0,
val rating: Float? = 0f,
val status: Status? = Status(),
val tag: List<String?>? = listOf(),
val user: User? = User(),
val vol_status: Int? = 0,
)

View file

@ -1,17 +0,0 @@
package eu.kanade.tachiyomi.data.track.bangumi
import kotlinx.serialization.Serializable
@Serializable
data class OAuth(
val access_token: String,
val token_type: String,
val created_at: Long = System.currentTimeMillis() / 1000,
val expires_in: Long,
val refresh_token: String?,
val user_id: Long?,
) {
// Access token refresh before expired
fun isExpired() = (System.currentTimeMillis() / 1000) > (created_at + expires_in - 3600)
}

View file

@ -1,10 +0,0 @@
package eu.kanade.tachiyomi.data.track.bangumi
import kotlinx.serialization.Serializable
@Serializable
data class Status(
val id: Int? = 0,
val name: String? = "",
val type: String? = "",
)

View file

@ -1,14 +0,0 @@
package eu.kanade.tachiyomi.data.track.bangumi
import kotlinx.serialization.Serializable
@Serializable
data class User(
val avatar: Avatar? = Avatar(),
val id: Int? = 0,
val nickname: String? = "",
val sign: String? = "",
val url: String? = "",
val usergroup: Int? = 0,
val username: String? = "",
)

View file

@ -4,6 +4,7 @@ import androidx.annotation.CallSuper
import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.data.track.model.TrackSearch import eu.kanade.tachiyomi.data.track.model.TrackSearch
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.contentOrNull import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.int import kotlinx.serialization.json.int
@ -93,6 +94,17 @@ class KitsuLibManga(obj: JsonObject, manga: JsonObject) {
} }
} }
@Serializable
data class OAuth(
val access_token: String,
val token_type: String,
val created_at: Long,
val expires_in: Long,
val refresh_token: String?,
)
fun OAuth.isExpired() = (System.currentTimeMillis() / 1000) > (created_at + expires_in - 3600)
fun Track.toKitsuStatus() = when (status) { fun Track.toKitsuStatus() = when (status) {
Kitsu.READING -> "current" Kitsu.READING -> "current"
Kitsu.COMPLETED -> "completed" Kitsu.COMPLETED -> "completed"

View file

@ -1,15 +0,0 @@
package eu.kanade.tachiyomi.data.track.kitsu
import kotlinx.serialization.Serializable
@Serializable
data class OAuth(
val access_token: String,
val token_type: String,
val created_at: Long,
val expires_in: Long,
val refresh_token: String?,
) {
fun isExpired() = (System.currentTimeMillis() / 1000) > (created_at + expires_in - 3600)
}

View file

@ -1,6 +1,18 @@
package eu.kanade.tachiyomi.data.track.myanimelist package eu.kanade.tachiyomi.data.track.myanimelist
import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.database.models.Track
import kotlinx.serialization.Serializable
@Serializable
data class OAuth(
val refresh_token: String,
val access_token: String,
val token_type: String,
val created_at: Long = System.currentTimeMillis(),
val expires_in: Long,
)
fun OAuth.isExpired() = System.currentTimeMillis() > created_at + (expires_in * 1000)
fun Track.toMyAnimeListStatus() = when (status) { fun Track.toMyAnimeListStatus() = when (status) {
MyAnimeList.READING -> "reading" MyAnimeList.READING -> "reading"

View file

@ -1,15 +0,0 @@
package eu.kanade.tachiyomi.data.track.myanimelist
import kotlinx.serialization.Serializable
@Serializable
data class OAuth(
val refresh_token: String,
val access_token: String,
val token_type: String,
val created_at: Long = System.currentTimeMillis(),
val expires_in: Long,
) {
fun isExpired() = System.currentTimeMillis() > created_at + (expires_in * 1000)
}

View file

@ -1,16 +0,0 @@
package eu.kanade.tachiyomi.data.track.shikimori
import kotlinx.serialization.Serializable
@Serializable
data class OAuth(
val access_token: String,
val token_type: String,
val created_at: Long,
val expires_in: Long,
val refresh_token: String?,
) {
// Access token lives 1 day
fun isExpired() = (System.currentTimeMillis() / 1000) > (created_at + expires_in - 3600)
}

View file

@ -1,6 +1,19 @@
package eu.kanade.tachiyomi.data.track.shikimori package eu.kanade.tachiyomi.data.track.shikimori
import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.database.models.Track
import kotlinx.serialization.Serializable
@Serializable
data class OAuth(
val access_token: String,
val token_type: String,
val created_at: Long,
val expires_in: Long,
val refresh_token: String?,
)
// Access token lives 1 day
fun OAuth.isExpired() = (System.currentTimeMillis() / 1000) > (created_at + expires_in - 3600)
fun Track.toShikimoriStatus() = when (status) { fun Track.toShikimoriStatus() = when (status) {
Shikimori.READING -> "watching" Shikimori.READING -> "watching"

View file

@ -73,7 +73,7 @@ class ExtensionManager(
private val _availableExtensionsFlow = MutableStateFlow(emptyList<Extension.Available>()) private val _availableExtensionsFlow = MutableStateFlow(emptyList<Extension.Available>())
val availableExtensionsFlow = _availableExtensionsFlow.asStateFlow() val availableExtensionsFlow = _availableExtensionsFlow.asStateFlow()
private var availableExtensionsSourcesData: Map<Long, SourceData> = mapOf() private var availableExtensionsSourcesData: Map<Long, SourceData> = emptyMap()
private fun setupAvailableExtensionsSourcesDataMap(extensions: List<Extension.Available>) { private fun setupAvailableExtensionsSourcesDataMap(extensions: List<Extension.Available>) {
if (extensions.isEmpty()) return if (extensions.isEmpty()) return

View file

@ -475,7 +475,7 @@ class DownloadController :
?.filterIsInstance<DownloadItem>() ?.filterIsInstance<DownloadItem>()
?.map(DownloadItem::download) ?.map(DownloadItem::download)
?.partition { item.download.manga.id == it.manga.id } ?.partition { item.download.manga.id == it.manga.id }
?: Pair(listOf(), listOf()) ?: Pair(emptyList(), emptyList())
presenter.reorder(selectedSeries + otherSeries) presenter.reorder(selectedSeries + otherSeries)
} }
R.id.cancel_download -> { R.id.cancel_download -> {