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.AlertDialog;
 34import android.app.Fragment;
 35import android.content.Intent;
 36import androidx.databinding.DataBindingUtil;
 37import android.graphics.Canvas;
 38import android.graphics.Paint;
 39import android.os.Bundle;
 40import com.google.android.material.snackbar.Snackbar;
 41import androidx.recyclerview.widget.LinearLayoutManager;
 42import androidx.recyclerview.widget.RecyclerView;
 43import androidx.recyclerview.widget.ItemTouchHelper;
 44import android.util.Log;
 45import android.view.LayoutInflater;
 46import android.view.Menu;
 47import android.view.MenuInflater;
 48import android.view.MenuItem;
 49import android.view.View;
 50import android.view.ViewGroup;
 51import android.widget.Toast;
 52
 53import com.google.common.collect.Collections2;
 54
 55import java.util.ArrayList;
 56import java.util.List;
 57import java.util.concurrent.atomic.AtomicReference;
 58
 59import eu.siacs.conversations.Config;
 60import eu.siacs.conversations.R;
 61import eu.siacs.conversations.databinding.FragmentConversationsOverviewBinding;
 62import eu.siacs.conversations.entities.Account;
 63import eu.siacs.conversations.entities.Conversation;
 64import eu.siacs.conversations.entities.Conversational;
 65import eu.siacs.conversations.ui.adapter.ConversationAdapter;
 66import eu.siacs.conversations.ui.interfaces.OnConversationArchived;
 67import eu.siacs.conversations.ui.interfaces.OnConversationSelected;
 68import eu.siacs.conversations.ui.util.MenuDoubleTabUtil;
 69import eu.siacs.conversations.ui.util.PendingActionHelper;
 70import eu.siacs.conversations.ui.util.PendingItem;
 71import eu.siacs.conversations.ui.util.ScrollState;
 72import eu.siacs.conversations.ui.util.StyledAttributes;
 73import eu.siacs.conversations.utils.AccountUtils;
 74import eu.siacs.conversations.utils.EasyOnboardingInvite;
 75import eu.siacs.conversations.utils.ThemeHelper;
 76
 77import static androidx.recyclerview.widget.ItemTouchHelper.LEFT;
 78import static androidx.recyclerview.widget.ItemTouchHelper.RIGHT;
 79
 80public class ConversationsOverviewFragment extends XmppFragment {
 81
 82	private static final String STATE_SCROLL_POSITION = ConversationsOverviewFragment.class.getName()+".scroll_state";
 83
 84	private final List<Conversation> conversations = new ArrayList<>();
 85	private final PendingItem<Conversation> swipedConversation = new PendingItem<>();
 86	private final PendingItem<ScrollState> pendingScrollState = new PendingItem<>();
 87	private FragmentConversationsOverviewBinding binding;
 88	private ConversationAdapter conversationsAdapter;
 89	private XmppActivity activity;
 90	private float mSwipeEscapeVelocity = 0f;
 91	private PendingActionHelper pendingActionHelper = new PendingActionHelper();
 92
 93	private ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(0,LEFT|RIGHT) {
 94		@Override
 95		public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
 96			//todo maybe we can manually changing the position of the conversation
 97			return false;
 98		}
 99
100		@Override
101		public float getSwipeEscapeVelocity (float defaultValue) {
102			return mSwipeEscapeVelocity;
103		}
104
105		@Override
106		public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
107									float dX, float dY, int actionState, boolean isCurrentlyActive) {
108			super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
109			if(actionState != ItemTouchHelper.ACTION_STATE_IDLE){
110				Paint paint = new Paint();
111				paint.setColor(StyledAttributes.getColor(activity,R.attr.conversations_overview_background));
112				paint.setStyle(Paint.Style.FILL);
113				c.drawRect(viewHolder.itemView.getLeft(),viewHolder.itemView.getTop()
114						,viewHolder.itemView.getRight(),viewHolder.itemView.getBottom(), paint);
115			}
116		}
117
118		@Override
119		public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
120			super.clearView(recyclerView, viewHolder);
121			viewHolder.itemView.setAlpha(1f);
122		}
123
124		@Override
125		public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
126			pendingActionHelper.execute();
127			int position = viewHolder.getLayoutPosition();
128			try {
129				swipedConversation.push(conversations.get(position));
130			} catch (IndexOutOfBoundsException e) {
131				return;
132			}
133			conversationsAdapter.remove(swipedConversation.peek(), position);
134			activity.xmppConnectionService.markRead(swipedConversation.peek());
135
136			if (position == 0 && conversationsAdapter.getItemCount() == 0) {
137				final Conversation c = swipedConversation.pop();
138				activity.xmppConnectionService.archiveConversation(c);
139				return;
140			}
141			final boolean formerlySelected = ConversationFragment.getConversation(getActivity()) == swipedConversation.peek();
142			if (activity instanceof OnConversationArchived) {
143				((OnConversationArchived) activity).onConversationArchived(swipedConversation.peek());
144			}
145			final Conversation c = swipedConversation.peek();
146			final int title;
147			if (c.getMode() == Conversational.MODE_MULTI) {
148				if (c.getMucOptions().isPrivateAndNonAnonymous()) {
149					title = R.string.title_undo_swipe_out_group_chat;
150				} else {
151					title = R.string.title_undo_swipe_out_channel;
152				}
153			} else {
154				title = R.string.title_undo_swipe_out_conversation;
155			}
156
157			final Snackbar snackbar = Snackbar.make(binding.list, title, 5000)
158					.setAction(R.string.undo, v -> {
159						pendingActionHelper.undo();
160						Conversation conversation = swipedConversation.pop();
161						conversationsAdapter.insert(conversation, position);
162						if (formerlySelected) {
163							if (activity instanceof OnConversationSelected) {
164								((OnConversationSelected) activity).onConversationSelected(c);
165							}
166						}
167						LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
168						if (position > layoutManager.findLastVisibleItemPosition()) {
169							binding.list.smoothScrollToPosition(position);
170						}
171					})
172					.addCallback(new Snackbar.Callback() {
173						@Override
174						public void onDismissed(Snackbar transientBottomBar, int event) {
175							switch (event) {
176								case DISMISS_EVENT_SWIPE:
177								case DISMISS_EVENT_TIMEOUT:
178									pendingActionHelper.execute();
179									break;
180							}
181						}
182					});
183
184			pendingActionHelper.push(() -> {
185				if (snackbar.isShownOrQueued()) {
186					snackbar.dismiss();
187				}
188				final Conversation conversation = swipedConversation.pop();
189				if(conversation != null){
190					if (!conversation.isRead() && conversation.getMode() == Conversation.MODE_SINGLE) {
191						return;
192					}
193					activity.xmppConnectionService.archiveConversation(c);
194				}
195			});
196
197			ThemeHelper.fix(snackbar);
198			snackbar.show();
199		}
200	};
201
202	private ItemTouchHelper touchHelper;
203
204	public static Conversation getSuggestion(Activity activity) {
205		final Conversation exception;
206		Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
207		if (fragment instanceof ConversationsOverviewFragment) {
208			exception = ((ConversationsOverviewFragment) fragment).swipedConversation.peek();
209		} else {
210			exception = null;
211		}
212		return getSuggestion(activity, exception);
213	}
214
215	public static Conversation getSuggestion(Activity activity, Conversation exception) {
216		Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
217		if (fragment instanceof ConversationsOverviewFragment) {
218			List<Conversation> conversations = ((ConversationsOverviewFragment) fragment).conversations;
219			if (conversations.size() > 0) {
220				Conversation suggestion = conversations.get(0);
221				if (suggestion == exception) {
222					if (conversations.size() > 1) {
223						return conversations.get(1);
224					}
225				} else {
226					return suggestion;
227				}
228			}
229		}
230		return null;
231
232	}
233
234	@Override
235	public void onActivityCreated(Bundle savedInstanceState) {
236		super.onActivityCreated(savedInstanceState);
237		if (savedInstanceState == null) {
238			return;
239		}
240		pendingScrollState.push(savedInstanceState.getParcelable(STATE_SCROLL_POSITION));
241	}
242
243	@Override
244	public void onAttach(Activity activity) {
245		super.onAttach(activity);
246		if (activity instanceof XmppActivity) {
247			this.activity = (XmppActivity) activity;
248		} else {
249			throw new IllegalStateException("Trying to attach fragment to activity that is not an XmppActivity");
250		}
251	}
252	@Override
253	public void onDestroyView() {
254		Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onDestroyView()");
255		super.onDestroyView();
256		this.binding = null;
257		this.conversationsAdapter = null;
258		this.touchHelper = null;
259	}
260	@Override
261	public void onDestroy() {
262		Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onDestroy()");
263		super.onDestroy();
264
265	}
266	@Override
267	public void onPause() {
268		Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onPause()");
269		pendingActionHelper.execute();
270		super.onPause();
271	}
272
273	@Override
274	public void onDetach() {
275		super.onDetach();
276		this.activity = null;
277	}
278
279	@Override
280	public void onCreate(Bundle savedInstanceState) {
281		super.onCreate(savedInstanceState);
282		setHasOptionsMenu(true);
283	}
284
285	@Override
286	public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
287		this.mSwipeEscapeVelocity = getResources().getDimension(R.dimen.swipe_escape_velocity);
288		this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversations_overview, container, false);
289		this.binding.fab.setOnClickListener((view) -> StartConversationActivity.launch(getActivity()));
290
291		this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
292		this.conversationsAdapter.setConversationClickListener((view, conversation) -> {
293			if (activity instanceof OnConversationSelected) {
294				((OnConversationSelected) activity).onConversationSelected(conversation);
295			} else {
296				Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
297			}
298		});
299		this.binding.list.setAdapter(this.conversationsAdapter);
300		this.binding.list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
301		this.touchHelper = new ItemTouchHelper(this.callback);
302		this.touchHelper.attachToRecyclerView(this.binding.list);
303		return binding.getRoot();
304	}
305
306	@Override
307	public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
308		menuInflater.inflate(R.menu.fragment_conversations_overview, menu);
309		AccountUtils.showHideMenuItems(menu);
310		final MenuItem easyOnboardInvite = menu.findItem(R.id.action_easy_invite);
311		easyOnboardInvite.setVisible(EasyOnboardingInvite.anyHasSupport(activity == null ? null : activity.xmppConnectionService));
312	}
313
314	@Override
315	public void onBackendConnected() {
316		refresh();
317	}
318
319	@Override
320	public void onSaveInstanceState(Bundle bundle) {
321		super.onSaveInstanceState(bundle);
322		ScrollState scrollState = getScrollState();
323		if (scrollState != null) {
324			bundle.putParcelable(STATE_SCROLL_POSITION, scrollState);
325		}
326	}
327
328	private ScrollState getScrollState() {
329		if (this.binding == null) {
330			return null;
331		}
332		LinearLayoutManager layoutManager = (LinearLayoutManager) this.binding.list.getLayoutManager();
333		int position = layoutManager.findFirstVisibleItemPosition();
334		final View view = this.binding.list.getChildAt(0);
335		if (view != null) {
336			return new ScrollState(position,view.getTop());
337		} else {
338			return new ScrollState(position, 0);
339		}
340	}
341
342	@Override
343	public void onStart() {
344		super.onStart();
345		Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onStart()");
346		if (activity.xmppConnectionService != null) {
347			refresh();
348		}
349	}
350
351	@Override
352	public void onResume() {
353		super.onResume();
354		Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onResume()");
355	}
356
357	@Override
358	public boolean onOptionsItemSelected(final MenuItem item) {
359		if (MenuDoubleTabUtil.shouldIgnoreTap()) {
360			return false;
361		}
362		switch (item.getItemId()) {
363			case R.id.action_search:
364				startActivity(new Intent(getActivity(), SearchActivity.class));
365				return true;
366			case R.id.action_easy_invite:
367				selectAccountToStartEasyInvite();
368				return true;
369		}
370		return super.onOptionsItemSelected(item);
371	}
372
373	private void selectAccountToStartEasyInvite() {
374		final List<Account> accounts = EasyOnboardingInvite.getSupportingAccounts(activity.xmppConnectionService);
375		if (accounts.size() == 0) {
376			//This can technically happen if opening the menu item races with accounts reconnecting or something
377			Toast.makeText(getActivity(),R.string.no_active_accounts_support_this, Toast.LENGTH_LONG).show();
378		} else if (accounts.size() == 1) {
379			openEasyInviteScreen(accounts.get(0));
380		} else {
381			final AtomicReference<Account> selectedAccount = new AtomicReference<>(accounts.get(0));
382			final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
383			alertDialogBuilder.setTitle(R.string.choose_account);
384			final String[] asStrings = Collections2.transform(accounts, a -> a.getJid().asBareJid().toEscapedString()).toArray(new String[0]);
385			alertDialogBuilder.setSingleChoiceItems(asStrings, 0, (dialog, which) -> selectedAccount.set(accounts.get(which)));
386			alertDialogBuilder.setNegativeButton(R.string.cancel, null);
387			alertDialogBuilder.setPositiveButton(R.string.ok, (dialog, which) -> openEasyInviteScreen(selectedAccount.get()));
388			alertDialogBuilder.create().show();
389		}
390	}
391
392	private void openEasyInviteScreen(final Account account) {
393		EasyOnboardingInviteActivity.launch(account, activity);
394	}
395
396	@Override
397	void refresh() {
398		if (this.binding == null || this.activity == null) {
399			Log.d(Config.LOGTAG,"ConversationsOverviewFragment.refresh() skipped updated because view binding or activity was null");
400			return;
401		}
402		this.activity.xmppConnectionService.populateWithOrderedConversations(this.conversations);
403		Conversation removed = this.swipedConversation.peek();
404		if (removed != null) {
405			if (removed.isRead()) {
406				this.conversations.remove(removed);
407			} else {
408				pendingActionHelper.execute();
409			}
410		}
411		this.conversationsAdapter.notifyDataSetChanged();
412		ScrollState scrollState = pendingScrollState.pop();
413		if (scrollState != null) {
414			setScrollPosition(scrollState);
415		}
416	}
417
418	private void setScrollPosition(ScrollState scrollPosition) {
419		if (scrollPosition != null) {
420			LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
421			layoutManager.scrollToPositionWithOffset(scrollPosition.position, scrollPosition.offset);
422		}
423	}
424}