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