ConversationsOverviewFragment.java

  1/*
  2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
  3 *
  4 * Redistribution and use in source and binary forms, with or without modification,
  5 * are permitted provided that the following conditions are met:
  6 *
  7 * 1. Redistributions of source code must retain the above copyright notice, this
  8 * list of conditions and the following disclaimer.
  9 *
 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
 11 * this list of conditions and the following disclaimer in the documentation and/or
 12 * other materials provided with the distribution.
 13 *
 14 * 3. Neither the name of the copyright holder nor the names of its contributors
 15 * may be used to endorse or promote products derived from this software without
 16 * specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28 */
 29
 30package eu.siacs.conversations.ui;
 31
 32import android.app.Activity;
 33import android.app.Fragment;
 34import android.databinding.DataBindingUtil;
 35import android.os.Bundle;
 36import android.util.Log;
 37import android.view.LayoutInflater;
 38import android.view.View;
 39import android.view.ViewGroup;
 40
 41import java.util.ArrayList;
 42import java.util.List;
 43
 44import de.timroes.android.listview.EnhancedListView;
 45import eu.siacs.conversations.Config;
 46import eu.siacs.conversations.R;
 47import eu.siacs.conversations.databinding.FragmentConversationsOverviewBinding;
 48import eu.siacs.conversations.entities.Conversation;
 49import eu.siacs.conversations.ui.adapter.ConversationAdapter;
 50import eu.siacs.conversations.ui.interfaces.OnConversationArchived;
 51import eu.siacs.conversations.ui.interfaces.OnConversationSelected;
 52import eu.siacs.conversations.ui.util.PendingItem;
 53import eu.siacs.conversations.ui.util.ScrollState;
 54
 55public class ConversationsOverviewFragment extends XmppFragment implements EnhancedListView.OnDismissCallback {
 56
 57	private static final String STATE_SCROLL_POSITION = ConversationsOverviewFragment.class.getName()+".scroll_state";
 58
 59	private final List<Conversation> conversations = new ArrayList<>();
 60	private final PendingItem<Conversation> swipedConversation = new PendingItem<>();
 61	private final PendingItem<ScrollState> pendingScrollState = new PendingItem<>();
 62	private FragmentConversationsOverviewBinding binding;
 63	private ConversationAdapter conversationsAdapter;
 64	private XmppActivity activity;
 65
 66	public static Conversation getSuggestion(Activity activity) {
 67		final Conversation exception;
 68		Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
 69		if (fragment != null && fragment instanceof ConversationsOverviewFragment) {
 70			exception = ((ConversationsOverviewFragment) fragment).swipedConversation.peek();
 71		} else {
 72			exception = null;
 73		}
 74		return getSuggestion(activity, exception);
 75	}
 76
 77	public static Conversation getSuggestion(Activity activity, Conversation exception) {
 78		Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
 79		if (fragment != null && fragment instanceof ConversationsOverviewFragment) {
 80			List<Conversation> conversations = ((ConversationsOverviewFragment) fragment).conversations;
 81			if (conversations.size() > 0) {
 82				Conversation suggestion = conversations.get(0);
 83				if (suggestion == exception) {
 84					if (conversations.size() > 1) {
 85						return conversations.get(1);
 86					}
 87				} else {
 88					return suggestion;
 89				}
 90			}
 91		}
 92		return null;
 93
 94	}
 95
 96	@Override
 97	public void onActivityCreated(Bundle savedInstanceState) {
 98		super.onActivityCreated(savedInstanceState);
 99		if (savedInstanceState == null) {
100			return;
101		}
102		pendingScrollState.push(savedInstanceState.getParcelable(STATE_SCROLL_POSITION));
103	}
104
105	@Override
106	public void onAttach(Activity activity) {
107		super.onAttach(activity);
108		if (activity instanceof XmppActivity) {
109			this.activity = (XmppActivity) activity;
110		} else {
111			throw new IllegalStateException("Trying to attach fragment to activity that is not an XmppActivity");
112		}
113	}
114
115	@Override
116	public void onDetach() {
117		super.onDetach();
118		this.activity = null;
119	}
120
121	@Override
122	public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
123		Log.d(Config.LOGTAG, "onCreateView");
124		this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversations_overview, container, false);
125		this.binding.fab.setOnClickListener((view) -> StartConversationActivity.launch(getActivity()));
126
127		this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
128		this.binding.list.setAdapter(this.conversationsAdapter);
129		this.binding.list.setOnItemClickListener((parent, view, position, id) -> {
130			Conversation conversation = this.conversations.get(position);
131			if (activity instanceof OnConversationSelected) {
132				((OnConversationSelected) activity).onConversationSelected(conversation);
133			} else {
134				Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
135			}
136		});
137		this.binding.list.setDismissCallback(this);
138		this.binding.list.enableSwipeToDismiss();
139		this.binding.list.setSwipeDirection(EnhancedListView.SwipeDirection.BOTH);
140		this.binding.list.setSwipingLayout(R.id.swipeable_item);
141		this.binding.list.setUndoStyle(EnhancedListView.UndoStyle.SINGLE_POPUP);
142		this.binding.list.setUndoHideDelay(5000);
143		this.binding.list.setRequireTouchBeforeDismiss(false);
144		return binding.getRoot();
145	}
146
147	@Override
148	public void onBackendConnected() {
149		refresh();
150	}
151
152	@Override
153	public void onSaveInstanceState(Bundle bundle) {
154		super.onSaveInstanceState(bundle);
155		ScrollState scrollState = getScrollState();
156		if (scrollState != null) {
157			bundle.putParcelable(STATE_SCROLL_POSITION, scrollState);
158		}
159	}
160
161	private ScrollState getScrollState() {
162		if (this.binding == null) {
163			return null;
164		}
165		int position = this.binding.list.getFirstVisiblePosition();
166		final View view = this.binding.list.getChildAt(0);
167		if (view != null) {
168			return new ScrollState(position,view.getTop());
169		} else {
170			return new ScrollState(position, 0);
171		}
172	}
173
174	@Override
175	public void onStart() {
176		super.onStart();
177		Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onStart()");
178		if (activity.xmppConnectionService != null) {
179			refresh();
180		}
181	}
182
183	@Override
184	public void onResume() {
185		super.onResume();
186		Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onResume()");
187	}
188
189	@Override
190	void refresh() {
191		if (this.binding == null || this.activity == null) {
192			Log.d(Config.LOGTAG,"ConversationsOverviewFragment.refresh() skipped updated because view binding or activity was null");
193			return;
194		}
195		this.activity.xmppConnectionService.populateWithOrderedConversations(this.conversations);
196		Conversation removed = this.swipedConversation.peek();
197		if (removed != null) {
198			if (removed.isRead()) {
199				this.conversations.remove(removed);
200			} else {
201				this.binding.list.discardUndo(); //will be ignored during discard when conversation is unRead
202			}
203		}
204		this.conversationsAdapter.notifyDataSetChanged();
205		ScrollState scrollState = pendingScrollState.pop();
206		if (scrollState != null) {
207			setScrollPosition(scrollState);
208		}
209	}
210
211	private void setScrollPosition(ScrollState scrollPosition) {
212		if (scrollPosition != null) {
213			this.binding.list.setSelectionFromTop(scrollPosition.position, scrollPosition.offset);
214		}
215	}
216
217	@Override
218	public EnhancedListView.Undoable onDismiss(EnhancedListView listView, int position) {
219		try {
220			swipedConversation.push(this.conversationsAdapter.getItem(position));
221		} catch (IndexOutOfBoundsException e) {
222			return null;
223		}
224		this.conversationsAdapter.remove(swipedConversation.peek());
225		this.activity.xmppConnectionService.markRead(swipedConversation.peek());
226
227		if (position == 0 && this.conversationsAdapter.getCount() == 0) {
228			final Conversation c = swipedConversation.pop();
229			activity.xmppConnectionService.archiveConversation(c);
230			return null;
231		}
232		final boolean formerlySelected = ConversationFragment.getConversation(getActivity()) == swipedConversation.peek();
233		if (activity instanceof OnConversationArchived) {
234			((OnConversationArchived) activity).onConversationArchived(swipedConversation.peek());
235		}
236		return new EnhancedListView.Undoable() {
237
238			@Override
239			public void undo() {
240				Conversation c = swipedConversation.pop();
241				conversationsAdapter.insert(c, position);
242				if (formerlySelected) {
243					if (activity instanceof OnConversationSelected) {
244						((OnConversationSelected) activity).onConversationSelected(c);
245					}
246				}
247				if (position > listView.getLastVisiblePosition()) {
248					listView.smoothScrollToPosition(position);
249				}
250			}
251
252			@Override
253			public void discard() {
254				Conversation c = swipedConversation.pop();
255				if (!c.isRead() && c.getMode() == Conversation.MODE_SINGLE) {
256					return;
257				}
258				activity.xmppConnectionService.archiveConversation(c);
259			}
260
261			@Override
262			public String getTitle() {
263				if (swipedConversation.peek().getMode() == Conversation.MODE_MULTI) {
264					return getResources().getString(R.string.title_undo_swipe_out_muc);
265				} else {
266					return getResources().getString(R.string.title_undo_swipe_out_conversation);
267				}
268			}
269		};
270	}
271}