2019-04-30 00:52:55 +02:00
|
|
|
package org.mozilla.fenix.collections
|
|
|
|
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2019-05-31 19:59:51 +02:00
|
|
|
import androidx.core.content.ContextCompat
|
2019-04-30 00:52:55 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import io.reactivex.Observer
|
|
|
|
import kotlinx.android.synthetic.main.collections_list_item.view.*
|
2019-05-31 19:59:51 +02:00
|
|
|
import kotlinx.android.synthetic.main.collections_list_item.view.collection_description
|
|
|
|
import kotlinx.android.synthetic.main.collections_list_item.view.collection_icon
|
2019-04-30 00:52:55 +02:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.Job
|
|
|
|
import org.mozilla.fenix.R
|
2019-05-31 19:59:51 +02:00
|
|
|
import org.mozilla.fenix.ext.urlToTrimmedHost
|
2019-05-23 19:11:22 +02:00
|
|
|
import org.mozilla.fenix.home.sessioncontrol.Tab
|
2019-05-06 20:20:19 +02:00
|
|
|
import org.mozilla.fenix.home.sessioncontrol.TabCollection
|
2019-04-30 00:52:55 +02:00
|
|
|
import kotlin.coroutines.CoroutineContext
|
|
|
|
|
|
|
|
class SaveCollectionListAdapter(
|
|
|
|
val actionEmitter: Observer<CollectionCreationAction>
|
|
|
|
) : RecyclerView.Adapter<CollectionViewHolder>() {
|
|
|
|
|
|
|
|
private lateinit var job: Job
|
2019-05-16 23:35:15 +02:00
|
|
|
private var tabCollections = listOf<TabCollection>()
|
2019-05-23 19:11:22 +02:00
|
|
|
private var selectedTabs: Set<Tab> = setOf()
|
2019-04-30 00:52:55 +02:00
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CollectionViewHolder {
|
|
|
|
val view = LayoutInflater.from(parent.context)
|
|
|
|
.inflate(CollectionViewHolder.LAYOUT_ID, parent, false)
|
|
|
|
|
|
|
|
return CollectionViewHolder(view, actionEmitter, job)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: CollectionViewHolder, position: Int) {
|
2019-05-16 23:35:15 +02:00
|
|
|
val collection = tabCollections[position]
|
2019-04-30 00:52:55 +02:00
|
|
|
holder.bind(collection)
|
2019-05-23 19:11:22 +02:00
|
|
|
holder.view.setOnClickListener {
|
|
|
|
collection.apply {
|
|
|
|
val action = CollectionCreationAction.SelectCollection(this, selectedTabs.toList())
|
|
|
|
actionEmitter.onNext(action)
|
|
|
|
}
|
|
|
|
}
|
2019-04-30 00:52:55 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 23:35:15 +02:00
|
|
|
override fun getItemCount(): Int = tabCollections.size
|
2019-04-30 00:52:55 +02:00
|
|
|
|
|
|
|
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
|
|
|
|
super.onAttachedToRecyclerView(recyclerView)
|
|
|
|
job = Job()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
|
|
|
|
super.onDetachedFromRecyclerView(recyclerView)
|
|
|
|
job.cancel()
|
|
|
|
}
|
2019-05-16 23:35:15 +02:00
|
|
|
|
2019-05-23 19:11:22 +02:00
|
|
|
fun updateData(tabCollections: List<TabCollection>, selectedTabs: Set<Tab>) {
|
2019-05-16 23:35:15 +02:00
|
|
|
this.tabCollections = tabCollections
|
2019-05-23 19:11:22 +02:00
|
|
|
this.selectedTabs = selectedTabs
|
2019-05-16 23:35:15 +02:00
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
2019-04-30 00:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class CollectionViewHolder(
|
|
|
|
val view: View,
|
|
|
|
actionEmitter: Observer<CollectionCreationAction>,
|
|
|
|
val job: Job
|
|
|
|
) :
|
|
|
|
RecyclerView.ViewHolder(view), CoroutineScope {
|
|
|
|
|
|
|
|
override val coroutineContext: CoroutineContext
|
|
|
|
get() = Dispatchers.IO + job
|
|
|
|
|
2019-05-06 20:20:19 +02:00
|
|
|
private var collection: TabCollection? = null
|
2019-04-30 00:52:55 +02:00
|
|
|
|
2019-05-06 20:20:19 +02:00
|
|
|
fun bind(collection: TabCollection) {
|
2019-04-30 00:52:55 +02:00
|
|
|
this.collection = collection
|
|
|
|
view.collection_item.text = collection.title
|
2019-05-31 19:59:51 +02:00
|
|
|
|
|
|
|
val hostNameList = collection.tabs.map { it.url.urlToTrimmedHost().capitalize() }
|
|
|
|
|
|
|
|
var tabsDisplayed = 0
|
|
|
|
val tabTitlesList = hostNameList.joinToString(", ") {
|
|
|
|
if (it.length > maxTitleLength) {
|
|
|
|
it.substring(
|
|
|
|
0,
|
|
|
|
maxTitleLength
|
|
|
|
) + "..."
|
|
|
|
} else {
|
|
|
|
tabsDisplayed += 1
|
|
|
|
it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
view.collection_description.text = tabTitlesList
|
|
|
|
|
|
|
|
view.collection_icon.setColorFilter(
|
|
|
|
ContextCompat.getColor(
|
|
|
|
view.context,
|
|
|
|
getIconColor(collection.id)
|
|
|
|
),
|
|
|
|
android.graphics.PorterDuff.Mode.SRC_IN
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("ComplexMethod", "MagicNumber")
|
|
|
|
private fun getIconColor(id: Long): Int {
|
|
|
|
return when ((id % 4).toInt()) {
|
|
|
|
0 -> R.color.collection_icon_color_violet
|
|
|
|
1 -> R.color.collection_icon_color_blue
|
|
|
|
2 -> R.color.collection_icon_color_pink
|
|
|
|
3 -> R.color.collection_icon_color_green
|
|
|
|
4 -> R.color.collection_icon_color_yellow
|
|
|
|
else -> R.color.white_color
|
|
|
|
}
|
2019-04-30 00:52:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val LAYOUT_ID = R.layout.collections_list_item
|
2019-05-31 19:59:51 +02:00
|
|
|
const val maxTitleLength = 20
|
2019-04-30 00:52:55 +02:00
|
|
|
}
|
|
|
|
}
|