mirror of
https://github.com/fork-maintainers/iceraven-browser
synced 2024-11-15 18:12:54 +00:00
[fenix] For https://github.com/mozilla-mobile/fenix/issues/17917: Use View binding in collections
This commit is contained in:
parent
12541dfb2a
commit
c441adb0d8
@ -12,11 +12,11 @@ import android.view.ViewGroup
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.fragment.navArgs
|
||||
import kotlinx.android.synthetic.main.fragment_create_collection.view.*
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import mozilla.components.lib.state.ext.consumeFrom
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.components.StoreProvider
|
||||
import org.mozilla.fenix.databinding.FragmentCreateCollectionBinding
|
||||
import org.mozilla.fenix.ext.requireComponents
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
@ -25,6 +25,9 @@ class CollectionCreationFragment : DialogFragment() {
|
||||
private lateinit var collectionCreationStore: CollectionCreationStore
|
||||
private lateinit var collectionCreationInteractor: CollectionCreationInteractor
|
||||
|
||||
private var _binding: FragmentCreateCollectionBinding? = null
|
||||
private val binding get() = _binding!!
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
isCancelable = false
|
||||
@ -35,8 +38,8 @@ class CollectionCreationFragment : DialogFragment() {
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
val view = inflater.inflate(R.layout.fragment_create_collection, container, false)
|
||||
): View {
|
||||
_binding = FragmentCreateCollectionBinding.inflate(inflater, container, false)
|
||||
val args: CollectionCreationFragmentArgs by navArgs()
|
||||
|
||||
collectionCreationStore = StoreProvider.get(this) {
|
||||
@ -63,11 +66,11 @@ class CollectionCreationFragment : DialogFragment() {
|
||||
)
|
||||
)
|
||||
collectionCreationView = CollectionCreationView(
|
||||
view.createCollectionWrapper,
|
||||
binding.createCollectionWrapper,
|
||||
collectionCreationInteractor
|
||||
)
|
||||
|
||||
return view
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
@ -78,6 +81,11 @@ class CollectionCreationFragment : DialogFragment() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
collectionCreationView.onResumed()
|
||||
|
@ -5,14 +5,13 @@
|
||||
package org.mozilla.fenix.collections
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.view.isGone
|
||||
import androidx.core.view.isInvisible
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kotlinx.android.synthetic.main.collection_tab_list_row.*
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.databinding.CollectionTabListRowBinding
|
||||
import org.mozilla.fenix.ext.components
|
||||
import org.mozilla.fenix.ext.loadIntoView
|
||||
import org.mozilla.fenix.home.Tab
|
||||
@ -26,11 +25,16 @@ class CollectionCreationTabListAdapter(
|
||||
private var selectedTabs: MutableSet<Tab> = mutableSetOf()
|
||||
private var hideCheckboxes = false
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabViewHolder {
|
||||
val view = LayoutInflater.from(parent.context)
|
||||
.inflate(TabViewHolder.LAYOUT_ID, parent, false)
|
||||
private lateinit var binding: CollectionTabListRowBinding
|
||||
|
||||
return TabViewHolder(view)
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabViewHolder {
|
||||
binding = CollectionTabListRowBinding.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
parent,
|
||||
false
|
||||
)
|
||||
|
||||
return TabViewHolder(binding)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: TabViewHolder, position: Int, payloads: MutableList<Any>) {
|
||||
@ -41,11 +45,11 @@ class CollectionCreationTabListAdapter(
|
||||
is CheckChanged -> {
|
||||
val checkChanged = payloads[0] as CheckChanged
|
||||
if (checkChanged.shouldBeChecked) {
|
||||
holder.tab_selected_checkbox.isChecked = true
|
||||
binding.tabSelectedCheckbox.isChecked = true
|
||||
} else if (checkChanged.shouldBeUnchecked) {
|
||||
holder.tab_selected_checkbox.isChecked = false
|
||||
binding.tabSelectedCheckbox.isChecked = false
|
||||
}
|
||||
holder.tab_selected_checkbox.isGone = checkChanged.shouldHideCheckBox
|
||||
binding.tabSelectedCheckbox.isGone = checkChanged.shouldHideCheckBox
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -54,7 +58,7 @@ class CollectionCreationTabListAdapter(
|
||||
override fun onBindViewHolder(holder: TabViewHolder, position: Int) {
|
||||
val tab = tabs[position]
|
||||
val isSelected = selectedTabs.contains(tab)
|
||||
holder.tab_selected_checkbox.setOnCheckedChangeListener { _, isChecked ->
|
||||
binding.tabSelectedCheckbox.setOnCheckedChangeListener { _, isChecked ->
|
||||
if (isChecked) {
|
||||
selectedTabs.add(tab)
|
||||
interactor.addTabToSelection(tab)
|
||||
@ -88,24 +92,24 @@ class CollectionCreationTabListAdapter(
|
||||
}
|
||||
}
|
||||
|
||||
class TabViewHolder(view: View) : ViewHolder(view) {
|
||||
class TabViewHolder(private val binding: CollectionTabListRowBinding) : ViewHolder(binding.root) {
|
||||
|
||||
init {
|
||||
collection_item_tab.setOnClickListener {
|
||||
tab_selected_checkbox.isChecked = !tab_selected_checkbox.isChecked
|
||||
binding.collectionItemTab.setOnClickListener {
|
||||
binding.tabSelectedCheckbox.isChecked = !binding.tabSelectedCheckbox.isChecked
|
||||
}
|
||||
}
|
||||
|
||||
fun bind(tab: Tab, isSelected: Boolean, shouldHideCheckBox: Boolean) {
|
||||
hostname.text = tab.hostname
|
||||
tab_title.text = tab.title
|
||||
tab_selected_checkbox.isInvisible = shouldHideCheckBox
|
||||
binding.hostname.text = tab.hostname
|
||||
binding.tabTitle.text = tab.title
|
||||
binding.tabSelectedCheckbox.isInvisible = shouldHideCheckBox
|
||||
itemView.isClickable = !shouldHideCheckBox
|
||||
if (tab_selected_checkbox.isChecked != isSelected) {
|
||||
tab_selected_checkbox.isChecked = isSelected
|
||||
if (binding.tabSelectedCheckbox.isChecked != isSelected) {
|
||||
binding.tabSelectedCheckbox.isChecked = isSelected
|
||||
}
|
||||
|
||||
itemView.context.components.core.icons.loadIntoView(favicon_image, tab.url)
|
||||
itemView.context.components.core.icons.loadIntoView(binding.faviconImage, tab.url)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@ -19,12 +19,12 @@ import androidx.transition.AutoTransition
|
||||
import androidx.transition.Transition
|
||||
import androidx.transition.TransitionManager
|
||||
import kotlinx.android.extensions.LayoutContainer
|
||||
import kotlinx.android.synthetic.main.component_collection_creation.*
|
||||
import mozilla.components.feature.tab.collections.TabCollection
|
||||
import mozilla.components.support.ktx.android.view.hideKeyboard
|
||||
import mozilla.components.support.ktx.android.view.showKeyboard
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.components.metrics.Event
|
||||
import org.mozilla.fenix.databinding.ComponentCollectionCreationBinding
|
||||
import org.mozilla.fenix.ext.components
|
||||
import org.mozilla.fenix.ext.toShortUrl
|
||||
import org.mozilla.fenix.home.Tab
|
||||
@ -34,15 +34,19 @@ class CollectionCreationView(
|
||||
private val interactor: CollectionCreationInteractor
|
||||
) : LayoutContainer {
|
||||
|
||||
override val containerView: View = LayoutInflater.from(container.context)
|
||||
.inflate(R.layout.component_collection_creation, container, true)
|
||||
private val binding = ComponentCollectionCreationBinding.inflate(
|
||||
LayoutInflater.from(container.context),
|
||||
container,
|
||||
true
|
||||
)
|
||||
override val containerView: View = binding.root
|
||||
|
||||
private val bottomBarView = CollectionCreationBottomBarView(
|
||||
interactor = interactor,
|
||||
layout = bottom_button_bar_layout,
|
||||
iconButton = bottom_bar_icon_button,
|
||||
textView = bottom_bar_text,
|
||||
saveButton = save_button
|
||||
layout = binding.bottomButtonBarLayout,
|
||||
iconButton = binding.bottomBarIconButton,
|
||||
textView = binding.bottomBarText,
|
||||
saveButton = binding.saveButton
|
||||
)
|
||||
private val collectionCreationTabListAdapter = CollectionCreationTabListAdapter(interactor)
|
||||
private val collectionSaveListAdapter = SaveCollectionListAdapter(interactor)
|
||||
@ -58,10 +62,10 @@ class CollectionCreationView(
|
||||
|
||||
init {
|
||||
transition.duration = TRANSITION_DURATION
|
||||
transition.excludeTarget(back_button, true)
|
||||
transition.excludeTarget(binding.backButton, true)
|
||||
|
||||
name_collection_edittext.filters += InputFilter.LengthFilter(COLLECTION_NAME_MAX_LENGTH)
|
||||
name_collection_edittext.setOnEditorActionListener { view, actionId, _ ->
|
||||
binding.nameCollectionEdittext.filters += InputFilter.LengthFilter(COLLECTION_NAME_MAX_LENGTH)
|
||||
binding.nameCollectionEdittext.setOnEditorActionListener { view, actionId, _ ->
|
||||
val text = view.text.toString()
|
||||
if (actionId == EditorInfo.IME_ACTION_DONE && text.isNotBlank()) {
|
||||
when (step) {
|
||||
@ -75,13 +79,13 @@ class CollectionCreationView(
|
||||
false
|
||||
}
|
||||
|
||||
tab_list.run {
|
||||
binding.tabList.run {
|
||||
adapter = collectionCreationTabListAdapter
|
||||
itemAnimator = null
|
||||
layoutManager = LinearLayoutManager(containerView.context, RecyclerView.VERTICAL, true)
|
||||
}
|
||||
|
||||
collections_list.run {
|
||||
binding.collectionsList.run {
|
||||
adapter = collectionSaveListAdapter
|
||||
layoutManager = LinearLayoutManager(containerView.context, RecyclerView.VERTICAL, true)
|
||||
}
|
||||
@ -111,16 +115,16 @@ class CollectionCreationView(
|
||||
private fun updateForSelectTabs(state: CollectionCreationState) {
|
||||
containerView.context.components.analytics.metrics.track(Event.CollectionTabSelectOpened)
|
||||
|
||||
tab_list.isClickable = true
|
||||
binding.tabList.isClickable = true
|
||||
|
||||
back_button.apply {
|
||||
binding.backButton.apply {
|
||||
text = context.getString(R.string.create_collection_select_tabs)
|
||||
setOnClickListener {
|
||||
interactor.onBackPressed(SaveCollectionStep.SelectTabs)
|
||||
}
|
||||
}
|
||||
|
||||
select_all_button.apply {
|
||||
binding.selectAllButton.apply {
|
||||
val allSelected = state.selectedTabs.size == state.tabs.size
|
||||
text =
|
||||
if (allSelected) context.getString(R.string.create_collection_deselect_all)
|
||||
@ -136,39 +140,39 @@ class CollectionCreationView(
|
||||
R.layout.component_collection_creation
|
||||
)
|
||||
collectionCreationTabListAdapter.updateData(state.tabs, state.selectedTabs)
|
||||
selectTabsConstraints.applyTo(collection_constraint_layout)
|
||||
selectTabsConstraints.applyTo(binding.collectionConstraintLayout)
|
||||
}
|
||||
|
||||
private fun updateForSelectCollection() {
|
||||
tab_list.isClickable = false
|
||||
binding.tabList.isClickable = false
|
||||
selectCollectionConstraints.clone(
|
||||
containerView.context,
|
||||
R.layout.component_collection_creation_select_collection
|
||||
)
|
||||
selectCollectionConstraints.applyTo(collection_constraint_layout)
|
||||
selectCollectionConstraints.applyTo(binding.collectionConstraintLayout)
|
||||
|
||||
back_button.apply {
|
||||
binding.backButton.apply {
|
||||
text = context.getString(R.string.create_collection_select_collection)
|
||||
setOnClickListener {
|
||||
interactor.onBackPressed(SaveCollectionStep.SelectCollection)
|
||||
}
|
||||
}
|
||||
TransitionManager.beginDelayedTransition(collection_constraint_layout, transition)
|
||||
TransitionManager.beginDelayedTransition(binding.collectionConstraintLayout, transition)
|
||||
}
|
||||
|
||||
private fun updateForNameCollection(state: CollectionCreationState) {
|
||||
tab_list.isClickable = false
|
||||
binding.tabList.isClickable = false
|
||||
nameCollectionConstraints.clone(
|
||||
containerView.context,
|
||||
R.layout.component_collection_creation_name_collection
|
||||
)
|
||||
nameCollectionConstraints.applyTo(collection_constraint_layout)
|
||||
nameCollectionConstraints.applyTo(binding.collectionConstraintLayout)
|
||||
|
||||
collectionCreationTabListAdapter.updateData(state.selectedTabs.toList(), state.selectedTabs, true)
|
||||
back_button.apply {
|
||||
binding.backButton.apply {
|
||||
text = context.getString(R.string.create_collection_name_collection)
|
||||
setOnClickListener {
|
||||
name_collection_edittext.hideKeyboard()
|
||||
binding.nameCollectionEdittext.hideKeyboard()
|
||||
val handler = Handler(Looper.getMainLooper())
|
||||
handler.postDelayed(
|
||||
{
|
||||
@ -179,19 +183,19 @@ class CollectionCreationView(
|
||||
}
|
||||
}
|
||||
|
||||
name_collection_edittext.showKeyboard()
|
||||
binding.nameCollectionEdittext.showKeyboard()
|
||||
|
||||
name_collection_edittext.setText(
|
||||
binding.nameCollectionEdittext.setText(
|
||||
containerView.context.getString(
|
||||
R.string.create_collection_default_name,
|
||||
state.defaultCollectionNumber
|
||||
)
|
||||
)
|
||||
name_collection_edittext.setSelection(0, name_collection_edittext.text.length)
|
||||
binding.nameCollectionEdittext.setSelection(0, binding.nameCollectionEdittext.text.length)
|
||||
}
|
||||
|
||||
private fun updateForRenameCollection(state: CollectionCreationState) {
|
||||
tab_list.isClickable = false
|
||||
binding.tabList.isClickable = false
|
||||
|
||||
state.selectedTabCollection?.let { tabCollection ->
|
||||
val publicSuffixList = containerView.context.components.publicSuffixList
|
||||
@ -210,14 +214,14 @@ class CollectionCreationView(
|
||||
containerView.context,
|
||||
R.layout.component_collection_creation_name_collection
|
||||
)
|
||||
nameCollectionConstraints.applyTo(collection_constraint_layout)
|
||||
name_collection_edittext.setText(state.selectedTabCollection?.title)
|
||||
name_collection_edittext.setSelection(0, name_collection_edittext.text.length)
|
||||
nameCollectionConstraints.applyTo(binding.collectionConstraintLayout)
|
||||
binding.nameCollectionEdittext.setText(state.selectedTabCollection?.title)
|
||||
binding.nameCollectionEdittext.setSelection(0, binding.nameCollectionEdittext.text.length)
|
||||
|
||||
back_button.apply {
|
||||
binding.backButton.apply {
|
||||
text = context.getString(R.string.collection_rename)
|
||||
setOnClickListener {
|
||||
name_collection_edittext.hideKeyboard()
|
||||
binding.nameCollectionEdittext.hideKeyboard()
|
||||
val handler = Handler(Looper.getMainLooper())
|
||||
handler.postDelayed(
|
||||
{
|
||||
@ -231,7 +235,7 @@ class CollectionCreationView(
|
||||
override fun onTransitionStart(transition: Transition) { /* noop */ }
|
||||
|
||||
override fun onTransitionEnd(transition: Transition) {
|
||||
name_collection_edittext.showKeyboard()
|
||||
binding.nameCollectionEdittext.showKeyboard()
|
||||
transition.removeListener(this)
|
||||
}
|
||||
|
||||
@ -239,12 +243,12 @@ class CollectionCreationView(
|
||||
override fun onTransitionPause(transition: Transition) { /* noop */ }
|
||||
override fun onTransitionResume(transition: Transition) { /* noop */ }
|
||||
})
|
||||
TransitionManager.beginDelayedTransition(collection_constraint_layout, transition)
|
||||
TransitionManager.beginDelayedTransition(binding.collectionConstraintLayout, transition)
|
||||
}
|
||||
|
||||
fun onResumed() {
|
||||
if (step == SaveCollectionStep.NameCollection || step == SaveCollectionStep.RenameCollection) {
|
||||
name_collection_edittext.showKeyboard()
|
||||
binding.nameCollectionEdittext.showKeyboard()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,15 +5,14 @@
|
||||
package org.mozilla.fenix.collections
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.graphics.BlendModeColorFilterCompat.createBlendModeColorFilterCompat
|
||||
import androidx.core.graphics.BlendModeCompat.SRC_IN
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kotlinx.android.synthetic.main.collections_list_item.*
|
||||
import mozilla.components.feature.tab.collections.TabCollection
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.components.description
|
||||
import org.mozilla.fenix.databinding.CollectionsListItemBinding
|
||||
import org.mozilla.fenix.ext.getIconColor
|
||||
import org.mozilla.fenix.home.Tab
|
||||
import org.mozilla.fenix.utils.view.ViewHolder
|
||||
@ -26,10 +25,13 @@ class SaveCollectionListAdapter(
|
||||
private var selectedTabs: Set<Tab> = setOf()
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CollectionViewHolder {
|
||||
val view = LayoutInflater.from(parent.context)
|
||||
.inflate(CollectionViewHolder.LAYOUT_ID, parent, false)
|
||||
val binding = CollectionsListItemBinding.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
parent,
|
||||
false
|
||||
)
|
||||
|
||||
return CollectionViewHolder(view)
|
||||
return CollectionViewHolder(binding)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: CollectionViewHolder, position: Int) {
|
||||
@ -49,12 +51,12 @@ class SaveCollectionListAdapter(
|
||||
}
|
||||
}
|
||||
|
||||
class CollectionViewHolder(view: View) : ViewHolder(view) {
|
||||
class CollectionViewHolder(private val binding: CollectionsListItemBinding) : ViewHolder(binding.root) {
|
||||
|
||||
fun bind(collection: TabCollection) {
|
||||
collection_item.text = collection.title
|
||||
collection_description.text = collection.description(itemView.context)
|
||||
collection_icon.colorFilter =
|
||||
binding.collectionItem.text = collection.title
|
||||
binding.collectionDescription.text = collection.description(itemView.context)
|
||||
binding.collectionIcon.colorFilter =
|
||||
createBlendModeColorFilterCompat(collection.getIconColor(itemView.context), SRC_IN)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user