2016-07-07 05:42:08 +02:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.mariotaku.twidere.adapter
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.graphics.drawable.Drawable
|
|
|
|
import android.os.Bundle
|
2020-01-26 08:35:15 +01:00
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import androidx.fragment.app.FragmentManager
|
|
|
|
import androidx.viewpager.widget.PagerAdapter
|
2016-07-07 05:42:08 +02:00
|
|
|
import android.view.View
|
|
|
|
import org.mariotaku.twidere.fragment.iface.RefreshScrollTopInterface
|
|
|
|
import org.mariotaku.twidere.fragment.iface.SupportFragmentCallback
|
|
|
|
import org.mariotaku.twidere.model.SupportTabSpec
|
2016-11-30 08:18:43 +01:00
|
|
|
import org.mariotaku.twidere.model.tab.DrawableHolder
|
2016-07-07 05:42:08 +02:00
|
|
|
import org.mariotaku.twidere.util.CustomTabUtils.getTabIconDrawable
|
|
|
|
import org.mariotaku.twidere.util.Utils.announceForAccessibilityCompat
|
|
|
|
import org.mariotaku.twidere.view.iface.PagerIndicator
|
|
|
|
import org.mariotaku.twidere.view.iface.PagerIndicator.TabListener
|
|
|
|
import org.mariotaku.twidere.view.iface.PagerIndicator.TabProvider
|
|
|
|
import java.util.*
|
|
|
|
|
2017-03-27 05:08:09 +02:00
|
|
|
class SupportTabsAdapter(
|
2016-07-07 05:42:08 +02:00
|
|
|
private val context: Context,
|
|
|
|
fm: FragmentManager,
|
2017-01-17 18:59:44 +01:00
|
|
|
private val indicator: PagerIndicator? = null
|
2016-07-07 05:42:08 +02:00
|
|
|
) : SupportFixedFragmentStatePagerAdapter(fm), TabProvider, TabListener {
|
|
|
|
|
2017-01-18 08:35:00 +01:00
|
|
|
var hasMultipleColumns: Boolean = false
|
2017-02-18 07:54:08 +01:00
|
|
|
var preferredColumnWidth: Float = 0f
|
2017-01-18 08:35:00 +01:00
|
|
|
|
2017-03-27 05:08:09 +02:00
|
|
|
var tabs = ArrayList<SupportTabSpec>()
|
|
|
|
set(value) {
|
|
|
|
field = tabs
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
2016-07-07 05:42:08 +02:00
|
|
|
|
|
|
|
init {
|
|
|
|
clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getCount(): Int {
|
2017-03-27 05:08:09 +02:00
|
|
|
return this.tabs.size
|
2016-07-07 05:42:08 +02:00
|
|
|
}
|
|
|
|
|
2020-01-26 08:35:15 +01:00
|
|
|
override fun getItemPosition(obj: Any): Int {
|
2016-07-07 09:39:32 +02:00
|
|
|
if (obj !is Fragment) return PagerAdapter.POSITION_NONE
|
|
|
|
val args = obj.arguments ?: return PagerAdapter.POSITION_NONE
|
2016-07-07 05:42:08 +02:00
|
|
|
return args.getInt(EXTRA_ADAPTER_POSITION, PagerAdapter.POSITION_NONE)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun notifyDataSetChanged() {
|
|
|
|
super.notifyDataSetChanged()
|
|
|
|
indicator?.notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getPageTitle(position: Int): CharSequence? {
|
2017-03-27 05:08:09 +02:00
|
|
|
return this.tabs[position].name
|
2016-07-07 05:42:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun getPageWidth(position: Int): Float {
|
2017-01-23 15:26:29 +01:00
|
|
|
val columnCount = count
|
|
|
|
if (columnCount == 0) return 1f
|
2017-02-18 07:54:08 +01:00
|
|
|
if (hasMultipleColumns && preferredColumnWidth > 0) {
|
2017-01-18 08:35:00 +01:00
|
|
|
val resources = context.resources
|
2017-01-23 15:26:29 +01:00
|
|
|
val screenWidth = resources.displayMetrics.widthPixels
|
|
|
|
val pageWidth = preferredColumnWidth / screenWidth
|
|
|
|
if (columnCount * preferredColumnWidth < screenWidth) {
|
|
|
|
return 1f / columnCount
|
|
|
|
}
|
|
|
|
return pageWidth
|
2017-01-18 08:35:00 +01:00
|
|
|
}
|
|
|
|
return 1f
|
2016-07-07 05:42:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun getItem(position: Int): Fragment {
|
2017-03-27 05:08:09 +02:00
|
|
|
val fragment = Fragment.instantiate(context, this.tabs[position].cls.name)
|
|
|
|
fragment.arguments = getPageArguments(this.tabs[position], position)
|
2016-07-07 05:42:08 +02:00
|
|
|
return fragment
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getPageIcon(position: Int): Drawable {
|
2017-03-27 05:08:09 +02:00
|
|
|
return getTabIconDrawable(context, this.tabs[position].icon)
|
2016-07-07 05:42:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPageReselected(position: Int) {
|
|
|
|
if (context !is SupportFragmentCallback) return
|
|
|
|
val f = context.currentVisibleFragment
|
|
|
|
if (f is RefreshScrollTopInterface) {
|
|
|
|
f.scrollToStart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPageSelected(position: Int) {
|
2017-04-20 14:04:37 +02:00
|
|
|
if (position < 0 || position >= count) return
|
|
|
|
val text = getPageTitle(position) ?: return
|
|
|
|
val view = indicator as? View ?: return
|
|
|
|
announceForAccessibilityCompat(context, view, text, javaClass)
|
2016-07-07 05:42:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onTabLongClick(position: Int): Boolean {
|
|
|
|
if (context !is SupportFragmentCallback) return false
|
|
|
|
if (context.triggerRefresh(position)) return true
|
|
|
|
val f = context.currentVisibleFragment
|
|
|
|
if (f is RefreshScrollTopInterface)
|
|
|
|
return f.triggerRefresh()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-27 05:08:09 +02:00
|
|
|
fun add(cls: Class<out Fragment>, args: Bundle? = null, name: String,
|
|
|
|
icon: DrawableHolder? = null, type: String? = null, position: Int = 0, tag: String? = null) {
|
|
|
|
add(SupportTabSpec(name = name, icon = icon, cls = cls, args = args,
|
|
|
|
position = position, type = type, tag = tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
fun add(spec: SupportTabSpec) {
|
|
|
|
this.tabs.add(spec)
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun addAll(specs: Collection<SupportTabSpec>) {
|
|
|
|
this.tabs.addAll(specs)
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun get(position: Int): SupportTabSpec {
|
|
|
|
return this.tabs[position]
|
|
|
|
}
|
|
|
|
|
|
|
|
fun clear() {
|
|
|
|
this.tabs.clear()
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun setLabel(position: Int, label: CharSequence) {
|
|
|
|
this.tabs.filter { position == it.position }.forEach { it.name = label }
|
2016-07-07 05:42:08 +02:00
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getPageArguments(spec: SupportTabSpec, position: Int): Bundle {
|
|
|
|
val args = spec.args ?: Bundle()
|
|
|
|
args.putInt(EXTRA_ADAPTER_POSITION, position)
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
2020-06-08 23:11:06 +02:00
|
|
|
private const val EXTRA_ADAPTER_POSITION = "adapter_position"
|
2016-07-07 05:42:08 +02:00
|
|
|
}
|
|
|
|
}
|