From 71d1df43ed50e4355b53dbe761db43b7632de29b Mon Sep 17 00:00:00 2001 From: MorenoTropical <154519856+morenotropical@users.noreply.github.com> Date: Tue, 19 Nov 2024 07:26:58 -0300 Subject: [PATCH] use backend for show audio play buttons --- .../preferences/AppearanceSettingsFragment.kt | 21 +++++++++++++++---- .../src/main/java/com/ichi2/libanki/Sound.kt | 5 ++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/preferences/AppearanceSettingsFragment.kt b/AnkiDroid/src/main/java/com/ichi2/anki/preferences/AppearanceSettingsFragment.kt index 94f13c6ed305..5fa53dc9b332 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/preferences/AppearanceSettingsFragment.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/preferences/AppearanceSettingsFragment.kt @@ -22,7 +22,6 @@ import androidx.core.app.ActivityCompat import androidx.preference.ListPreference import androidx.preference.Preference import androidx.preference.SwitchPreferenceCompat -import anki.config.ConfigKey import anki.config.copy import com.ichi2.anki.CollectionManager import com.ichi2.anki.CollectionManager.withCol @@ -139,9 +138,11 @@ class AppearanceSettingsFragment : SettingsFragment() { // Note: Stored inverted in the collection as HIDE_AUDIO_PLAY_BUTTONS requirePreference(R.string.show_audio_play_buttons_key).apply { title = CollectionManager.TR.preferencesShowPlayButtonsOnCardsWith() - launchCatchingTask { isChecked = withCol { !config.getBool(ConfigKey.Bool.HIDE_AUDIO_PLAY_BUTTONS) } } - setOnPreferenceChangeListener { newValue -> - launchCatchingTask { withCol { config.setBool(ConfigKey.Bool.HIDE_AUDIO_PLAY_BUTTONS, !(newValue as Boolean)) } } + launchCatchingTask { isChecked = !getHidePlayAudioButtons() } + setOnPreferenceChangeListener { _, newValue -> + val newValueBool = newValue as? Boolean ?: return@setOnPreferenceChangeListener false + launchCatchingTask { setHideAudioPlayButtons(!newValueBool) } + true } } } @@ -208,7 +209,19 @@ class AppearanceSettingsFragment : SettingsFragment() { undoableOp { setPreferences(newPrefs) } Timber.i("Set showRemainingDueCounts to %b", value) } + + private suspend fun setHideAudioPlayButtons(value: Boolean) { + val prefs = withCol { getPreferences() } + val newPrefs = prefs.copy { + reviewing = reviewing.copy { hideAudioPlayButtons = value } + } + undoableOp { setPreferences(newPrefs) } + Timber.i("Set hideAudioPlayButtons to %b", value) + } } suspend fun getShowIntervalOnButtons(): Boolean = withCol { getPreferences().reviewing.showIntervalsOnButtons } + +suspend fun getHidePlayAudioButtons(): Boolean = + withCol { getPreferences().reviewing.hideAudioPlayButtons } diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/Sound.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/Sound.kt index 84a59cb2605b..8edf296fbfeb 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/Sound.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/Sound.kt @@ -26,8 +26,8 @@ package com.ichi2.libanki import android.text.TextUtils -import anki.config.ConfigKey import com.ichi2.anki.CollectionManager +import com.ichi2.anki.preferences.getHidePlayAudioButtons import com.ichi2.compat.CompatHelper import com.ichi2.libanki.TemplateManager.TemplateRenderContext.TemplateRenderOutput import com.ichi2.libanki.utils.NotInLibAnki @@ -180,8 +180,7 @@ object Sound { renderOutput: TemplateRenderOutput ): String { val mediaDir = CollectionManager.withCol { media.dir } - val hidePlayButtons = - CollectionManager.withCol { config.getBool(ConfigKey.Bool.HIDE_AUDIO_PLAY_BUTTONS) } + val hidePlayButtons = getHidePlayAudioButtons() return expandSounds(text, renderOutput, showAudioPlayButtons = !hidePlayButtons, mediaDir) }