diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java index 959e4c863..f46d5365d 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java @@ -49,7 +49,6 @@ import android.widget.Toast; import androidx.activity.OnBackPressedCallback; -import androidx.activity.OnBackPressedDispatcher; import androidx.annotation.NonNull; import androidx.annotation.VisibleForTesting; import androidx.appcompat.app.ActionBarDrawerToggle; @@ -79,6 +78,7 @@ import com.nextcloud.android.sso.exceptions.TokenMismatchException; import com.nextcloud.android.sso.helper.SingleAccountHelper; import com.nextcloud.android.sso.ui.UiExceptionManager; +import com.sothree.slidinguppanel.SlidingUpPanelLayout; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; @@ -140,7 +140,7 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements public static final String ITEM_ID = "ITEM_ID"; public static final String TITLE = "TITLE"; - public static HashSet stayUnreadItems = new HashSet<>(); + public static HashSet stayUnreadItems = new HashSet<>(); private MenuItem menuItemOnlyUnread; private MenuItem menuItemDownloadMoreItems; @@ -149,6 +149,26 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements @VisibleForTesting(otherwise = PROTECTED) public ActivityNewsreaderBinding binding; + OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) { + // we need to handle two cases: + // - The user has the "Open Sidebar on Backpress" option enabled + // - the callback need to be set because we want to close the podcast pane on back navigation (in case it's open) + // - set callback will be enabled/disabled based on whether the podcast pane is open/closed + // - The user has the "Open Sidebar on Backpress" option disabled + // - the callback needs to check first if the podcast is open - if so - close it and on + // the next back navigation open the sidebar - and then close the app + // - once the podcast pane is open - the callback will be disabled + // - the event listener (onDrawerClosed) will enable the back pressed callback again + @Override + public void handleOnBackPressed() { + Log.d(TAG, "handleOnBackPressed() 1"); + if (!handlePodcastBackPressed()) { + Log.d(TAG, "handleOnBackPressed() 2"); + binding.drawerLayout.openDrawer(GravityCompat.START); + setEnabled(false); + } + } + }; //private ServiceConnection mConnection = null; @@ -197,6 +217,80 @@ public void onPostCreate(Bundle savedInstanceState) { private boolean isUserLoggedIn() { return (mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, null) != null); } + private boolean mBackOpensDrawer = false; + + @Override + protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { + restoreInstanceState(savedInstanceState); + super.onRestoreInstanceState(savedInstanceState); + } + + @Override + protected void onSaveInstanceState(@NonNull Bundle outState) { + saveInstanceState(outState); + super.onSaveInstanceState(outState); + } + + private void saveInstanceState(Bundle outState) { + NewsReaderDetailFragment ndf = getNewsReaderDetailFragment(); + if (ndf != null) { + outState.putLong(OPTIONAL_FOLDER_ID, ndf.getIdFolder()); + outState.putBoolean(IS_FOLDER_BOOLEAN, ndf.getIdFeed() == null); + outState.putLong(ID_FEED_STRING, ndf.getIdFeed() != null ? ndf.getIdFeed() : ndf.getIdFolder()); + + NewsListRecyclerAdapter adapter = (NewsListRecyclerAdapter) ndf.getRecyclerView().getAdapter(); + if (adapter != null) { + outState.putInt(LIST_ADAPTER_TOTAL_COUNT, adapter.getTotalItemCount()); + outState.putInt(LIST_ADAPTER_PAGE_COUNT, adapter.getCachedPages()); + } + } + if (mSearchView != null) { + mSearchString = mSearchView.getQuery().toString(); + outState.putString(SEARCH_KEY, mSearchString); + } + } + + private void restoreInstanceState(Bundle savedInstanceState) { + if (savedInstanceState.containsKey(ID_FEED_STRING) && + savedInstanceState.containsKey(IS_FOLDER_BOOLEAN) && + savedInstanceState.containsKey(OPTIONAL_FOLDER_ID)) { + + NewsListRecyclerAdapter adapter = new NewsListRecyclerAdapter(this, getNewsReaderDetailFragment().binding.list, this, mPostDelayHandler, mPrefs); + + adapter.setTotalItemCount(savedInstanceState.getInt(LIST_ADAPTER_TOTAL_COUNT)); + adapter.setCachedPages(savedInstanceState.getInt(LIST_ADAPTER_PAGE_COUNT)); + + getNewsReaderDetailFragment() + .getRecyclerView() + .setAdapter(adapter); + + updateDetailFragment(savedInstanceState.getLong(ID_FEED_STRING), + savedInstanceState.getBoolean(IS_FOLDER_BOOLEAN), + savedInstanceState.getLong(OPTIONAL_FOLDER_ID), + false); + } + mSearchString = savedInstanceState.getString(SEARCH_KEY, null); + } + + @Override + public void onConfigurationChanged(@NonNull Configuration newConfig) { + super.onConfigurationChanged(newConfig); + if (drawerToggle != null) { + drawerToggle.onConfigurationChanged(newConfig); + } + } + SlidingUpPanelLayout.PanelSlideListener panelSlideListener = new SlidingUpPanelLayout.PanelSlideListener() { + @Override + public void onPanelSlide(View panel, float slideOffset) { + } + + @Override + public void onPanelStateChanged(View panel, SlidingUpPanelLayout.PanelState previousState, SlidingUpPanelLayout.PanelState newState) { + boolean panelIsOpen = newState.equals(SlidingUpPanelLayout.PanelState.EXPANDED); + // in case the podcast panel is open, we need to close it first (intercept back presses) + onBackPressedCallback.setEnabled(panelIsOpen || mBackOpensDrawer); + } + }; @Override protected void onCreate(Bundle savedInstanceState) { @@ -211,33 +305,6 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - - //trying to get back button working again - OnBackPressedDispatcher dispatcher = getOnBackPressedDispatcher(); - getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) { - @Override - public void handleOnBackPressed() { - Log.d(TAG, "NewsReaderListActivity handleOnBackPressed() called"); - if (!handlePodcastBackPressed()) { - - //Add in check for preference to determine if closes app or opens drawer - if (mPrefs.getBoolean(SettingsActivity.CB_PREF_BACK_OPENS_DRAWER, false)) { //change this line to the preference test - or add into an AND in the below iff - - if (binding.drawerLayout != null) { - if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) - setEnabled(false); - else - binding.drawerLayout.openDrawer(GravityCompat.START); - } else { - setEnabled(false); - } - } else { - setEnabled(false); - } - } - } - }); - binding = ActivityNewsreaderBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); @@ -272,6 +339,7 @@ public void handleOnBackPressed() { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); + onBackPressedCallback.setEnabled(mBackOpensDrawer); syncState(); } @@ -281,6 +349,9 @@ public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); reloadCountNumbersOfSlidingPaneAdapter(); + // -> handleOnBackPressed() will disable it + // onBackPressedCallback.setEnabled(false); + syncState(); } }; @@ -295,77 +366,19 @@ public void onDrawerOpened(View drawerView) { drawerToggle.syncState(); } + getPodcastSlidingUpPanelLayout().addPanelSlideListener(panelSlideListener); + getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback); + //AppRater.app_launched(this); //AppRater.rateNow(this); - if (savedInstanceState == null) { //When the app starts (no orientation change) + if (savedInstanceState == null) { // When the app starts (no orientation change) updateDetailFragment(SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_UNREAD_ITEMS.getValue(), true, null, true); } showChangelogIfNecessary(); } - @Override - protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { - restoreInstanceState(savedInstanceState); - super.onRestoreInstanceState(savedInstanceState); - } - - @Override - protected void onSaveInstanceState(@NonNull Bundle outState) { - saveInstanceState(outState); - super.onSaveInstanceState(outState); - } - - private void saveInstanceState(Bundle outState) { - NewsReaderDetailFragment ndf = getNewsReaderDetailFragment(); - if (ndf != null) { - outState.putLong(OPTIONAL_FOLDER_ID, ndf.getIdFolder()); - outState.putBoolean(IS_FOLDER_BOOLEAN, ndf.getIdFeed() == null); - outState.putLong(ID_FEED_STRING, ndf.getIdFeed() != null ? ndf.getIdFeed() : ndf.getIdFolder()); - - NewsListRecyclerAdapter adapter = (NewsListRecyclerAdapter) ndf.getRecyclerView().getAdapter(); - if (adapter != null) { - outState.putInt(LIST_ADAPTER_TOTAL_COUNT, adapter.getTotalItemCount()); - outState.putInt(LIST_ADAPTER_PAGE_COUNT, adapter.getCachedPages()); - } - } - if (mSearchView != null) { - mSearchString = mSearchView.getQuery().toString(); - outState.putString(SEARCH_KEY, mSearchString); - } - } - - private void restoreInstanceState(Bundle savedInstanceState) { - if (savedInstanceState.containsKey(ID_FEED_STRING) && - savedInstanceState.containsKey(IS_FOLDER_BOOLEAN) && - savedInstanceState.containsKey(OPTIONAL_FOLDER_ID)) { - - NewsListRecyclerAdapter adapter = new NewsListRecyclerAdapter(this, getNewsReaderDetailFragment().binding.list, this, mPostDelayHandler, mPrefs); - - adapter.setTotalItemCount(savedInstanceState.getInt(LIST_ADAPTER_TOTAL_COUNT)); - adapter.setCachedPages(savedInstanceState.getInt(LIST_ADAPTER_PAGE_COUNT)); - - getNewsReaderDetailFragment() - .getRecyclerView() - .setAdapter(adapter); - - updateDetailFragment(savedInstanceState.getLong(ID_FEED_STRING), - savedInstanceState.getBoolean(IS_FOLDER_BOOLEAN), - savedInstanceState.getLong(OPTIONAL_FOLDER_ID), - false); - } - mSearchString = savedInstanceState.getString(SEARCH_KEY, null); - } - - @Override - public void onConfigurationChanged(@NonNull Configuration newConfig) { - super.onConfigurationChanged(newConfig); - if (drawerToggle != null) { - drawerToggle.onConfigurationChanged(newConfig); - } - } - void showChangelogIfNecessary() { // on first app start with new version - always show the changelog int currentVersionCode = BuildConfig.VERSION_CODE; @@ -566,12 +579,16 @@ private boolean syncFinishedHandler() { @Override protected void onResume() { + mBackOpensDrawer = mPrefs.getBoolean(SettingsActivity.CB_PREF_BACK_OPENS_DRAWER, false); + onBackPressedCallback.setEnabled(mBackOpensDrawer); + NewsReaderListFragment newsReaderListFragment = getSlidingListFragment(); if (newsReaderListFragment != null) { - newsReaderListFragment.reloadAdapter(); + newsReaderListFragment.reloadAdapter(); newsReaderListFragment.bindUserInfoToUI(); } - invalidateOptionsMenu(); + + invalidateOptionsMenu(); super.onResume(); } @@ -888,30 +905,6 @@ public boolean onMenuItemActionCollapse(MenuItem item) { return true; } - // remove this method overide as no longer works when using the new android predictive back gestures - // cleanup once confirmed new approach is working as expected -/* - @Override - public void onBackPressed() { - Log.e(TAG,"NewsReaderListActivity onBackPressed() Called"); - if (!handlePodcastBackPressed()) { - - //Add in check for preference to determine if closes app or opens drawer - if (mPrefs.getBoolean(SettingsActivity.CB_PREF_BACK_OPENS_DRAWER,false)) { //change this line to the preference test - or add into an AND in the below iff - if (binding.drawerLayout != null) { - if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) - //super.onBackPressed(); - //else - binding.drawerLayout.openDrawer(GravityCompat.START); - } else { - //super.onBackPressed(); - } - }else { - super.onBackPressed(); - } - } - } -*/ public static final int RESULT_SETTINGS = 15642; private void syncMenuItemUnreadOnly() {