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