2019-05-07 22:28:38 +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/. */
|
2019-05-07 22:28:38 +02:00
|
|
|
|
|
|
|
package org.mozilla.fenix.exceptions
|
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import io.reactivex.Observer
|
|
|
|
import org.mozilla.fenix.exceptions.viewholders.ExceptionsDeleteButtonViewHolder
|
|
|
|
import org.mozilla.fenix.exceptions.viewholders.ExceptionsHeaderViewHolder
|
|
|
|
import org.mozilla.fenix.exceptions.viewholders.ExceptionsListItemViewHolder
|
2019-07-01 20:34:25 +02:00
|
|
|
import org.mozilla.fenix.utils.AdapterWithJob
|
2019-05-07 22:28:38 +02:00
|
|
|
|
|
|
|
private sealed class AdapterItem {
|
|
|
|
object DeleteButton : AdapterItem()
|
|
|
|
object Header : AdapterItem()
|
|
|
|
data class Item(val item: ExceptionsItem) : AdapterItem()
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ExceptionsList(val exceptions: List<ExceptionsItem>) {
|
|
|
|
val items: List<AdapterItem>
|
|
|
|
init {
|
|
|
|
val items = mutableListOf<AdapterItem>()
|
|
|
|
items.add(AdapterItem.Header)
|
|
|
|
for (exception in exceptions) {
|
|
|
|
items.add(AdapterItem.Item(exception))
|
|
|
|
}
|
|
|
|
items.add(AdapterItem.DeleteButton)
|
|
|
|
this.items = items
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ExceptionsAdapter(
|
|
|
|
private val actionEmitter: Observer<ExceptionsAction>
|
2019-07-01 20:34:25 +02:00
|
|
|
) : AdapterWithJob<RecyclerView.ViewHolder>() {
|
2019-05-07 22:28:38 +02:00
|
|
|
private var exceptionsList: ExceptionsList = ExceptionsList(emptyList())
|
|
|
|
|
|
|
|
fun updateData(items: List<ExceptionsItem>) {
|
|
|
|
this.exceptionsList = ExceptionsList(items)
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getItemCount(): Int = exceptionsList.items.size
|
|
|
|
|
|
|
|
override fun getItemViewType(position: Int): Int {
|
|
|
|
return when (exceptionsList.items[position]) {
|
|
|
|
is AdapterItem.DeleteButton -> ExceptionsDeleteButtonViewHolder.LAYOUT_ID
|
|
|
|
is AdapterItem.Header -> ExceptionsHeaderViewHolder.LAYOUT_ID
|
|
|
|
is AdapterItem.Item -> ExceptionsListItemViewHolder.LAYOUT_ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
|
|
|
val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
|
|
|
|
|
|
|
|
return when (viewType) {
|
|
|
|
ExceptionsDeleteButtonViewHolder.LAYOUT_ID -> ExceptionsDeleteButtonViewHolder(view, actionEmitter)
|
|
|
|
ExceptionsHeaderViewHolder.LAYOUT_ID -> ExceptionsHeaderViewHolder(view)
|
2019-07-01 20:34:25 +02:00
|
|
|
ExceptionsListItemViewHolder.LAYOUT_ID -> ExceptionsListItemViewHolder(view, actionEmitter, adapterJob)
|
2019-05-07 22:28:38 +02:00
|
|
|
else -> throw IllegalStateException()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
|
|
|
when (holder) {
|
|
|
|
is ExceptionsListItemViewHolder -> (exceptionsList.items[position] as AdapterItem.Item).also {
|
|
|
|
holder.bind(it.item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|