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.graphics.Canvas;
 36import android.graphics.Paint;
 37import android.os.Bundle;
 38import android.support.design.widget.Snackbar;
 39import android.support.v7.widget.LinearLayoutManager;
 40import android.support.v7.widget.RecyclerView;
 41import android.support.v7.widget.helper.ItemTouchHelper;
 42import android.util.Log;
 43import android.view.LayoutInflater;
 44import android.view.View;
 45import android.view.ViewGroup;
 46
 47import java.util.ArrayList;
 48import java.util.List;
 49
 50import eu.siacs.conversations.Config;
 51import eu.siacs.conversations.R;
 52import eu.siacs.conversations.databinding.FragmentConversationsOverviewBinding;
 53import eu.siacs.conversations.entities.Conversation;
 54import eu.siacs.conversations.ui.adapter.ConversationAdapter;
 55import eu.siacs.conversations.ui.interfaces.OnConversationArchived;
 56import eu.siacs.conversations.ui.interfaces.OnConversationSelected;
 57import eu.siacs.conversations.ui.util.Color;
 58import eu.siacs.conversations.ui.util.PendingActionHelper;
 59import eu.siacs.conversations.ui.util.PendingItem;
 60import eu.siacs.conversations.ui.util.ScrollState;
 61import eu.siacs.conversations.utils.ThemeHelper;
 62
 63import static android.support.v7.widget.helper.ItemTouchHelper.LEFT;
 64import static android.support.v7.widget.helper.ItemTouchHelper.RIGHT;
 65
 66public class ConversationsOverviewFragment extends XmppFragment {
 67
 68	private static final String STATE_SCROLL_POSITION = ConversationsOverviewFragment.class.getName()+".scroll_state";
 69
 70	private final List<Conversation> conversations = new ArrayList<>();
 71	private final PendingItem<Conversation> swipedConversation = new PendingItem<>();
 72	private final PendingItem<ScrollState> pendingScrollState = new PendingItem<>();
 73	private FragmentConversationsOverviewBinding binding;
 74	private ConversationAdapter conversationsAdapter;
 75	private XmppActivity activity;
 76	private PendingActionHelper pendingActionHelper = new PendingActionHelper();
 77
 78	private ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(0,LEFT|RIGHT) {
 79		@Override
 80		public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
 81			//todo maybe we can manually changing the position of the conversation
 82			return false;
 83		}
 84
 85		@Override
 86		public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
 87									float dX, float dY, int actionState, boolean isCurrentlyActive) {
 88			super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
 89			if(actionState != ItemTouchHelper.ACTION_STATE_IDLE){
 90				Paint paint = new Paint();
 91				paint.setColor(Color.get(activity,R.attr.conversations_overview_background));
 92				paint.setStyle(Paint.Style.FILL);
 93				c.drawRect(viewHolder.itemView.getLeft(),viewHolder.itemView.getTop()
 94						,viewHolder.itemView.getRight(),viewHolder.itemView.getBottom(), paint);
 95			}
 96		}
 97
 98		@Override
 99		public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
100			super.clearView(recyclerView, viewHolder);
101			viewHolder.itemView.setAlpha(1f);
102		}
103
104		@Override
105		public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
106			pendingActionHelper.execute();
107			int position = viewHolder.getLayoutPosition();
108			try {
109				swipedConversation.push(conversations.get(position));
110			} catch (IndexOutOfBoundsException e) {
111				return;
112			}
113			conversationsAdapter.remove(swipedConversation.peek(), position);
114			activity.xmppConnectionService.markRead(swipedConversation.peek());
115
116			if (position == 0 && conversationsAdapter.getItemCount() == 0) {
117				final Conversation c = swipedConversation.pop();
118				activity.xmppConnectionService.archiveConversation(c);
119				return;
120			}
121			final boolean formerlySelected = ConversationFragment.getConversation(getActivity()) == swipedConversation.peek();
122			if (activity instanceof OnConversationArchived) {
123				((OnConversationArchived) activity).onConversationArchived(swipedConversation.peek());
124			}
125			boolean isMuc = swipedConversation.peek().getMode() == Conversation.MODE_MULTI;
126			int title = isMuc ? R.string.title_undo_swipe_out_muc : R.string.title_undo_swipe_out_conversation;
127
128			pendingActionHelper.push(() -> {
129				Conversation c = swipedConversation.pop();
130				if(c != null){
131					if (!c.isRead() && c.getMode() == Conversation.MODE_SINGLE) {
132						return;
133					}
134					activity.xmppConnectionService.archiveConversation(c);
135				}
136			});
137			final Snackbar snackbar = Snackbar.make(binding.list, title, 5000)
138					.setAction(R.string.undo, v -> {
139						pendingActionHelper.undo();
140						Conversation c = swipedConversation.pop();
141						conversationsAdapter.insert(c, position);
142						if (formerlySelected) {
143							if (activity instanceof OnConversationSelected) {
144								((OnConversationSelected) activity).onConversationSelected(c);
145							}
146						}
147						LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
148						if (position > layoutManager.findLastVisibleItemPosition()) {
149							binding.list.smoothScrollToPosition(position);
150						}
151					})
152					.addCallback(new Snackbar.Callback() {
153						@Override
154						public void onDismissed(Snackbar transientBottomBar, int event) {
155							switch (event) {
156								case DISMISS_EVENT_SWIPE:
157								case DISMISS_EVENT_TIMEOUT:
158									pendingActionHelper.execute();
159									break;
160							}
161						}
162					});
163			ThemeHelper.fixTextSize(snackbar);
164			snackbar.show();
165		}
166	};
167
168	private ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
169
170	public static Conversation getSuggestion(Activity activity) {
171		final Conversation exception;
172		Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
173		if (fragment != null && fragment instanceof ConversationsOverviewFragment) {
174			exception = ((ConversationsOverviewFragment) fragment).swipedConversation.peek();
175		} else {
176			exception = null;
177		}
178		return getSuggestion(activity, exception);
179	}
180
181	public static Conversation getSuggestion(Activity activity, Conversation exception) {
182		Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
183		if (fragment != null && fragment instanceof ConversationsOverviewFragment) {
184			List<Conversation> conversations = ((ConversationsOverviewFragment) fragment).conversations;
185			if (conversations.size() > 0) {
186				Conversation suggestion = conversations.get(0);
187				if (suggestion == exception) {
188					if (conversations.size() > 1) {
189						return conversations.get(1);
190					}
191				} else {
192					return suggestion;
193				}
194			}
195		}
196		return null;
197
198	}
199
200	@Override
201	public void onActivityCreated(Bundle savedInstanceState) {
202		super.onActivityCreated(savedInstanceState);
203		if (savedInstanceState == null) {
204			return;
205		}
206		pendingScrollState.push(savedInstanceState.getParcelable(STATE_SCROLL_POSITION));
207	}
208
209	@Override
210	public void onAttach(Activity activity) {
211		super.onAttach(activity);
212		if (activity instanceof XmppActivity) {
213			this.activity = (XmppActivity) activity;
214		} else {
215			throw new IllegalStateException("Trying to attach fragment to activity that is not an XmppActivity");
216		}
217	}
218
219	@Override
220	public void onPause() {
221		Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onPause()");
222		pendingActionHelper.execute();
223		super.onPause();
224	}
225
226	@Override
227	public void onDetach() {
228		super.onDetach();
229		this.activity = null;
230	}
231
232	@Override
233	public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
234		Log.d(Config.LOGTAG, "onCreateView");
235		this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversations_overview, container, false);
236		this.binding.fab.setOnClickListener((view) -> StartConversationActivity.launch(getActivity()));
237
238		this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
239		this.conversationsAdapter.setConversationClickListener((view, conversation) -> {
240			if (activity instanceof OnConversationSelected) {
241				((OnConversationSelected) activity).onConversationSelected(conversation);
242			} else {
243				Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
244			}
245		});
246		this.binding.list.setAdapter(this.conversationsAdapter);
247		this.binding.list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
248		this.touchHelper.attachToRecyclerView(this.binding.list);
249		return binding.getRoot();
250	}
251
252	@Override
253	public void onBackendConnected() {
254		refresh();
255	}
256
257	@Override
258	public void onSaveInstanceState(Bundle bundle) {
259		super.onSaveInstanceState(bundle);
260		ScrollState scrollState = getScrollState();
261		if (scrollState != null) {
262			bundle.putParcelable(STATE_SCROLL_POSITION, scrollState);
263		}
264	}
265
266	private ScrollState getScrollState() {
267		if (this.binding == null) {
268			return null;
269		}
270		LinearLayoutManager layoutManager = (LinearLayoutManager) this.binding.list.getLayoutManager();
271		int position = layoutManager.findFirstVisibleItemPosition();
272		final View view = this.binding.list.getChildAt(0);
273		if (view != null) {
274			return new ScrollState(position,view.getTop());
275		} else {
276			return new ScrollState(position, 0);
277		}
278	}
279
280	@Override
281	public void onStart() {
282		super.onStart();
283		Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onStart()");
284		if (activity.xmppConnectionService != null) {
285			refresh();
286		}
287	}
288
289	@Override
290	public void onResume() {
291		super.onResume();
292		Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onResume()");
293	}
294
295	@Override
296	void refresh() {
297		if (this.binding == null || this.activity == null) {
298			Log.d(Config.LOGTAG,"ConversationsOverviewFragment.refresh() skipped updated because view binding or activity was null");
299			return;
300		}
301		this.activity.xmppConnectionService.populateWithOrderedConversations(this.conversations);
302		Conversation removed = this.swipedConversation.peek();
303		if (removed != null) {
304			if (removed.isRead()) {
305				this.conversations.remove(removed);
306			} else {
307				pendingActionHelper.execute();
308			}
309		}
310		this.conversationsAdapter.notifyDataSetChanged();
311		ScrollState scrollState = pendingScrollState.pop();
312		if (scrollState != null) {
313			setScrollPosition(scrollState);
314		}
315	}
316
317	private void setScrollPosition(ScrollState scrollPosition) {
318		if (scrollPosition != null) {
319			LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
320			layoutManager.scrollToPositionWithOffset(scrollPosition.position, scrollPosition.offset);
321		}
322	}
323}