Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* SPDX-FileCopyrightText: 2026 WizeStream contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/

package org.schabi.newpipe.settings

import android.graphics.Rect
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.children
import androidx.preference.Preference
import androidx.preference.PreferenceGroupAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ActivityScenario
import androidx.test.platform.app.InstrumentationRegistry
import com.google.android.material.appbar.AppBarLayout
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.schabi.newpipe.R
import org.schabi.newpipe.settings.preferencesearch.PreferenceSearchFragment

class SettingsToolbarLayoutTest {
@Test
fun compactToolbarRestoresSectionTitleAfterSearchAndRecreation() {
ActivityScenario.launch(SettingsActivity::class.java).use { scenario ->
scenario.onActivity(::openSponsorBlock)
idle()
scenario.onActivity { activity ->
assertCompactTitle(activity)
val search = activity.findViewById<Toolbar>(R.id.toolbar).menu.findItem(R.id.action_search)
assertNotNull(search)
assertTrue(search.isVisible)
assertTrue(activity.onOptionsItemSelected(search))
activity.supportFragmentManager.executePendingTransactions()
}
idle()
scenario.onActivity(::assertSearchVisible)
scenario.recreate()
idle()
scenario.onActivity { activity ->
assertSearchVisible(activity)
activity.onBackPressedDispatcher.onBackPressed()
activity.supportFragmentManager.executePendingTransactions()
}
idle()
scenario.onActivity { activity ->
assertTrue(current(activity) is SponsorBlockSettingsFragment)
assertEquals(View.GONE, activity.findViewById<View>(R.id.toolbar_search_container).visibility)
assertCompactTitle(activity)
}
}
}

@Test
fun finalPreferencesRemainAboveSystemNavigationWithoutCollapsingTheHeader() {
ActivityScenario.launch(SettingsActivity::class.java).use { scenario ->
scenario.onActivity { activity ->
val main = categories(activity)
// Release builds end at Learning Mode rather than the debug-only category.
main.findPreference<Preference>(activity.getString(R.string.debug_pref_screen_key))?.let {
main.preferenceScreen.removePreference(it)
}
}
idle()
scenario.onActivity { activity ->
val list = categories(activity).listView
val adapter = list.adapter as PreferenceGroupAdapter
assertEquals(LearningSettingsFragment::class.java.name, adapter.getItem(adapter.itemCount - 1)?.fragment)
list.scrollToPosition(adapter.itemCount - 1)
}
idle()
scenario.onActivity { activity ->
assertLastRowInsideSafeWindow(activity, categories(activity).listView)
openSponsorBlock(activity)
}
idle()
scenario.onActivity { activity ->
val list = (current(activity) as SponsorBlockSettingsFragment).listView
val adapter = list.adapter as PreferenceGroupAdapter
assertEquals(activity.getString(R.string.dearrow_thumbnails_key), adapter.getItem(adapter.itemCount - 1)?.key)
list.scrollToPosition(adapter.itemCount - 1)
}
idle()
scenario.onActivity { activity ->
assertLastRowInsideSafeWindow(activity, (current(activity) as SponsorBlockSettingsFragment).listView)
}
}
}

private fun assertCompactTitle(activity: SettingsActivity) {
val toolbar = activity.findViewById<Toolbar>(R.id.toolbar)
val appBar = activity.findViewById<AppBarLayout>(R.id.settings_app_bar)
val title = (current(activity) as BasePreferenceFragment).preferenceScreen.title.toString()
assertEquals(title, toolbar.title?.toString())
assertNotNull(toolbar.navigationIcon)
val label = toolbar.children.filterIsInstance<TextView>().firstOrNull { it.text.toString() == title }
assertNotNull("The section title should be drawn in the navigation toolbar", label)
assertTrue(label!!.width > 0 && label.height > 0)
assertTrue(label.top >= 0 && label.bottom <= toolbar.height)
assertEquals("Settings should not reserve a second title row", toolbar.height, appBar.height)
assertEquals("Scrolling settings must not change the header height", 0, appBar.totalScrollRange)
}

private fun assertSearchVisible(activity: SettingsActivity) {
assertTrue(current(activity) is PreferenceSearchFragment)
assertEquals(View.VISIBLE, activity.findViewById<View>(R.id.toolbar_search_container).visibility)
assertTrue("The section title must make room for search", activity.findViewById<Toolbar>(R.id.toolbar).title.isNullOrEmpty())
}

private fun assertLastRowInsideSafeWindow(activity: SettingsActivity, list: RecyclerView) {
val appBar = activity.findViewById<AppBarLayout>(R.id.settings_app_bar)
val root = appBar.parent as ViewGroup
val insets = ViewCompat.getRootWindowInsets(root)!!
val navigation = insets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout())
assertTrue("Settings must reserve the system navigation area", root.paddingBottom >= navigation.bottom)
val row = list.findViewHolderForAdapterPosition(list.adapter!!.itemCount - 1)?.itemView
assertNotNull("The final preference must be laid out after scrolling", row)
assertFalse("The list must have reached its end", list.canScrollVertically(1))
val bounds = Rect(0, 0, row!!.width, row.height)
root.offsetDescendantRectToMyCoords(row, bounds)
assertTrue("The complete preference must fit above system navigation: $bounds", bounds.bottom <= root.height - root.paddingBottom)
assertTrue("The preference must not overlap the toolbar: $bounds", bounds.top >= appBar.bottom)
val visible = Rect()
assertTrue(row.getGlobalVisibleRect(visible))
assertEquals("The final row must not be clipped by an ancestor", row.height, visible.height())
}

private fun openSponsorBlock(activity: SettingsActivity) {
val destination = Preference(activity).apply { fragment = SponsorBlockSettingsFragment::class.java.name }
assertTrue(activity.onPreferenceStartFragment(categories(activity), destination))
activity.supportFragmentManager.executePendingTransactions()
}

private fun categories(activity: SettingsActivity) = (
activity.supportFragmentManager.findFragmentById(R.id.settings_navigation_holder)
?: current(activity)
) as MainSettingsFragment

private fun current(activity: SettingsActivity) = activity.supportFragmentManager.findFragmentById(R.id.settings_fragment_holder)

private fun idle() = InstrumentationRegistry.getInstrumentation().waitForIdleSync()
}
5 changes: 5 additions & 0 deletions app/src/main/assets/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@
<section id="unreleased">
<h2>Unreleased</h2>
<div class="release-notes">
<h3>Settings layout</h3>
<ul>
<li>Use a compact toolbar with the settings title beside the back button, leaving more space for preferences.</li>
<li>Keep the last settings rows fully reachable above the system navigation buttons, including after restoring a scrolled page.</li>
</ul>
<h3>Single-channel feed refresh</h3>
<ul>
<li>Add Refresh channel feed to subscribed channels&#x27; menus to fetch their saved feed immediately, including muted channels, while keeping the mute setting enabled for regular updates.</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.appbar.AppBarLayout;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -74,8 +72,6 @@ public class SettingsActivity extends AppCompatActivity implements
"org.schabi.newpipe.settings.OPEN_PLUGIN_SETTINGS";
private static final String SEARCH_TEXT = "settings_search_text";
private static final String TWO_PANE = "settings_two_pane";
private AppBarLayout settingsAppBar;
private CollapsingToolbarLayout settingsCollapsingToolbar;
private CharSequence sectionTitle = "";

private boolean twoPane;
Expand Down Expand Up @@ -111,8 +107,6 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {

final SettingsLayoutBinding binding = SettingsLayoutBinding.inflate(getLayoutInflater());
settingsRoot = binding.getRoot();
settingsAppBar = binding.settingsAppBar;
settingsCollapsingToolbar = binding.settingsCollapsingToolbar;
setContentView(settingsRoot);
twoPane = getResources().getBoolean(R.bool.use_two_pane_settings);
binding.settingsNavigationHolder.setVisibility(twoPane ? View.VISIBLE : View.GONE);
Expand All @@ -121,8 +115,7 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
// The shared collapsing toolbar draws both the expanded and compact title.
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
}

if (savedInstanceState != null) {
Expand Down Expand Up @@ -358,29 +351,25 @@ private boolean isSearchActive() {
}

/**
* Keeps the expanded and collapsed settings titles synchronized across fragments.
* Keeps the compact toolbar title synchronized across settings fragments.
*
* @param title current settings section title
*/
public void setSettingsSectionTitle(@NonNull final CharSequence title) {
sectionTitle = title;
if (!isSearchActive() && settingsCollapsingToolbar != null) {
settingsCollapsingToolbar.setTitle(title);
final ActionBar actionBar = getSupportActionBar();
if (!isSearchActive() && actionBar != null) {
actionBar.setTitle(title);
}
}

private void updateSearchUi() {
final boolean active = isSearchActive();
searchContainer.setVisibility(active ? View.VISIBLE : View.GONE);
if (settingsCollapsingToolbar != null) {
settingsCollapsingToolbar.setTitle(active ? "" : sectionTitle);
}
if (active && settingsAppBar != null) {
settingsAppBar.setExpanded(false, false);
}
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setTitle(sectionTitle);
actionBar.setDisplayShowTitleEnabled(!active);
}
invalidateOptionsMenu();
if (active) {
Expand Down
24 changes: 4 additions & 20 deletions app/src/main/res/layout/settings_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,11 @@
android:background="?attr/colorSurface"
app:liftOnScroll="true">

<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/settings_collapsing_toolbar"
<include
android:id="@+id/settings_toolbar_layout"
layout="@layout/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/settings_collapsing_header_height"
app:contentScrim="?attr/colorSurface"
app:expandedTitleTextAppearance="@style/TextAppearance.Material3.HeadlineMedium"
app:collapsedTitleTextAppearance="@style/TextAppearance.Material3.TitleLarge"
app:expandedTitleMarginStart="24dp"
app:expandedTitleMarginEnd="16dp"
app:expandedTitleMarginBottom="16dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@id/toolbar">

<include
android:id="@+id/settings_toolbar_layout"
layout="@layout/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
app:layout_collapseMode="pin" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
android:layout_height="wrap_content" />
</com.google.android.material.appbar.AppBarLayout>

<LinearLayout
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<dimen name="spacing_micro">4dp</dimen>
<dimen name="spacing_nano">2dp</dimen>

<!-- Compact large-title app bar: room for the pinned toolbar and expanded title. -->
<dimen name="settings_collapsing_header_height">120dp</dimen>

<!-- Settings card dimensions -->
<dimen name="settings_card_margin_horizontal">16dp</dimen>
<dimen name="settings_card_margin_top">8dp</dimen>
Expand Down
Loading
Loading