2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-09 19:10:42 +00:00

For #1843 - Passes selected tabs through the MVI loop

This commit is contained in:
Jeff Boek 2019-04-22 22:07:15 -07:00 committed by Emily Kager
parent 9df64b61d4
commit ff08d0dbb0
7 changed files with 85 additions and 18 deletions

View File

@ -19,14 +19,18 @@ data class Tab(
val title: String val title: String
) )
data class CollectionCreationState(val tabs: List<Tab> = listOf()) : ViewState data class CollectionCreationState(val tabs: List<Tab> = listOf(), val selectedTabs: List<Tab> = listOf()) : ViewState
sealed class CollectionCreationChange : Change { sealed class CollectionCreationChange : Change {
data class TabListChange(val tabs: List<Tab>) : CollectionCreationChange() data class TabListChange(val tabs: List<Tab>) : CollectionCreationChange()
data class TabAdded(val tab: Tab) : CollectionCreationChange()
data class TabRemoved(val tab: Tab) : CollectionCreationChange()
} }
sealed class CollectionCreationAction : Action { sealed class CollectionCreationAction : Action {
object Close : CollectionCreationAction() object Close : CollectionCreationAction()
data class AddTabToSelection(val tab: Tab) : CollectionCreationAction()
data class RemoveTabFromSelection(val tab: Tab) : CollectionCreationAction()
} }
class CollectionCreationComponent( class CollectionCreationComponent(
@ -40,6 +44,14 @@ class CollectionCreationComponent(
override val reducer: Reducer<CollectionCreationState, CollectionCreationChange> = { state, change -> override val reducer: Reducer<CollectionCreationState, CollectionCreationChange> = { state, change ->
when (change) { when (change) {
is CollectionCreationChange.TabListChange -> state.copy(tabs = change.tabs) is CollectionCreationChange.TabListChange -> state.copy(tabs = change.tabs)
is CollectionCreationChange.TabAdded -> {
val selectedTabs = listOf(change.tab) + state.selectedTabs
state.copy(selectedTabs = selectedTabs)
}
is CollectionCreationChange.TabRemoved -> {
val selectedTabs = state.selectedTabs.filter { it.sessionId != change.tab.sessionId }
state.copy(selectedTabs = selectedTabs)
}
} }
} }

View File

@ -3,10 +3,11 @@ package org.mozilla.fenix.collections
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.CompoundButton
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import io.reactivex.Observer import io.reactivex.Observer
import kotlinx.android.synthetic.main.collection_tab_list_row.view.* import kotlinx.android.synthetic.main.collection_tab_list_row.view.*
import kotlinx.android.synthetic.main.tab_list_row.*
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
@ -21,7 +22,8 @@ class CollectionCreationTabListAdapter(
) : RecyclerView.Adapter<TabViewHolder>() { ) : RecyclerView.Adapter<TabViewHolder>() {
private var data: List<Tab> = listOf() private var tabs: List<Tab> = listOf()
private var selectedTabs: List<Tab> = listOf()
private lateinit var job: Job private lateinit var job: Job
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TabViewHolder {
@ -31,10 +33,12 @@ class CollectionCreationTabListAdapter(
} }
override fun onBindViewHolder(holder: TabViewHolder, position: Int) { override fun onBindViewHolder(holder: TabViewHolder, position: Int) {
holder.bind(data[position]) val tab = tabs[position]
val isSelected = selectedTabs.contains(tab)
holder.bind(tab, isSelected)
} }
override fun getItemCount(): Int = data.size override fun getItemCount(): Int = tabs.size
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView) super.onAttachedToRecyclerView(recyclerView)
@ -46,12 +50,27 @@ class CollectionCreationTabListAdapter(
job.cancel() job.cancel()
} }
fun updateData(tabs: List<Tab>) { fun updateData(tabs: List<Tab>, selectedTabs: List<Tab>) {
data = tabs val diffUtil = DiffUtil.calculateDiff(TabDiffUtil(this.tabs, tabs))
notifyDataSetChanged()
this.tabs = tabs
this.selectedTabs = selectedTabs
diffUtil.dispatchUpdatesTo(this)
} }
} }
private class TabDiffUtil(val old: List<Tab>, val new: List<Tab>) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean =
old[oldItemPosition].sessionId == new[newItemPosition].sessionId
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean =
old[oldItemPosition].url == new[newItemPosition].url
override fun getOldListSize(): Int = old.size
override fun getNewListSize(): Int = new.size
}
class TabViewHolder( class TabViewHolder(
val view: View, val view: View,
actionEmitter: Observer<CollectionCreationAction>, actionEmitter: Observer<CollectionCreationAction>,
@ -62,15 +81,34 @@ class TabViewHolder(
override val coroutineContext: CoroutineContext override val coroutineContext: CoroutineContext
get() = Dispatchers.IO + job get() = Dispatchers.IO + job
var tab: Tab? = null private var tab: Tab? = null
private val checkbox = view.tab_selected_checkbox!!
private val checkboxListener = CompoundButton.OnCheckedChangeListener { _, isChecked ->
tab?.apply {
val action = if (isChecked) CollectionCreationAction.AddTabToSelection(this)
else CollectionCreationAction.RemoveTabFromSelection(this)
init { } actionEmitter.onNext(action)
}
}
fun bind(tab: Tab) { init {
view.collection_item_tab.setOnClickListener {
checkbox.isChecked = !checkbox.isChecked
}
}
fun bind(tab: Tab, isSelected: Boolean) {
this.tab = tab this.tab = tab
view.hostname.text = tab.hostname view.hostname.text = tab.hostname
view.tab_title.text = tab.title view.tab_title.text = tab.title
checkbox.setOnCheckedChangeListener(null)
if (checkbox.isChecked != isSelected) {
checkbox.isChecked = isSelected
}
checkbox.setOnCheckedChangeListener(checkboxListener)
launch(Dispatchers.IO) { launch(Dispatchers.IO) {
val bitmap = view.favicon_image.context.components.utils.icons val bitmap = view.favicon_image.context.components.utils.icons
.loadIcon(IconRequest(tab.url)).await().bitmap .loadIcon(IconRequest(tab.url)).await().bitmap

View File

@ -46,6 +46,6 @@ class CollectionCreationUIView(
} }
override fun updateView() = Consumer<CollectionCreationState> { override fun updateView() = Consumer<CollectionCreationState> {
collectionCreationTabListAdapter.updateData(it.tabs) collectionCreationTabListAdapter.updateData(it.tabs, it.selectedTabs)
} }
} }

View File

@ -4,10 +4,7 @@ package org.mozilla.fenix.collections
License, v. 2.0. If a copy of the MPL was not distributed with this 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/. */ file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -52,7 +49,11 @@ class CreateCollectionFragment : DialogFragment() {
getAutoDisposeObservable<CollectionCreationAction>().subscribe { getAutoDisposeObservable<CollectionCreationAction>().subscribe {
when (it) { when (it) {
is CollectionCreationAction.Close -> dismissAllowingStateLoss() is CollectionCreationAction.Close -> dismiss()
is CollectionCreationAction.AddTabToSelection -> getManagedEmitter<CollectionCreationChange>()
.onNext(CollectionCreationChange.TabAdded(it.tab))
is CollectionCreationAction.RemoveTabFromSelection -> getManagedEmitter<CollectionCreationChange>()
.onNext(CollectionCreationChange.TabRemoved(it.tab))
} }
} }
} }

View File

@ -106,7 +106,7 @@ class HistoryListItemViewHolder(
// This prevent us from cutting off the animation // This prevent us from cutting off the animation
val shouldCheck = mode.selectedItems.contains(item) val shouldCheck = mode.selectedItems.contains(item)
if (checkbox.isChecked != shouldCheck) { if (checkbox.isChecked != shouldCheck) {
checkbox.isChecked = mode.selectedItems.contains(item) checkbox.isChecked = shouldCheck
} }
checkbox.setOnCheckedChangeListener(checkListener) checkbox.setOnCheckedChangeListener(checkListener)
} }

View File

@ -64,5 +64,16 @@
app:layout_constraintEnd_toEndOf="@id/hostname" app:layout_constraintEnd_toEndOf="@id/hostname"
app:layout_constraintTop_toBottomOf="@id/hostname" app:layout_constraintTop_toBottomOf="@id/hostname"
app:layout_constraintBottom_toBottomOf="parent"/> app:layout_constraintBottom_toBottomOf="parent"/>
<CheckBox
android:id="@+id/tab_selected_checkbox"
android:clickable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:elevation="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView> </androidx.cardview.widget.CardView>

View File

@ -110,6 +110,11 @@
<style name="CreateCollectionDialogStyle" parent="Theme.AppCompat.DayNight.NoActionBar"> <style name="CreateCollectionDialogStyle" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:colorControlNormal">?accentBright</item>
<item name="android:colorControlActivated">?accentBright</item>
<item name="android:colorControlHighlight">?accent</item>
<item name="android:textAppearance">@style/TextAppearance.AppCompat</item>
<item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowAnimationStyle">@style/Animation.Design.BottomSheetDialog</item> <item name="android:windowAnimationStyle">@style/Animation.Design.BottomSheetDialog</item>
<item name="windowNoTitle">true</item> <item name="windowNoTitle">true</item>