Refactor tabbed bottom sheet

This commit is contained in:
arkon 2020-04-28 19:57:49 -04:00
parent 1972cc25a9
commit 3374481912
3 changed files with 63 additions and 35 deletions

View File

@ -4,54 +4,43 @@ import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import com.google.android.material.bottomsheet.BottomSheetDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.databinding.LibrarySettingsSheetBinding
import eu.kanade.tachiyomi.widget.ExtendedNavigationView
import eu.kanade.tachiyomi.widget.ViewPagerAdapter
import eu.kanade.tachiyomi.widget.TabbedBottomSheetDialog
import uy.kohesive.injekt.injectLazy
class LibrarySettingsSheet(
private val activity: Activity,
private val onGroupClickListener: (ExtendedNavigationView.Group) -> Unit
) : BottomSheetDialog(activity) {
onGroupClickListener: (ExtendedNavigationView.Group) -> Unit
) : TabbedBottomSheetDialog(activity) {
val filters = Filter(activity)
private val tabItems = listOf(
Pair(R.string.action_filter, filters),
Pair(R.string.action_sort, Sort(activity)),
Pair(R.string.action_display, Display(activity))
)
val filters: Filter
private val sort: Sort
private val display: Display
init {
val binding: LibrarySettingsSheetBinding = LibrarySettingsSheetBinding.inflate(activity.layoutInflater)
filters = Filter(activity)
filters.onGroupClicked = onGroupClickListener
val adapter = LibrarySettingsSheetAdapter()
binding.librarySettingsPager.adapter = adapter
binding.librarySettingsTabs.setupWithViewPager(binding.librarySettingsPager)
sort = Sort(activity)
sort.onGroupClicked = onGroupClickListener
setContentView(binding.root)
display = Display(activity)
display.onGroupClicked = onGroupClickListener
}
private inner class LibrarySettingsSheetAdapter : ViewPagerAdapter() {
override fun getTabViews(): List<View> = listOf(
filters,
sort,
display
)
override fun createView(container: ViewGroup, position: Int): View {
val view = tabItems[position].second
view.onGroupClicked = onGroupClickListener
return view
}
override fun getCount(): Int {
return tabItems.size
}
override fun getPageTitle(position: Int): CharSequence {
return activity.resources!!.getString(tabItems[position].first)
}
}
override fun getTabTitles(): List<Int> = listOf(
R.string.action_filter,
R.string.action_sort,
R.string.action_display
)
/**
* Filters group (unread, downloaded, ...).

View File

@ -0,0 +1,39 @@
package eu.kanade.tachiyomi.widget
import android.app.Activity
import android.view.View
import android.view.ViewGroup
import com.google.android.material.bottomsheet.BottomSheetDialog
import eu.kanade.tachiyomi.databinding.CommonTabbedSheetBinding
abstract class TabbedBottomSheetDialog(private val activity: Activity) : BottomSheetDialog(activity) {
init {
val binding: CommonTabbedSheetBinding = CommonTabbedSheetBinding.inflate(activity.layoutInflater)
val adapter = LibrarySettingsSheetAdapter()
binding.pager.adapter = adapter
binding.tabs.setupWithViewPager(binding.pager)
setContentView(binding.root)
}
abstract fun getTabViews(): List<View>
abstract fun getTabTitles(): List<Int>
private inner class LibrarySettingsSheetAdapter : ViewPagerAdapter() {
override fun createView(container: ViewGroup, position: Int): View {
return getTabViews()[position]
}
override fun getCount(): Int {
return getTabViews().size
}
override fun getPageTitle(position: Int): CharSequence {
return activity.resources!!.getString(getTabTitles()[position])
}
}
}

View File

@ -6,7 +6,7 @@
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/library_settings_tabs"
android:id="@+id/tabs"
style="@style/Theme.Widget.Tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -14,7 +14,7 @@
app:tabMode="fixed" />
<eu.kanade.tachiyomi.widget.MaxHeightViewPager
android:id="@+id/library_settings_pager"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />