2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-17 15:26:23 +00:00

[fenix] Use @JvmOverloads instead of multiple constructors

This commit is contained in:
Tiger Oakes 2019-06-23 13:26:32 -04:00 committed by Emily Kager
parent 0466bb6c55
commit 9ad4274d20
8 changed files with 83 additions and 112 deletions

View File

@ -8,6 +8,7 @@ import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.LayoutInflater import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.withStyledAttributes
import kotlinx.android.synthetic.main.library_list_item.view.* import kotlinx.android.synthetic.main.library_list_item.view.*
import org.mozilla.fenix.R import org.mozilla.fenix.R
@ -19,13 +20,7 @@ class LibraryListItem @JvmOverloads constructor(
init { init {
LayoutInflater.from(context).inflate(R.layout.library_list_item, this, true) LayoutInflater.from(context).inflate(R.layout.library_list_item, this, true)
attrs.let { context.withStyledAttributes(attrs, R.styleable.LibraryListItem, defStyleAttr, 0) {
context.theme.obtainStyledAttributes(
it,
R.styleable.LibraryListItem,
0, 0
).apply {
try {
val id = getResourceId( val id = getResourceId(
R.styleable.LibraryListItem_listItemIcon, R.styleable.LibraryListItem_listItemIcon,
R.drawable.library_icon_reading_list_circle_background R.drawable.library_icon_reading_list_circle_background
@ -37,10 +32,6 @@ class LibraryListItem @JvmOverloads constructor(
R.string.browser_menu_your_library R.string.browser_menu_your_library
) )
) )
} finally {
recycle()
}
}
} }
} }
} }

View File

@ -12,13 +12,13 @@ import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder import androidx.preference.PreferenceViewHolder
import org.mozilla.fenix.R 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 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 { init {
layoutResource = R.layout.account_auth_error_preference layoutResource = R.layout.account_auth_error_preference
} }

View File

@ -12,14 +12,14 @@ import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder import androidx.preference.PreferenceViewHolder
import org.mozilla.fenix.R 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 displayName: String? = null
var email: 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 { init {
layoutResource = R.layout.account_preference layoutResource = R.layout.account_preference
} }

View File

@ -12,18 +12,14 @@ import androidx.preference.PreferenceViewHolder
import mozilla.components.support.utils.Browsers import mozilla.components.support.utils.Browsers
import org.mozilla.fenix.R 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 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 { init {
widgetLayoutResource = R.layout.preference_default_browser widgetLayoutResource = R.layout.preference_default_browser
} }

View File

@ -9,6 +9,7 @@ import android.util.AttributeSet
import android.view.LayoutInflater import android.view.LayoutInflater
import android.widget.TextView import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.withStyledAttributes
import kotlinx.android.synthetic.main.delete_browsing_data_item.view.* import kotlinx.android.synthetic.main.delete_browsing_data_item.view.*
import org.mozilla.fenix.R import org.mozilla.fenix.R
@ -41,13 +42,7 @@ class DeleteBrowsingDataItem @JvmOverloads constructor(
onCheckListener?.invoke(isChecked) onCheckListener?.invoke(isChecked)
} }
attrs.let { context.withStyledAttributes(attrs, R.styleable.DeleteBrowsingDataItem, defStyleAttr, 0) {
context.theme.obtainStyledAttributes(
it,
R.styleable.DeleteBrowsingDataItem,
0, 0
).apply {
try {
val iconId = getResourceId( val iconId = getResourceId(
R.styleable.DeleteBrowsingDataItem_deleteBrowsingDataItemIcon, R.styleable.DeleteBrowsingDataItem_deleteBrowsingDataItemIcon,
R.drawable.library_icon_reading_list_circle_background R.drawable.library_icon_reading_list_circle_background
@ -64,10 +59,6 @@ class DeleteBrowsingDataItem @JvmOverloads constructor(
icon.background = resources.getDrawable(iconId, context.theme) icon.background = resources.getDrawable(iconId, context.theme)
title.text = resources.getString(titleId) title.text = resources.getString(titleId)
subtitle.text = resources.getString(subtitleId) subtitle.text = resources.getString(subtitleId)
} finally {
recycle()
}
}
} }
} }
} }

View File

@ -5,7 +5,6 @@
package org.mozilla.fenix.settings package org.mozilla.fenix.settings
import android.content.Context import android.content.Context
import android.content.res.TypedArray
import android.text.TextUtils import android.text.TextUtils
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View import android.view.View
@ -13,6 +12,7 @@ import android.widget.RadioButton
import android.widget.TextView import android.widget.TextView
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.res.TypedArrayUtils import androidx.core.content.res.TypedArrayUtils
import androidx.core.content.withStyledAttributes
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder import androidx.preference.PreferenceViewHolder
@ -20,7 +20,11 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.ThemeManager import org.mozilla.fenix.ThemeManager
import org.mozilla.fenix.utils.Settings 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<RadioButtonPreference>() private val radioGroups = mutableListOf<RadioButtonPreference>()
private lateinit var summaryView: TextView private lateinit var summaryView: TextView
private lateinit var radioButton: RadioButton private lateinit var radioButton: RadioButton
@ -30,6 +34,27 @@ class RadioButtonPreference : Preference {
init { init {
layoutResource = R.layout.preference_widget_radiobutton 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 /* 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) 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) { fun addToRadioGroup(radioPreference: RadioButtonPreference) {
radioGroups.add(radioPreference) radioGroups.add(radioPreference)
} }
@ -94,20 +109,6 @@ class RadioButtonPreference : Preference {
radioButton.setStartCheckedIndicator() 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() { private fun toggleRadioGroups() {
if (radioButton.isChecked) { if (radioButton.isChecked) {
radioGroups.forEach { it.updateRadioValue(false) } radioGroups.forEach { it.updateRadioValue(false) }

View File

@ -12,18 +12,14 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.components
import org.mozilla.fenix.utils.Settings 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 override val itemResId: Int
get() = R.layout.search_engine_radio_button 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) { override fun updateDefaultItem(defaultButton: CompoundButton) {
defaultButton.isChecked = true defaultButton.isChecked = true
} }

View File

@ -28,7 +28,11 @@ import org.mozilla.fenix.ext.components
import org.mozilla.fenix.utils.Settings import org.mozilla.fenix.utils.Settings
import kotlin.coroutines.CoroutineContext 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() private val job = Job()
override val coroutineContext: CoroutineContext override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main get() = job + Dispatchers.Main
@ -37,15 +41,7 @@ abstract class SearchEngineListPreference : Preference, CompoundButton.OnChecked
protected abstract val itemResId: Int protected abstract val itemResId: Int
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { init {
layoutResource = R.layout.preference_search_engine_chooser
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
layoutResource = R.layout.preference_search_engine_chooser layoutResource = R.layout.preference_search_engine_chooser
} }