From ee7b41f7b443605b5cdf21b0401903c44df98fbb Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 19 Aug 2018 05:25:49 +0000 Subject: [PATCH] Back to exit distracting free reading --- .../java/eu/faircode/email/ActivityBase.java | 21 ++++++ .../eu/faircode/email/FragmentMessage.java | 74 +++++++++++++------ app/src/main/res/layout/fragment_message.xml | 2 +- 3 files changed, 72 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ActivityBase.java b/app/src/main/java/eu/faircode/email/ActivityBase.java index 9ab157d7..4cb413c0 100644 --- a/app/src/main/java/eu/faircode/email/ActivityBase.java +++ b/app/src/main/java/eu/faircode/email/ActivityBase.java @@ -25,6 +25,9 @@ import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; +import java.util.ArrayList; +import java.util.List; + import androidx.appcompat.app.AppCompatActivity; abstract class ActivityBase extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener { @@ -69,4 +72,22 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc if ("theme".equals(key) || "debug".equals(key)) recreate(); } + + private List backPressedListeners = new ArrayList<>(); + + public void addBackPressedListener(IBackPressedListener listener) { + backPressedListeners.add(listener); + } + + @Override + public void onBackPressed() { + for (IBackPressedListener listener : backPressedListeners) + if (listener.onBackPressed()) + return; + super.onBackPressed(); + } + + public interface IBackPressedListener { + boolean onBackPressed(); + } } diff --git a/app/src/main/java/eu/faircode/email/FragmentMessage.java b/app/src/main/java/eu/faircode/email/FragmentMessage.java index f7e70c14..5be144b7 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessage.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessage.java @@ -200,31 +200,39 @@ public class FragmentMessage extends FragmentEx { }); fab.setOnClickListener(new View.OnClickListener() { - private boolean addresses; - @Override public void onClick(View view) { - free = (top_navigation.getVisibility() != View.GONE); + tvCount.setTag(tvCount.getVisibility()); + tvError.setTag(tvError.getVisibility()); + tvCc.setTag(grpAddresses.getVisibility()); + rvAttachment.setTag(grpAddresses.getVisibility()); + + free = true; getActivity().invalidateOptionsMenu(); grpHeader.setVisibility(free ? View.GONE : View.VISIBLE); - if (free) { - tvCount.setTag(tvCount.getVisibility()); - tvError.setTag(tvError.getVisibility()); - fab.setImageResource(R.drawable.baseline_fullscreen_exit_24); - addresses = (grpAddresses.getVisibility() != View.GONE); - grpAddresses.setVisibility(View.GONE); - grpAttachments.setVisibility(View.GONE); - tvCount.setVisibility(View.GONE); - tvError.setVisibility(View.GONE); - } else { - fab.setImageResource(R.drawable.baseline_fullscreen_24); - if (addresses) - grpAddresses.setVisibility(View.VISIBLE); - if (rvAttachment.getAdapter().getItemCount() > 0) - grpAttachments.setVisibility(View.VISIBLE); - tvCount.setVisibility((int) tvCount.getVisibility()); - tvError.setVisibility((int) tvError.getVisibility()); + grpAddresses.setVisibility(View.GONE); + grpAttachments.setVisibility(View.GONE); + tvCount.setVisibility(View.GONE); + tvError.setVisibility(View.GONE); + fab.setVisibility(View.GONE); + } + }); + + ((ActivityBase) getActivity()).addBackPressedListener(new ActivityBase.IBackPressedListener() { + @Override + public boolean onBackPressed() { + if (free && isVisible()) { + free = false; + getActivity().invalidateOptionsMenu(); + grpHeader.setVisibility(free ? View.GONE : View.VISIBLE); + grpAddresses.setVisibility((int) tvCc.getTag()); + rvAttachment.setVisibility((int) rvAttachment.getTag()); + tvCount.setVisibility((int) tvCount.getTag()); + tvError.setVisibility((int) tvError.getTag()); + fab.setVisibility(View.VISIBLE); + return true; } + return false; } }); @@ -259,6 +267,7 @@ public class FragmentMessage extends FragmentEx { grpMessage.setVisibility(View.GONE); tvCount.setVisibility(View.GONE); tvError.setVisibility(View.GONE); + fab.setVisibility(View.GONE); pbWait.setVisibility(View.VISIBLE); rvAttachment.setHasFixedSize(false); @@ -272,7 +281,15 @@ public class FragmentMessage extends FragmentEx { } @Override - public void onActivityCreated(@Nullable Bundle savedInstanceState) { + public void onSaveInstanceState(Bundle outState) { + outState.putBoolean("free", free); + outState.putInt("addresses", grpAddresses.getVisibility()); + outState.putInt("attachments", rvAttachment.getVisibility()); + super.onSaveInstanceState(outState); + } + + @Override + public void onActivityCreated(@Nullable final Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // Get arguments @@ -376,11 +393,20 @@ public class FragmentMessage extends FragmentEx { }); } + if (savedInstanceState != null) { + free = savedInstanceState.getBoolean("free"); + grpAddresses.setTag(savedInstanceState.getInt("addresses")); + rvAttachment.setTag(savedInstanceState.getInt("attachments")); + } + pbWait.setVisibility(View.GONE); - grpHeader.setVisibility(View.VISIBLE); + grpHeader.setVisibility(free ? View.GONE : View.VISIBLE); + if (free) + grpAddresses.setVisibility(View.GONE); grpMessage.setVisibility(View.VISIBLE); - tvCount.setVisibility(message.count > 1 ? View.VISIBLE : View.GONE); - tvError.setVisibility(message.error == null ? View.GONE : View.VISIBLE); + tvCount.setVisibility(!free && message.count > 1 ? View.VISIBLE : View.GONE); + tvError.setVisibility(free || message.error == null ? View.GONE : View.VISIBLE); + fab.setVisibility(free ? View.GONE : View.VISIBLE); } }); diff --git a/app/src/main/res/layout/fragment_message.xml b/app/src/main/res/layout/fragment_message.xml index 4abc502f..8dc16994 100644 --- a/app/src/main/res/layout/fragment_message.xml +++ b/app/src/main/res/layout/fragment_message.xml @@ -292,5 +292,5 @@ android:id="@+id/grpMessage" android:layout_width="0dp" android:layout_height="0dp" - app:constraint_referenced_ids="scroll,fab,bottom_navigation" /> + app:constraint_referenced_ids="scroll,bottom_navigation" />