From e568951396220b95447a025615a4b57b273f8864 Mon Sep 17 00:00:00 2001 From: Andreas Date: Wed, 21 Sep 2022 15:18:53 +0200 Subject: [PATCH] Split track preferences from PreferencesHelper (#8046) --- .../library/service/LibraryPreferences.kt | 4 +++ .../domain/track/service/TrackPreferences.kt | 26 +++++++++++++++++++ .../java/eu/kanade/tachiyomi/AppModule.kt | 4 +++ .../data/library/LibraryUpdateService.kt | 4 +-- .../data/preference/PreferencesHelper.kt | 21 --------------- .../tachiyomi/data/track/TrackService.kt | 10 ++++--- .../tachiyomi/data/track/anilist/Anilist.kt | 8 +++--- .../data/track/anilist/AnilistModels.kt | 4 +-- .../tachiyomi/data/track/bangumi/Bangumi.kt | 6 ++--- .../tachiyomi/data/track/kitsu/Kitsu.kt | 4 +-- .../data/track/mangaupdates/MangaUpdates.kt | 2 +- .../data/track/myanimelist/MyAnimeList.kt | 6 ++--- .../data/track/shikimori/Shikimori.kt | 6 ++--- .../tachiyomi/ui/reader/ReaderPresenter.kt | 4 ++- .../ui/setting/SettingsLibraryController.kt | 4 +-- .../ui/setting/SettingsTrackingController.kt | 4 ++- 16 files changed, 68 insertions(+), 49 deletions(-) create mode 100644 app/src/main/java/eu/kanade/domain/track/service/TrackPreferences.kt diff --git a/app/src/main/java/eu/kanade/domain/library/service/LibraryPreferences.kt b/app/src/main/java/eu/kanade/domain/library/service/LibraryPreferences.kt index 9d61a945dd..99498ee262 100644 --- a/app/src/main/java/eu/kanade/domain/library/service/LibraryPreferences.kt +++ b/app/src/main/java/eu/kanade/domain/library/service/LibraryPreferences.kt @@ -27,6 +27,10 @@ class LibraryPreferences( fun libraryUpdateDeviceRestriction() = preferenceStore.getStringSet("library_update_restriction", setOf(DEVICE_ONLY_ON_WIFI)) fun libraryUpdateMangaRestriction() = preferenceStore.getStringSet("library_update_manga_restriction", setOf(MANGA_HAS_UNREAD, MANGA_NON_COMPLETED, MANGA_NON_READ)) + fun autoUpdateMetadata() = preferenceStore.getBoolean("auto_update_metadata", false) + + fun autoUpdateTrackers() = preferenceStore.getBoolean("auto_update_trackers", false) + // region Filter fun filterDownloaded() = preferenceStore.getInt("pref_filter_library_downloaded", ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value) diff --git a/app/src/main/java/eu/kanade/domain/track/service/TrackPreferences.kt b/app/src/main/java/eu/kanade/domain/track/service/TrackPreferences.kt new file mode 100644 index 0000000000..867b8c3eb9 --- /dev/null +++ b/app/src/main/java/eu/kanade/domain/track/service/TrackPreferences.kt @@ -0,0 +1,26 @@ +package eu.kanade.domain.track.service + +import eu.kanade.tachiyomi.core.preference.PreferenceStore +import eu.kanade.tachiyomi.data.preference.PreferenceKeys +import eu.kanade.tachiyomi.data.track.TrackService +import eu.kanade.tachiyomi.data.track.anilist.Anilist + +class TrackPreferences( + private val preferenceStore: PreferenceStore, +) { + + fun trackUsername(sync: TrackService) = preferenceStore.getString(PreferenceKeys.trackUsername(sync.id), "") + + fun trackPassword(sync: TrackService) = preferenceStore.getString(PreferenceKeys.trackPassword(sync.id), "") + + fun setTrackCredentials(sync: TrackService, username: String, password: String) { + trackUsername(sync).set(username) + trackPassword(sync).set(password) + } + + fun trackToken(sync: TrackService) = preferenceStore.getString(PreferenceKeys.trackToken(sync.id), "") + + fun anilistScoreType() = preferenceStore.getString("anilist_score_type", Anilist.POINT_10) + + fun autoUpdateTrack() = preferenceStore.getBoolean("pref_auto_update_manga_sync_key", true) +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/AppModule.kt b/app/src/main/java/eu/kanade/tachiyomi/AppModule.kt index 8e96742633..77bc465d97 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/AppModule.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/AppModule.kt @@ -15,6 +15,7 @@ import eu.kanade.data.dateAdapter import eu.kanade.data.listOfStringsAdapter import eu.kanade.domain.library.service.LibraryPreferences import eu.kanade.domain.source.service.SourcePreferences +import eu.kanade.domain.track.service.TrackPreferences import eu.kanade.tachiyomi.core.preference.AndroidPreferenceStore import eu.kanade.tachiyomi.core.preference.PreferenceStore import eu.kanade.tachiyomi.core.security.SecurityPreferences @@ -155,6 +156,9 @@ class PreferenceModule(val application: Application) : InjektModule { addSingletonFactory { ReaderPreferences(get()) } + addSingletonFactory { + TrackPreferences(get()) + } addSingletonFactory { PreferencesHelper( context = application, diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index 0756c2f520..a55e3b7e8e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -382,7 +382,7 @@ class LibraryUpdateService( failedUpdates.add(mangaWithNotif to errorMessage) } - if (preferences.autoUpdateTrackers().get()) { + if (libraryPreferences.autoUpdateTrackers().get()) { updateTrackings(mangaWithNotif, loggedServices) } } @@ -432,7 +432,7 @@ class LibraryUpdateService( val source = sourceManager.getOrStub(manga.source) // Update manga metadata if needed - if (preferences.autoUpdateMetadata().get()) { + if (libraryPreferences.autoUpdateMetadata().get()) { val networkManga = source.getMangaDetails(manga.toSManga()) updateManga.awaitUpdateFromSource(manga, networkManga, manualFetch = false, coverCache) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 999dd586f3..653a1efe95 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -8,8 +8,6 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.core.preference.PreferenceStore import eu.kanade.tachiyomi.core.preference.getEnum import eu.kanade.tachiyomi.data.database.models.Manga -import eu.kanade.tachiyomi.data.track.TrackService -import eu.kanade.tachiyomi.data.track.anilist.Anilist import eu.kanade.tachiyomi.util.system.DeviceUtil import eu.kanade.tachiyomi.util.system.isDynamicColorAvailable import java.io.File @@ -41,10 +39,6 @@ class PreferencesHelper( fun sideNavIconAlignment() = preferenceStore.getInt("pref_side_nav_icon_alignment", 0) - fun autoUpdateMetadata() = preferenceStore.getBoolean("auto_update_metadata", false) - - fun autoUpdateTrackers() = preferenceStore.getBoolean("auto_update_trackers", false) - fun themeMode() = preferenceStore.getEnum( "pref_theme_mode_key", if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Values.ThemeMode.system } else { Values.ThemeMode.light }, @@ -59,19 +53,6 @@ class PreferencesHelper( fun lastVersionCode() = preferenceStore.getInt("last_version_code", 0) - fun trackUsername(sync: TrackService) = preferenceStore.getString(Keys.trackUsername(sync.id), "") - - fun trackPassword(sync: TrackService) = preferenceStore.getString(Keys.trackPassword(sync.id), "") - - fun setTrackCredentials(sync: TrackService, username: String, password: String) { - trackUsername(sync).set(username) - trackPassword(sync).set(password) - } - - fun trackToken(sync: TrackService) = preferenceStore.getString(Keys.trackToken(sync.id), "") - - fun anilistScoreType() = preferenceStore.getString("anilist_score_type", Anilist.POINT_10) - fun backupsDirectory() = preferenceStore.getString("backup_directory", defaultBackupDir.toString()) fun relativeTime() = preferenceStore.getInt("relative_time", 7) @@ -115,8 +96,6 @@ class PreferencesHelper( fun autoDownloadWhileReading() = preferenceStore.getInt("auto_download_while_reading", 0) - fun autoUpdateTrack() = preferenceStore.getBoolean("pref_auto_update_manga_sync_key", true) - fun migrateFlags() = preferenceStore.getInt("migrate_flags", Int.MAX_VALUE) fun filterChapterByRead() = preferenceStore.getInt("default_chapter_filter_by_read", DomainManga.SHOW_ALL.toInt()) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackService.kt index bc918ca02e..42d405d0f2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/TrackService.kt @@ -4,6 +4,7 @@ import androidx.annotation.CallSuper import androidx.annotation.ColorInt import androidx.annotation.DrawableRes import androidx.annotation.StringRes +import eu.kanade.domain.track.service.TrackPreferences import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.track.model.TrackSearch @@ -14,6 +15,7 @@ import uy.kohesive.injekt.injectLazy abstract class TrackService(val id: Long) { val preferences: PreferencesHelper by injectLazy() + val trackPreferences: TrackPreferences by injectLazy() val networkService: NetworkHelper by injectLazy() open val client: OkHttpClient @@ -62,18 +64,18 @@ abstract class TrackService(val id: Long) { @CallSuper open fun logout() { - preferences.setTrackCredentials(this, "", "") + trackPreferences.setTrackCredentials(this, "", "") } open val isLogged: Boolean get() = getUsername().isNotEmpty() && getPassword().isNotEmpty() - fun getUsername() = preferences.trackUsername(this).get() + fun getUsername() = trackPreferences.trackUsername(this).get() - fun getPassword() = preferences.trackPassword(this).get() + fun getPassword() = trackPreferences.trackPassword(this).get() fun saveCredentials(username: String, password: String) { - preferences.setTrackCredentials(this, username, password) + trackPreferences.setTrackCredentials(this, username, password) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/Anilist.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/Anilist.kt index 7e8c391015..f7ebb9a19e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/Anilist.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/Anilist.kt @@ -37,7 +37,7 @@ class Anilist(private val context: Context, id: Long) : TrackService(id) { override val supportsReadingDates: Boolean = true - private val scorePreference = preferences.anilistScoreType() + private val scorePreference = trackPreferences.anilistScoreType() init { // If the preference is an int from APIv1, logout user to force using APIv2 @@ -211,17 +211,17 @@ class Anilist(private val context: Context, id: Long) : TrackService(id) { override fun logout() { super.logout() - preferences.trackToken(this).delete() + trackPreferences.trackToken(this).delete() interceptor.setAuth(null) } fun saveOAuth(oAuth: OAuth?) { - preferences.trackToken(this).set(json.encodeToString(oAuth)) + trackPreferences.trackToken(this).set(json.encodeToString(oAuth)) } fun loadOAuth(): OAuth? { return try { - json.decodeFromString(preferences.trackToken(this).get()) + json.decodeFromString(trackPreferences.trackToken(this).get()) } catch (e: Exception) { null } 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 120cdc027b..95c1c876c4 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 @@ -1,7 +1,7 @@ package eu.kanade.tachiyomi.data.track.anilist +import eu.kanade.domain.track.service.TrackPreferences import eu.kanade.tachiyomi.data.database.models.Track -import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.model.TrackSearch import uy.kohesive.injekt.injectLazy @@ -82,7 +82,7 @@ fun Track.toAnilistStatus() = when (status) { else -> throw NotImplementedError("Unknown status: $status") } -private val preferences: PreferencesHelper by injectLazy() +private val preferences: TrackPreferences by injectLazy() fun Track.toAnilistScore(): String = when (preferences.anilistScoreType().get()) { // 10 point diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt index 59e6fd2412..b43cea74c1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/bangumi/Bangumi.kt @@ -124,12 +124,12 @@ class Bangumi(private val context: Context, id: Long) : TrackService(id) { } fun saveToken(oauth: OAuth?) { - preferences.trackToken(this).set(json.encodeToString(oauth)) + trackPreferences.trackToken(this).set(json.encodeToString(oauth)) } fun restoreToken(): OAuth? { return try { - json.decodeFromString(preferences.trackToken(this).get()) + json.decodeFromString(trackPreferences.trackToken(this).get()) } catch (e: Exception) { null } @@ -137,7 +137,7 @@ class Bangumi(private val context: Context, id: Long) : TrackService(id) { override fun logout() { super.logout() - preferences.trackToken(this).delete() + trackPreferences.trackToken(this).delete() interceptor.newAuth(null) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/Kitsu.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/Kitsu.kt index fc4f4e57f4..0799229e5e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/Kitsu.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/kitsu/Kitsu.kt @@ -141,12 +141,12 @@ class Kitsu(private val context: Context, id: Long) : TrackService(id) { } fun saveToken(oauth: OAuth?) { - preferences.trackToken(this).set(json.encodeToString(oauth)) + trackPreferences.trackToken(this).set(json.encodeToString(oauth)) } fun restoreToken(): OAuth? { return try { - json.decodeFromString(preferences.trackToken(this).get()) + json.decodeFromString(trackPreferences.trackToken(this).get()) } catch (e: Exception) { null } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/mangaupdates/MangaUpdates.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/mangaupdates/MangaUpdates.kt index f64b900881..17c6c0ae9f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/mangaupdates/MangaUpdates.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/mangaupdates/MangaUpdates.kt @@ -99,6 +99,6 @@ class MangaUpdates(private val context: Context, id: Long) : TrackService(id) { } fun restoreSession(): String? { - return preferences.trackPassword(this).get() + return trackPreferences.trackPassword(this).get() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt index 0db5d85466..d3e85a63f7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt @@ -147,17 +147,17 @@ class MyAnimeList(private val context: Context, id: Long) : TrackService(id) { override fun logout() { super.logout() - preferences.trackToken(this).delete() + trackPreferences.trackToken(this).delete() interceptor.setAuth(null) } fun saveOAuth(oAuth: OAuth?) { - preferences.trackToken(this).set(json.encodeToString(oAuth)) + trackPreferences.trackToken(this).set(json.encodeToString(oAuth)) } fun loadOAuth(): OAuth? { return try { - json.decodeFromString(preferences.trackToken(this).get()) + json.decodeFromString(trackPreferences.trackToken(this).get()) } catch (e: Exception) { null } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt index 8c5b757e5a..33c04e044e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/shikimori/Shikimori.kt @@ -130,12 +130,12 @@ class Shikimori(private val context: Context, id: Long) : TrackService(id) { } fun saveToken(oauth: OAuth?) { - preferences.trackToken(this).set(json.encodeToString(oauth)) + trackPreferences.trackToken(this).set(json.encodeToString(oauth)) } fun restoreToken(): OAuth? { return try { - json.decodeFromString(preferences.trackToken(this).get()) + json.decodeFromString(trackPreferences.trackToken(this).get()) } catch (e: Exception) { null } @@ -143,7 +143,7 @@ class Shikimori(private val context: Context, id: Long) : TrackService(id) { override fun logout() { super.logout() - preferences.trackToken(this).delete() + trackPreferences.trackToken(this).delete() interceptor.newAuth(null) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt index 6accaae288..231f2c0ee1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt @@ -18,6 +18,7 @@ import eu.kanade.domain.manga.model.toDbManga import eu.kanade.domain.track.interactor.GetTracks import eu.kanade.domain.track.interactor.InsertTrack import eu.kanade.domain.track.model.toDbTrack +import eu.kanade.domain.track.service.TrackPreferences import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.toDomainChapter import eu.kanade.tachiyomi.data.database.models.toDomainManga @@ -80,6 +81,7 @@ class ReaderPresenter( private val downloadManager: DownloadManager = Injekt.get(), private val preferences: PreferencesHelper = Injekt.get(), private val readerPreferences: ReaderPreferences = Injekt.get(), + private val trackPreferences: TrackPreferences = Injekt.get(), private val delayedTrackingStore: DelayedTrackingStore = Injekt.get(), private val getManga: GetManga = Injekt.get(), private val getChapterByMangaId: GetChapterByMangaId = Injekt.get(), @@ -820,7 +822,7 @@ class ReaderPresenter( * will run in a background thread and errors are ignored. */ private fun updateTrackChapterRead(readerChapter: ReaderChapter) { - if (!preferences.autoUpdateTrack().get()) return + if (!trackPreferences.autoUpdateTrack().get()) return val manga = manga ?: return val chapterRead = readerChapter.chapter.chapter_number.toDouble() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt index 6d63d7cd25..26698302ee 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsLibraryController.kt @@ -277,13 +277,13 @@ class SettingsLibraryController : SettingsController() { .launchIn(viewScope) } switchPreference { - bindTo(preferences.autoUpdateMetadata()) + bindTo(libraryPreferences.autoUpdateMetadata()) titleRes = R.string.pref_library_update_refresh_metadata summaryRes = R.string.pref_library_update_refresh_metadata_summary } if (trackManager.hasLoggedServices()) { switchPreference { - bindTo(preferences.autoUpdateTrackers()) + bindTo(libraryPreferences.autoUpdateTrackers()) titleRes = R.string.pref_library_update_refresh_trackers summaryRes = R.string.pref_library_update_refresh_trackers_summary } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsTrackingController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsTrackingController.kt index 5cc83f923f..69f7e129fd 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsTrackingController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsTrackingController.kt @@ -7,6 +7,7 @@ import android.view.MenuItem import android.widget.Toast import androidx.preference.PreferenceGroup import androidx.preference.PreferenceScreen +import eu.kanade.domain.track.service.TrackPreferences import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.track.NoLoginTrackService import eu.kanade.tachiyomi.data.track.TrackManager @@ -38,13 +39,14 @@ class SettingsTrackingController : TrackLogoutDialog.Listener { private val trackManager: TrackManager by injectLazy() + private val trackPreferences: TrackPreferences by injectLazy() private val sourceManager: SourceManager by injectLazy() override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply { titleRes = R.string.pref_category_tracking switchPreference { - bindTo(preferences.autoUpdateTrack()) + bindTo(trackPreferences.autoUpdateTrack()) titleRes = R.string.pref_auto_update_manga_sync }