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.graphics.Canvas;
36import android.graphics.Paint;
37import android.os.Bundle;
38import android.support.design.widget.Snackbar;
39import android.support.v7.widget.LinearLayoutManager;
40import android.support.v7.widget.RecyclerView;
41import android.support.v7.widget.helper.ItemTouchHelper;
42import android.util.Log;
43import android.view.LayoutInflater;
44import android.view.View;
45import android.view.ViewGroup;
46
47import java.util.ArrayList;
48import java.util.List;
49
50import eu.siacs.conversations.Config;
51import eu.siacs.conversations.R;
52import eu.siacs.conversations.databinding.FragmentConversationsOverviewBinding;
53import eu.siacs.conversations.entities.Conversation;
54import eu.siacs.conversations.ui.adapter.ConversationAdapter;
55import eu.siacs.conversations.ui.interfaces.OnConversationArchived;
56import eu.siacs.conversations.ui.interfaces.OnConversationSelected;
57import eu.siacs.conversations.ui.util.Color;
58import eu.siacs.conversations.ui.util.PendingActionHelper;
59import eu.siacs.conversations.ui.util.PendingItem;
60import eu.siacs.conversations.ui.util.ScrollState;
61
62import static android.support.v7.widget.helper.ItemTouchHelper.LEFT;
63import static android.support.v7.widget.helper.ItemTouchHelper.RIGHT;
64
65public class ConversationsOverviewFragment extends XmppFragment {
66
67 private static final String STATE_SCROLL_POSITION = ConversationsOverviewFragment.class.getName()+".scroll_state";
68
69 private final List<Conversation> conversations = new ArrayList<>();
70 private final PendingItem<Conversation> swipedConversation = new PendingItem<>();
71 private final PendingItem<ScrollState> pendingScrollState = new PendingItem<>();
72 private FragmentConversationsOverviewBinding binding;
73 private ConversationAdapter conversationsAdapter;
74 private XmppActivity activity;
75 private PendingActionHelper pendingActionHelper = new PendingActionHelper();
76
77 private ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(0,LEFT|RIGHT) {
78 @Override
79 public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
80 //todo maybe we can manually changing the position of the conversation
81 return false;
82 }
83
84 @Override
85 public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
86 float dX, float dY, int actionState, boolean isCurrentlyActive) {
87 super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
88 if(actionState != ItemTouchHelper.ACTION_STATE_IDLE){
89 Paint paint = new Paint();
90 paint.setColor(Color.get(activity,R.attr.conversations_overview_background));
91 paint.setStyle(Paint.Style.FILL);
92 c.drawRect(viewHolder.itemView.getLeft(),viewHolder.itemView.getTop()
93 ,viewHolder.itemView.getRight(),viewHolder.itemView.getBottom(), paint);
94 }
95 }
96
97 @Override
98 public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
99 super.clearView(recyclerView, viewHolder);
100 viewHolder.itemView.setAlpha(1f);
101 }
102
103 @Override
104 public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
105 pendingActionHelper.execute();
106 int position = viewHolder.getLayoutPosition();
107 try {
108 swipedConversation.push(conversations.get(position));
109 } catch (IndexOutOfBoundsException e) {
110 return;
111 }
112 conversationsAdapter.remove(swipedConversation.peek(), position);
113 activity.xmppConnectionService.markRead(swipedConversation.peek());
114
115 if (position == 0 && conversationsAdapter.getItemCount() == 0) {
116 final Conversation c = swipedConversation.pop();
117 activity.xmppConnectionService.archiveConversation(c);
118 return;
119 }
120 final boolean formerlySelected = ConversationFragment.getConversation(getActivity()) == swipedConversation.peek();
121 if (activity instanceof OnConversationArchived) {
122 ((OnConversationArchived) activity).onConversationArchived(swipedConversation.peek());
123 }
124 boolean isMuc = swipedConversation.peek().getMode() == Conversation.MODE_MULTI;
125 int title = isMuc ? R.string.title_undo_swipe_out_muc : R.string.title_undo_swipe_out_conversation;
126
127 pendingActionHelper.push(() -> {
128 Conversation c = swipedConversation.pop();
129 if(c != null){
130 if (!c.isRead() && c.getMode() == Conversation.MODE_SINGLE) {
131 return;
132 }
133 activity.xmppConnectionService.archiveConversation(c);
134 }
135 });
136 Snackbar.make(binding.list, title, 5000)
137 .setAction(R.string.undo, v -> {
138 pendingActionHelper.undo();
139 Conversation c = swipedConversation.pop();
140 conversationsAdapter.insert(c, position);
141 if (formerlySelected) {
142 if (activity instanceof OnConversationSelected) {
143 ((OnConversationSelected) activity).onConversationSelected(c);
144 }
145 }
146 LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
147 if (position > layoutManager.findLastVisibleItemPosition()) {
148 binding.list.smoothScrollToPosition(position);
149 }
150 })
151 .addCallback(new Snackbar.Callback() {
152 @Override
153 public void onDismissed(Snackbar transientBottomBar, int event) {
154 switch (event) {
155 case DISMISS_EVENT_SWIPE:
156 case DISMISS_EVENT_TIMEOUT:
157 pendingActionHelper.execute();
158 break;
159 }
160 }
161 })
162 .show();
163 }
164 };
165
166 private ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
167
168 public static Conversation getSuggestion(Activity activity) {
169 final Conversation exception;
170 Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
171 if (fragment != null && fragment instanceof ConversationsOverviewFragment) {
172 exception = ((ConversationsOverviewFragment) fragment).swipedConversation.peek();
173 } else {
174 exception = null;
175 }
176 return getSuggestion(activity, exception);
177 }
178
179 public static Conversation getSuggestion(Activity activity, Conversation exception) {
180 Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
181 if (fragment != null && fragment instanceof ConversationsOverviewFragment) {
182 List<Conversation> conversations = ((ConversationsOverviewFragment) fragment).conversations;
183 if (conversations.size() > 0) {
184 Conversation suggestion = conversations.get(0);
185 if (suggestion == exception) {
186 if (conversations.size() > 1) {
187 return conversations.get(1);
188 }
189 } else {
190 return suggestion;
191 }
192 }
193 }
194 return null;
195
196 }
197
198 @Override
199 public void onActivityCreated(Bundle savedInstanceState) {
200 super.onActivityCreated(savedInstanceState);
201 if (savedInstanceState == null) {
202 return;
203 }
204 pendingScrollState.push(savedInstanceState.getParcelable(STATE_SCROLL_POSITION));
205 }
206
207 @Override
208 public void onAttach(Activity activity) {
209 super.onAttach(activity);
210 if (activity instanceof XmppActivity) {
211 this.activity = (XmppActivity) activity;
212 } else {
213 throw new IllegalStateException("Trying to attach fragment to activity that is not an XmppActivity");
214 }
215 }
216
217 @Override
218 public void onPause() {
219 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onPause()");
220 pendingActionHelper.execute();
221 super.onPause();
222 }
223
224 @Override
225 public void onDetach() {
226 super.onDetach();
227 this.activity = null;
228 }
229
230 @Override
231 public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
232 Log.d(Config.LOGTAG, "onCreateView");
233 this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversations_overview, container, false);
234 this.binding.fab.setOnClickListener((view) -> StartConversationActivity.launch(getActivity()));
235
236 this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
237 this.conversationsAdapter.setConversationClickListener((view, conversation) -> {
238 if (activity instanceof OnConversationSelected) {
239 ((OnConversationSelected) activity).onConversationSelected(conversation);
240 } else {
241 Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
242 }
243 });
244 this.binding.list.setAdapter(this.conversationsAdapter);
245 this.binding.list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
246 this.touchHelper.attachToRecyclerView(this.binding.list);
247 return binding.getRoot();
248 }
249
250 @Override
251 public void onBackendConnected() {
252 refresh();
253 }
254
255 @Override
256 public void onSaveInstanceState(Bundle bundle) {
257 super.onSaveInstanceState(bundle);
258 ScrollState scrollState = getScrollState();
259 if (scrollState != null) {
260 bundle.putParcelable(STATE_SCROLL_POSITION, scrollState);
261 }
262 }
263
264 private ScrollState getScrollState() {
265 if (this.binding == null) {
266 return null;
267 }
268 LinearLayoutManager layoutManager = (LinearLayoutManager) this.binding.list.getLayoutManager();
269 int position = layoutManager.findFirstVisibleItemPosition();
270 final View view = this.binding.list.getChildAt(0);
271 if (view != null) {
272 return new ScrollState(position,view.getTop());
273 } else {
274 return new ScrollState(position, 0);
275 }
276 }
277
278 @Override
279 public void onStart() {
280 super.onStart();
281 Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onStart()");
282 if (activity.xmppConnectionService != null) {
283 refresh();
284 }
285 }
286
287 @Override
288 public void onResume() {
289 super.onResume();
290 Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onResume()");
291 }
292
293 @Override
294 void refresh() {
295 if (this.binding == null || this.activity == null) {
296 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.refresh() skipped updated because view binding or activity was null");
297 return;
298 }
299 this.activity.xmppConnectionService.populateWithOrderedConversations(this.conversations);
300 Conversation removed = this.swipedConversation.peek();
301 if (removed != null) {
302 if (removed.isRead()) {
303 this.conversations.remove(removed);
304 } else {
305 pendingActionHelper.execute();
306 }
307 }
308 this.conversationsAdapter.notifyDataSetChanged();
309 ScrollState scrollState = pendingScrollState.pop();
310 if (scrollState != null) {
311 setScrollPosition(scrollState);
312 }
313 }
314
315 private void setScrollPosition(ScrollState scrollPosition) {
316 if (scrollPosition != null) {
317 LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
318 layoutManager.scrollToPositionWithOffset(scrollPosition.position, scrollPosition.offset);
319 }
320 }
321}