2019-04-06 06:24:28 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
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.home.sessioncontrol
|
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import io.reactivex.Observer
|
|
|
|
import kotlinx.coroutines.Job
|
2019-04-20 23:15:18 +02:00
|
|
|
import org.mozilla.fenix.home.sessioncontrol.viewholders.SaveTabGroupViewHolder
|
2019-04-06 06:24:28 +02:00
|
|
|
import org.mozilla.fenix.home.sessioncontrol.viewholders.DeleteTabsViewHolder
|
2019-04-19 20:56:12 +02:00
|
|
|
import org.mozilla.fenix.home.sessioncontrol.viewholders.NoTabMessageViewHolder
|
2019-04-06 06:24:28 +02:00
|
|
|
import org.mozilla.fenix.home.sessioncontrol.viewholders.PrivateBrowsingDescriptionViewHolder
|
|
|
|
import org.mozilla.fenix.home.sessioncontrol.viewholders.TabHeaderViewHolder
|
|
|
|
import org.mozilla.fenix.home.sessioncontrol.viewholders.TabViewHolder
|
|
|
|
import java.lang.IllegalStateException
|
|
|
|
|
|
|
|
sealed class AdapterItem {
|
|
|
|
object TabHeader : AdapterItem()
|
2019-04-19 20:56:12 +02:00
|
|
|
object NoTabMessage : AdapterItem()
|
2019-04-06 06:24:28 +02:00
|
|
|
data class TabItem(val tab: Tab) : AdapterItem()
|
|
|
|
object PrivateBrowsingDescription : AdapterItem()
|
2019-04-20 23:15:18 +02:00
|
|
|
object SaveTabGroup : AdapterItem()
|
2019-04-06 06:24:28 +02:00
|
|
|
object DeleteTabs : AdapterItem()
|
|
|
|
|
|
|
|
val viewType: Int
|
|
|
|
get() = when (this) {
|
|
|
|
TabHeader -> TabHeaderViewHolder.LAYOUT_ID
|
2019-04-19 20:56:12 +02:00
|
|
|
NoTabMessage -> NoTabMessageViewHolder.LAYOUT_ID
|
2019-04-06 06:24:28 +02:00
|
|
|
is TabItem -> TabViewHolder.LAYOUT_ID
|
2019-04-20 23:15:18 +02:00
|
|
|
SaveTabGroup -> SaveTabGroupViewHolder.LAYOUT_ID
|
2019-04-06 06:24:28 +02:00
|
|
|
PrivateBrowsingDescription -> PrivateBrowsingDescriptionViewHolder.LAYOUT_ID
|
|
|
|
DeleteTabs -> DeleteTabsViewHolder.LAYOUT_ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SessionControlAdapter(
|
|
|
|
private val actionEmitter: Observer<SessionControlAction>
|
|
|
|
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
|
|
|
|
|
|
|
var items: List<AdapterItem> = listOf()
|
|
|
|
private lateinit var job: Job
|
|
|
|
|
|
|
|
fun reloadData(items: List<AdapterItem>) {
|
|
|
|
this.items = items
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method triggers the ComplexMethod lint error when in fact it's quite simple.
|
|
|
|
@SuppressWarnings("ComplexMethod")
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
|
|
|
val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
|
|
|
|
return when (viewType) {
|
|
|
|
TabHeaderViewHolder.LAYOUT_ID -> TabHeaderViewHolder(view, actionEmitter)
|
2019-04-19 20:56:12 +02:00
|
|
|
NoTabMessageViewHolder.LAYOUT_ID -> NoTabMessageViewHolder(view)
|
2019-04-06 06:24:28 +02:00
|
|
|
TabViewHolder.LAYOUT_ID -> TabViewHolder(view, actionEmitter, job)
|
2019-04-20 23:15:18 +02:00
|
|
|
SaveTabGroupViewHolder.LAYOUT_ID -> SaveTabGroupViewHolder(view, actionEmitter)
|
2019-04-12 02:17:10 +02:00
|
|
|
PrivateBrowsingDescriptionViewHolder.LAYOUT_ID -> PrivateBrowsingDescriptionViewHolder(
|
|
|
|
view,
|
|
|
|
actionEmitter
|
|
|
|
)
|
2019-04-06 06:24:28 +02:00
|
|
|
DeleteTabsViewHolder.LAYOUT_ID -> DeleteTabsViewHolder(view, actionEmitter)
|
|
|
|
else -> throw IllegalStateException()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
|
|
|
|
super.onAttachedToRecyclerView(recyclerView)
|
|
|
|
job = Job()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
|
|
|
|
super.onDetachedFromRecyclerView(recyclerView)
|
|
|
|
job.cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getItemViewType(position: Int) = items[position].viewType
|
|
|
|
|
|
|
|
override fun getItemCount(): Int = items.size
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
|
|
|
when (holder) {
|
2019-04-12 02:17:10 +02:00
|
|
|
is TabViewHolder -> holder.bindSession(
|
2019-04-23 17:19:35 +02:00
|
|
|
(items[position] as AdapterItem.TabItem).tab
|
2019-04-12 02:17:10 +02:00
|
|
|
)
|
2019-04-06 06:24:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|