From dcd6b7c3849c6f62948ad1faa64d665b26cbd51c Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 20 Oct 2018 14:42:03 +0000 Subject: [PATCH] Use bottom bar for previous/next navigation --- .../java/eu/faircode/email/ActivityView.java | 7 ++- .../eu/faircode/email/FragmentMessages.java | 48 +++++++++++++------ .../eu/faircode/email/ViewModelMessages.java | 36 ++++++++++++++ .../drawable/baseline_navigate_before_24.xml | 10 ++++ .../drawable/baseline_navigate_next_24.xml | 10 ++++ app/src/main/res/layout/fragment_messages.xml | 15 +++++- app/src/main/res/menu/action_messages.xml | 13 +++++ 7 files changed, 123 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/eu/faircode/email/ViewModelMessages.java create mode 100644 app/src/main/res/drawable/baseline_navigate_before_24.xml create mode 100644 app/src/main/res/drawable/baseline_navigate_next_24.xml create mode 100644 app/src/main/res/menu/action_messages.xml diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index a172472d..6a57c6f9 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -74,6 +74,7 @@ import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Observer; +import androidx.lifecycle.ViewModelProviders; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.paging.PagedList; @@ -303,8 +304,10 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB intent.setAction(null); setIntent(intent); + ViewModelMessages model = ViewModelProviders.of(this).get(ViewModelMessages.class); + model.setMessages(null); + intent.putExtra("thread", action.split(":", 2)[1]); - getSupportFragmentManager().popBackStack("unified", 0); onViewThread(intent); } } @@ -767,6 +770,8 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB } private void onViewThread(Intent intent) { + getSupportFragmentManager().popBackStack("thread", FragmentManager.POP_BACK_STACK_INCLUSIVE); + Bundle args = new Bundle(); args.putLong("account", intent.getLongExtra("account", -1)); args.putString("thread", intent.getStringExtra("thread")); diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 7edc6626..1a6ebcfa 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -39,6 +39,7 @@ import android.widget.ImageButton; import android.widget.ProgressBar; import android.widget.TextView; +import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; @@ -56,6 +57,7 @@ import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.LiveData; import androidx.lifecycle.Observer; +import androidx.lifecycle.ViewModelProviders; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.paging.LivePagedListBuilder; import androidx.paging.PagedList; @@ -70,6 +72,7 @@ public class FragmentMessages extends FragmentEx { private ImageButton ibHintActions; private RecyclerView rvMessage; private TextView tvNoEmail; + private BottomNavigationView bottom_navigation; private ProgressBar pbWait; private Group grpSupport; private Group grpHintSupport; @@ -139,6 +142,7 @@ public class FragmentMessages extends FragmentEx { ibHintActions = view.findViewById(R.id.ibHintActions); rvMessage = view.findViewById(R.id.rvFolder); tvNoEmail = view.findViewById(R.id.tvNoEmail); + bottom_navigation = view.findViewById(R.id.bottom_navigation); pbWait = view.findViewById(R.id.pbWait); grpSupport = view.findViewById(R.id.grpSupport); grpHintSupport = view.findViewById(R.id.grpHintSupport); @@ -457,6 +461,20 @@ public class FragmentMessages extends FragmentEx { } }).attachToRecyclerView(rvMessage); + bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { + @Override + public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { + String[] pn = (String[]) bottom_navigation.getTag(); + String thread = (menuItem.getItemId() == R.id.action_prev ? pn[0] : pn[1]); + LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext()); + lbm.sendBroadcast( + new Intent(ActivityView.ACTION_VIEW_THREAD) + .putExtra("account", account) + .putExtra("thread", thread)); + return true; + } + }); + fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -467,19 +485,9 @@ public class FragmentMessages extends FragmentEx { } }); - View.OnClickListener navigate = new View.OnClickListener() { - @Override - public void onClick(View v) { - LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext()); - lbm.sendBroadcast( - new Intent(ActivityView.ACTION_VIEW_THREAD) - .putExtra("account", account) - .putExtra("thread", (String) v.getTag())); - } - }; - // Initialize tvNoEmail.setVisibility(View.GONE); + bottom_navigation.setVisibility(View.GONE); grpReady.setVisibility(View.GONE); pbWait.setVisibility(View.VISIBLE); @@ -576,8 +584,16 @@ public class FragmentMessages extends FragmentEx { // Messages loadMessages(); - // Compose FAB - if (viewType != AdapterMessage.ViewType.THREAD) { + if (viewType == AdapterMessage.ViewType.THREAD) { + // Navigation + ViewModelMessages model = ViewModelProviders.of(getActivity()).get(ViewModelMessages.class); + String[] pn = model.getPrevNext(thread); + bottom_navigation.setTag(pn); + bottom_navigation.getMenu().findItem(R.id.action_prev).setEnabled(pn[0] != null); + bottom_navigation.getMenu().findItem(R.id.action_next).setEnabled(pn[1] != null); + bottom_navigation.setVisibility(pn[0] == null && pn[1] == null ? View.GONE : View.VISIBLE); + } else { + // Compose FAB Bundle args = new Bundle(); args.putLong("account", account); @@ -832,7 +848,7 @@ public class FragmentMessages extends FragmentEx { return; } - if (viewType == AdapterMessage.ViewType.THREAD) + if (viewType == AdapterMessage.ViewType.THREAD) { if (autoExpand) { autoExpand = false; @@ -886,6 +902,10 @@ public class FragmentMessages extends FragmentEx { finish(); } } + } else { + ViewModelMessages model = ViewModelProviders.of(getActivity()).get(ViewModelMessages.class); + model.setMessages(messages); + } Log.i(Helper.TAG, "Submit messages=" + messages.size()); adapter.submitList(messages); diff --git a/app/src/main/java/eu/faircode/email/ViewModelMessages.java b/app/src/main/java/eu/faircode/email/ViewModelMessages.java new file mode 100644 index 00000000..baa22ee4 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/ViewModelMessages.java @@ -0,0 +1,36 @@ +package eu.faircode.email; + +import androidx.lifecycle.ViewModel; +import androidx.paging.PagedList; + +public class ViewModelMessages extends ViewModel { + private PagedList messages = null; + + void setMessages(PagedList messages) { + this.messages = messages; + } + + String[] getPrevNext(String thread) { + if (messages == null) + return new String[]{null, null}; + + boolean found = false; + TupleMessageEx prev = null; + TupleMessageEx next = null; + for (int i = 0; i < messages.size(); i++) { + TupleMessageEx item = messages.get(i); + if (item == null) + continue; + if (found) { + next = item; + messages.loadAround(i); + break; + } + if (thread.equals(item.thread)) + found = true; + else + prev = item; + } + return new String[]{prev == null ? null : prev.thread, next == null ? null : next.thread}; + } +} diff --git a/app/src/main/res/drawable/baseline_navigate_before_24.xml b/app/src/main/res/drawable/baseline_navigate_before_24.xml new file mode 100644 index 00000000..200be2d1 --- /dev/null +++ b/app/src/main/res/drawable/baseline_navigate_before_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/baseline_navigate_next_24.xml b/app/src/main/res/drawable/baseline_navigate_next_24.xml new file mode 100644 index 00000000..a9c50783 --- /dev/null +++ b/app/src/main/res/drawable/baseline_navigate_next_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/fragment_messages.xml b/app/src/main/res/layout/fragment_messages.xml index 4378c0b5..180f92d5 100644 --- a/app/src/main/res/layout/fragment_messages.xml +++ b/app/src/main/res/layout/fragment_messages.xml @@ -110,7 +110,7 @@ android:layout_height="0dp" android:scrollbarStyle="outsideOverlay" android:scrollbars="vertical" - app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintBottom_toTopOf="@+id/bottom_navigation" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/vSeparatorHintActions" /> @@ -126,6 +126,19 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> + + + + + + + +