From ff8a56e49c0429b3a8251348160cd6c0bbe6f161 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Fri, 17 Jul 2020 09:40:50 +0200 Subject: [PATCH] Persistent drawer --- .../antennapod/activity/MainActivity.java | 50 +++++++++++++------ app/src/main/res/layout-sw600dp/main.xml | 46 +++++++++++++++++ app/src/main/res/layout/main.xml | 4 +- .../{dimens-fabspeeddial.xml => dimens.xml} | 0 4 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 app/src/main/res/layout-sw600dp/main.xml rename app/src/main/res/values-w300dp/{dimens-fabspeeddial.xml => dimens.xml} (100%) diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java index eaa423708..fea9e82eb 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java @@ -68,9 +68,9 @@ public class MainActivity extends CastEnabledActivity { public static final String EXTRA_OPEN_PLAYER = "open_player"; public static final String EXTRA_REFRESH_ON_START = "refresh_on_start"; - private DrawerLayout drawerLayout; + private @Nullable DrawerLayout drawerLayout; + private @Nullable ActionBarDrawerToggle drawerToggle; private View navDrawer; - private ActionBarDrawerToggle drawerToggle; private LockableBottomSheetBehavior sheetBehavior; private long lastBackButtonPressTime = 0; private RecyclerView.RecycledViewPool recycledViewPool = new RecyclerView.RecycledViewPool(); @@ -97,8 +97,11 @@ public class MainActivity extends CastEnabledActivity { navDrawer = findViewById(R.id.navDrawerFragment); final FragmentManager fm = getSupportFragmentManager(); - fm.addOnBackStackChangedListener(() -> - drawerToggle.setDrawerIndicatorEnabled(fm.getBackStackEntryCount() == 0)); + fm.addOnBackStackChangedListener(() -> { + if (drawerToggle != null) { // Tablet layout does not have a drawer + drawerToggle.setDrawerIndicatorEnabled(fm.getBackStackEntryCount() == 0); + } + }); if (fm.findFragmentByTag(MAIN_FRAGMENT_TAG) == null) { String lastFragment = NavDrawerFragment.getLastNavFragment(this); @@ -151,12 +154,14 @@ public class MainActivity extends CastEnabledActivity { @Override public void setSupportActionBar(@Nullable Toolbar toolbar) { - drawerLayout.removeDrawerListener(drawerToggle); - drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, - R.string.drawer_open, R.string.drawer_close); - drawerLayout.addDrawerListener(drawerToggle); - drawerToggle.syncState(); - drawerToggle.setDrawerIndicatorEnabled(getSupportFragmentManager().getBackStackEntryCount() == 0); + if (drawerLayout != null) { // Tablet layout does not have a drawer + drawerLayout.removeDrawerListener(drawerToggle); + drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, + R.string.drawer_open, R.string.drawer_close); + drawerLayout.addDrawerListener(drawerToggle); + drawerToggle.syncState(); + drawerToggle.setDrawerIndicatorEnabled(getSupportFragmentManager().getBackStackEntryCount() == 0); + } super.setSupportActionBar(toolbar); } @@ -164,7 +169,11 @@ public class MainActivity extends CastEnabledActivity { SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE); if (prefs.getBoolean(PREF_IS_FIRST_LAUNCH, true)) { loadFragment(AddFeedFragment.TAG, null); - new Handler().postDelayed(() -> drawerLayout.openDrawer(navDrawer), 1500); + new Handler().postDelayed(() -> { + if (drawerLayout != null) { // Tablet layout does not have a drawer + drawerLayout.openDrawer(navDrawer); + } + }, 1500); // for backward compatibility, we only change defaults for fresh installs UserPreferences.setUpdateInterval(12); @@ -258,7 +267,10 @@ public class MainActivity extends CastEnabledActivity { // not commit anything in an AsyncTask, but that's a bigger // change than we want now. t.commitAllowingStateLoss(); - drawerLayout.closeDrawer(navDrawer); + + if (drawerLayout != null) { // Tablet layout does not have a drawer + drawerLayout.closeDrawer(navDrawer); + } } public void loadChildFragment(Fragment fragment, TransitionEffect transition) { @@ -292,13 +304,17 @@ public class MainActivity extends CastEnabledActivity { @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); - drawerToggle.syncState(); + if (drawerToggle != null) { // Tablet layout does not have a drawer + drawerToggle.syncState(); + } } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); - drawerToggle.onConfigurationChanged(newConfig); + if (drawerToggle != null) { // Tablet layout does not have a drawer + drawerToggle.onConfigurationChanged(newConfig); + } } @Override @@ -351,7 +367,7 @@ public class MainActivity extends CastEnabledActivity { @Override public boolean onOptionsItemSelected(MenuItem item) { - if (drawerToggle.onOptionsItemSelected(item)) { + if (drawerToggle != null && drawerToggle.onOptionsItemSelected(item)) { // Tablet layout does not have a drawer return true; } else if (item.getItemId() == android.R.id.home) { if (getSupportFragmentManager().getBackStackEntryCount() > 0) { @@ -374,7 +390,9 @@ public class MainActivity extends CastEnabledActivity { } else { switch (UserPreferences.getBackButtonBehavior()) { case OPEN_DRAWER: - drawerLayout.openDrawer(navDrawer); + if (drawerLayout != null) { // Tablet layout does not have drawer + drawerLayout.openDrawer(navDrawer); + } break; case SHOW_PROMPT: new AlertDialog.Builder(this) diff --git a/app/src/main/res/layout-sw600dp/main.xml b/app/src/main/res/layout-sw600dp/main.xml new file mode 100644 index 000000000..79b7213e0 --- /dev/null +++ b/app/src/main/res/layout-sw600dp/main.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/main.xml b/app/src/main/res/layout/main.xml index 6da514b4a..effa87684 100644 --- a/app/src/main/res/layout/main.xml +++ b/app/src/main/res/layout/main.xml @@ -5,7 +5,9 @@ xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="match_parent" + tools:ignore="InconsistentLayout"> +