handle view conversation action when ConversationFragment is already open

This commit is contained in:
Daniel Gultsch 2018-02-23 12:53:47 +01:00
parent 6cfc6af886
commit 46feb7bf42
3 changed files with 44 additions and 27 deletions

View File

@ -168,17 +168,24 @@ public class ConversationActivity extends XmppActivity implements OnConversation
final boolean mainNeedsRefresh; final boolean mainNeedsRefresh;
if (conversationFragment == null) { if (conversationFragment == null) {
mainNeedsRefresh = false; mainNeedsRefresh = false;
Fragment mainFragment = getFragmentManager().findFragmentById(R.id.main_fragment);
if (mainFragment != null && mainFragment instanceof ConversationFragment) {
conversationFragment = (ConversationFragment) mainFragment;
} else {
conversationFragment = new ConversationFragment(); conversationFragment = new ConversationFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_fragment, conversationFragment); fragmentTransaction.replace(R.id.main_fragment, conversationFragment);
fragmentTransaction.addToBackStack(null); fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit(); fragmentTransaction.commit();
}
} else { } else {
mainNeedsRefresh = true; mainNeedsRefresh = true;
} }
conversationFragment.reInit(conversation); conversationFragment.reInit(conversation);
if (mainNeedsRefresh) { if (mainNeedsRefresh) {
refreshFragment(R.id.main_fragment); refreshFragment(R.id.main_fragment);
} else {
invalidateActionBarTitle();
} }
} }

View File

@ -727,6 +727,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
} }
} }
@Override
public void onDetach() {
super.onDetach();
this.activity = null;
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);

View File

@ -54,18 +54,28 @@ import eu.siacs.conversations.ui.util.PendingItem;
public class ConversationsOverviewFragment extends XmppFragment implements EnhancedListView.OnDismissCallback { public class ConversationsOverviewFragment extends XmppFragment implements EnhancedListView.OnDismissCallback {
private FragmentConversationsOverviewBinding binding;
private final List<Conversation> conversations = new ArrayList<>(); private final List<Conversation> conversations = new ArrayList<>();
private final PendingItem<Conversation> swipedConversation = new PendingItem<>();
private FragmentConversationsOverviewBinding binding;
private ConversationAdapter conversationsAdapter; private ConversationAdapter conversationsAdapter;
private XmppActivity activity; private XmppActivity activity;
private final PendingItem<Conversation> 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<Conversation> conversations = ((ConversationsOverviewFragment) fragment).conversations;
if (conversations.size() > 0) {
return conversations.get(0);
}
}
return null;
}
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
super.onAttach(activity); super.onAttach(activity);
Log.d(Config.LOGTAG,"on attach"); Log.d(Config.LOGTAG, "on attach");
if (activity instanceof XmppActivity) { if (activity instanceof XmppActivity) {
this.activity = (XmppActivity) activity; this.activity = (XmppActivity) activity;
} else { } else {
@ -73,11 +83,17 @@ public class ConversationsOverviewFragment extends XmppFragment implements Enhan
} }
} }
@Override
public void onDetach() {
super.onDetach();
this.activity = null;
}
@Override @Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 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 = 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.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
this.binding.list.setAdapter(this.conversationsAdapter); this.binding.list.setAdapter(this.conversationsAdapter);
@ -86,7 +102,7 @@ public class ConversationsOverviewFragment extends XmppFragment implements Enhan
if (activity instanceof OnConversationSelected) { if (activity instanceof OnConversationSelected) {
((OnConversationSelected) activity).onConversationSelected(conversation); ((OnConversationSelected) activity).onConversationSelected(conversation);
} else { } 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); this.binding.list.setDismissCallback(this);
@ -101,14 +117,14 @@ public class ConversationsOverviewFragment extends XmppFragment implements Enhan
@Override @Override
void onBackendConnected() { void onBackendConnected() {
Log.d(Config.LOGTAG,"nice!"); Log.d(Config.LOGTAG, "nice!");
refresh(); refresh();
} }
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onStart()"); Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onStart()");
if (activity.xmppConnectionService != null) { if (activity.xmppConnectionService != null) {
refresh(); refresh();
} }
@ -117,7 +133,7 @@ public class ConversationsOverviewFragment extends XmppFragment implements Enhan
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onResume()"); Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onResume()");
} }
@Override @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<Conversation> conversations = ((ConversationsOverviewFragment) fragment).conversations;
if (conversations.size() > 0) {
return conversations.get(0);
}
}
return null;
}
} }