SettingsGeneralController.kt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package eu.kanade.tachiyomi.ui.setting
  2. import android.content.Intent
  3. import android.os.Build
  4. import android.provider.Settings
  5. import androidx.preference.PreferenceScreen
  6. import eu.kanade.tachiyomi.R
  7. import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
  8. import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values
  9. import eu.kanade.tachiyomi.data.preference.asImmediateFlow
  10. import eu.kanade.tachiyomi.util.preference.defaultValue
  11. import eu.kanade.tachiyomi.util.preference.entriesRes
  12. import eu.kanade.tachiyomi.util.preference.intListPreference
  13. import eu.kanade.tachiyomi.util.preference.listPreference
  14. import eu.kanade.tachiyomi.util.preference.onChange
  15. import eu.kanade.tachiyomi.util.preference.onClick
  16. import eu.kanade.tachiyomi.util.preference.preference
  17. import eu.kanade.tachiyomi.util.preference.preferenceCategory
  18. import eu.kanade.tachiyomi.util.preference.switchPreference
  19. import eu.kanade.tachiyomi.util.preference.titleRes
  20. import eu.kanade.tachiyomi.util.system.LocaleHelper
  21. import java.util.Date
  22. import kotlinx.coroutines.flow.launchIn
  23. class SettingsGeneralController : SettingsController() {
  24. override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
  25. titleRes = R.string.pref_category_general
  26. intListPreference {
  27. key = Keys.startScreen
  28. titleRes = R.string.pref_start_screen
  29. entriesRes = arrayOf(
  30. R.string.label_library,
  31. R.string.label_recent_updates,
  32. R.string.label_recent_manga,
  33. R.string.browse
  34. )
  35. entryValues = arrayOf("1", "3", "2", "4")
  36. defaultValue = "1"
  37. summary = "%s"
  38. }
  39. switchPreference {
  40. key = Keys.confirmExit
  41. titleRes = R.string.pref_confirm_exit
  42. defaultValue = false
  43. }
  44. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  45. preference {
  46. titleRes = R.string.pref_manage_notifications
  47. onClick {
  48. val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
  49. putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
  50. }
  51. startActivity(intent)
  52. }
  53. }
  54. }
  55. preferenceCategory {
  56. titleRes = R.string.pref_category_display
  57. listPreference {
  58. key = Keys.lang
  59. titleRes = R.string.pref_language
  60. val langs = mutableListOf<Pair<String, String>>()
  61. langs += Pair("", "${context.getString(R.string.system_default)} (${LocaleHelper.getDisplayName("")})")
  62. // Due to compatibility issues:
  63. // - Hebrew: `he` is copied into `iw` at build time
  64. langs += arrayOf(
  65. "ar", "be", "bg", "bn", "ca", "cs", "de", "el", "es", "es-419", "en-US", "en-GB", "fi", "fr", "he",
  66. "hi", "hr", "hu", "in", "it", "ja", "ka-rGE", "kn", "ko", "lv", "mr", "ms", "nb-rNO", "nl", "pl", "pt",
  67. "pt-BR", "ro", "ru", "sc", "sk", "sr", "sv", "th", "tl", "tr", "uk", "ur-rPK", "vi", "zh-rCN", "zh-rTW"
  68. )
  69. .map {
  70. Pair(it, LocaleHelper.getDisplayName(it))
  71. }
  72. .sortedBy { it.second }
  73. entryValues = langs.map { it.first }.toTypedArray()
  74. entries = langs.map { it.second }.toTypedArray()
  75. defaultValue = ""
  76. summary = "%s"
  77. onChange { newValue ->
  78. val activity = activity ?: return@onChange false
  79. val app = activity.application
  80. LocaleHelper.changeLocale(newValue.toString())
  81. LocaleHelper.updateConfiguration(app, app.resources.configuration)
  82. activity.recreate()
  83. true
  84. }
  85. }
  86. listPreference {
  87. key = Keys.dateFormat
  88. titleRes = R.string.pref_date_format
  89. entryValues = arrayOf("", "MM/dd/yy", "dd/MM/yy", "yyyy-MM-dd")
  90. val now = Date().time
  91. entries = entryValues.map { value ->
  92. val formattedDate = preferences.dateFormat(value.toString()).format(now)
  93. if (value == "") {
  94. "${context.getString(R.string.system_default)} ($formattedDate)"
  95. } else {
  96. "$value ($formattedDate)"
  97. }
  98. }.toTypedArray()
  99. defaultValue = ""
  100. summary = "%s"
  101. }
  102. listPreference {
  103. key = Keys.themeMode
  104. titleRes = R.string.pref_theme_mode
  105. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  106. entriesRes = arrayOf(
  107. R.string.theme_system,
  108. R.string.theme_light,
  109. R.string.theme_dark
  110. )
  111. entryValues = arrayOf(
  112. Values.ThemeMode.system.name,
  113. Values.ThemeMode.light.name,
  114. Values.ThemeMode.dark.name
  115. )
  116. defaultValue = Values.ThemeMode.system.name
  117. } else {
  118. entriesRes = arrayOf(
  119. R.string.theme_light,
  120. R.string.theme_dark
  121. )
  122. entryValues = arrayOf(
  123. Values.ThemeMode.light.name,
  124. Values.ThemeMode.dark.name
  125. )
  126. defaultValue = Values.ThemeMode.light.name
  127. }
  128. summary = "%s"
  129. onChange {
  130. activity?.recreate()
  131. true
  132. }
  133. }
  134. listPreference {
  135. key = Keys.themeLight
  136. titleRes = R.string.pref_theme_light
  137. entriesRes = arrayOf(
  138. R.string.theme_light_default,
  139. R.string.theme_light_blue
  140. )
  141. entryValues = arrayOf(
  142. Values.LightThemeVariant.default.name,
  143. Values.LightThemeVariant.blue.name
  144. )
  145. defaultValue = Values.LightThemeVariant.default.name
  146. summary = "%s"
  147. preferences.themeMode().asImmediateFlow { isVisible = it != Values.ThemeMode.dark }
  148. .launchIn(scope)
  149. onChange {
  150. if (preferences.themeMode().get() != Values.ThemeMode.dark) {
  151. activity?.recreate()
  152. }
  153. true
  154. }
  155. }
  156. listPreference {
  157. key = Keys.themeDark
  158. titleRes = R.string.pref_theme_dark
  159. entriesRes = arrayOf(
  160. R.string.theme_dark_default,
  161. R.string.theme_dark_blue,
  162. R.string.theme_dark_amoled
  163. )
  164. entryValues = arrayOf(
  165. Values.DarkThemeVariant.default.name,
  166. Values.DarkThemeVariant.blue.name,
  167. Values.DarkThemeVariant.amoled.name
  168. )
  169. defaultValue = Values.DarkThemeVariant.default.name
  170. summary = "%s"
  171. preferences.themeMode().asImmediateFlow { isVisible = it != Values.ThemeMode.light }
  172. .launchIn(scope)
  173. onChange {
  174. if (preferences.themeMode().get() != Values.ThemeMode.light) {
  175. activity?.recreate()
  176. }
  177. true
  178. }
  179. }
  180. }
  181. }
  182. }