Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back button preference and functionality to open sidebar #1304

Merged
merged 7 commits into from
Oct 31, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import android.widget.SearchView;
import android.widget.Toast;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.ActionBarDrawerToggle;
Expand Down Expand Up @@ -77,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;
Expand Down Expand Up @@ -138,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<Long> stayUnreadItems = new HashSet<>();
public static HashSet<Long> stayUnreadItems = new HashSet<>();

private MenuItem menuItemOnlyUnread;
private MenuItem menuItemDownloadMoreItems;
Expand All @@ -148,6 +150,8 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
@VisibleForTesting(otherwise = PROTECTED)
public ActivityNewsreaderBinding binding;

private boolean mBackOpensDrawer = false;

//private ServiceConnection mConnection = null;

private ActionBarDrawerToggle drawerToggle;
Expand Down Expand Up @@ -196,6 +200,40 @@ private boolean isUserLoggedIn() {
return (mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, null) != null);
}

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);
}
};

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);
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
((NewsReaderApplication) getApplication()).getAppComponent().injectActivity(this);
Expand Down Expand Up @@ -243,6 +281,7 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
onBackPressedCallback.setEnabled(mBackOpensDrawer);

syncState();
}
Expand All @@ -252,6 +291,9 @@ public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
reloadCountNumbersOfSlidingPaneAdapter();

// -> handleOnBackPressed() will disable it
// onBackPressedCallback.setEnabled(false);

syncState();
}
};
Expand All @@ -266,10 +308,13 @@ 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);
}

Expand Down Expand Up @@ -304,13 +349,13 @@ private void saveInstanceState(Bundle outState) {
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)) {
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);

Expand Down Expand Up @@ -537,12 +582,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();
}

Expand Down Expand Up @@ -859,13 +908,6 @@ public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}

@Override
public void onBackPressed() {
if (!handlePodcastBackPressed()) {
super.onBackPressed();
}
}

public static final int RESULT_SETTINGS = 15642;

private void syncMenuItemUnreadOnly() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class SettingsActivity extends AppCompatActivity {

public static final String CB_MARK_AS_READ_WHILE_SCROLLING_STRING = "cb_MarkAsReadWhileScrolling";
public static final String CB_SHOW_FAST_ACTIONS = "cb_ShowFastActions";
public static final String CB_PREF_BACK_OPENS_DRAWER = "cb_prefBackButtonOpensDrawer";
public static final String CB_DISABLE_HOSTNAME_VERIFICATION_STRING = "cb_DisableHostnameVerification";
public static final String CB_SKIP_DETAILVIEW_AND_OPEN_BROWSER_DIRECTLY_STRING = "cb_openInBrowserDirectly";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SWIPE_LEFT_ACTION;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SWIPE_RIGHT_ACTION;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SYNC_INTERVAL_IN_MINUTES_STRING_DEPRECATED;
import static de.luhmer.owncloudnewsreader.SettingsActivity.CB_PREF_BACK_OPENS_DRAWER;

import android.accounts.Account;
import android.accounts.AccountManager;
Expand Down Expand Up @@ -239,6 +240,7 @@ private void bindGeneralPreferences(DialogPreference.TargetFragment prefFrag)
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_MARK_AS_READ_WHILE_SCROLLING_STRING));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_SHOW_FAST_ACTIONS));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_SKIP_DETAILVIEW_AND_OPEN_BROWSER_DIRECTLY_STRING));
bindPreferenceBooleanToValue(prefFrag.findPreference(CB_PREF_BACK_OPENS_DRAWER));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SORT_ORDER));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SEARCH_IN));
bindPreferenceSummaryToValue(prefFrag.findPreference(SP_SWIPE_RIGHT_ACTION));
Expand Down
1 change: 1 addition & 0 deletions News-Android-App/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
<string name="pref_title_MarkAsReadWhileScrolling">Mark as read while scrolling</string>
<string name="pref_title_ShowFastActions">Activate fast access functions</string>
<string name="pref_title_OpenInBrowserDirectly">Skip detailed view and open article in the browser</string>
<string name="pref_title_BackButtonOpensDrawer">Back button opens drawer</string>

<!-- MemorizingTrustManager -->
<string name="mtm_accept_cert">Accept Unknown Certificate?</string>
Expand Down
5 changes: 5 additions & 0 deletions News-Android-App/src/main/res/xml/pref_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
android:title="@string/pref_title_OpenInBrowserDirectly"
app:iconSpaceReserved="false"/>

<SwitchPreference
android:key="cb_prefBackButtonOpensDrawer"
android:title="@string/pref_title_BackButtonOpensDrawer"
app:iconSpaceReserved="false" />

<ListPreference
android:defaultValue="1"
android:entries="@array/pref_general_swipe_action"
Expand Down