1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-01-03 14:00:32 +00:00

Only update main tabs position when it changes

This commit is contained in:
Stypox 2023-02-26 15:58:09 +01:00
parent c8ffe65acf
commit 65680b2ccf
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23

View File

@ -8,6 +8,7 @@ import static com.google.android.material.tabs.TabLayout.INDICATOR_GRAVITY_BOTTO
import static com.google.android.material.tabs.TabLayout.INDICATOR_GRAVITY_TOP; import static com.google.android.material.tabs.TabLayout.INDICATOR_GRAVITY_TOP;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
@ -56,8 +57,11 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
private boolean hasTabsChanged = false; private boolean hasTabsChanged = false;
private boolean previousYoutubeRestrictedModeEnabled; private SharedPreferences prefs;
private boolean youtubeRestrictedModeEnabled;
private String youtubeRestrictedModeEnabledKey; private String youtubeRestrictedModeEnabledKey;
private boolean mainTabsPositionBottom;
private String mainTabsPositionKey;
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Fragment's LifeCycle // Fragment's LifeCycle
@ -80,10 +84,11 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
} }
}); });
prefs = PreferenceManager.getDefaultSharedPreferences(requireContext());
youtubeRestrictedModeEnabledKey = getString(R.string.youtube_restricted_mode_enabled); youtubeRestrictedModeEnabledKey = getString(R.string.youtube_restricted_mode_enabled);
previousYoutubeRestrictedModeEnabled = youtubeRestrictedModeEnabled = prefs.getBoolean(youtubeRestrictedModeEnabledKey, false);
PreferenceManager.getDefaultSharedPreferences(requireContext()) mainTabsPositionKey = getString(R.string.main_tabs_position_key);
.getBoolean(youtubeRestrictedModeEnabledKey, false); mainTabsPositionBottom = prefs.getBoolean(mainTabsPositionKey, false);
} }
@Override @Override
@ -103,23 +108,25 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
binding.mainTabLayout.addOnTabSelectedListener(this); binding.mainTabLayout.addOnTabSelectedListener(this);
setupTabs(); setupTabs();
updateTabLayoutPosition();
} }
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
final boolean youtubeRestrictedModeEnabled = final boolean newYoutubeRestrictedModeEnabled =
PreferenceManager.getDefaultSharedPreferences(requireContext()) prefs.getBoolean(youtubeRestrictedModeEnabledKey, false);
.getBoolean(youtubeRestrictedModeEnabledKey, false); if (youtubeRestrictedModeEnabled != newYoutubeRestrictedModeEnabled || hasTabsChanged) {
if (previousYoutubeRestrictedModeEnabled != youtubeRestrictedModeEnabled) { youtubeRestrictedModeEnabled = newYoutubeRestrictedModeEnabled;
previousYoutubeRestrictedModeEnabled = youtubeRestrictedModeEnabled;
setupTabs();
} else if (hasTabsChanged) {
setupTabs(); setupTabs();
} }
updateTabLayoutPosition(); final boolean newMainTabsPosition = prefs.getBoolean(mainTabsPositionKey, false);
if (mainTabsPositionBottom != newMainTabsPosition) {
mainTabsPositionBottom = newMainTabsPosition;
updateTabLayoutPosition();
}
} }
@Override @Override
@ -207,8 +214,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
private void updateTabLayoutPosition() { private void updateTabLayoutPosition() {
final ScrollableTabLayout tabLayout = binding.mainTabLayout; final ScrollableTabLayout tabLayout = binding.mainTabLayout;
final ViewPager viewPager = binding.pager; final ViewPager viewPager = binding.pager;
final boolean bottom = PreferenceManager.getDefaultSharedPreferences(requireContext()) final boolean bottom = mainTabsPositionBottom;
.getBoolean(getString(R.string.main_tabs_position_key), false);
// change layout params to make the tab layout appear either at the top or at the bottom // change layout params to make the tab layout appear either at the top or at the bottom
final var tabParams = (RelativeLayout.LayoutParams) tabLayout.getLayoutParams(); final var tabParams = (RelativeLayout.LayoutParams) tabLayout.getLayoutParams();