From 5bd55ea99320941b8f9b40f0680d6980f8e09dc4 Mon Sep 17 00:00:00 2001 From: alansley Date: Thu, 29 Aug 2024 08:01:30 +1000 Subject: [PATCH] Converted SwitchPreferenceCompat to Kotlin and fixed the BlockedDialog using the joinCommunity string for some bizarre reason --- .../components/SwitchPreferenceCompat.java | 66 ------------------- .../components/SwitchPreferenceCompat.kt | 59 +++++++++++++++++ .../conversation/v2/dialogs/BlockedDialog.kt | 10 +-- .../org/thoughtcrime/securesms/ui/Util.kt | 5 ++ 4 files changed, 69 insertions(+), 71 deletions(-) delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/components/SwitchPreferenceCompat.java create mode 100644 app/src/main/java/org/thoughtcrime/securesms/components/SwitchPreferenceCompat.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/SwitchPreferenceCompat.java b/app/src/main/java/org/thoughtcrime/securesms/components/SwitchPreferenceCompat.java deleted file mode 100644 index e0ad4d2609..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/components/SwitchPreferenceCompat.java +++ /dev/null @@ -1,66 +0,0 @@ -package org.thoughtcrime.securesms.components; - -import static org.session.libsession.utilities.StringSubstitutionConstants.APP_NAME_KEY; - -import android.content.Context; -import android.util.AttributeSet; -import androidx.preference.CheckBoxPreference; -import androidx.preference.Preference; -import com.squareup.phrase.Phrase; -import network.loki.messenger.R; - -public class SwitchPreferenceCompat extends CheckBoxPreference { - - private static String LOCK_SCREEN_KEY = "pref_android_screen_lock"; - - private Preference.OnPreferenceClickListener listener; - - public SwitchPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - setLayoutRes(); - } - - public SwitchPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { - super(context, attrs, defStyleAttr, defStyleRes); - setLayoutRes(); - } - - public SwitchPreferenceCompat(Context context, AttributeSet attrs) { - super(context, attrs); - setLayoutRes(); - } - - public SwitchPreferenceCompat(Context context) { - super(context); - setLayoutRes(); - } - - private void setLayoutRes() { - setWidgetLayoutResource(R.layout.switch_compat_preference); - - if (this.hasKey()) { - String key = this.getKey(); - - // Substitute app name into lockscreen preference summary - if (key.equalsIgnoreCase(LOCK_SCREEN_KEY)) { - Context c = getContext(); - CharSequence substitutedSummaryCS = Phrase.from(c, R.string.lockAppDescription) - .put(APP_NAME_KEY, c.getString(R.string.app_name)) - .format(); - this.setSummary(substitutedSummaryCS); - } - } - } - - @Override - public void setOnPreferenceClickListener(Preference.OnPreferenceClickListener listener) { - this.listener = listener; - } - - @Override - protected void onClick() { - if (listener == null || !listener.onPreferenceClick(this)) { - super.onClick(); - } - } -} diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/SwitchPreferenceCompat.kt b/app/src/main/java/org/thoughtcrime/securesms/components/SwitchPreferenceCompat.kt new file mode 100644 index 0000000000..9161dd828d --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/components/SwitchPreferenceCompat.kt @@ -0,0 +1,59 @@ +package org.thoughtcrime.securesms.components + +import android.content.Context +import android.util.AttributeSet +import androidx.preference.CheckBoxPreference +import com.squareup.phrase.Phrase +import network.loki.messenger.R +import org.session.libsession.utilities.StringSubstitutionConstants.APP_NAME_KEY +import org.thoughtcrime.securesms.ui.getSubbedCharSequence +import org.thoughtcrime.securesms.ui.getSubbedString + +class SwitchPreferenceCompat : CheckBoxPreference { + private var listener: OnPreferenceClickListener? = null + + constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context!!, attrs, defStyleAttr) { + setLayoutRes() + } + + constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context!!, attrs, defStyleAttr, defStyleRes) { + setLayoutRes() + } + + constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) { + setLayoutRes() + } + + constructor(context: Context?) : super(context!!) { + setLayoutRes() + } + + private fun setLayoutRes() { + widgetLayoutResource = R.layout.switch_compat_preference + + if (this.hasKey()) { + val key = this.key + + // Substitute app name into lockscreen preference summary + if (key.equals(LOCK_SCREEN_KEY, ignoreCase = true)) { + val c = context + val substitutedSummaryCS = c.getSubbedCharSequence(R.string.lockAppDescription, APP_NAME_KEY to c.getString(R.string.app_name)) + this.summary = substitutedSummaryCS + } + } + } + + override fun setOnPreferenceClickListener(listener: OnPreferenceClickListener?) { + this.listener = listener + } + + override fun onClick() { + if (listener == null || !listener!!.onPreferenceClick(this)) { + super.onClick() + } + } + + companion object { + private const val LOCK_SCREEN_KEY = "pref_android_screen_lock" + } +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/dialogs/BlockedDialog.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/dialogs/BlockedDialog.kt index 3dc87b5e91..9113f8ed46 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/dialogs/BlockedDialog.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/dialogs/BlockedDialog.kt @@ -8,14 +8,14 @@ import android.text.Spannable import android.text.SpannableStringBuilder import android.text.style.StyleSpan import androidx.fragment.app.DialogFragment -import com.squareup.phrase.Phrase import network.loki.messenger.R import org.session.libsession.messaging.MessagingModuleConfiguration import org.session.libsession.messaging.contacts.Contact -import org.session.libsession.utilities.StringSubstitutionConstants.COMMUNITY_NAME_KEY +import org.session.libsession.utilities.StringSubstitutionConstants.NAME_KEY import org.session.libsession.utilities.recipients.Recipient import org.thoughtcrime.securesms.createSessionDialog import org.thoughtcrime.securesms.dependencies.DatabaseComponent +import org.thoughtcrime.securesms.ui.getSubbedCharSequence /** Shown upon sending a message to a user that's blocked. */ class BlockedDialog(private val recipient: Recipient, private val context: Context) : DialogFragment() { @@ -26,9 +26,9 @@ class BlockedDialog(private val recipient: Recipient, private val context: Conte val contact = contactDB.getContactWithAccountID(accountID) val name = contact?.displayName(Contact.ContactContext.REGULAR) ?: accountID - val explanation = Phrase.from(context, R.string.communityJoinDescription).put(COMMUNITY_NAME_KEY, name).format() - val spannable = SpannableStringBuilder(explanation) - val startIndex = explanation.indexOf(name) + val explanationCS = context.getSubbedCharSequence(R.string.blockUnblockName, NAME_KEY to name) + val spannable = SpannableStringBuilder(explanationCS) + val startIndex = explanationCS.indexOf(name) spannable.setSpan(StyleSpan(Typeface.BOLD), startIndex, startIndex + name.count(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) title(resources.getString(R.string.blockUnblock)) diff --git a/app/src/main/java/org/thoughtcrime/securesms/ui/Util.kt b/app/src/main/java/org/thoughtcrime/securesms/ui/Util.kt index fbe4850de8..6f0bda8c96 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ui/Util.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/ui/Util.kt @@ -19,12 +19,17 @@ fun Context.createThemedComposeView(content: @Composable () -> Unit): ComposeVie setThemedContent(content) } +// Extension method to use the Phrase library to substitute strings & return a CharSequence. +// The pair is the key name, such as APP_NAME_KEY and the value is the localised string, such as context.getString(R.string.app_name). +// Note: We cannot have Pair versions of this or the `getSubbedString` method because the JVM sees the signatures as identical. fun Context.getSubbedCharSequence(stringId: Int, vararg substitutionPairs: Pair): CharSequence { val phrase = Phrase.from(this, stringId) for ((key, value) in substitutionPairs) { phrase.put(key, value) } return phrase.format() } +// Extension method to use the Phrase library to substitute strings & return the substituted String. +// The pair is the key name, such as APP_NAME_KEY and the value is the localised string, such as context.getString(R.string.app_name). fun Context.getSubbedString(stringId: Int, vararg substitutionPairs: Pair): String { return getSubbedCharSequence(stringId, *substitutionPairs).toString() }