From 6bf8d6be34d5c0d65ef59dbb1632c0e12f0bc6a6 Mon Sep 17 00:00:00 2001 From: Tiger Oakes Date: Sun, 23 Jun 2019 13:26:32 -0400 Subject: [PATCH] Use @JvmOverloads instead of multiple constructors --- .../mozilla/fenix/library/LibraryListItem.kt | 35 +++++------- .../settings/AccountAuthErrorPreference.kt | 10 ++-- .../fenix/settings/AccountPreference.kt | 10 ++-- .../settings/DefaultBrowserPreference.kt | 14 ++--- .../fenix/settings/DeleteBrowsingDataItem.kt | 43 ++++++--------- .../fenix/settings/RadioButtonPreference.kt | 53 ++++++++++--------- .../RadioSearchEngineListPreference.kt | 14 ++--- .../settings/SearchEngineListPreference.kt | 16 +++--- 8 files changed, 83 insertions(+), 112 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/library/LibraryListItem.kt b/app/src/main/java/org/mozilla/fenix/library/LibraryListItem.kt index 1925e16a4..affecbbf7 100644 --- a/app/src/main/java/org/mozilla/fenix/library/LibraryListItem.kt +++ b/app/src/main/java/org/mozilla/fenix/library/LibraryListItem.kt @@ -8,6 +8,7 @@ import android.content.Context import android.util.AttributeSet import android.view.LayoutInflater import androidx.constraintlayout.widget.ConstraintLayout +import androidx.core.content.withStyledAttributes import kotlinx.android.synthetic.main.library_list_item.view.* import org.mozilla.fenix.R @@ -19,28 +20,18 @@ class LibraryListItem @JvmOverloads constructor( init { LayoutInflater.from(context).inflate(R.layout.library_list_item, this, true) - attrs.let { - context.theme.obtainStyledAttributes( - it, - R.styleable.LibraryListItem, - 0, 0 - ).apply { - try { - val id = getResourceId( - R.styleable.LibraryListItem_listItemIcon, - R.drawable.library_icon_reading_list_circle_background - ) - libraryIcon?.background = resources.getDrawable(id, context.theme) - libraryItemTitle?.text = resources.getString( - getResourceId( - R.styleable.LibraryListItem_listItemTitle, - R.string.browser_menu_your_library - ) - ) - } finally { - recycle() - } - } + context.withStyledAttributes(attrs, R.styleable.LibraryListItem, defStyleAttr, 0) { + val id = getResourceId( + R.styleable.LibraryListItem_listItemIcon, + R.drawable.library_icon_reading_list_circle_background + ) + libraryIcon?.background = resources.getDrawable(id, context.theme) + libraryItemTitle?.text = resources.getString( + getResourceId( + R.styleable.LibraryListItem_listItemTitle, + R.string.browser_menu_your_library + ) + ) } } } diff --git a/app/src/main/java/org/mozilla/fenix/settings/AccountAuthErrorPreference.kt b/app/src/main/java/org/mozilla/fenix/settings/AccountAuthErrorPreference.kt index dca3e3df0..277c64580 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/AccountAuthErrorPreference.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/AccountAuthErrorPreference.kt @@ -12,13 +12,13 @@ import androidx.preference.Preference import androidx.preference.PreferenceViewHolder import org.mozilla.fenix.R -class AccountAuthErrorPreference : Preference { +class AccountAuthErrorPreference @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + attributeSetId: Int = 0 +) : Preference(context, attrs, attributeSetId) { var email: String? = null - constructor(context: Context) : super(context) - constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - constructor(context: Context, attrs: AttributeSet?, attributeSetId: Int) : super(context, attrs, attributeSetId) - init { layoutResource = R.layout.account_auth_error_preference } diff --git a/app/src/main/java/org/mozilla/fenix/settings/AccountPreference.kt b/app/src/main/java/org/mozilla/fenix/settings/AccountPreference.kt index 34e77f549..6e67b3775 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/AccountPreference.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/AccountPreference.kt @@ -12,14 +12,14 @@ import androidx.preference.Preference import androidx.preference.PreferenceViewHolder import org.mozilla.fenix.R -class AccountPreference : Preference { +class AccountPreference @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + attributeSetId: Int = 0 +) : Preference(context, attrs, attributeSetId) { var displayName: String? = null var email: String? = null - constructor(context: Context) : super(context) - constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - constructor(context: Context, attrs: AttributeSet?, attributeSetId: Int) : super(context, attrs, attributeSetId) - init { layoutResource = R.layout.account_preference } diff --git a/app/src/main/java/org/mozilla/fenix/settings/DefaultBrowserPreference.kt b/app/src/main/java/org/mozilla/fenix/settings/DefaultBrowserPreference.kt index 58d851d87..eec2c095f 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/DefaultBrowserPreference.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/DefaultBrowserPreference.kt @@ -12,18 +12,14 @@ import androidx.preference.PreferenceViewHolder import mozilla.components.support.utils.Browsers import org.mozilla.fenix.R -class DefaultBrowserPreference : Preference { +class DefaultBrowserPreference @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + attributeSetId: Int = 0 +) : Preference(context, attrs, attributeSetId) { private var switchView: Switch? = null - constructor(context: Context) : super(context) - constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - constructor(context: Context, attrs: AttributeSet?, attributeSetId: Int) : super( - context, - attrs, - attributeSetId - ) - init { widgetLayoutResource = R.layout.preference_default_browser } diff --git a/app/src/main/java/org/mozilla/fenix/settings/DeleteBrowsingDataItem.kt b/app/src/main/java/org/mozilla/fenix/settings/DeleteBrowsingDataItem.kt index 629097325..e1a7d749b 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/DeleteBrowsingDataItem.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/DeleteBrowsingDataItem.kt @@ -9,6 +9,7 @@ import android.util.AttributeSet import android.view.LayoutInflater import android.widget.TextView import androidx.constraintlayout.widget.ConstraintLayout +import androidx.core.content.withStyledAttributes import kotlinx.android.synthetic.main.delete_browsing_data_item.view.* import org.mozilla.fenix.R @@ -41,33 +42,23 @@ class DeleteBrowsingDataItem @JvmOverloads constructor( onCheckListener?.invoke(isChecked) } - attrs.let { - context.theme.obtainStyledAttributes( - it, - R.styleable.DeleteBrowsingDataItem, - 0, 0 - ).apply { - try { - val iconId = getResourceId( - R.styleable.DeleteBrowsingDataItem_deleteBrowsingDataItemIcon, - R.drawable.library_icon_reading_list_circle_background - ) - val titleId = getResourceId( - R.styleable.DeleteBrowsingDataItem_deleteBrowsingDataItemTitle, - R.string.browser_menu_your_library - ) - val subtitleId = getResourceId( - R.styleable.DeleteBrowsingDataItem_deleteBrowsingDataItemSubtitle, - R.string.browser_menu_your_library - ) + context.withStyledAttributes(attrs, R.styleable.DeleteBrowsingDataItem, defStyleAttr, 0) { + val iconId = getResourceId( + R.styleable.DeleteBrowsingDataItem_deleteBrowsingDataItemIcon, + R.drawable.library_icon_reading_list_circle_background + ) + val titleId = getResourceId( + R.styleable.DeleteBrowsingDataItem_deleteBrowsingDataItemTitle, + R.string.browser_menu_your_library + ) + val subtitleId = getResourceId( + R.styleable.DeleteBrowsingDataItem_deleteBrowsingDataItemSubtitle, + R.string.browser_menu_your_library + ) - icon.background = resources.getDrawable(iconId, context.theme) - title.text = resources.getString(titleId) - subtitle.text = resources.getString(subtitleId) - } finally { - recycle() - } - } + icon.background = resources.getDrawable(iconId, context.theme) + title.text = resources.getString(titleId) + subtitle.text = resources.getString(subtitleId) } } } diff --git a/app/src/main/java/org/mozilla/fenix/settings/RadioButtonPreference.kt b/app/src/main/java/org/mozilla/fenix/settings/RadioButtonPreference.kt index 5906788e5..2a192c81e 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/RadioButtonPreference.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/RadioButtonPreference.kt @@ -5,7 +5,6 @@ package org.mozilla.fenix.settings import android.content.Context -import android.content.res.TypedArray import android.text.TextUtils import android.util.AttributeSet import android.view.View @@ -13,6 +12,7 @@ import android.widget.RadioButton import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.content.res.TypedArrayUtils +import androidx.core.content.withStyledAttributes import androidx.core.text.HtmlCompat import androidx.preference.Preference import androidx.preference.PreferenceViewHolder @@ -20,7 +20,11 @@ import org.mozilla.fenix.R import org.mozilla.fenix.ThemeManager import org.mozilla.fenix.utils.Settings -class RadioButtonPreference : Preference { +class RadioButtonPreference @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + attributeSetId: Int = 0 +) : Preference(context, attrs, attributeSetId) { private val radioGroups = mutableListOf() private lateinit var summaryView: TextView private lateinit var radioButton: RadioButton @@ -30,6 +34,27 @@ class RadioButtonPreference : Preference { init { layoutResource = R.layout.preference_widget_radiobutton + + context.withStyledAttributes( + attrs, + androidx.preference.R.styleable.Preference, + TypedArrayUtils.getAttr( + context, androidx.preference.R.attr.preferenceStyle, android.R.attr.preferenceStyle + ), + 0 + ) { + if (hasValue(androidx.preference.R.styleable.Preference_defaultValue)) { + defaultValue = getBoolean( + androidx.preference.R.styleable.Preference_defaultValue, + false + ) + } else if (hasValue(androidx.preference.R.styleable.Preference_android_defaultValue)) { + defaultValue = getBoolean( + androidx.preference.R.styleable.Preference_android_defaultValue, + false + ) + } + } } /* In devices with Android 6, when we use android:button="@null" android:drawableStart doesn't work via xml @@ -45,16 +70,6 @@ class RadioButtonPreference : Preference { this.setCompoundDrawables(buttonDrawable, null, null, null) } - constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { - val typedArray = context.obtainStyledAttributes( - attrs, androidx.preference.R.styleable.Preference, TypedArrayUtils.getAttr( - context, androidx.preference.R.attr.preferenceStyle, - android.R.attr.preferenceStyle - ), 0 - ) - initDefaultValue(typedArray) - } - fun addToRadioGroup(radioPreference: RadioButtonPreference) { radioGroups.add(radioPreference) } @@ -94,20 +109,6 @@ class RadioButtonPreference : Preference { radioButton.setStartCheckedIndicator() } - private fun initDefaultValue(typedArray: TypedArray) { - if (typedArray.hasValue(androidx.preference.R.styleable.Preference_defaultValue)) { - defaultValue = typedArray.getBoolean( - androidx.preference.R.styleable.Preference_defaultValue, - false - ) - } else if (typedArray.hasValue(androidx.preference.R.styleable.Preference_android_defaultValue)) { - defaultValue = typedArray.getBoolean( - androidx.preference.R.styleable.Preference_android_defaultValue, - false - ) - } - } - private fun toggleRadioGroups() { if (radioButton.isChecked) { radioGroups.forEach { it.updateRadioValue(false) } diff --git a/app/src/main/java/org/mozilla/fenix/settings/RadioSearchEngineListPreference.kt b/app/src/main/java/org/mozilla/fenix/settings/RadioSearchEngineListPreference.kt index 2c59312e4..ad58ed73c 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/RadioSearchEngineListPreference.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/RadioSearchEngineListPreference.kt @@ -12,18 +12,14 @@ import org.mozilla.fenix.R import org.mozilla.fenix.ext.components import org.mozilla.fenix.utils.Settings -class RadioSearchEngineListPreference : SearchEngineListPreference { +class RadioSearchEngineListPreference @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0 +) : SearchEngineListPreference(context, attrs, defStyleAttr) { override val itemResId: Int get() = R.layout.search_engine_radio_button - constructor(context: Context, attrs: AttributeSet) : super(context, attrs) - - constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super( - context, - attrs, - defStyleAttr - ) - override fun updateDefaultItem(defaultButton: CompoundButton) { defaultButton.isChecked = true } diff --git a/app/src/main/java/org/mozilla/fenix/settings/SearchEngineListPreference.kt b/app/src/main/java/org/mozilla/fenix/settings/SearchEngineListPreference.kt index 05336b42c..8c052dca1 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/SearchEngineListPreference.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/SearchEngineListPreference.kt @@ -28,7 +28,11 @@ import org.mozilla.fenix.ext.components import org.mozilla.fenix.utils.Settings import kotlin.coroutines.CoroutineContext -abstract class SearchEngineListPreference : Preference, CompoundButton.OnCheckedChangeListener, CoroutineScope { +abstract class SearchEngineListPreference @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0 +) : Preference(context, attrs, defStyleAttr), CompoundButton.OnCheckedChangeListener, CoroutineScope { private val job = Job() override val coroutineContext: CoroutineContext get() = job + Dispatchers.Main @@ -37,15 +41,7 @@ abstract class SearchEngineListPreference : Preference, CompoundButton.OnChecked protected abstract val itemResId: Int - constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { - layoutResource = R.layout.preference_search_engine_chooser - } - - constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super( - context, - attrs, - defStyleAttr - ) { + init { layoutResource = R.layout.preference_search_engine_chooser }