You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iceraven-browser/app/src/main/java/org/mozilla/fenix/library/LibrarySiteItemView.kt

109 lines
3.2 KiB
Kotlin

/* 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.library
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import kotlinx.android.synthetic.main.library_site_item.view.*
import mozilla.components.concept.menu.MenuController
import mozilla.components.concept.menu.Orientation
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.increaseTapArea
import org.mozilla.fenix.ext.loadIntoView
import org.mozilla.fenix.selection.SelectionHolder
import org.mozilla.fenix.selection.SelectionInteractor
class LibrarySiteItemView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr, defStyleRes) {
val titleView: TextView get() = title
val urlView: TextView get() = url
val iconView: ImageView get() = favicon
val overflowView: ImageButton get() = overflow_menu
init {
LayoutInflater.from(context).inflate(R.layout.library_site_item, this, true)
overflow_menu.increaseTapArea(OVERFLOW_EXTRA_DIPS)
}
/**
* Change visibility of parts of this view based on what type of item is being represented.
*/
fun displayAs(mode: ItemType) {
url.isVisible = mode == ItemType.SITE
}
/**
* Changes the icon to show a check mark if [isSelected]
*/
fun changeSelected(isSelected: Boolean) {
icon.displayedChild = if (isSelected) 1 else 0
}
fun loadFavicon(url: String) {
context.components.core.icons.loadIntoView(favicon, url)
}
fun attachMenu(menuController: MenuController) {
overflow_menu.setOnClickListener {
menuController.show(
anchor = it,
orientation = Orientation.DOWN
)
}
}
fun <T> setSelectionInteractor(item: T, holder: SelectionHolder<T>, interactor: SelectionInteractor<T>) {
setOnClickListener {
val selected = holder.selectedItems
when {
selected.isEmpty() -> interactor.open(item)
item in selected -> interactor.deselect(item)
else -> interactor.select(item)
}
}
setOnLongClickListener {
if (holder.selectedItems.isEmpty()) {
interactor.select(item)
true
} else {
false
}
}
favicon.setOnClickListener {
if (item in holder.selectedItems) {
interactor.deselect(item)
} else {
interactor.select(item)
}
}
}
enum class ItemType {
SITE, FOLDER;
}
companion object {
private const val OVERFLOW_EXTRA_DIPS = 16
}
}