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