2019-05-06 20:20:19 +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.viewholders
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import io.reactivex.Observer
|
|
|
|
import kotlinx.android.extensions.LayoutContainer
|
|
|
|
import kotlinx.android.synthetic.main.collection_home_list_row.*
|
|
|
|
import kotlinx.android.synthetic.main.collection_home_list_row.view.*
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.Job
|
|
|
|
import mozilla.components.browser.menu.BrowserMenu
|
|
|
|
import mozilla.components.browser.menu.BrowserMenuBuilder
|
|
|
|
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
|
|
|
|
import org.mozilla.fenix.R
|
2019-05-31 00:49:58 +02:00
|
|
|
import org.mozilla.fenix.ThemeManager
|
2019-05-25 02:06:17 +02:00
|
|
|
import org.mozilla.fenix.ext.increaseTapArea
|
2019-05-16 23:02:24 +02:00
|
|
|
import org.mozilla.fenix.ext.urlToTrimmedHost
|
2019-05-06 20:20:19 +02:00
|
|
|
import org.mozilla.fenix.home.sessioncontrol.CollectionAction
|
|
|
|
import org.mozilla.fenix.home.sessioncontrol.SessionControlAction
|
|
|
|
import org.mozilla.fenix.home.sessioncontrol.TabCollection
|
|
|
|
import org.mozilla.fenix.home.sessioncontrol.onNext
|
|
|
|
import kotlin.coroutines.CoroutineContext
|
|
|
|
|
|
|
|
class CollectionViewHolder(
|
|
|
|
val view: View,
|
|
|
|
val actionEmitter: Observer<SessionControlAction>,
|
|
|
|
val job: Job,
|
|
|
|
override val containerView: View? = view
|
|
|
|
) :
|
|
|
|
RecyclerView.ViewHolder(view), LayoutContainer, CoroutineScope {
|
|
|
|
|
|
|
|
override val coroutineContext: CoroutineContext
|
|
|
|
get() = Dispatchers.IO + job
|
|
|
|
|
|
|
|
private lateinit var collection: TabCollection
|
2019-05-16 23:02:24 +02:00
|
|
|
private var expanded = false
|
2019-05-06 20:20:19 +02:00
|
|
|
private var collectionMenu: CollectionItemMenu
|
|
|
|
|
|
|
|
init {
|
|
|
|
collectionMenu = CollectionItemMenu(view.context) {
|
|
|
|
when (it) {
|
|
|
|
is CollectionItemMenu.Item.DeleteCollection -> actionEmitter.onNext(CollectionAction.Delete(collection))
|
|
|
|
is CollectionItemMenu.Item.AddTab -> actionEmitter.onNext(CollectionAction.AddTab(collection))
|
|
|
|
is CollectionItemMenu.Item.RenameCollection -> actionEmitter.onNext(CollectionAction.Rename(collection))
|
|
|
|
is CollectionItemMenu.Item.OpenTabs -> actionEmitter.onNext(CollectionAction.OpenTabs(collection))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
collection_overflow_button.run {
|
2019-05-25 02:06:17 +02:00
|
|
|
increaseTapArea(buttonIncreaseDps)
|
2019-05-06 20:20:19 +02:00
|
|
|
setOnClickListener {
|
2019-06-04 17:37:57 +02:00
|
|
|
val location = IntArray(2)
|
|
|
|
it.getLocationInWindow(location)
|
2019-05-06 20:20:19 +02:00
|
|
|
collectionMenu.menuBuilder
|
|
|
|
.build(view.context)
|
2019-06-04 17:37:57 +02:00
|
|
|
.show(
|
|
|
|
anchor = it,
|
|
|
|
orientation = if (location[1] > (rootView.measuredHeight / 2))
|
|
|
|
BrowserMenu.Orientation.UP else
|
|
|
|
BrowserMenu.Orientation.DOWN
|
|
|
|
)
|
2019-05-06 20:20:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
collection_share_button.run {
|
2019-05-25 02:06:17 +02:00
|
|
|
increaseTapArea(buttonIncreaseDps)
|
2019-05-06 20:20:19 +02:00
|
|
|
setOnClickListener {
|
|
|
|
actionEmitter.onNext(CollectionAction.ShareTabs(collection))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
view.setOnClickListener {
|
2019-05-18 01:26:20 +02:00
|
|
|
handleExpansion(expanded)
|
2019-05-06 20:20:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 23:02:24 +02:00
|
|
|
fun bindSession(collection: TabCollection, expanded: Boolean) {
|
2019-05-06 20:20:19 +02:00
|
|
|
this.collection = collection
|
2019-05-16 23:02:24 +02:00
|
|
|
this.expanded = expanded
|
2019-05-06 20:20:19 +02:00
|
|
|
updateCollectionUI()
|
2019-05-18 01:26:20 +02:00
|
|
|
|
|
|
|
// See #2625 for why we're invalidating
|
|
|
|
view.invalidate()
|
2019-05-06 20:20:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateCollectionUI() {
|
2019-05-21 23:31:45 +02:00
|
|
|
view.collection_title.text = collection.title
|
2019-05-06 20:20:19 +02:00
|
|
|
|
2019-05-21 20:03:32 +02:00
|
|
|
val hostNameList = collection.tabs.map { it.url.urlToTrimmedHost().capitalize() }
|
2019-05-06 20:20:19 +02:00
|
|
|
|
|
|
|
var tabsDisplayed = 0
|
2019-05-17 23:44:11 +02:00
|
|
|
val tabTitlesList = hostNameList.joinToString(", ") {
|
2019-05-06 20:20:19 +02:00
|
|
|
if (it.length > maxTitleLength) {
|
2019-05-18 01:26:20 +02:00
|
|
|
it.substring(
|
|
|
|
0,
|
2019-05-06 20:20:19 +02:00
|
|
|
maxTitleLength
|
|
|
|
) + "..."
|
|
|
|
} else {
|
|
|
|
tabsDisplayed += 1
|
|
|
|
it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 23:44:11 +02:00
|
|
|
view.collection_description.text = tabTitlesList
|
2019-05-06 20:20:19 +02:00
|
|
|
|
2019-05-16 23:02:24 +02:00
|
|
|
if (expanded) {
|
2019-05-06 20:20:19 +02:00
|
|
|
(view.layoutParams as ViewGroup.MarginLayoutParams).bottomMargin = 0
|
|
|
|
collection_title.setPadding(0, 0, 0, EXPANDED_PADDING)
|
|
|
|
view.background = ContextCompat.getDrawable(view.context, R.drawable.rounded_top_corners)
|
|
|
|
view.collection_description.visibility = View.GONE
|
2019-05-21 23:31:45 +02:00
|
|
|
|
2019-05-25 02:06:17 +02:00
|
|
|
view.chevron.setBackgroundResource(R.drawable.ic_chevron_up)
|
2019-05-06 20:20:19 +02:00
|
|
|
} else {
|
|
|
|
(view.layoutParams as ViewGroup.MarginLayoutParams).bottomMargin = COLLAPSED_MARGIN
|
|
|
|
view.background = ContextCompat.getDrawable(view.context, R.drawable.rounded_all_corners)
|
|
|
|
view.collection_description.visibility = View.VISIBLE
|
2019-05-21 23:31:45 +02:00
|
|
|
|
2019-05-25 02:06:17 +02:00
|
|
|
view.chevron.setBackgroundResource(R.drawable.ic_chevron_down)
|
2019-05-06 20:20:19 +02:00
|
|
|
}
|
2019-05-18 01:26:20 +02:00
|
|
|
|
|
|
|
view.collection_icon.setColorFilter(
|
|
|
|
ContextCompat.getColor(
|
|
|
|
view.context,
|
|
|
|
getIconColor(collection.id)
|
|
|
|
),
|
|
|
|
android.graphics.PorterDuff.Mode.SRC_IN
|
|
|
|
)
|
2019-05-06 20:20:19 +02:00
|
|
|
}
|
|
|
|
|
2019-05-18 01:26:20 +02:00
|
|
|
private fun handleExpansion(isExpanded: Boolean) {
|
|
|
|
if (isExpanded) {
|
|
|
|
actionEmitter.onNext(CollectionAction.Collapse(collection))
|
|
|
|
} else {
|
|
|
|
actionEmitter.onNext(CollectionAction.Expand(collection))
|
2019-05-06 20:20:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("ComplexMethod", "MagicNumber")
|
2019-05-18 01:26:20 +02:00
|
|
|
private fun getIconColor(id: Long): Int {
|
|
|
|
val sessionColorIndex = (id % 4).toInt()
|
|
|
|
return when (sessionColorIndex) {
|
|
|
|
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-05-06 20:20:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
2019-05-25 02:06:17 +02:00
|
|
|
const val buttonIncreaseDps = 16
|
2019-05-06 20:20:19 +02:00
|
|
|
const val EXPANDED_PADDING = 60
|
|
|
|
const val COLLAPSED_MARGIN = 12
|
|
|
|
const val LAYOUT_ID = R.layout.collection_home_list_row
|
|
|
|
const val maxTitleLength = 20
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CollectionItemMenu(
|
|
|
|
private val context: Context,
|
|
|
|
private val onItemTapped: (Item) -> Unit = {}
|
|
|
|
) {
|
|
|
|
sealed class Item {
|
|
|
|
object DeleteCollection : Item()
|
|
|
|
object AddTab : Item()
|
|
|
|
object RenameCollection : Item()
|
|
|
|
object OpenTabs : Item()
|
|
|
|
}
|
|
|
|
|
|
|
|
val menuBuilder by lazy { BrowserMenuBuilder(menuItems) }
|
|
|
|
|
|
|
|
private val menuItems by lazy {
|
|
|
|
listOf(
|
|
|
|
SimpleBrowserMenuItem(
|
|
|
|
context.getString(R.string.collection_delete),
|
2019-05-31 00:49:58 +02:00
|
|
|
textColorResource = ThemeManager.resolveAttribute(R.attr.destructive, context)
|
2019-05-06 20:20:19 +02:00
|
|
|
) {
|
|
|
|
onItemTapped.invoke(Item.DeleteCollection)
|
|
|
|
},
|
|
|
|
SimpleBrowserMenuItem(
|
|
|
|
context.getString(R.string.add_tab)
|
|
|
|
) {
|
|
|
|
onItemTapped.invoke(Item.AddTab)
|
|
|
|
},
|
|
|
|
SimpleBrowserMenuItem(
|
|
|
|
context.getString(R.string.collection_rename)
|
|
|
|
) {
|
|
|
|
onItemTapped.invoke(Item.RenameCollection)
|
|
|
|
},
|
|
|
|
SimpleBrowserMenuItem(
|
|
|
|
context.getString(R.string.collection_open_tabs)
|
|
|
|
) {
|
|
|
|
onItemTapped.invoke(Item.OpenTabs)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|