Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import com.ichi2.anki.deckpicker.BackgroundImage.FileSizeResult
import com.ichi2.anki.launchCatchingTask
import com.ichi2.anki.settings.Prefs
import com.ichi2.anki.settings.enums.AppTheme
import com.ichi2.anki.settings.enums.DayTheme
import com.ichi2.anki.settings.enums.NightTheme
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.ui.internationalization.sentenceCase
import com.ichi2.anki.utils.CollectionPreferences
Expand Down Expand Up @@ -135,15 +137,25 @@ class AppearanceSettingsFragment : SettingsFragment() {
val dayThemePref = requirePreference<ListPreference>(R.string.day_theme_key)
val nightThemePref = requirePreference<ListPreference>(R.string.night_theme_key)

dayThemePref.entries = DayTheme.entries.map { it.label(requireContext()) }.toTypedArray()
dayThemePref.entryValues = DayTheme.entries.map { getString(it.entryResId) }.toTypedArray()
nightThemePref.entries = NightTheme.entries.map { it.label(requireContext()) }.toTypedArray()
nightThemePref.entryValues = NightTheme.entries.map { getString(it.entryResId) }.toTypedArray()

val appThemeEntries = AppTheme.entries.map { it.label(requireContext()) }.toMutableList()
val appThemeValues = AppTheme.entries.map { getString(it.entryResId) }.toMutableList()

// Remove follow system options in android versions which do not have system dark mode
// When minSdk reaches 29, the only necessary change is to remove this if-block
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
// Drop "Follow system" option (the first one)
appThemePref.entries = resources.getStringArray(R.array.app_theme_labels).drop(1).toTypedArray()
appThemePref.entryValues = resources.getStringArray(R.array.app_theme_values).drop(1).toTypedArray()
if (appTheme == AppTheme.FOLLOW_SYSTEM) {
appThemePref.value = getString(Themes.currentTheme.entryResId)
}
appThemeEntries.removeAt(0)
appThemeValues.removeAt(0)
}
appThemePref.entries = appThemeEntries.toTypedArray()
appThemePref.entryValues = appThemeValues.toTypedArray()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q && appTheme == AppTheme.FOLLOW_SYSTEM) {
appThemePref.value = getString(Themes.currentTheme.entryResId)
}

appThemePref.setOnPreferenceChangeListener { newValue ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
*/
package com.ichi2.anki.settings.enums

import android.content.Context
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.R
import com.ichi2.anki.ui.internationalization.sentenceCase

/** [R.array.app_theme_values] */
/** Values for the `app_theme` [ListPreference][androidx.preference.ListPreference]. */
enum class AppTheme(
override val entryResId: Int,
/** The label shown for this option in the preference's list. */
val label: Context.() -> String,
) : PrefEnum {
FOLLOW_SYSTEM(R.string.theme_follow_system_value),
DAY(R.string.theme_day_scheme_value),
NIGHT(R.string.theme_night_scheme_value),
FOLLOW_SYSTEM(R.string.theme_follow_system_value, { TR.sentenceCase.themeFollowSystem }),
DAY(R.string.theme_day_scheme_value, { getString(R.string.theme_day) }),
NIGHT(R.string.theme_night_scheme_value, { getString(R.string.theme_night) }),
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@
*/
package com.ichi2.anki.settings.enums

import android.content.Context
import androidx.annotation.StyleRes
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.R

sealed interface Theme : PrefEnum {
@get:StyleRes
val styleResId: Int
}

/** [R.array.day_theme_values] */
/** Values for the `day_theme` [ListPreference][androidx.preference.ListPreference]. */
enum class DayTheme(
override val entryResId: Int,
override val styleResId: Int,
/** The label shown for this option in the preference's list. */
val label: Context.() -> String,
) : Theme {
LIGHT(R.string.theme_light_value, R.style.Theme_Light),
PLAIN(R.string.theme_plain_value, R.style.Theme_Light_Plain),
LIGHT(R.string.theme_light_value, R.style.Theme_Light, { TR.preferencesThemeLight() }),
PLAIN(R.string.theme_plain_value, R.style.Theme_Light_Plain, { getString(R.string.day_theme_plain) }),
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
*/
package com.ichi2.anki.settings.enums

import android.content.Context
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.R

/** [R.array.night_theme_values] */
/** Values for the `night_theme` [ListPreference][androidx.preference.ListPreference]. */
enum class NightTheme(
override val entryResId: Int,
override val styleResId: Int,
/** The label shown for this option in the preference's list. */
val label: Context.() -> String,
) : Theme {
BLACK(R.string.theme_black_value, R.style.Theme_Dark_Black),
DARK(R.string.theme_dark_value, R.style.Theme_Dark),
BLACK(R.string.theme_black_value, R.style.Theme_Dark_Black, { getString(R.string.night_theme_black) }),
DARK(R.string.theme_dark_value, R.style.Theme_Dark, { TR.preferencesThemeDark() }),
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ object SentenceCase {
context(_: Context)
val browserAppearance get() = TR.browsingBrowserAppearance().toSentenceCase(R.string.sentence_browser_appearance)

context(_: Context)
val themeFollowSystem get() = TR.preferencesThemeFollowSystem().toSentenceCase(R.string.sentence_follow_system)

context(_: Fragment)
val browserAppearance get() = TR.browsingBrowserAppearance().toSentenceCase(R.string.sentence_browser_appearance)

Expand Down
7 changes: 2 additions & 5 deletions AnkiDroid/src/main/res/values/11-arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
<string name="add_to_deck_decide_by_note_type">Decide by note type</string>

<!-- theme labels -->
<string name="theme_follow_system">Follow system</string>
<string name="theme_day" comment="Day/Light theme color scheme">Day</string>
<string name="theme_night" comment="Night/Dark theme color scheme">Night</string>

<!-- day_theme_labels -->
<string name="day_theme_light">Light</string>
<!-- day theme labels -->
<string name="day_theme_plain">Plain</string>

<!-- night_theme_labels -->
<!-- night theme labels -->
<string name="night_theme_black">Black</string>
<string name="night_theme_dark">Dark</string>

<!-- html_size_code_labels -->
<string name="html_size_code_xx_small">xx-small</string>
Expand Down
28 changes: 0 additions & 28 deletions AnkiDroid/src/main/res/values/constants.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@
<item>@string/add_to_deck_use_current_deck</item>
<item>@string/add_to_deck_decide_by_note_type</item>
</string-array>
<string-array name="day_theme_labels">
<item>@string/day_theme_light</item>
<item>@string/day_theme_plain</item>
</string-array>
<string-array name="night_theme_labels">
<item>@string/night_theme_black</item>
<item>@string/night_theme_dark</item>
</string-array>
<string-array name="html_size_code_labels">
<item>@string/html_size_code_xx_small</item>
<item>@string/html_size_code_x_small</item>
Expand Down Expand Up @@ -169,12 +161,6 @@
</string-array>

<!-- Themes -->
<string-array name="app_theme_labels">
<item>@string/theme_follow_system</item>
<item>@string/theme_day</item>
<item>@string/theme_night</item>
</string-array>

<string name="theme_follow_system_value">0</string>
<string name="theme_day_scheme_value">1</string>
<string name="theme_night_scheme_value">2</string>
Expand All @@ -184,20 +170,6 @@
<string name="theme_black_value">3</string>
<string name="theme_dark_value">4</string>

<string-array name="app_theme_values">
<item>@string/theme_follow_system_value</item>
<item>@string/theme_day_scheme_value</item>
<item>@string/theme_night_scheme_value</item>
</string-array>
<string-array name="day_theme_values">
<item>@string/theme_light_value</item>
<item>@string/theme_plain_value</item>
</string-array>
<string-array name="night_theme_values">
<item>@string/theme_black_value</item>
<item>@string/theme_dark_value</item>
</string-array>

<string-array name="sync_media_entries">
<item>@string/sync_media_always</item>
<item>@string/sync_media_only_unmetered</item>
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/sentence-case.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ undoActionUndone()
<string name="sentence_answer_again" maxLength="41">Answer again</string>
<string name="sentence_answer_hard" maxLength="41">Answer hard</string>
<string name="sentence_answer_good" maxLength="41">Answer good</string>
<string name="sentence_follow_system" maxLength="41">Follow system</string>
</resources>
6 changes: 0 additions & 6 deletions AnkiDroid/src/main/res/xml/preferences_appearance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@
<PreferenceCategory android:title="@string/pref_cat_themes" >
<ListPreference
android:defaultValue="@string/theme_follow_system_value"
android:entries="@array/app_theme_labels"
android:entryValues="@array/app_theme_values"
android:key="@string/app_theme_key"
android:title="@string/app_theme"
android:icon="@drawable/ic_color_lens_white_24dp"
app:useSimpleSummaryProvider="true"/>
<ListPreference
android:defaultValue="@string/theme_light_value"
android:entries="@array/day_theme_labels"
android:entryValues="@array/day_theme_values"
android:key="@string/day_theme_key"
android:shouldDisableView="true"
android:title="@string/day_theme"
Expand All @@ -47,8 +43,6 @@
/>
<ListPreference
android:defaultValue="@string/theme_black_value"
android:entries="@array/night_theme_labels"
android:entryValues="@array/night_theme_values"
android:key="@string/night_theme_key"
android:shouldDisableView="true"
android:title="@string/night_theme"
Expand Down
3 changes: 0 additions & 3 deletions AnkiDroid/src/test/java/com/ichi2/anki/TranslationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ class TranslationTest : RobolectricTest() {
"Copied to clipboard", // R.string.about_ankidroid_successfully_copied_debug_info
// TR.aboutCopiedToClipboard()
// TR.errorsCopiedToClipboard()
"Dark", // R.string.night_theme_dark | TR.preferencesThemeDark()
"Delete", // R.string.dialog_positive_delete
// TR.actionsDelete()
// TR.editingImageOcclusionDelete()
Expand Down Expand Up @@ -245,7 +244,6 @@ class TranslationTest : RobolectricTest() {
"Later", // R.string.button_backup_later | TR.schedulingUpdateLaterButton()
"Learn More", // R.string.scoped_storage_learn_more | TR.schedulingUpdateMoreInfoButton()
"Learn ahead limit", // R.string.learn_cutoff | TR.preferencesLearnAheadLimit()
"Light", // R.string.day_theme_light | TR.preferencesThemeLight()
"Media", // R.string.media
// TR.editingMedia()
// TR.preferencesMedia()
Expand Down Expand Up @@ -331,7 +329,6 @@ class TranslationTest : RobolectricTest() {
// // TR.mediaCheckCheckMediaAction()
// // TR.mediaCheckWindowTitle()
"Answer buttons", // R.string.answer_buttons | TR.statisticsAnswerButtonsTitle()
"Follow system", // R.string.theme_follow_system | TR.preferencesThemeFollowSystem()
"Select all", // R.string.card_browser_select_all | TR.editingImageOcclusionSelectAll()
"Show answer", // R.string.show_answer
// TR.studyingShowAnswer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.res.Resources
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.github.ivanshafran.sharedpreferencesmock.SPMockBuilder
import com.ichi2.anki.EmptyApplicationCategory
import com.ichi2.anki.R
import com.ichi2.anki.RobolectricTest
import com.ichi2.anki.libanki.utils.append
import com.ichi2.anki.preferences.HeaderFragment
Expand Down Expand Up @@ -147,7 +148,16 @@ class PrefsRobolectricTest : RobolectricTest() {
PreferenceTestUtils.attrToStringArray(it["entryValues"]!!, targetContext).toList()
}

// Theme preferences are the source of truth, and use `TR` so no longer fit the pattern.
val enumDerivedEntryValueKeys =
setOf(
R.string.app_theme_key,
R.string.day_theme_key,
R.string.night_theme_key,
).map { targetContext.getString(it) }

for ((key, enumValues) in prefsEnumKeysAndValues) {
if (key in enumDerivedEntryValueKeys) continue
assertContains(listPreferences, key)
assertEquals(enumValues, listPreferences[key])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class SentenceCaseTest : RobolectricTest() {
assertThat(TR.sentenceCase.previousCardInfo, equalTo("Previous card info"))
assertThat(TR.sentenceCase.ankiWebAccount, equalTo("AnkiWeb account"))
assertThat(TR.sentenceCase.browserAppearance, equalTo("Browser appearance"))
assertThat(TR.sentenceCase.themeFollowSystem, equalTo("Follow system"))
assertThat(TR.sentenceCase.copyDebugInfo, equalTo("Copy debug info"))
assertThat(TR.sentenceCase.addField, equalTo("Add field"))
assertThat(TR.sentenceCase.allDecks, equalTo("All decks"))
Expand Down
Loading