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;
 53
 54public class ConversationsOverviewFragment extends XmppFragment implements EnhancedListView.OnDismissCallback {
 55
 56	private final List<Conversation> conversations = new ArrayList<>();
 57	private final PendingItem<Conversation> swipedConversation = new PendingItem<>();
 58	private FragmentConversationsOverviewBinding binding;
 59	private ConversationAdapter conversationsAdapter;
 60	private XmppActivity activity;
 61
 62	public static Conversation getSuggestion(Activity activity) {
 63		final Conversation exception;
 64		Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
 65		if (fragment != null && fragment instanceof ConversationsOverviewFragment) {
 66			exception = ((ConversationsOverviewFragment) fragment).swipedConversation.peek();
 67		} else {
 68			exception = null;
 69		}
 70		return getSuggestion(activity, exception);
 71	}
 72
 73	public static Conversation getSuggestion(Activity activity, Conversation exception) {
 74		Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
 75		if (fragment != null && fragment instanceof ConversationsOverviewFragment) {
 76			List<Conversation> conversations = ((ConversationsOverviewFragment) fragment).conversations;
 77			if (conversations.size() > 0) {
 78				Conversation suggestion = conversations.get(0);
 79				if (suggestion == exception) {
 80					if (conversations.size() > 1) {
 81						return conversations.get(1);
 82					}
 83				} else {
 84					return suggestion;
 85				}
 86			}
 87		}
 88		return null;
 89
 90	}
 91
 92	@Override
 93	public void onAttach(Activity activity) {
 94		super.onAttach(activity);
 95		if (activity instanceof XmppActivity) {
 96			this.activity = (XmppActivity) activity;
 97		} else {
 98			throw new IllegalStateException("Trying to attach fragment to activity that is not an XmppActivity");
 99		}
100	}
101
102	@Override
103	public void onDetach() {
104		super.onDetach();
105		this.activity = null;
106	}
107
108	@Override
109	public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
110		Log.d(Config.LOGTAG, "onCreateView");
111		this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversations_overview, container, false);
112		this.binding.fab.setOnClickListener((view) -> StartConversationActivity.launch(getActivity()));
113
114		this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
115		this.binding.list.setAdapter(this.conversationsAdapter);
116		this.binding.list.setOnItemClickListener((parent, view, position, id) -> {
117			Conversation conversation = this.conversations.get(position);
118			if (activity instanceof OnConversationSelected) {
119				((OnConversationSelected) activity).onConversationSelected(conversation);
120			} else {
121				Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
122			}
123		});
124		this.binding.list.setDismissCallback(this);
125		this.binding.list.enableSwipeToDismiss();
126		this.binding.list.setSwipeDirection(EnhancedListView.SwipeDirection.BOTH);
127		this.binding.list.setSwipingLayout(R.id.swipeable_item);
128		this.binding.list.setUndoStyle(EnhancedListView.UndoStyle.SINGLE_POPUP);
129		this.binding.list.setUndoHideDelay(5000);
130		this.binding.list.setRequireTouchBeforeDismiss(false);
131		return binding.getRoot();
132	}
133
134	@Override
135	void onBackendConnected() {
136		Log.d(Config.LOGTAG, "nice!");
137		refresh();
138	}
139
140	@Override
141	public void onStart() {
142		super.onStart();
143		Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onStart()");
144		if (activity.xmppConnectionService != null) {
145			refresh();
146		}
147	}
148
149	@Override
150	public void onResume() {
151		super.onResume();
152		Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onResume()");
153	}
154
155	@Override
156	void refresh() {
157		if (this.binding == null || this.activity == null) {
158			Log.d(Config.LOGTAG,"ConversationsOverviewFragment.refresh() skipped updated because view binding or activity was null");
159			return;
160		}
161		this.activity.xmppConnectionService.populateWithOrderedConversations(this.conversations);
162		Conversation removed = this.swipedConversation.peek();
163		if (removed != null) {
164			if (removed.isRead()) {
165				this.conversations.remove(removed);
166			} else {
167				this.binding.list.discardUndo(); //will be ignored during discard when conversation is unRead
168			}
169		}
170		this.conversationsAdapter.notifyDataSetChanged();
171	}
172
173	@Override
174	public EnhancedListView.Undoable onDismiss(EnhancedListView listView, int position) {
175		try {
176			swipedConversation.push(this.conversationsAdapter.getItem(position));
177		} catch (IndexOutOfBoundsException e) {
178			return null;
179		}
180		this.conversationsAdapter.remove(swipedConversation.peek());
181		this.activity.xmppConnectionService.markRead(swipedConversation.peek());
182
183		if (position == 0 && this.conversationsAdapter.getCount() == 0) {
184			final Conversation c = swipedConversation.pop();
185			activity.xmppConnectionService.archiveConversation(c);
186			if (activity instanceof OnConversationArchived) {
187				((OnConversationArchived) activity).onConversationArchived(c);
188			}
189			return null;
190		}
191		final boolean formerlySelected = ConversationFragment.getConversation(getActivity()) == swipedConversation.peek();
192		if (activity instanceof OnConversationArchived) {
193			((OnConversationArchived) activity).onConversationArchived(swipedConversation.peek());
194		}
195		return new EnhancedListView.Undoable() {
196
197			@Override
198			public void undo() {
199				Conversation c = swipedConversation.pop();
200				conversationsAdapter.insert(c, position);
201				if (formerlySelected) {
202					if (activity instanceof OnConversationSelected) {
203						((OnConversationSelected) activity).onConversationSelected(c);
204					}
205				}
206				if (position > listView.getLastVisiblePosition()) {
207					listView.smoothScrollToPosition(position);
208				}
209			}
210
211			@Override
212			public void discard() {
213				Conversation c = swipedConversation.pop();
214				if (!c.isRead() && c.getMode() == Conversation.MODE_SINGLE) {
215					return;
216				}
217				activity.xmppConnectionService.archiveConversation(c);
218			}
219
220			@Override
221			public String getTitle() {
222				if (swipedConversation.peek().getMode() == Conversation.MODE_MULTI) {
223					return getResources().getString(R.string.title_undo_swipe_out_muc);
224				} else {
225					return getResources().getString(R.string.title_undo_swipe_out_conversation);
226				}
227			}
228		};
229	}
230}