PreferencesHelper.kt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package eu.kanade.tachiyomi.data.preference
  2. import android.content.Context
  3. import android.os.Build
  4. import android.os.Environment
  5. import androidx.core.content.edit
  6. import androidx.core.net.toUri
  7. import androidx.preference.PreferenceManager
  8. import com.fredporciuncula.flow.preferences.FlowSharedPreferences
  9. import eu.kanade.domain.source.interactor.SetMigrateSorting
  10. import eu.kanade.tachiyomi.R
  11. import eu.kanade.tachiyomi.data.database.models.Manga
  12. import eu.kanade.tachiyomi.data.track.TrackService
  13. import eu.kanade.tachiyomi.data.track.anilist.Anilist
  14. import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting
  15. import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting
  16. import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting
  17. import eu.kanade.tachiyomi.ui.reader.setting.OrientationType
  18. import eu.kanade.tachiyomi.ui.reader.setting.ReadingModeType
  19. import eu.kanade.tachiyomi.util.system.DeviceUtil
  20. import eu.kanade.tachiyomi.util.system.isDevFlavor
  21. import eu.kanade.tachiyomi.widget.ExtendedNavigationView
  22. import java.io.File
  23. import java.text.DateFormat
  24. import java.text.SimpleDateFormat
  25. import java.util.Locale
  26. import eu.kanade.domain.manga.model.Manga as DomainManga
  27. import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
  28. import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values
  29. class PreferencesHelper(val context: Context) {
  30. private val prefs = PreferenceManager.getDefaultSharedPreferences(context)
  31. private val flowPrefs = FlowSharedPreferences(prefs)
  32. private val defaultDownloadsDir = File(
  33. Environment.getExternalStorageDirectory().absolutePath + File.separator +
  34. context.getString(R.string.app_name),
  35. "downloads",
  36. ).toUri()
  37. private val defaultBackupDir = File(
  38. Environment.getExternalStorageDirectory().absolutePath + File.separator +
  39. context.getString(R.string.app_name),
  40. "backup",
  41. ).toUri()
  42. fun confirmExit() = prefs.getBoolean(Keys.confirmExit, false)
  43. fun sideNavIconAlignment() = flowPrefs.getInt("pref_side_nav_icon_alignment", 0)
  44. fun useAuthenticator() = flowPrefs.getBoolean("use_biometric_lock", false)
  45. fun lockAppAfter() = flowPrefs.getInt("lock_app_after", 0)
  46. fun lastAppClosed() = flowPrefs.getLong("last_app_closed", 0)
  47. fun secureScreen() = flowPrefs.getEnum("secure_screen_v2", Values.SecureScreenMode.INCOGNITO)
  48. fun hideNotificationContent() = prefs.getBoolean(Keys.hideNotificationContent, false)
  49. fun autoUpdateMetadata() = prefs.getBoolean(Keys.autoUpdateMetadata, false)
  50. fun autoUpdateTrackers() = prefs.getBoolean(Keys.autoUpdateTrackers, false)
  51. fun themeMode() = flowPrefs.getEnum(
  52. "pref_theme_mode_key",
  53. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Values.ThemeMode.system } else { Values.ThemeMode.light },
  54. )
  55. fun appTheme() = flowPrefs.getEnum(
  56. "pref_app_theme",
  57. if (DeviceUtil.isDynamicColorAvailable) { Values.AppTheme.MONET } else { Values.AppTheme.DEFAULT },
  58. )
  59. fun themeDarkAmoled() = flowPrefs.getBoolean("pref_theme_dark_amoled_key", false)
  60. fun pageTransitions() = flowPrefs.getBoolean("pref_enable_transitions_key", true)
  61. fun doubleTapAnimSpeed() = flowPrefs.getInt("pref_double_tap_anim_speed", 500)
  62. fun showPageNumber() = flowPrefs.getBoolean("pref_show_page_number_key", true)
  63. fun dualPageSplitPaged() = flowPrefs.getBoolean("pref_dual_page_split", false)
  64. fun dualPageSplitWebtoon() = flowPrefs.getBoolean("pref_dual_page_split_webtoon", false)
  65. fun dualPageInvertPaged() = flowPrefs.getBoolean("pref_dual_page_invert", false)
  66. fun dualPageInvertWebtoon() = flowPrefs.getBoolean("pref_dual_page_invert_webtoon", false)
  67. fun showReadingMode() = prefs.getBoolean(Keys.showReadingMode, true)
  68. fun trueColor() = flowPrefs.getBoolean("pref_true_color_key", false)
  69. fun fullscreen() = flowPrefs.getBoolean("fullscreen", true)
  70. fun cutoutShort() = flowPrefs.getBoolean("cutout_short", true)
  71. fun keepScreenOn() = flowPrefs.getBoolean("pref_keep_screen_on_key", true)
  72. fun customBrightness() = flowPrefs.getBoolean("pref_custom_brightness_key", false)
  73. fun customBrightnessValue() = flowPrefs.getInt("custom_brightness_value", 0)
  74. fun colorFilter() = flowPrefs.getBoolean("pref_color_filter_key", false)
  75. fun colorFilterValue() = flowPrefs.getInt("color_filter_value", 0)
  76. fun colorFilterMode() = flowPrefs.getInt("color_filter_mode", 0)
  77. fun grayscale() = flowPrefs.getBoolean("pref_grayscale", false)
  78. fun invertedColors() = flowPrefs.getBoolean("pref_inverted_colors", false)
  79. fun defaultReadingMode() = prefs.getInt(Keys.defaultReadingMode, ReadingModeType.RIGHT_TO_LEFT.flagValue)
  80. fun defaultOrientationType() = prefs.getInt(Keys.defaultOrientationType, OrientationType.FREE.flagValue)
  81. fun imageScaleType() = flowPrefs.getInt("pref_image_scale_type_key", 1)
  82. fun zoomStart() = flowPrefs.getInt("pref_zoom_start_key", 1)
  83. fun readerTheme() = flowPrefs.getInt("pref_reader_theme_key", 1)
  84. fun alwaysShowChapterTransition() = flowPrefs.getBoolean("always_show_chapter_transition", true)
  85. fun cropBorders() = flowPrefs.getBoolean("crop_borders", false)
  86. fun navigateToPan() = flowPrefs.getBoolean("navigate_pan", true)
  87. fun landscapeZoom() = flowPrefs.getBoolean("landscape_zoom", true)
  88. fun cropBordersWebtoon() = flowPrefs.getBoolean("crop_borders_webtoon", false)
  89. fun webtoonSidePadding() = flowPrefs.getInt("webtoon_side_padding", 0)
  90. fun pagerNavInverted() = flowPrefs.getEnum("reader_tapping_inverted", Values.TappingInvertMode.NONE)
  91. fun webtoonNavInverted() = flowPrefs.getEnum("reader_tapping_inverted_webtoon", Values.TappingInvertMode.NONE)
  92. fun readWithLongTap() = flowPrefs.getBoolean("reader_long_tap", true)
  93. fun readWithVolumeKeys() = flowPrefs.getBoolean("reader_volume_keys", false)
  94. fun readWithVolumeKeysInverted() = flowPrefs.getBoolean("reader_volume_keys_inverted", false)
  95. fun navigationModePager() = flowPrefs.getInt("reader_navigation_mode_pager", 0)
  96. fun navigationModeWebtoon() = flowPrefs.getInt("reader_navigation_mode_webtoon", 0)
  97. fun showNavigationOverlayNewUser() = flowPrefs.getBoolean("reader_navigation_overlay_new_user", true)
  98. fun showNavigationOverlayOnStart() = flowPrefs.getBoolean("reader_navigation_overlay_on_start", false)
  99. fun readerHideThreshold() = flowPrefs.getEnum("reader_hide_threshold", Values.ReaderHideThreshold.LOW)
  100. fun portraitColumns() = flowPrefs.getInt("pref_library_columns_portrait_key", 0)
  101. fun landscapeColumns() = flowPrefs.getInt("pref_library_columns_landscape_key", 0)
  102. fun autoUpdateTrack() = prefs.getBoolean(Keys.autoUpdateTrack, true)
  103. fun lastUsedSource() = flowPrefs.getLong("last_catalogue_source", -1)
  104. fun lastUsedCategory() = flowPrefs.getInt("last_used_category", 0)
  105. fun lastVersionCode() = flowPrefs.getInt("last_version_code", 0)
  106. fun sourceDisplayMode() = flowPrefs.getEnum("pref_display_mode_catalogue", DisplayModeSetting.COMPACT_GRID)
  107. fun enabledLanguages() = flowPrefs.getStringSet("source_languages", setOf("all", "en", Locale.getDefault().language))
  108. fun trackUsername(sync: TrackService) = prefs.getString(Keys.trackUsername(sync.id), "")
  109. fun trackPassword(sync: TrackService) = prefs.getString(Keys.trackPassword(sync.id), "")
  110. fun setTrackCredentials(sync: TrackService, username: String, password: String) {
  111. prefs.edit {
  112. putString(Keys.trackUsername(sync.id), username)
  113. putString(Keys.trackPassword(sync.id), password)
  114. }
  115. }
  116. fun trackToken(sync: TrackService) = flowPrefs.getString(Keys.trackToken(sync.id), "")
  117. fun anilistScoreType() = flowPrefs.getString("anilist_score_type", Anilist.POINT_10)
  118. fun backupsDirectory() = flowPrefs.getString("backup_directory", defaultBackupDir.toString())
  119. fun relativeTime() = flowPrefs.getInt("relative_time", 7)
  120. fun dateFormat(format: String = flowPrefs.getString(Keys.dateFormat, "").get()): DateFormat = when (format) {
  121. "" -> DateFormat.getDateInstance(DateFormat.SHORT)
  122. else -> SimpleDateFormat(format, Locale.getDefault())
  123. }
  124. fun downloadsDirectory() = flowPrefs.getString("download_directory", defaultDownloadsDir.toString())
  125. fun downloadOnlyOverWifi() = prefs.getBoolean(Keys.downloadOnlyOverWifi, true)
  126. fun saveChaptersAsCBZ() = flowPrefs.getBoolean("save_chapter_as_cbz", true)
  127. fun splitTallImages() = flowPrefs.getBoolean("split_tall_images", false)
  128. fun folderPerManga() = prefs.getBoolean(Keys.folderPerManga, false)
  129. fun numberOfBackups() = flowPrefs.getInt("backup_slots", 2)
  130. fun backupInterval() = flowPrefs.getInt("backup_interval", 0)
  131. fun removeAfterReadSlots() = prefs.getInt(Keys.removeAfterReadSlots, -1)
  132. fun removeAfterMarkedAsRead() = prefs.getBoolean(Keys.removeAfterMarkedAsRead, false)
  133. fun removeBookmarkedChapters() = prefs.getBoolean(Keys.removeBookmarkedChapters, false)
  134. fun removeExcludeCategories() = flowPrefs.getStringSet("remove_exclude_categories", emptySet())
  135. fun libraryUpdateInterval() = flowPrefs.getInt("pref_library_update_interval_key", 24)
  136. fun libraryUpdateDeviceRestriction() = flowPrefs.getStringSet("library_update_restriction", setOf(DEVICE_ONLY_ON_WIFI))
  137. fun libraryUpdateMangaRestriction() = flowPrefs.getStringSet("library_update_manga_restriction", setOf(MANGA_HAS_UNREAD, MANGA_NON_COMPLETED, MANGA_NON_READ))
  138. fun showUpdatesNavBadge() = flowPrefs.getBoolean("library_update_show_tab_badge", false)
  139. fun unreadUpdatesCount() = flowPrefs.getInt("library_unread_updates_count", 0)
  140. fun libraryUpdateCategories() = flowPrefs.getStringSet("library_update_categories", emptySet())
  141. fun libraryUpdateCategoriesExclude() = flowPrefs.getStringSet("library_update_categories_exclude", emptySet())
  142. fun libraryDisplayMode() = flowPrefs.getEnum("pref_display_mode_library", DisplayModeSetting.COMPACT_GRID)
  143. fun downloadBadge() = flowPrefs.getBoolean("display_download_badge", false)
  144. fun localBadge() = flowPrefs.getBoolean("display_local_badge", true)
  145. fun downloadedOnly() = flowPrefs.getBoolean("pref_downloaded_only", false)
  146. fun unreadBadge() = flowPrefs.getBoolean("display_unread_badge", true)
  147. fun languageBadge() = flowPrefs.getBoolean("display_language_badge", false)
  148. fun categoryTabs() = flowPrefs.getBoolean("display_category_tabs", true)
  149. fun categoryNumberOfItems() = flowPrefs.getBoolean("display_number_of_items", false)
  150. fun filterDownloaded() = flowPrefs.getInt(Keys.filterDownloaded, ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)
  151. fun filterUnread() = flowPrefs.getInt(Keys.filterUnread, ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)
  152. fun filterStarted() = flowPrefs.getInt(Keys.filterStarted, ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)
  153. fun filterCompleted() = flowPrefs.getInt(Keys.filterCompleted, ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)
  154. fun filterTracking(name: Int) = flowPrefs.getInt("${Keys.filterTracked}_$name", ExtendedNavigationView.Item.TriStateGroup.State.IGNORE.value)
  155. fun librarySortingMode() = flowPrefs.getEnum(Keys.librarySortingMode, SortModeSetting.ALPHABETICAL)
  156. fun librarySortingAscending() = flowPrefs.getEnum(Keys.librarySortingDirection, SortDirectionSetting.ASCENDING)
  157. fun migrationSortingMode() = flowPrefs.getEnum(Keys.migrationSortingMode, SetMigrateSorting.Mode.ALPHABETICAL)
  158. fun migrationSortingDirection() = flowPrefs.getEnum(Keys.migrationSortingDirection, SetMigrateSorting.Direction.ASCENDING)
  159. fun automaticExtUpdates() = flowPrefs.getBoolean("automatic_ext_updates", true)
  160. fun showNsfwSource() = flowPrefs.getBoolean("show_nsfw_source", true)
  161. fun extensionUpdatesCount() = flowPrefs.getInt("ext_updates_count", 0)
  162. fun lastAppCheck() = flowPrefs.getLong("last_app_check", 0)
  163. fun lastExtCheck() = flowPrefs.getLong("last_ext_check", 0)
  164. fun searchPinnedSourcesOnly() = prefs.getBoolean(Keys.searchPinnedSourcesOnly, false)
  165. fun disabledSources() = flowPrefs.getStringSet("hidden_catalogues", emptySet())
  166. fun pinnedSources() = flowPrefs.getStringSet("pinned_catalogues", emptySet())
  167. fun downloadNewChapter() = flowPrefs.getBoolean("download_new", false)
  168. fun downloadNewChapterCategories() = flowPrefs.getStringSet("download_new_categories", emptySet())
  169. fun downloadNewChapterCategoriesExclude() = flowPrefs.getStringSet("download_new_categories_exclude", emptySet())
  170. fun defaultCategory() = prefs.getInt(Keys.defaultCategory, -1)
  171. fun categorizedDisplaySettings() = flowPrefs.getBoolean("categorized_display", false)
  172. fun skipRead() = prefs.getBoolean(Keys.skipRead, false)
  173. fun skipFiltered() = prefs.getBoolean(Keys.skipFiltered, true)
  174. fun migrateFlags() = flowPrefs.getInt("migrate_flags", Int.MAX_VALUE)
  175. fun trustedSignatures() = flowPrefs.getStringSet("trusted_signatures", emptySet())
  176. fun dohProvider() = prefs.getInt(Keys.dohProvider, -1)
  177. fun defaultUserAgent() = flowPrefs.getString(Keys.defaultUserAgent, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44")
  178. fun lastSearchQuerySearchSettings() = flowPrefs.getString("last_search_query", "")
  179. fun filterChapterByRead() = prefs.getInt(Keys.defaultChapterFilterByRead, DomainManga.SHOW_ALL.toInt())
  180. fun filterChapterByDownloaded() = prefs.getInt(Keys.defaultChapterFilterByDownloaded, DomainManga.SHOW_ALL.toInt())
  181. fun filterChapterByBookmarked() = prefs.getInt(Keys.defaultChapterFilterByBookmarked, DomainManga.SHOW_ALL.toInt())
  182. fun sortChapterBySourceOrNumber() = prefs.getInt(Keys.defaultChapterSortBySourceOrNumber, DomainManga.CHAPTER_SORTING_SOURCE.toInt())
  183. fun displayChapterByNameOrNumber() = prefs.getInt(Keys.defaultChapterDisplayByNameOrNumber, DomainManga.CHAPTER_DISPLAY_NAME.toInt())
  184. fun sortChapterByAscendingOrDescending() = prefs.getInt(Keys.defaultChapterSortByAscendingOrDescending, DomainManga.CHAPTER_SORT_DESC.toInt())
  185. fun incognitoMode() = flowPrefs.getBoolean("incognito_mode", false)
  186. fun tabletUiMode() = flowPrefs.getEnum("tablet_ui_mode", Values.TabletUiMode.AUTOMATIC)
  187. fun extensionInstaller() = flowPrefs.getEnum(
  188. "extension_installer",
  189. if (DeviceUtil.isMiui) Values.ExtensionInstaller.LEGACY else Values.ExtensionInstaller.PACKAGEINSTALLER,
  190. )
  191. fun verboseLogging() = prefs.getBoolean(Keys.verboseLogging, isDevFlavor)
  192. fun autoClearChapterCache() = prefs.getBoolean(Keys.autoClearChapterCache, false)
  193. fun duplicatePinnedSources() = flowPrefs.getBoolean("duplicate_pinned_sources", false)
  194. fun setChapterSettingsDefault(manga: Manga) {
  195. prefs.edit {
  196. putInt(Keys.defaultChapterFilterByRead, manga.readFilter)
  197. putInt(Keys.defaultChapterFilterByDownloaded, manga.downloadedFilter)
  198. putInt(Keys.defaultChapterFilterByBookmarked, manga.bookmarkedFilter)
  199. putInt(Keys.defaultChapterSortBySourceOrNumber, manga.sorting)
  200. putInt(Keys.defaultChapterDisplayByNameOrNumber, manga.displayMode)
  201. putInt(Keys.defaultChapterSortByAscendingOrDescending, if (manga.sortDescending()) DomainManga.CHAPTER_SORT_DESC.toInt() else DomainManga.CHAPTER_SORT_ASC.toInt())
  202. }
  203. }
  204. }