From 811931ccc0f21b0fd88c56b8567358982c48c31e Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 10 Nov 2022 22:23:34 -0500 Subject: [PATCH] Minor cleanup --- .../util/{ListUtils.kt => CollectionUtils.kt} | 11 ++++ .../java/eu/kanade/data/DatabaseAdapter.kt | 2 +- .../tachiyomi/data/download/DownloadCache.kt | 22 +++----- .../data/track/anilist/AnilistModels.kt | 11 ++++ .../tachiyomi/data/track/anilist/OAuth.kt | 14 ----- .../tachiyomi/data/track/bangumi/Avatar.kt | 10 ---- .../data/track/bangumi/BangumiModels.kt | 52 +++++++++++++++++++ .../data/track/bangumi/Collection.kt | 16 ------ .../tachiyomi/data/track/bangumi/OAuth.kt | 17 ------ .../tachiyomi/data/track/bangumi/Status.kt | 10 ---- .../tachiyomi/data/track/bangumi/User.kt | 14 ----- .../tachiyomi/data/track/kitsu/KitsuModels.kt | 12 +++++ .../tachiyomi/data/track/kitsu/OAuth.kt | 15 ------ .../track/myanimelist/MyAnimeListModels.kt | 12 +++++ .../tachiyomi/data/track/myanimelist/OAuth.kt | 15 ------ .../tachiyomi/data/track/shikimori/OAuth.kt | 16 ------ .../data/track/shikimori/ShikimoriModels.kt | 13 +++++ .../tachiyomi/extension/ExtensionManager.kt | 2 +- .../ui/download/DownloadController.kt | 2 +- 19 files changed, 120 insertions(+), 146 deletions(-) rename app/src/main/java/eu/kanade/core/util/{ListUtils.kt => CollectionUtils.kt} (52%) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/OAuth.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Avatar.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Collection.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/OAuth.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Status.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/User.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/OAuth.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/OAuth.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/OAuth.kt diff --git a/app/src/main/java/eu/kanade/core/util/ListUtils.kt b/app/src/main/java/eu/kanade/core/util/CollectionUtils.kt similarity index 52% rename from app/src/main/java/eu/kanade/core/util/ListUtils.kt rename to app/src/main/java/eu/kanade/core/util/CollectionUtils.kt index 702f3d0f06..636f0a4f5b 100644 --- a/app/src/main/java/eu/kanade/core/util/ListUtils.kt +++ b/app/src/main/java/eu/kanade/core/util/CollectionUtils.kt @@ -1,5 +1,7 @@ package eu.kanade.core.util +import java.util.concurrent.ConcurrentHashMap + fun List.insertSeparators( generator: (T?, T?) -> R?, ): List { @@ -14,3 +16,12 @@ fun List.insertSeparators( } return newList } + +/** + * Returns a new map containing only the key entries of [transform] that are not null. + */ +inline fun Map.mapNotNullKeys(transform: (Map.Entry) -> R?): ConcurrentHashMap { + val mutableMap = ConcurrentHashMap() + forEach { element -> transform(element)?.let { mutableMap[it] = element.value } } + return mutableMap +} diff --git a/app/src/main/java/eu/kanade/data/DatabaseAdapter.kt b/app/src/main/java/eu/kanade/data/DatabaseAdapter.kt index 48c8974961..bf6e205f4c 100644 --- a/app/src/main/java/eu/kanade/data/DatabaseAdapter.kt +++ b/app/src/main/java/eu/kanade/data/DatabaseAdapter.kt @@ -13,7 +13,7 @@ private const val listOfStringsSeparator = ", " val listOfStringsAdapter = object : ColumnAdapter, String> { override fun decode(databaseValue: String) = if (databaseValue.isEmpty()) { - listOf() + emptyList() } else { databaseValue.split(listOfStringsSeparator) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt index 5fa3994ec1..e86b6f0491 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.data.download import android.content.Context import androidx.core.net.toUri import com.hippo.unifile.UniFile +import eu.kanade.core.util.mapNotNullKeys import eu.kanade.domain.download.service.DownloadPreferences import eu.kanade.domain.manga.model.Manga import eu.kanade.tachiyomi.data.database.models.Chapter @@ -68,9 +69,7 @@ class DownloadCache( downloadPreferences.downloadsDirectory().changes() .onEach { rootDownloadsDir = RootDirectory(getDirectoryFromPreference()) - - // Invalidate cache - lastRenew = 0L + invalidateCache() } .launchIn(scope) } @@ -225,6 +224,10 @@ class DownloadCache( notifyChanges() } + fun invalidateCache() { + lastRenew = 0L + } + /** * Returns the downloads directory from the user's preferences. */ @@ -233,10 +236,6 @@ class DownloadCache( return UniFile.fromUri(context, dir.toUri()) } - fun invalidateCache() { - lastRenew = 0L - } - /** * Renews the downloads cache. */ @@ -315,15 +314,6 @@ class DownloadCache( _changes.send(Unit) } } - - /** - * Returns a new map containing only the key entries of [transform] that are not null. - */ - private inline fun Map.mapNotNullKeys(transform: (Map.Entry) -> R?): ConcurrentHashMap { - val mutableMap = ConcurrentHashMap() - forEach { element -> transform(element)?.let { mutableMap[it] = element.value } } - return mutableMap - } } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistModels.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistModels.kt index 95c1c876c4..0e39d680f7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistModels.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistModels.kt @@ -4,6 +4,7 @@ import eu.kanade.domain.track.service.TrackPreferences import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.model.TrackSearch +import kotlinx.serialization.Serializable import uy.kohesive.injekt.injectLazy import java.text.SimpleDateFormat 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) { Anilist.READING -> "CURRENT" Anilist.COMPLETED -> "COMPLETED" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/OAuth.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/OAuth.kt deleted file mode 100644 index 49067070f4..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/OAuth.kt +++ /dev/null @@ -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 -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Avatar.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Avatar.kt deleted file mode 100644 index 1cea3438dc..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Avatar.kt +++ /dev/null @@ -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? = "", -) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiModels.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiModels.kt index e5811d707c..92fa49f245 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiModels.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/BangumiModels.kt @@ -1,6 +1,58 @@ package eu.kanade.tachiyomi.data.track.bangumi 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? = 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) { Bangumi.READING -> "do" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Collection.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Collection.kt deleted file mode 100644 index 301167366f..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Collection.kt +++ /dev/null @@ -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? = listOf(), - val user: User? = User(), - val vol_status: Int? = 0, -) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/OAuth.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/OAuth.kt deleted file mode 100644 index 25776d3416..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/OAuth.kt +++ /dev/null @@ -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) -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Status.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Status.kt deleted file mode 100644 index f69bac3f5e..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Status.kt +++ /dev/null @@ -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? = "", -) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/User.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/User.kt deleted file mode 100644 index 514be8fb0a..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/User.kt +++ /dev/null @@ -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? = "", -) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/KitsuModels.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/KitsuModels.kt index 453e65ce7a..915c7ae8b9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/KitsuModels.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/KitsuModels.kt @@ -4,6 +4,7 @@ import androidx.annotation.CallSuper import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.model.TrackSearch +import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.contentOrNull 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) { Kitsu.READING -> "current" Kitsu.COMPLETED -> "completed" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/OAuth.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/OAuth.kt deleted file mode 100644 index 33dade31cc..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/OAuth.kt +++ /dev/null @@ -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) -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListModels.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListModels.kt index ea8366716b..70401313cd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListModels.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListModels.kt @@ -1,6 +1,18 @@ package eu.kanade.tachiyomi.data.track.myanimelist 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) { MyAnimeList.READING -> "reading" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/OAuth.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/OAuth.kt deleted file mode 100644 index 17cd1c3efa..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/OAuth.kt +++ /dev/null @@ -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) -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/OAuth.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/OAuth.kt deleted file mode 100644 index 16b01db2f1..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/OAuth.kt +++ /dev/null @@ -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) -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriModels.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriModels.kt index fe59f6e80e..5a28eceb40 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriModels.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/ShikimoriModels.kt @@ -1,6 +1,19 @@ package eu.kanade.tachiyomi.data.track.shikimori 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) { Shikimori.READING -> "watching" diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt index 4d216ca7fb..ccbbcacd94 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt @@ -73,7 +73,7 @@ class ExtensionManager( private val _availableExtensionsFlow = MutableStateFlow(emptyList()) val availableExtensionsFlow = _availableExtensionsFlow.asStateFlow() - private var availableExtensionsSourcesData: Map = mapOf() + private var availableExtensionsSourcesData: Map = emptyMap() private fun setupAvailableExtensionsSourcesDataMap(extensions: List) { if (extensions.isEmpty()) return diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt index ee003037c7..7964a1de51 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/download/DownloadController.kt @@ -475,7 +475,7 @@ class DownloadController : ?.filterIsInstance() ?.map(DownloadItem::download) ?.partition { item.download.manga.id == it.manga.id } - ?: Pair(listOf(), listOf()) + ?: Pair(emptyList(), emptyList()) presenter.reorder(selectedSeries + otherSeries) } R.id.cancel_download -> {