From e794f84c6f83824afc7a0cb2aab368b7a678c78f Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 5 Jun 2023 11:52:56 +0300 Subject: [PATCH] Get rid of SlidingPaneLayout in settings --- .../kotatsu/core/ui/BasePreferenceFragment.kt | 10 ++-- .../kotatsu/settings/SettingsActivity.kt | 44 +++++++++++------ .../settings/SettingsHeadersFragment.kt | 49 ------------------- .../settings/sources/SourcesListFragment.kt | 4 +- .../res/layout-w600dp/activity_settings.xml | 46 +++++++++++++---- app/src/main/res/layout/activity_settings.xml | 3 +- 6 files changed, 74 insertions(+), 82 deletions(-) delete mode 100644 app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsHeadersFragment.kt diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BasePreferenceFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BasePreferenceFragment.kt index bfffb7ab3..ce040a63e 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BasePreferenceFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BasePreferenceFragment.kt @@ -9,10 +9,10 @@ import androidx.core.view.updatePadding import androidx.preference.PreferenceFragmentCompat import androidx.recyclerview.widget.RecyclerView import dagger.hilt.android.AndroidEntryPoint +import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.ui.util.RecyclerViewOwner import org.koitharu.kotatsu.core.ui.util.WindowInsetsDelegate -import org.koitharu.kotatsu.settings.SettingsHeadersFragment import javax.inject.Inject @Suppress("LeakingThis") @@ -56,9 +56,11 @@ abstract class BasePreferenceFragment(@StringRes private val titleId: Int) : ) } - @Suppress("UsePropertyAccessSyntax") protected fun setTitle(title: CharSequence) { - (parentFragment as? SettingsHeadersFragment)?.setTitle(title) - ?: activity?.setTitle(title) + activity?.let { + if (!it.resources.getBoolean(R.bool.is_tablet)) { + it.title = title + } + } } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsActivity.kt index 9b608aa96..366cd8c5e 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsActivity.kt @@ -6,7 +6,9 @@ import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem +import android.view.ViewGroup.MarginLayoutParams import androidx.core.graphics.Insets +import androidx.core.view.updateLayoutParams import androidx.core.view.updatePadding import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager @@ -43,10 +45,18 @@ class SettingsActivity : super.onCreate(savedInstanceState) setContentView(ActivitySettingsBinding.inflate(layoutInflater)) supportActionBar?.setDisplayHomeAsUpEnabled(true) - - if (supportFragmentManager.findFragmentById(R.id.container) == null) { + val isMasterDetails = viewBinding.containerMaster != null + val fm = supportFragmentManager + val currentFragment = fm.findFragmentById(R.id.container) + if (currentFragment == null || (isMasterDetails && currentFragment is RootSettingsFragment)) { openDefaultFragment() } + if (isMasterDetails && fm.findFragmentById(R.id.container_master) == null) { + supportFragmentManager.commit { + setReorderingAllowed(true) + replace(R.id.container_master, RootSettingsFragment()) + } + } } override fun onTitleChanged(title: CharSequence?, color: Int) { @@ -90,7 +100,6 @@ class SettingsActivity : } } - @Suppress("DEPRECATION") override fun onPreferenceStartFragment( caller: PreferenceFragmentCompat, pref: Preference, @@ -98,32 +107,35 @@ class SettingsActivity : val fm = supportFragmentManager val fragment = fm.fragmentFactory.instantiate(classLoader, pref.fragment ?: return false) fragment.arguments = pref.extras - fragment.setTargetFragment(caller, 0) - openFragment(fragment) + openFragment(fragment, isFromRoot = caller is RootSettingsFragment) return true } override fun onWindowInsetsChanged(insets: Insets) { - viewBinding.appbar.updatePadding( - left = insets.left, - right = insets.right, - ) - viewBinding.container.updatePadding( + viewBinding.root.updatePadding( left = insets.left, right = insets.right, ) + viewBinding.cardDetails?.updateLayoutParams { + bottomMargin = marginStart + insets.bottom + } } - fun openFragment(fragment: Fragment) { + fun openFragment(fragment: Fragment, isFromRoot: Boolean) { + val hasFragment = supportFragmentManager.findFragmentById(R.id.container) != null + val isMasterDetail = viewBinding.containerMaster != null supportFragmentManager.commit { setReorderingAllowed(true) replace(R.id.container, fragment) - setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) - addToBackStack(null) + setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) + if (!isMasterDetail || (hasFragment && !isFromRoot)) { + addToBackStack(null) + } } } private fun openDefaultFragment() { + val hasMaster = viewBinding.containerMaster != null val fragment = when (intent?.action) { ACTION_READER -> ReaderSettingsFragment() ACTION_SUGGESTIONS -> SuggestionsSettingsFragment() @@ -138,12 +150,12 @@ class SettingsActivity : when (intent.data?.host) { HOST_ABOUT -> AboutSettingsFragment() HOST_SYNC_SETTINGS -> SyncSettingsFragment() - else -> SettingsHeadersFragment() + else -> null } } - else -> SettingsHeadersFragment() - } + else -> null + } ?: if (hasMaster) AppearanceSettingsFragment() else RootSettingsFragment() supportFragmentManager.commit { setReorderingAllowed(true) replace(R.id.container, fragment) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsHeadersFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsHeadersFragment.kt deleted file mode 100644 index 53e1af007..000000000 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/SettingsHeadersFragment.kt +++ /dev/null @@ -1,49 +0,0 @@ -package org.koitharu.kotatsu.settings - -import android.os.Bundle -import android.view.View -import androidx.fragment.app.Fragment -import androidx.fragment.app.FragmentTransaction -import androidx.fragment.app.commit -import androidx.preference.PreferenceFragmentCompat -import androidx.preference.PreferenceHeaderFragmentCompat -import androidx.slidingpanelayout.widget.SlidingPaneLayout -import org.koitharu.kotatsu.R - -class SettingsHeadersFragment : PreferenceHeaderFragmentCompat(), SlidingPaneLayout.PanelSlideListener { - - private var currentTitle: CharSequence? = null - - override fun onCreatePreferenceHeader(): PreferenceFragmentCompat = RootSettingsFragment() - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - slidingPaneLayout.addPanelSlideListener(this) - } - - override fun onPanelSlide(panel: View, slideOffset: Float) = Unit - - override fun onPanelOpened(panel: View) { - activity?.title = currentTitle ?: getString(R.string.settings) - } - - override fun onPanelClosed(panel: View) { - activity?.setTitle(R.string.settings) - } - - fun setTitle(title: CharSequence?) { - currentTitle = title - if (slidingPaneLayout.width != 0 && slidingPaneLayout.isOpen) { - activity?.title = title - } - } - - fun openFragment(fragment: Fragment) { - childFragmentManager.commit { - setReorderingAllowed(true) - replace(androidx.preference.R.id.preferences_detail, fragment) - setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) - addToBackStack(null) - } - } -} \ No newline at end of file diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesListFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesListFragment.kt index 12b20babc..18d0f2d97 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesListFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesListFragment.kt @@ -26,7 +26,6 @@ import org.koitharu.kotatsu.core.util.ext.observeEvent import org.koitharu.kotatsu.databinding.FragmentSettingsSourcesBinding import org.koitharu.kotatsu.main.ui.owners.AppBarOwner import org.koitharu.kotatsu.settings.SettingsActivity -import org.koitharu.kotatsu.settings.SettingsHeadersFragment import org.koitharu.kotatsu.settings.SourceSettingsFragment import org.koitharu.kotatsu.settings.sources.adapter.SourceConfigAdapter import org.koitharu.kotatsu.settings.sources.adapter.SourceConfigListener @@ -90,8 +89,7 @@ class SourcesListFragment : override fun onItemSettingsClick(item: SourceConfigItem.SourceItem) { val fragment = SourceSettingsFragment.newInstance(item.source) - (parentFragment as? SettingsHeadersFragment)?.openFragment(fragment) - ?: (activity as? SettingsActivity)?.openFragment(fragment) + (activity as? SettingsActivity)?.openFragment(fragment, false) } override fun onItemEnabledChanged(item: SourceConfigItem.SourceItem, isEnabled: Boolean) { diff --git a/app/src/main/res/layout-w600dp/activity_settings.xml b/app/src/main/res/layout-w600dp/activity_settings.xml index 642380efc..63aef2083 100644 --- a/app/src/main/res/layout-w600dp/activity_settings.xml +++ b/app/src/main/res/layout-w600dp/activity_settings.xml @@ -1,17 +1,17 @@ - + android:layout_height="match_parent"> + android:id="@+id/container_master" + android:layout_width="0dp" + android:layout_height="0dp" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toStartOf="@id/card_details" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/appbar" + app:layout_constraintWidth_max="400dp" + app:layout_constraintWidth_min="320dp" + app:layout_constraintWidth_percent="0.35" /> - \ No newline at end of file + + + + + + + diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 3b0616df6..459c2008f 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -13,8 +13,9 @@