parent
9430546d12
commit
04dcfa5cab
@ -0,0 +1,57 @@
|
|||||||
|
/* 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.view.View
|
||||||
|
import androidx.core.view.isVisible
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import androidx.viewpager2.widget.ViewPager2
|
||||||
|
import kotlinx.android.synthetic.main.component_top_sites_pager.view.*
|
||||||
|
import mozilla.components.feature.top.sites.TopSite
|
||||||
|
import org.mozilla.fenix.R
|
||||||
|
import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor
|
||||||
|
import org.mozilla.fenix.home.sessioncontrol.viewholders.topsites.TopSitesPagerAdapter
|
||||||
|
|
||||||
|
class TopSitePagerViewHolder(
|
||||||
|
view: View,
|
||||||
|
interactor: TopSiteInteractor
|
||||||
|
) : RecyclerView.ViewHolder(view) {
|
||||||
|
|
||||||
|
private val topSitesPagerAdapter = TopSitesPagerAdapter(interactor)
|
||||||
|
private val pageIndicator = view.page_indicator
|
||||||
|
|
||||||
|
private val topSitesPageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
|
||||||
|
override fun onPageSelected(position: Int) {
|
||||||
|
pageIndicator.setSelection(position)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
view.top_sites_pager.apply {
|
||||||
|
adapter = topSitesPagerAdapter
|
||||||
|
registerOnPageChangeCallback(topSitesPageChangeCallback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bind(topSites: List<TopSite>) {
|
||||||
|
topSitesPagerAdapter.updateData(topSites)
|
||||||
|
|
||||||
|
// Don't show any page indicator if there is only 1 page.
|
||||||
|
val numPages = if (topSites.size > TOP_SITES_PER_PAGE) {
|
||||||
|
TOP_SITES_MAX_PAGE_SIZE
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
pageIndicator.isVisible = numPages > 1
|
||||||
|
pageIndicator.setSize(numPages)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val LAYOUT_ID = R.layout.component_top_sites_pager
|
||||||
|
const val TOP_SITES_MAX_PAGE_SIZE = 2
|
||||||
|
const val TOP_SITES_PER_PAGE = 8
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,82 @@
|
|||||||
|
/* 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.topsites
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.util.AttributeSet
|
||||||
|
import android.util.TypedValue
|
||||||
|
import android.view.View
|
||||||
|
import android.widget.LinearLayout
|
||||||
|
import androidx.core.view.MarginLayoutParamsCompat
|
||||||
|
import org.mozilla.fenix.R
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A pager indicator widget to display the number of pages and the current selected page.
|
||||||
|
*/
|
||||||
|
class PagerIndicator : LinearLayout {
|
||||||
|
|
||||||
|
constructor(context: Context) : super(context)
|
||||||
|
|
||||||
|
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
|
||||||
|
|
||||||
|
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
|
||||||
|
|
||||||
|
private var selectedIndex = 0
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the number of pager dots to display.
|
||||||
|
*/
|
||||||
|
fun setSize(size: Int) {
|
||||||
|
if (childCount == size) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (selectedIndex >= size) {
|
||||||
|
selectedIndex = size - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
removeAllViews()
|
||||||
|
for (i in 0 until size) {
|
||||||
|
val isLast = i == size - 1
|
||||||
|
addView(
|
||||||
|
View(context).apply {
|
||||||
|
setBackgroundResource(R.drawable.pager_dot)
|
||||||
|
isSelected = i == selectedIndex
|
||||||
|
},
|
||||||
|
LayoutParams(dpToPx(DOT_SIZE_IN_DP), dpToPx(DOT_SIZE_IN_DP)).apply {
|
||||||
|
if (!isLast) {
|
||||||
|
MarginLayoutParamsCompat.setMarginEnd(this, dpToPx(DOT_MARGIN))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the current selected pager dot.
|
||||||
|
*/
|
||||||
|
fun setSelection(index: Int) {
|
||||||
|
if (selectedIndex == index) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
getChildAt(selectedIndex)?.run {
|
||||||
|
isSelected = false
|
||||||
|
}
|
||||||
|
getChildAt(index)?.run {
|
||||||
|
isSelected = true
|
||||||
|
}
|
||||||
|
selectedIndex = index
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val DOT_SIZE_IN_DP = 6f
|
||||||
|
private const val DOT_MARGIN = 4f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Context.dpToPx(value: Float): Int =
|
||||||
|
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, resources.displayMetrics).toInt()
|
||||||
|
|
||||||
|
fun View.dpToPx(value: Float): Int = context.dpToPx(value)
|
@ -0,0 +1,40 @@
|
|||||||
|
/* 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.topsites
|
||||||
|
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import mozilla.components.feature.top.sites.TopSite
|
||||||
|
import org.mozilla.fenix.home.sessioncontrol.TopSiteInteractor
|
||||||
|
import org.mozilla.fenix.home.sessioncontrol.viewholders.TopSiteViewHolder
|
||||||
|
|
||||||
|
class TopSitesPagerAdapter(
|
||||||
|
private val interactor: TopSiteInteractor
|
||||||
|
) : RecyclerView.Adapter<TopSiteViewHolder>() {
|
||||||
|
|
||||||
|
private var topSites: List<List<TopSite>> = listOf()
|
||||||
|
|
||||||
|
fun updateData(topSites: List<TopSite>) {
|
||||||
|
this.topSites = topSites.chunked(TOP_SITES_PER_PAGE)
|
||||||
|
notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TopSiteViewHolder {
|
||||||
|
val view = LayoutInflater.from(parent.context)
|
||||||
|
.inflate(TopSiteViewHolder.LAYOUT_ID, parent, false)
|
||||||
|
return TopSiteViewHolder(view, interactor)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: TopSiteViewHolder, position: Int) {
|
||||||
|
holder.bind(this.topSites[position])
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemCount(): Int = this.topSites.size
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val TOP_SITES_PER_PAGE = 8
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- 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/. -->
|
||||||
|
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item android:color="@color/top_site_pager_dot_selected" android:state_selected="true" />
|
||||||
|
<item android:color="@color/top_site_pager_dot" />
|
||||||
|
</selector>
|
@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- 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/. -->
|
||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:shape="oval">
|
||||||
|
<solid android:color="@color/pager_dot"/>
|
||||||
|
|
||||||
|
<size
|
||||||
|
android:width="6dp"
|
||||||
|
android:height="6dp"/>
|
||||||
|
</shape>
|
@ -0,0 +1,24 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- 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/. -->
|
||||||
|
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<androidx.viewpager2.widget.ViewPager2
|
||||||
|
android:id="@+id/top_sites_pager"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
<org.mozilla.fenix.home.sessioncontrol.viewholders.topsites.PagerIndicator
|
||||||
|
android:id="@+id/page_indicator"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="6dp"
|
||||||
|
android:layout_gravity="center_horizontal"
|
||||||
|
android:layout_marginBottom="8dp"
|
||||||
|
android:orientation="horizontal" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
Loading…
Reference in New Issue