Converted SwitchPreferenceCompat to Kotlin and fixed the BlockedDialog using the joinCommunity string for some bizarre reason

This commit is contained in:
alansley 2024-08-29 08:01:30 +10:00
parent d3fb440d05
commit 5bd55ea993
4 changed files with 69 additions and 71 deletions

View File

@ -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();
}
}
}

View File

@ -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"
}
}

View File

@ -8,14 +8,14 @@ import android.text.Spannable
import android.text.SpannableStringBuilder import android.text.SpannableStringBuilder
import android.text.style.StyleSpan import android.text.style.StyleSpan
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import com.squareup.phrase.Phrase
import network.loki.messenger.R import network.loki.messenger.R
import org.session.libsession.messaging.MessagingModuleConfiguration import org.session.libsession.messaging.MessagingModuleConfiguration
import org.session.libsession.messaging.contacts.Contact 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.session.libsession.utilities.recipients.Recipient
import org.thoughtcrime.securesms.createSessionDialog import org.thoughtcrime.securesms.createSessionDialog
import org.thoughtcrime.securesms.dependencies.DatabaseComponent import org.thoughtcrime.securesms.dependencies.DatabaseComponent
import org.thoughtcrime.securesms.ui.getSubbedCharSequence
/** Shown upon sending a message to a user that's blocked. */ /** Shown upon sending a message to a user that's blocked. */
class BlockedDialog(private val recipient: Recipient, private val context: Context) : DialogFragment() { 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 contact = contactDB.getContactWithAccountID(accountID)
val name = contact?.displayName(Contact.ContactContext.REGULAR) ?: accountID val name = contact?.displayName(Contact.ContactContext.REGULAR) ?: accountID
val explanation = Phrase.from(context, R.string.communityJoinDescription).put(COMMUNITY_NAME_KEY, name).format() val explanationCS = context.getSubbedCharSequence(R.string.blockUnblockName, NAME_KEY to name)
val spannable = SpannableStringBuilder(explanation) val spannable = SpannableStringBuilder(explanationCS)
val startIndex = explanation.indexOf(name) val startIndex = explanationCS.indexOf(name)
spannable.setSpan(StyleSpan(Typeface.BOLD), startIndex, startIndex + name.count(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) spannable.setSpan(StyleSpan(Typeface.BOLD), startIndex, startIndex + name.count(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
title(resources.getString(R.string.blockUnblock)) title(resources.getString(R.string.blockUnblock))

View File

@ -19,12 +19,17 @@ fun Context.createThemedComposeView(content: @Composable () -> Unit): ComposeVie
setThemedContent(content) 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<String, Int> versions of this or the `getSubbedString` method because the JVM sees the signatures as identical.
fun Context.getSubbedCharSequence(stringId: Int, vararg substitutionPairs: Pair<String, String>): CharSequence { fun Context.getSubbedCharSequence(stringId: Int, vararg substitutionPairs: Pair<String, String>): CharSequence {
val phrase = Phrase.from(this, stringId) val phrase = Phrase.from(this, stringId)
for ((key, value) in substitutionPairs) { phrase.put(key, value) } for ((key, value) in substitutionPairs) { phrase.put(key, value) }
return phrase.format() 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, String>): String { fun Context.getSubbedString(stringId: Int, vararg substitutionPairs: Pair<String, String>): String {
return getSubbedCharSequence(stringId, *substitutionPairs).toString() return getSubbedCharSequence(stringId, *substitutionPairs).toString()
} }