2019-05-02 17:53:40 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2019-07-12 20:38:15 +02:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.fenix.collections
|
2019-05-02 17:53:40 +02:00
|
|
|
|
2019-04-23 04:49:44 +02:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2019-08-20 19:45:41 +02:00
|
|
|
import androidx.core.view.isGone
|
|
|
|
import androidx.core.view.isInvisible
|
2019-04-23 07:07:15 +02:00
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
2019-04-23 04:49:44 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import kotlinx.android.synthetic.main.collection_tab_list_row.view.*
|
|
|
|
import org.mozilla.fenix.R
|
|
|
|
import org.mozilla.fenix.ext.components
|
2019-06-26 22:40:20 +02:00
|
|
|
import org.mozilla.fenix.ext.loadIntoView
|
2019-05-06 20:20:19 +02:00
|
|
|
import org.mozilla.fenix.home.sessioncontrol.Tab
|
2019-04-23 04:49:44 +02:00
|
|
|
|
|
|
|
class CollectionCreationTabListAdapter(
|
#4596 migrate collections (#5911)
* For #4596: move code from CollectionCreationComponent to CollectionCreationStore
Other than adding comments, no changes were made. The code will be updated in a following commit. This is in order to make the commit diff more readable.
* For 4596: update CollectionCreateStore to libstate
* For 4596: copied CollectionCreationUIView into CollectionCreationView
Otherwise, no code was changed. The next commit will update this code. This is in order to make the commit diff more readable.
* For 4596: update CollectionCreationView to LibState
Note that the minimal changes possible to enable migration were made. Refactoring will happen in a later commit.
* For 4596: updated CollectionCreationTabListAdapter to work with the new View
* For 4596: updated SaveCollectionListAdapter to work with the new View
* For 4596: implemented CollectionCreationController
For now, it has an identical interface to the interactor. In a later commit several of its responsibilities will be moved around, some to the interactor and some to the reducer
* For 4596: copied over previous reducer code
No other changes were made. The code will be updated in the following commit. This is done to make changes more readable for the reviewer
* For 4596: update reducer code param names
Otherwise, no changes at this time
* For 4596: add arguments to CreateCollectionFragment in nav_graph
These will be used to replace the current CreateCollectionViewModel, which shares data between fragments in a way that doesn't fit within our architecture.
* For 4596: pass arguments to collection via transaction instead of VM
The VM will be removed in a later commit
* For 4596: update BrowserToolbarController to share state to collection via its Direction
* For 4596: removed CreateCollectionViewModel
* For 4596: test tab retrieval in CreateCollectionFragment
* For 4596: fix crashing CreateCollectionFragmentTest
* For 4596: removed classes create collection classes used by old architecture
* For 4596: collection interactor rename + kdoc
* For 4596: moved collection interactor interface
* For 4596: renamed CreateCollectionFragment
All related classes followed the pattern of CollectionCreationX
* For 4596: kdoc CollectionCreationController
There's no effective difference between these calls and their interactor equivalent, so I linked to them
* For 4596: fix bug that caused rename to not work
* For 4596: removed unused collection actions
These were unused before the LibState refactor
* For 4596: kdoc StepChanged
* For 4596: removed todos about moving logic to the reducer
saveTabsToCollection: this could be moved, but that would involve creating a new action. SaveCollectionStep should probably be refactored out, so adding this layer of indirection seemed counterproductive
handleBackPress: needs to be able to call dismiss(). The reducer doesn't (and shouldn't) be able to do that, so this needs to live here
stepBack: called by handleBackPress. See above
* For 4596: wrote tests for CollectionCreationController#stepback
* For 4596: fixed tests broken by changes to collections
* For 4596: small readability refactor for CollectionController#stepBack
No change to functionality (see tests)
* For 4596: broke apart CollectionView#update
There's probably a lot more that could be done here, but smaller changes were made to reduce scope
* For 4596: remove unnecessary todos
It looks like we don't follow the suggested pattern in this project
* For 4596: test CollectionCreationController#normalSessionSize
* For 4596: updated naming in CollectionCreationController per review
2019-10-23 02:33:54 +02:00
|
|
|
private val interactor: CollectionCreationInteractor
|
2019-06-26 22:40:20 +02:00
|
|
|
) : RecyclerView.Adapter<TabViewHolder>() {
|
2019-04-23 07:07:15 +02:00
|
|
|
private var tabs: List<Tab> = listOf()
|
2019-05-31 19:59:51 +02:00
|
|
|
private var selectedTabs: MutableSet<Tab> = mutableSetOf()
|
2019-05-02 17:53:40 +02:00
|
|
|
private var hideCheckboxes = false
|
2019-04-23 04:49:44 +02:00
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabViewHolder {
|
2019-05-02 17:53:40 +02:00
|
|
|
val view =
|
|
|
|
LayoutInflater.from(parent.context).inflate(TabViewHolder.LAYOUT_ID, parent, false)
|
2019-04-23 04:49:44 +02:00
|
|
|
|
2019-06-26 22:40:20 +02:00
|
|
|
return TabViewHolder(view)
|
2019-05-31 19:59:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: TabViewHolder, position: Int, payloads: MutableList<Any>) {
|
|
|
|
if (payloads.isEmpty()) {
|
|
|
|
super.onBindViewHolder(holder, position, payloads)
|
|
|
|
} else {
|
|
|
|
when (payloads[0]) {
|
|
|
|
is CheckChanged -> {
|
|
|
|
val checkChanged = payloads[0] as CheckChanged
|
|
|
|
if (checkChanged.shouldBeChecked) {
|
2019-06-26 22:40:20 +02:00
|
|
|
holder.itemView.tab_selected_checkbox.isChecked = true
|
2019-05-31 19:59:51 +02:00
|
|
|
} else if (checkChanged.shouldBeUnchecked) {
|
2019-06-26 22:40:20 +02:00
|
|
|
holder.itemView.tab_selected_checkbox.isChecked = false
|
2019-05-31 19:59:51 +02:00
|
|
|
}
|
2019-08-20 19:45:41 +02:00
|
|
|
holder.itemView.tab_selected_checkbox.isGone = checkChanged.shouldHideCheckBox
|
2019-05-31 19:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 04:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: TabViewHolder, position: Int) {
|
2019-04-23 07:07:15 +02:00
|
|
|
val tab = tabs[position]
|
|
|
|
val isSelected = selectedTabs.contains(tab)
|
2019-06-26 22:40:20 +02:00
|
|
|
holder.itemView.tab_selected_checkbox.setOnCheckedChangeListener { _, isChecked ->
|
#4596 migrate collections (#5911)
* For #4596: move code from CollectionCreationComponent to CollectionCreationStore
Other than adding comments, no changes were made. The code will be updated in a following commit. This is in order to make the commit diff more readable.
* For 4596: update CollectionCreateStore to libstate
* For 4596: copied CollectionCreationUIView into CollectionCreationView
Otherwise, no code was changed. The next commit will update this code. This is in order to make the commit diff more readable.
* For 4596: update CollectionCreationView to LibState
Note that the minimal changes possible to enable migration were made. Refactoring will happen in a later commit.
* For 4596: updated CollectionCreationTabListAdapter to work with the new View
* For 4596: updated SaveCollectionListAdapter to work with the new View
* For 4596: implemented CollectionCreationController
For now, it has an identical interface to the interactor. In a later commit several of its responsibilities will be moved around, some to the interactor and some to the reducer
* For 4596: copied over previous reducer code
No other changes were made. The code will be updated in the following commit. This is done to make changes more readable for the reviewer
* For 4596: update reducer code param names
Otherwise, no changes at this time
* For 4596: add arguments to CreateCollectionFragment in nav_graph
These will be used to replace the current CreateCollectionViewModel, which shares data between fragments in a way that doesn't fit within our architecture.
* For 4596: pass arguments to collection via transaction instead of VM
The VM will be removed in a later commit
* For 4596: update BrowserToolbarController to share state to collection via its Direction
* For 4596: removed CreateCollectionViewModel
* For 4596: test tab retrieval in CreateCollectionFragment
* For 4596: fix crashing CreateCollectionFragmentTest
* For 4596: removed classes create collection classes used by old architecture
* For 4596: collection interactor rename + kdoc
* For 4596: moved collection interactor interface
* For 4596: renamed CreateCollectionFragment
All related classes followed the pattern of CollectionCreationX
* For 4596: kdoc CollectionCreationController
There's no effective difference between these calls and their interactor equivalent, so I linked to them
* For 4596: fix bug that caused rename to not work
* For 4596: removed unused collection actions
These were unused before the LibState refactor
* For 4596: kdoc StepChanged
* For 4596: removed todos about moving logic to the reducer
saveTabsToCollection: this could be moved, but that would involve creating a new action. SaveCollectionStep should probably be refactored out, so adding this layer of indirection seemed counterproductive
handleBackPress: needs to be able to call dismiss(). The reducer doesn't (and shouldn't) be able to do that, so this needs to live here
stepBack: called by handleBackPress. See above
* For 4596: wrote tests for CollectionCreationController#stepback
* For 4596: fixed tests broken by changes to collections
* For 4596: small readability refactor for CollectionController#stepBack
No change to functionality (see tests)
* For 4596: broke apart CollectionView#update
There's probably a lot more that could be done here, but smaller changes were made to reduce scope
* For 4596: remove unnecessary todos
It looks like we don't follow the suggested pattern in this project
* For 4596: test CollectionCreationController#normalSessionSize
* For 4596: updated naming in CollectionCreationController per review
2019-10-23 02:33:54 +02:00
|
|
|
if (isChecked) {
|
2019-05-31 19:59:51 +02:00
|
|
|
selectedTabs.add(tab)
|
#4596 migrate collections (#5911)
* For #4596: move code from CollectionCreationComponent to CollectionCreationStore
Other than adding comments, no changes were made. The code will be updated in a following commit. This is in order to make the commit diff more readable.
* For 4596: update CollectionCreateStore to libstate
* For 4596: copied CollectionCreationUIView into CollectionCreationView
Otherwise, no code was changed. The next commit will update this code. This is in order to make the commit diff more readable.
* For 4596: update CollectionCreationView to LibState
Note that the minimal changes possible to enable migration were made. Refactoring will happen in a later commit.
* For 4596: updated CollectionCreationTabListAdapter to work with the new View
* For 4596: updated SaveCollectionListAdapter to work with the new View
* For 4596: implemented CollectionCreationController
For now, it has an identical interface to the interactor. In a later commit several of its responsibilities will be moved around, some to the interactor and some to the reducer
* For 4596: copied over previous reducer code
No other changes were made. The code will be updated in the following commit. This is done to make changes more readable for the reviewer
* For 4596: update reducer code param names
Otherwise, no changes at this time
* For 4596: add arguments to CreateCollectionFragment in nav_graph
These will be used to replace the current CreateCollectionViewModel, which shares data between fragments in a way that doesn't fit within our architecture.
* For 4596: pass arguments to collection via transaction instead of VM
The VM will be removed in a later commit
* For 4596: update BrowserToolbarController to share state to collection via its Direction
* For 4596: removed CreateCollectionViewModel
* For 4596: test tab retrieval in CreateCollectionFragment
* For 4596: fix crashing CreateCollectionFragmentTest
* For 4596: removed classes create collection classes used by old architecture
* For 4596: collection interactor rename + kdoc
* For 4596: moved collection interactor interface
* For 4596: renamed CreateCollectionFragment
All related classes followed the pattern of CollectionCreationX
* For 4596: kdoc CollectionCreationController
There's no effective difference between these calls and their interactor equivalent, so I linked to them
* For 4596: fix bug that caused rename to not work
* For 4596: removed unused collection actions
These were unused before the LibState refactor
* For 4596: kdoc StepChanged
* For 4596: removed todos about moving logic to the reducer
saveTabsToCollection: this could be moved, but that would involve creating a new action. SaveCollectionStep should probably be refactored out, so adding this layer of indirection seemed counterproductive
handleBackPress: needs to be able to call dismiss(). The reducer doesn't (and shouldn't) be able to do that, so this needs to live here
stepBack: called by handleBackPress. See above
* For 4596: wrote tests for CollectionCreationController#stepback
* For 4596: fixed tests broken by changes to collections
* For 4596: small readability refactor for CollectionController#stepBack
No change to functionality (see tests)
* For 4596: broke apart CollectionView#update
There's probably a lot more that could be done here, but smaller changes were made to reduce scope
* For 4596: remove unnecessary todos
It looks like we don't follow the suggested pattern in this project
* For 4596: test CollectionCreationController#normalSessionSize
* For 4596: updated naming in CollectionCreationController per review
2019-10-23 02:33:54 +02:00
|
|
|
interactor.addTabToSelection(tab)
|
2019-05-31 19:59:51 +02:00
|
|
|
} else {
|
|
|
|
selectedTabs.remove(tab)
|
#4596 migrate collections (#5911)
* For #4596: move code from CollectionCreationComponent to CollectionCreationStore
Other than adding comments, no changes were made. The code will be updated in a following commit. This is in order to make the commit diff more readable.
* For 4596: update CollectionCreateStore to libstate
* For 4596: copied CollectionCreationUIView into CollectionCreationView
Otherwise, no code was changed. The next commit will update this code. This is in order to make the commit diff more readable.
* For 4596: update CollectionCreationView to LibState
Note that the minimal changes possible to enable migration were made. Refactoring will happen in a later commit.
* For 4596: updated CollectionCreationTabListAdapter to work with the new View
* For 4596: updated SaveCollectionListAdapter to work with the new View
* For 4596: implemented CollectionCreationController
For now, it has an identical interface to the interactor. In a later commit several of its responsibilities will be moved around, some to the interactor and some to the reducer
* For 4596: copied over previous reducer code
No other changes were made. The code will be updated in the following commit. This is done to make changes more readable for the reviewer
* For 4596: update reducer code param names
Otherwise, no changes at this time
* For 4596: add arguments to CreateCollectionFragment in nav_graph
These will be used to replace the current CreateCollectionViewModel, which shares data between fragments in a way that doesn't fit within our architecture.
* For 4596: pass arguments to collection via transaction instead of VM
The VM will be removed in a later commit
* For 4596: update BrowserToolbarController to share state to collection via its Direction
* For 4596: removed CreateCollectionViewModel
* For 4596: test tab retrieval in CreateCollectionFragment
* For 4596: fix crashing CreateCollectionFragmentTest
* For 4596: removed classes create collection classes used by old architecture
* For 4596: collection interactor rename + kdoc
* For 4596: moved collection interactor interface
* For 4596: renamed CreateCollectionFragment
All related classes followed the pattern of CollectionCreationX
* For 4596: kdoc CollectionCreationController
There's no effective difference between these calls and their interactor equivalent, so I linked to them
* For 4596: fix bug that caused rename to not work
* For 4596: removed unused collection actions
These were unused before the LibState refactor
* For 4596: kdoc StepChanged
* For 4596: removed todos about moving logic to the reducer
saveTabsToCollection: this could be moved, but that would involve creating a new action. SaveCollectionStep should probably be refactored out, so adding this layer of indirection seemed counterproductive
handleBackPress: needs to be able to call dismiss(). The reducer doesn't (and shouldn't) be able to do that, so this needs to live here
stepBack: called by handleBackPress. See above
* For 4596: wrote tests for CollectionCreationController#stepback
* For 4596: fixed tests broken by changes to collections
* For 4596: small readability refactor for CollectionController#stepBack
No change to functionality (see tests)
* For 4596: broke apart CollectionView#update
There's probably a lot more that could be done here, but smaller changes were made to reduce scope
* For 4596: remove unnecessary todos
It looks like we don't follow the suggested pattern in this project
* For 4596: test CollectionCreationController#normalSessionSize
* For 4596: updated naming in CollectionCreationController per review
2019-10-23 02:33:54 +02:00
|
|
|
interactor.removeTabFromSelection(tab)
|
2019-05-31 19:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-11 23:38:32 +02:00
|
|
|
holder.bind(tab, isSelected, hideCheckboxes)
|
2019-04-23 04:49:44 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 07:07:15 +02:00
|
|
|
override fun getItemCount(): Int = tabs.size
|
2019-04-23 04:49:44 +02:00
|
|
|
|
2019-05-02 17:53:40 +02:00
|
|
|
fun updateData(tabs: List<Tab>, selectedTabs: Set<Tab>, hideCheckboxes: Boolean = false) {
|
|
|
|
val diffUtil = DiffUtil.calculateDiff(
|
|
|
|
TabDiffUtil(
|
|
|
|
this.tabs,
|
|
|
|
tabs,
|
|
|
|
this.selectedTabs,
|
|
|
|
selectedTabs,
|
|
|
|
this.hideCheckboxes,
|
|
|
|
hideCheckboxes
|
|
|
|
)
|
|
|
|
)
|
2019-04-23 07:07:15 +02:00
|
|
|
|
|
|
|
this.tabs = tabs
|
2019-05-31 19:59:51 +02:00
|
|
|
this.selectedTabs = selectedTabs.toMutableSet()
|
2019-05-02 17:53:40 +02:00
|
|
|
this.hideCheckboxes = hideCheckboxes
|
2019-04-23 07:07:15 +02:00
|
|
|
|
|
|
|
diffUtil.dispatchUpdatesTo(this)
|
2019-04-23 04:49:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-23 19:50:24 +02:00
|
|
|
private class TabDiffUtil(
|
|
|
|
val old: List<Tab>,
|
|
|
|
val new: List<Tab>,
|
|
|
|
val oldSelected: Set<Tab>,
|
2019-05-02 17:53:40 +02:00
|
|
|
val newSelected: Set<Tab>,
|
|
|
|
val oldHideCheckboxes: Boolean,
|
|
|
|
val newHideCheckboxes: Boolean
|
2019-04-23 19:50:24 +02:00
|
|
|
) : DiffUtil.Callback() {
|
2019-04-23 07:07:15 +02:00
|
|
|
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean =
|
|
|
|
old[oldItemPosition].sessionId == new[newItemPosition].sessionId
|
|
|
|
|
2019-04-23 19:50:24 +02:00
|
|
|
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
|
2019-05-31 19:59:51 +02:00
|
|
|
val isSameTab = old[oldItemPosition].sessionId == new[newItemPosition].sessionId
|
|
|
|
val sameSelectedState = oldSelected.contains(old[oldItemPosition]) == newSelected.contains(new[newItemPosition])
|
2019-05-02 17:53:40 +02:00
|
|
|
val isSameHideCheckboxes = oldHideCheckboxes == newHideCheckboxes
|
|
|
|
return isSameTab && sameSelectedState && isSameHideCheckboxes
|
2019-04-23 19:50:24 +02:00
|
|
|
}
|
2019-04-23 07:07:15 +02:00
|
|
|
|
2019-05-31 19:59:51 +02:00
|
|
|
override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
|
|
|
|
val shouldBeChecked = newSelected.contains(new[newItemPosition]) && !oldSelected.contains(old[oldItemPosition])
|
|
|
|
val shouldBeUnchecked =
|
|
|
|
!newSelected.contains(new[newItemPosition]) && oldSelected.contains(old[oldItemPosition])
|
2019-06-13 18:57:04 +02:00
|
|
|
return CheckChanged(shouldBeChecked, shouldBeUnchecked, newHideCheckboxes)
|
2019-05-31 19:59:51 +02:00
|
|
|
}
|
|
|
|
|
2019-04-23 07:07:15 +02:00
|
|
|
override fun getOldListSize(): Int = old.size
|
|
|
|
override fun getNewListSize(): Int = new.size
|
|
|
|
}
|
|
|
|
|
2019-06-13 18:57:04 +02:00
|
|
|
data class CheckChanged(val shouldBeChecked: Boolean, val shouldBeUnchecked: Boolean, val shouldHideCheckBox: Boolean)
|
2019-05-31 19:59:51 +02:00
|
|
|
|
2019-06-26 22:40:20 +02:00
|
|
|
class TabViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
2019-04-23 04:49:44 +02:00
|
|
|
|
2019-04-23 07:07:15 +02:00
|
|
|
private val checkbox = view.tab_selected_checkbox!!
|
2019-04-23 04:49:44 +02:00
|
|
|
|
2019-04-23 07:07:15 +02:00
|
|
|
init {
|
|
|
|
view.collection_item_tab.setOnClickListener {
|
|
|
|
checkbox.isChecked = !checkbox.isChecked
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 04:49:44 +02:00
|
|
|
|
2019-05-02 17:53:40 +02:00
|
|
|
fun bind(tab: Tab, isSelected: Boolean, shouldHideCheckBox: Boolean) {
|
2019-06-26 22:40:20 +02:00
|
|
|
itemView.hostname.text = tab.hostname
|
|
|
|
itemView.tab_title.text = tab.title
|
2019-08-20 19:45:41 +02:00
|
|
|
checkbox.isInvisible = shouldHideCheckBox
|
2019-06-26 22:40:20 +02:00
|
|
|
itemView.isClickable = !shouldHideCheckBox
|
2019-04-23 07:07:15 +02:00
|
|
|
if (checkbox.isChecked != isSelected) {
|
|
|
|
checkbox.isChecked = isSelected
|
|
|
|
}
|
|
|
|
|
2019-06-26 22:40:20 +02:00
|
|
|
itemView.context.components.core.icons.loadIntoView(itemView.favicon_image, tab.url)
|
2019-04-23 04:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val LAYOUT_ID = R.layout.collection_tab_list_row
|
|
|
|
}
|
2019-04-23 07:07:15 +02:00
|
|
|
}
|