|
|
|
@ -17,13 +17,17 @@ import android.widget.TextView
|
|
|
|
|
import androidx.appcompat.app.AlertDialog
|
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
|
import androidx.paging.LoadState
|
|
|
|
|
import androidx.paging.Pager
|
|
|
|
|
import androidx.paging.PagingConfig
|
|
|
|
|
import androidx.paging.PagingDataAdapter
|
|
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
|
|
|
|
import kotlinx.coroutines.flow.collect
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
|
import mozilla.components.concept.engine.permission.SitePermissions
|
|
|
|
|
import mozilla.components.support.ktx.kotlin.stripDefaultPort
|
|
|
|
|
import org.mozilla.fenix.NavHostActivity
|
|
|
|
@ -34,7 +38,6 @@ import org.mozilla.fenix.ext.nav
|
|
|
|
|
|
|
|
|
|
private const val MAX_ITEMS_PER_PAGE = 50
|
|
|
|
|
|
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
|
class SitePermissionsExceptionsFragment :
|
|
|
|
|
Fragment(R.layout.fragment_site_permissions_exceptions), View.OnClickListener {
|
|
|
|
|
private lateinit var emptyContainerMessage: View
|
|
|
|
@ -57,27 +60,33 @@ class SitePermissionsExceptionsFragment :
|
|
|
|
|
recyclerView = rootView.findViewById(R.id.exceptions)
|
|
|
|
|
recyclerView.layoutManager = LinearLayoutManager(requireContext())
|
|
|
|
|
|
|
|
|
|
val adapter = ExceptionsAdapter(this).apply {
|
|
|
|
|
addLoadStateListener { loadState ->
|
|
|
|
|
if (loadState.source.refresh is LoadState.NotLoading &&
|
|
|
|
|
loadState.append.endOfPaginationReached &&
|
|
|
|
|
itemCount < 1
|
|
|
|
|
) {
|
|
|
|
|
showEmptyListMessage()
|
|
|
|
|
} else {
|
|
|
|
|
hideEmptyListMessage()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
|
|
|
|
|
val sitePermissionsPaged = requireContext().components.core.permissionStorage.getSitePermissionsPaged()
|
|
|
|
|
|
|
|
|
|
withContext(Main) {
|
|
|
|
|
val adapter = ExceptionsAdapter(this@SitePermissionsExceptionsFragment)
|
|
|
|
|
val liveData = androidx.paging.LivePagedListBuilder(sitePermissionsPaged, MAX_ITEMS_PER_PAGE).build()
|
|
|
|
|
|
|
|
|
|
liveData.observe(
|
|
|
|
|
viewLifecycleOwner,
|
|
|
|
|
{
|
|
|
|
|
if (it.isEmpty()) {
|
|
|
|
|
showEmptyListMessage()
|
|
|
|
|
} else {
|
|
|
|
|
hideEmptyListMessage()
|
|
|
|
|
adapter.submitList(it)
|
|
|
|
|
recyclerView.adapter = adapter
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
val dataSourceFactory =
|
|
|
|
|
requireContext().components.core.permissionStorage.getSitePermissionsPaged()
|
|
|
|
|
|
|
|
|
|
val permissions = Pager(PagingConfig(MAX_ITEMS_PER_PAGE), null) {
|
|
|
|
|
dataSourceFactory.asPagingSourceFactory().invoke()
|
|
|
|
|
}.flow
|
|
|
|
|
|
|
|
|
|
permissions.collect {
|
|
|
|
|
adapter.submitData(it)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
recyclerView.adapter = adapter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun hideEmptyListMessage() {
|
|
|
|
@ -131,17 +140,21 @@ class SitePermissionsExceptionsFragment :
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SitePermissionsViewHolder(val view: View, val iconView: ImageView, val siteTextView: TextView) :
|
|
|
|
|
class SitePermissionsViewHolder(
|
|
|
|
|
val view: View,
|
|
|
|
|
val iconView: ImageView,
|
|
|
|
|
val siteTextView: TextView
|
|
|
|
|
) :
|
|
|
|
|
RecyclerView.ViewHolder(view)
|
|
|
|
|
|
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
|
class ExceptionsAdapter(private val clickListener: View.OnClickListener) :
|
|
|
|
|
androidx.paging.PagedListAdapter<SitePermissions, SitePermissionsViewHolder>(diffCallback) {
|
|
|
|
|
PagingDataAdapter<SitePermissions, SitePermissionsViewHolder>(diffCallback) {
|
|
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SitePermissionsViewHolder {
|
|
|
|
|
val context = parent.context
|
|
|
|
|
val inflater = LayoutInflater.from(context)
|
|
|
|
|
val view = inflater.inflate(R.layout.fragment_site_permissions_exceptions_item, parent, false)
|
|
|
|
|
val view =
|
|
|
|
|
inflater.inflate(R.layout.fragment_site_permissions_exceptions_item, parent, false)
|
|
|
|
|
val iconView = view.findViewById<ImageView>(R.id.exception_icon)
|
|
|
|
|
val siteTextView = view.findViewById<TextView>(R.id.exception_text)
|
|
|
|
|
return SitePermissionsViewHolder(view, iconView, siteTextView)
|
|
|
|
@ -160,7 +173,9 @@ class ExceptionsAdapter(private val clickListener: View.OnClickListener) :
|
|
|
|
|
|
|
|
|
|
private val diffCallback = object :
|
|
|
|
|
DiffUtil.ItemCallback<SitePermissions>() {
|
|
|
|
|
override fun areItemsTheSame(old: SitePermissions, new: SitePermissions) = old.origin == new.origin
|
|
|
|
|
override fun areItemsTheSame(old: SitePermissions, new: SitePermissions) =
|
|
|
|
|
old.origin == new.origin
|
|
|
|
|
|
|
|
|
|
override fun areContentsTheSame(old: SitePermissions, new: SitePermissions) = old == new
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|