From 46feb7bf42a94ec2eee8d890d9c9c75f26412b31 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 23 Feb 2018 12:53:47 +0100 Subject: [PATCH] handle view conversation action when ConversationFragment is already open --- .../ui/ConversationActivity.java | 17 +++++-- .../ui/ConversationFragment.java | 6 +++ .../ui/ConversationsOverviewFragment.java | 48 ++++++++++--------- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index 4d15be96a..d4f0ffa20 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -168,17 +168,24 @@ public class ConversationActivity extends XmppActivity implements OnConversation final boolean mainNeedsRefresh; if (conversationFragment == null) { mainNeedsRefresh = false; - conversationFragment = new ConversationFragment(); - FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); - fragmentTransaction.replace(R.id.main_fragment, conversationFragment); - fragmentTransaction.addToBackStack(null); - fragmentTransaction.commit(); + Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment); + if (mainFragment != null && mainFragment instanceof ConversationFragment) { + conversationFragment = (ConversationFragment) mainFragment; + } else { + conversationFragment = new ConversationFragment(); + FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); + fragmentTransaction.replace(R.id.main_fragment, conversationFragment); + fragmentTransaction.addToBackStack(null); + fragmentTransaction.commit(); + } } else { mainNeedsRefresh = true; } conversationFragment.reInit(conversation); if (mainNeedsRefresh) { refreshFragment(R.id.main_fragment); + } else { + invalidateActionBarTitle(); } } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index b15dec1cb..c6fb891c7 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -727,6 +727,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } } + @Override + public void onDetach() { + super.onDetach(); + this.activity = null; + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java index 004f22eed..91bf9f64a 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java @@ -54,18 +54,28 @@ import eu.siacs.conversations.ui.util.PendingItem; public class ConversationsOverviewFragment extends XmppFragment implements EnhancedListView.OnDismissCallback { - private FragmentConversationsOverviewBinding binding; - private final List conversations = new ArrayList<>(); + private final PendingItem swipedConversation = new PendingItem<>(); + private FragmentConversationsOverviewBinding binding; private ConversationAdapter conversationsAdapter; private XmppActivity activity; - private final PendingItem swipedConversation = new PendingItem<>(); + public static Conversation getSuggestion(Activity activity) { + Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment); + if (fragment != null && fragment instanceof ConversationsOverviewFragment) { + List conversations = ((ConversationsOverviewFragment) fragment).conversations; + if (conversations.size() > 0) { + return conversations.get(0); + } + } + return null; + + } @Override public void onAttach(Activity activity) { super.onAttach(activity); - Log.d(Config.LOGTAG,"on attach"); + Log.d(Config.LOGTAG, "on attach"); if (activity instanceof XmppActivity) { this.activity = (XmppActivity) activity; } else { @@ -73,11 +83,17 @@ public class ConversationsOverviewFragment extends XmppFragment implements Enhan } } + @Override + public void onDetach() { + super.onDetach(); + this.activity = null; + } + @Override public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - Log.d(Config.LOGTAG,"onCreateView"); + Log.d(Config.LOGTAG, "onCreateView"); this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversations_overview, container, false); - this.binding.fab.setOnClickListener((view)-> StartConversationActivity.launch(getActivity())); + this.binding.fab.setOnClickListener((view) -> StartConversationActivity.launch(getActivity())); this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations); this.binding.list.setAdapter(this.conversationsAdapter); @@ -86,7 +102,7 @@ public class ConversationsOverviewFragment extends XmppFragment implements Enhan if (activity instanceof OnConversationSelected) { ((OnConversationSelected) activity).onConversationSelected(conversation); } else { - Log.w(ConversationsOverviewFragment.class.getCanonicalName(),"Activity does not implement OnConversationSelected"); + Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected"); } }); this.binding.list.setDismissCallback(this); @@ -101,14 +117,14 @@ public class ConversationsOverviewFragment extends XmppFragment implements Enhan @Override void onBackendConnected() { - Log.d(Config.LOGTAG,"nice!"); + Log.d(Config.LOGTAG, "nice!"); refresh(); } @Override public void onStart() { super.onStart(); - Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onStart()"); + Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onStart()"); if (activity.xmppConnectionService != null) { refresh(); } @@ -117,7 +133,7 @@ public class ConversationsOverviewFragment extends XmppFragment implements Enhan @Override public void onResume() { super.onResume(); - Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onResume()"); + Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onResume()"); } @Override @@ -191,16 +207,4 @@ public class ConversationsOverviewFragment extends XmppFragment implements Enhan } }; } - - public static Conversation getSuggestion(Activity activity) { - Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment); - if (fragment != null && fragment instanceof ConversationsOverviewFragment) { - List conversations = ((ConversationsOverviewFragment) fragment).conversations; - if (conversations.size() > 0) { - return conversations.get(0); - } - } - return null; - - } }