From a15a10a8a15bee1547fb4ee1f74e116a9bea28cc Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 4 Mar 2020 11:14:20 +0100 Subject: [PATCH] Added option to hide action bar on scrolling down --- .../java/eu/faircode/email/FragmentBase.java | 33 +++++++++++++++++++ .../eu/faircode/email/FragmentFolders.java | 28 +++++++++++++++- .../eu/faircode/email/FragmentMessages.java | 29 ++++++++++++++++ .../email/FragmentOptionsBehavior.java | 12 ++++++- .../res/layout/fragment_options_behavior.xml | 14 +++++++- app/src/main/res/values/strings.xml | 1 + 6 files changed, 114 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentBase.java b/app/src/main/java/eu/faircode/email/FragmentBase.java index 2a190552ab..5858884f02 100644 --- a/app/src/main/java/eu/faircode/email/FragmentBase.java +++ b/app/src/main/java/eu/faircode/email/FragmentBase.java @@ -27,6 +27,7 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.IntentSender; +import android.content.SharedPreferences; import android.content.res.Configuration; import android.net.Uri; import android.os.Build; @@ -48,6 +49,7 @@ import androidx.documentfile.provider.DocumentFile; import androidx.fragment.app.Fragment; import androidx.lifecycle.Lifecycle; import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import androidx.preference.PreferenceManager; import java.io.File; import java.io.FileInputStream; @@ -64,6 +66,7 @@ import static android.app.Activity.RESULT_OK; public class FragmentBase extends Fragment { private String title = null; private String subtitle = " "; + private boolean action = true; private boolean finish = false; private long message = -1; @@ -94,6 +97,28 @@ public class FragmentBase extends Fragment { updateSubtitle(); } + protected void setActionBar(boolean show) { + Log.i("Set action bar=" + show); + + AppCompatActivity activity = (AppCompatActivity) getActivity(); + if (activity == null) + return; + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity); + boolean hide_actionbar = prefs.getBoolean("hide_actionbar", false); + + ActionBar actionBar = activity.getSupportActionBar(); + if (actionBar == null) + return; + + if (show || !hide_actionbar) + actionBar.show(); + else + actionBar.hide(); + + this.action = show; + } + @Override public void startActivity(Intent intent) { try { @@ -176,6 +201,7 @@ public class FragmentBase extends Fragment { Log.d("Resume " + this); super.onResume(); updateSubtitle(); + setActionBar(action); if (finish) { getParentFragmentManager().popBackStack(); finish = false; @@ -230,6 +256,13 @@ public class FragmentBase extends Fragment { View focused = getActivity().getCurrentFocus(); if (focused != null) im.hideSoftInputFromWindow(focused.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); + + AppCompatActivity activity = (AppCompatActivity) getActivity(); + if (activity != null) { + ActionBar actionBar = activity.getSupportActionBar(); + if (actionBar != null) + actionBar.show(); + } } @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentFolders.java b/app/src/main/java/eu/faircode/email/FragmentFolders.java index 433dcc7461..d57ed38b8c 100644 --- a/app/src/main/java/eu/faircode/email/FragmentFolders.java +++ b/app/src/main/java/eu/faircode/email/FragmentFolders.java @@ -155,9 +155,35 @@ public class FragmentFolders extends FragmentBase { }); rvFolder.setHasFixedSize(false); - LinearLayoutManager llm = new LinearLayoutManager(getContext()); + LinearLayoutManager llm = new LinearLayoutManager(getContext()) { + @Override + public void onScrollStateChanged(int state) { + super.onScrollStateChanged(state); + + try { + int y = rvFolder.computeVerticalScrollOffset(); + Log.i("Scroll state=" + state + " y=" + y); + setActionBar(y == 0); + } catch (Throwable ex) { + Log.w(ex); + } + } + }; rvFolder.setLayoutManager(llm); + rvFolder.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { + @Override + public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { + try { + int y = rvFolder.computeVerticalScrollOffset(); + Log.i("Layout completed y=" + y); + setActionBar(y == 0); + } catch (Throwable ex) { + Log.w(ex); + } + } + }); + if (!cards) { DividerItemDecoration itemDecorator = new DividerItemDecoration(getContext(), llm.getOrientation()) { @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 42fa8ca863..b68966de7a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -90,6 +90,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.PopupMenu; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.Group; @@ -202,6 +203,8 @@ import static android.os.Process.THREAD_PRIORITY_BACKGROUND; import static android.text.format.DateUtils.DAY_IN_MILLIS; import static android.text.format.DateUtils.FORMAT_SHOW_DATE; import static android.text.format.DateUtils.FORMAT_SHOW_WEEKDAY; +import static androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE; +import static androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_SETTLING; import static org.openintents.openpgp.OpenPgpSignatureResult.RESULT_KEY_MISSING; import static org.openintents.openpgp.OpenPgpSignatureResult.RESULT_NO_SIGNATURE; import static org.openintents.openpgp.OpenPgpSignatureResult.RESULT_VALID_KEY_CONFIRMED; @@ -552,9 +555,35 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. Log.w(ex); } } + + @Override + public void onScrollStateChanged(int state) { + super.onScrollStateChanged(state); + + try { + int y = rvMessage.computeVerticalScrollOffset(); + Log.i("Scroll state=" + state + " y=" + y); + setActionBar(y == 0); + } catch (Throwable ex) { + Log.w(ex); + } + } }; rvMessage.setLayoutManager(llm); + rvMessage.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { + @Override + public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { + try { + int y = rvMessage.computeVerticalScrollOffset(); + Log.i("Layout completed y=" + y); + setActionBar(y == 0); + } catch (Throwable ex) { + Log.w(ex); + } + } + }); + if (!cards) { DividerItemDecoration itemDecorator = new DividerItemDecoration(getContext(), llm.getOrientation()) { @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsBehavior.java b/app/src/main/java/eu/faircode/email/FragmentOptionsBehavior.java index fd0ea65857..3af441ce29 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsBehavior.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsBehavior.java @@ -43,6 +43,7 @@ import androidx.preference.PreferenceManager; public class FragmentOptionsBehavior extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener { private SwitchCompat swDoubleBack; private SwitchCompat swPull; + private SwitchCompat swHideActionBar; private SwitchCompat swAutoScroll; private SwitchCompat swDoubleTap; private SwitchCompat swSwipeNav; @@ -65,7 +66,7 @@ public class FragmentOptionsBehavior extends FragmentBase implements SharedPrefe private NumberPicker npDefaultSnooze; private final static String[] RESET_OPTIONS = new String[]{ - "double_back", "pull", "autoscroll", "doubletap", "swipenav", "volumenav", "reversed", + "double_back", "pull", "hide_actionbar", "autoscroll", "doubletap", "swipenav", "volumenav", "reversed", "autoexpand", "expand_all", "expand_one", "collapse_multiple", "autoclose", "onclose", "quick_filter", "quick_scroll", "autoread", "flag_snoozed", "autounflag", "auto_important", "reset_importance", "discard_delete", @@ -84,6 +85,7 @@ public class FragmentOptionsBehavior extends FragmentBase implements SharedPrefe swDoubleBack = view.findViewById(R.id.swDoubleBack); swPull = view.findViewById(R.id.swPull); + swHideActionBar = view.findViewById(R.id.swHideActionBar); swAutoScroll = view.findViewById(R.id.swAutoScroll); swDoubleTap = view.findViewById(R.id.swDoubleTap); swSwipeNav = view.findViewById(R.id.swSwipeNav); @@ -128,6 +130,13 @@ public class FragmentOptionsBehavior extends FragmentBase implements SharedPrefe } }); + swHideActionBar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { + prefs.edit().putBoolean("hide_actionbar", checked).apply(); + } + }); + swAutoScroll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { @@ -330,6 +339,7 @@ public class FragmentOptionsBehavior extends FragmentBase implements SharedPrefe swDoubleBack.setChecked(prefs.getBoolean("double_back", true)); swPull.setChecked(prefs.getBoolean("pull", true)); + swHideActionBar.setChecked(prefs.getBoolean("hide_actionbar", false)); swAutoScroll.setChecked(prefs.getBoolean("autoscroll", true)); swDoubleTap.setChecked(prefs.getBoolean("doubletap", false)); swSwipeNav.setChecked(prefs.getBoolean("swipenav", true)); diff --git a/app/src/main/res/layout/fragment_options_behavior.xml b/app/src/main/res/layout/fragment_options_behavior.xml index b86a98c8f1..0dacf3807c 100644 --- a/app/src/main/res/layout/fragment_options_behavior.xml +++ b/app/src/main/res/layout/fragment_options_behavior.xml @@ -40,15 +40,27 @@ app:layout_constraintTop_toBottomOf="@id/swDoubleBack" app:switchPadding="12dp" /> + + Double \'back\' to exit Pull down to refresh + Hide action bar on scrolling down Scroll to top on receiving new messages Double tap to mark message read/unread Swipe left/right to go to next/previous conversation