From 940c5b3838eeb2e1cdd657779eedb733fec673c3 Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 19 May 2021 22:20:32 -0400 Subject: [PATCH] Refactor BaseBottomSheetDialog to set maxWidth with newer API --- .../browse/source/browse/SourceFilterSheet.kt | 12 +++++------- .../ui/manga/chapter/ChaptersSettingsSheet.kt | 18 ++++++++---------- .../tachiyomi/ui/manga/track/TrackSheet.kt | 15 +++++++-------- .../tachiyomi/ui/reader/ReaderPageSheet.kt | 10 +++++++--- .../ui/reader/setting/ReaderSettingsSheet.kt | 12 ++++++------ .../widget/sheet/BaseBottomSheetDialog.kt | 18 ++++++++++++++++-- .../widget/sheet/TabbedBottomSheetDialog.kt | 9 ++++++--- 7 files changed, 55 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceFilterSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceFilterSheet.kt index 632f3ec0d9..fe1740752e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceFilterSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceFilterSheet.kt @@ -4,6 +4,7 @@ import android.app.Activity import android.content.Context import android.util.AttributeSet import android.view.LayoutInflater +import android.view.View import android.view.ViewGroup import com.google.android.material.bottomsheet.BottomSheetBehavior import eu.davidea.flexibleadapter.FlexibleAdapter @@ -14,23 +15,20 @@ import eu.kanade.tachiyomi.widget.sheet.BaseBottomSheetDialog class SourceFilterSheet( activity: Activity, - onFilterClicked: () -> Unit, - onResetClicked: () -> Unit + private val onFilterClicked: () -> Unit, + private val onResetClicked: () -> Unit ) : BaseBottomSheetDialog(activity) { private var filterNavView: FilterNavigationView = FilterNavigationView(activity) - private val sheetBehavior: BottomSheetBehavior<*> - init { + override fun createView(inflater: LayoutInflater): View { filterNavView.onFilterClicked = { onFilterClicked() this.dismiss() } filterNavView.onResetClicked = onResetClicked - setContentView(filterNavView) - - sheetBehavior = BottomSheetBehavior.from(filterNavView.parent as ViewGroup) + return filterNavView } override fun show() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt index 884840551c..f4cd39a661 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersSettingsSheet.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.ui.manga.chapter import android.content.Context +import android.os.Bundle import android.util.AttributeSet import android.view.View import androidx.core.view.isVisible @@ -16,21 +17,18 @@ import eu.kanade.tachiyomi.widget.sheet.TabbedBottomSheetDialog class ChaptersSettingsSheet( private val router: Router, private val presenter: MangaPresenter, - onGroupClickListener: (ExtendedNavigationView.Group) -> Unit + private val onGroupClickListener: (ExtendedNavigationView.Group) -> Unit ) : TabbedBottomSheetDialog(router.activity!!) { - val filters: Filter - private val sort: Sort - private val display: Display + val filters = Filter(router.activity!!) + private val sort = Sort(router.activity!!) + private val display = Display(router.activity!!) + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) - init { - filters = Filter(router.activity!!) filters.onGroupClicked = onGroupClickListener - - sort = Sort(router.activity!!) sort.onGroupClicked = onGroupClickListener - - display = Display(router.activity!!) display.onGroupClicked = onGroupClickListener binding.menu.isVisible = true diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackSheet.kt index 5699bcef68..2099ca5726 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackSheet.kt @@ -1,7 +1,8 @@ package eu.kanade.tachiyomi.ui.manga.track import android.os.Bundle -import android.view.ViewGroup +import android.view.LayoutInflater +import android.view.View import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.bottomsheet.BottomSheetBehavior import eu.kanade.tachiyomi.data.database.models.Manga @@ -23,22 +24,20 @@ class TrackSheet( private lateinit var binding: TrackControllerBinding - private lateinit var sheetBehavior: BottomSheetBehavior<*> - private lateinit var adapter: TrackAdapter + override fun createView(inflater: LayoutInflater): View { + binding = TrackControllerBinding.inflate(layoutInflater) + return binding.root + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - binding = TrackControllerBinding.inflate(layoutInflater) - setContentView(binding.root) - adapter = TrackAdapter(this) binding.trackRecycler.layoutManager = LinearLayoutManager(context) binding.trackRecycler.adapter = adapter - sheetBehavior = BottomSheetBehavior.from(binding.root.parent as ViewGroup) - adapter.items = controller.presenter.trackList } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPageSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPageSheet.kt index 7edcabfef7..21ce88725a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPageSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPageSheet.kt @@ -1,5 +1,7 @@ package eu.kanade.tachiyomi.ui.reader +import android.view.LayoutInflater +import android.view.View import com.afollestad.materialdialogs.MaterialDialog import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.databinding.ReaderPageSheetBinding @@ -15,14 +17,16 @@ class ReaderPageSheet( private val page: ReaderPage ) : BaseBottomSheetDialog(activity) { - private val binding = ReaderPageSheetBinding.inflate(activity.layoutInflater, null, false) + private lateinit var binding: ReaderPageSheetBinding - init { - setContentView(binding.root) + override fun createView(inflater: LayoutInflater): View { + binding = ReaderPageSheetBinding.inflate(activity.layoutInflater, null, false) binding.setAsCoverLayout.setOnClickListener { setAsCover() } binding.shareLayout.setOnClickListener { share() } binding.saveLayout.setOnClickListener { save() } + + return binding.root } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderSettingsSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderSettingsSheet.kt index c1bad95cb0..12c6e37684 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderSettingsSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderSettingsSheet.kt @@ -1,7 +1,6 @@ package eu.kanade.tachiyomi.ui.reader.setting -import android.view.ViewGroup -import com.google.android.material.bottomsheet.BottomSheetBehavior +import android.os.Bundle import com.google.android.material.tabs.TabLayout import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.ui.reader.ReaderActivity @@ -10,20 +9,21 @@ import eu.kanade.tachiyomi.widget.sheet.TabbedBottomSheetDialog class ReaderSettingsSheet( private val activity: ReaderActivity, - showColorFilterSettings: Boolean = false, + private val showColorFilterSettings: Boolean = false, ) : TabbedBottomSheetDialog(activity) { private val readingModeSettings = ReaderReadingModeSettings(activity) private val generalSettings = ReaderGeneralSettings(activity) private val colorFilterSettings = ReaderColorFilterSettings(activity) - private val sheetBackgroundDim = window?.attributes?.dimAmount ?: 0.25f + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) - init { - val sheetBehavior = BottomSheetBehavior.from(binding.root.parent as ViewGroup) sheetBehavior.isFitToContents = false sheetBehavior.halfExpandedRatio = 0.25f + val sheetBackgroundDim = window?.attributes?.dimAmount ?: 0.25f + val filterTabIndex = getTabViews().indexOf(colorFilterSettings) binding.tabs.addOnTabSelectedListener(object : SimpleTabSelectedListener() { override fun onTabSelected(tab: TabLayout.Tab?) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/sheet/BaseBottomSheetDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/sheet/BaseBottomSheetDialog.kt index 1fb501d0ef..42d8cef4b4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/sheet/BaseBottomSheetDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/sheet/BaseBottomSheetDialog.kt @@ -2,17 +2,31 @@ package eu.kanade.tachiyomi.widget.sheet import android.content.Context import android.os.Bundle +import android.view.LayoutInflater +import android.view.View import android.view.ViewGroup +import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetDialog import eu.kanade.tachiyomi.R -open class BaseBottomSheetDialog(context: Context) : BottomSheetDialog(context) { +abstract class BaseBottomSheetDialog(context: Context) : BottomSheetDialog(context) { + + internal lateinit var sheetBehavior: BottomSheetBehavior<*> + + abstract fun createView(inflater: LayoutInflater): View override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + + val rootView = createView(layoutInflater) + setContentView(rootView) + + sheetBehavior = BottomSheetBehavior.from(rootView.parent as ViewGroup) + + // Enforce max width for tablets val width = context.resources.getDimensionPixelSize(R.dimen.bottom_sheet_width) if (width > 0) { - window?.setLayout(width, ViewGroup.LayoutParams.MATCH_PARENT) + sheetBehavior.maxWidth = width } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/sheet/TabbedBottomSheetDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/sheet/TabbedBottomSheetDialog.kt index ea40f6b4b1..447a9c6b63 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/sheet/TabbedBottomSheetDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/sheet/TabbedBottomSheetDialog.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.widget.sheet import android.app.Activity +import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import eu.kanade.tachiyomi.databinding.CommonTabbedSheetBinding @@ -8,15 +9,17 @@ import eu.kanade.tachiyomi.widget.ViewPagerAdapter abstract class TabbedBottomSheetDialog(private val activity: Activity) : BaseBottomSheetDialog(activity) { - val binding = CommonTabbedSheetBinding.inflate(activity.layoutInflater) + lateinit var binding: CommonTabbedSheetBinding + + override fun createView(inflater: LayoutInflater): View { + binding = CommonTabbedSheetBinding.inflate(activity.layoutInflater) - init { val adapter = LibrarySettingsSheetAdapter() binding.pager.offscreenPageLimit = 2 binding.pager.adapter = adapter binding.tabs.setupWithViewPager(binding.pager) - setContentView(binding.root) + return binding.root } abstract fun getTabViews(): List