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