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