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