From 7ea9379e59631ff150b1dcbbcf9922b31c368af0 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 24 Oct 2018 10:34:02 +0000 Subject: [PATCH] Check if answers defined --- .../eu/faircode/email/AdapterMessage.java | 79 ++++++++++++------- .../eu/faircode/email/FragmentMessages.java | 2 +- 2 files changed, 51 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 0d81bc21..6daafb51 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -63,6 +63,7 @@ import android.widget.TextView; import android.widget.Toast; import com.google.android.material.bottomnavigation.BottomNavigationView; +import com.google.android.material.snackbar.Snackbar; import org.xml.sax.XMLReader; @@ -90,6 +91,8 @@ import androidx.annotation.Nullable; import androidx.appcompat.widget.PopupMenu; import androidx.constraintlayout.widget.Group; import androidx.core.content.ContextCompat; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.Observer; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -103,6 +106,7 @@ import androidx.recyclerview.widget.RecyclerView; public class AdapterMessage extends PagedListAdapter { private Context context; private LifecycleOwner owner; + private FragmentManager fragmentManager; private ViewType viewType; private IProperties properties; @@ -906,40 +910,56 @@ public class AdapterMessage extends PagedListAdapter>() { @Override public void onChanged(List answers) { - final Collator collator = Collator.getInstance(Locale.getDefault()); - collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc + if (answers == null || answers.size() == 0) { + Snackbar snackbar = Snackbar.make( + itemView, + context.getString(R.string.title_no_answers), + Snackbar.LENGTH_LONG); + snackbar.setAction(R.string.title_fix, new View.OnClickListener() { + @Override + public void onClick(View v) { + FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); + fragmentTransaction.replace(R.id.content_frame, new FragmentAnswers()).addToBackStack("answers"); + fragmentTransaction.commit(); + } + }); + snackbar.show(); + } else { + final Collator collator = Collator.getInstance(Locale.getDefault()); + collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc - Collections.sort(answers, new Comparator() { - @Override - public int compare(EntityAnswer a1, EntityAnswer a2) { - return collator.compare(a1.name, a2.name); - } - }); + Collections.sort(answers, new Comparator() { + @Override + public int compare(EntityAnswer a1, EntityAnswer a2) { + return collator.compare(a1.name, a2.name); + } + }); - View anchor = bnvActions.findViewById(R.id.action_more); - PopupMenu popupMenu = new PopupMenu(context, anchor); + View anchor = bnvActions.findViewById(R.id.action_more); + PopupMenu popupMenu = new PopupMenu(context, anchor); - int order = 0; - for (EntityAnswer answer : answers) - popupMenu.getMenu().add(Menu.NONE, answer.id.intValue(), order++, answer.name); + int order = 0; + for (EntityAnswer answer : answers) + popupMenu.getMenu().add(Menu.NONE, answer.id.intValue(), order++, answer.name); - popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { - @Override - public boolean onMenuItemClick(MenuItem target) { - if (Helper.isPro(context)) - context.startActivity(new Intent(context, ActivityCompose.class) - .putExtra("action", "reply") - .putExtra("reference", data.message.id) - .putExtra("answer", (long) target.getItemId())); - else { - LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); - lbm.sendBroadcast(new Intent(ActivityView.ACTION_SHOW_PRO)); + popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem target) { + if (Helper.isPro(context)) + context.startActivity(new Intent(context, ActivityCompose.class) + .putExtra("action", "reply") + .putExtra("reference", data.message.id) + .putExtra("answer", (long) target.getItemId())); + else { + LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); + lbm.sendBroadcast(new Intent(ActivityView.ACTION_SHOW_PRO)); + } + return true; } - return true; - } - }); + }); - popupMenu.show(); + popupMenu.show(); + } db.answer().liveAnswers().removeObservers(owner); } @@ -1345,10 +1365,11 @@ public class AdapterMessage extends PagedListAdapter