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.AlertDialog;
34import android.app.Fragment;
35import android.content.Intent;
36import android.databinding.DataBindingUtil;
37import android.graphics.Canvas;
38import android.graphics.Paint;
39import android.os.Bundle;
40import android.support.design.widget.Snackbar;
41import android.support.v7.widget.LinearLayoutManager;
42import android.support.v7.widget.RecyclerView;
43import android.support.v7.widget.helper.ItemTouchHelper;
44import android.util.Log;
45import android.view.LayoutInflater;
46import android.view.Menu;
47import android.view.MenuInflater;
48import android.view.MenuItem;
49import android.view.View;
50import android.view.ViewGroup;
51
52import com.google.common.collect.Collections2;
53
54import java.util.ArrayList;
55import java.util.List;
56import java.util.concurrent.atomic.AtomicReference;
57
58import eu.siacs.conversations.Config;
59import eu.siacs.conversations.R;
60import eu.siacs.conversations.databinding.FragmentConversationsOverviewBinding;
61import eu.siacs.conversations.entities.Account;
62import eu.siacs.conversations.entities.Conversation;
63import eu.siacs.conversations.entities.Conversational;
64import eu.siacs.conversations.ui.adapter.ConversationAdapter;
65import eu.siacs.conversations.ui.interfaces.OnConversationArchived;
66import eu.siacs.conversations.ui.interfaces.OnConversationSelected;
67import eu.siacs.conversations.ui.util.MenuDoubleTabUtil;
68import eu.siacs.conversations.ui.util.PendingActionHelper;
69import eu.siacs.conversations.ui.util.PendingItem;
70import eu.siacs.conversations.ui.util.ScrollState;
71import eu.siacs.conversations.ui.util.StyledAttributes;
72import eu.siacs.conversations.utils.AccountUtils;
73import eu.siacs.conversations.utils.EasyOnboardingInvite;
74import eu.siacs.conversations.utils.ThemeHelper;
75
76import static android.support.v7.widget.helper.ItemTouchHelper.LEFT;
77import static android.support.v7.widget.helper.ItemTouchHelper.RIGHT;
78
79public class ConversationsOverviewFragment extends XmppFragment {
80
81 private static final String STATE_SCROLL_POSITION = ConversationsOverviewFragment.class.getName()+".scroll_state";
82
83 private final List<Conversation> conversations = new ArrayList<>();
84 private final PendingItem<Conversation> swipedConversation = new PendingItem<>();
85 private final PendingItem<ScrollState> pendingScrollState = new PendingItem<>();
86 private FragmentConversationsOverviewBinding binding;
87 private ConversationAdapter conversationsAdapter;
88 private XmppActivity activity;
89 private float mSwipeEscapeVelocity = 0f;
90 private PendingActionHelper pendingActionHelper = new PendingActionHelper();
91
92 private ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(0,LEFT|RIGHT) {
93 @Override
94 public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
95 //todo maybe we can manually changing the position of the conversation
96 return false;
97 }
98
99 @Override
100 public float getSwipeEscapeVelocity (float defaultValue) {
101 return mSwipeEscapeVelocity;
102 }
103
104 @Override
105 public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
106 float dX, float dY, int actionState, boolean isCurrentlyActive) {
107 super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
108 if(actionState != ItemTouchHelper.ACTION_STATE_IDLE){
109 Paint paint = new Paint();
110 paint.setColor(StyledAttributes.getColor(activity,R.attr.conversations_overview_background));
111 paint.setStyle(Paint.Style.FILL);
112 c.drawRect(viewHolder.itemView.getLeft(),viewHolder.itemView.getTop()
113 ,viewHolder.itemView.getRight(),viewHolder.itemView.getBottom(), paint);
114 }
115 }
116
117 @Override
118 public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
119 super.clearView(recyclerView, viewHolder);
120 viewHolder.itemView.setAlpha(1f);
121 }
122
123 @Override
124 public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
125 pendingActionHelper.execute();
126 int position = viewHolder.getLayoutPosition();
127 try {
128 swipedConversation.push(conversations.get(position));
129 } catch (IndexOutOfBoundsException e) {
130 return;
131 }
132 conversationsAdapter.remove(swipedConversation.peek(), position);
133 activity.xmppConnectionService.markRead(swipedConversation.peek());
134
135 if (position == 0 && conversationsAdapter.getItemCount() == 0) {
136 final Conversation c = swipedConversation.pop();
137 activity.xmppConnectionService.archiveConversation(c);
138 return;
139 }
140 final boolean formerlySelected = ConversationFragment.getConversation(getActivity()) == swipedConversation.peek();
141 if (activity instanceof OnConversationArchived) {
142 ((OnConversationArchived) activity).onConversationArchived(swipedConversation.peek());
143 }
144 final Conversation c = swipedConversation.peek();
145 final int title;
146 if (c.getMode() == Conversational.MODE_MULTI) {
147 if (c.getMucOptions().isPrivateAndNonAnonymous()) {
148 title = R.string.title_undo_swipe_out_group_chat;
149 } else {
150 title = R.string.title_undo_swipe_out_channel;
151 }
152 } else {
153 title = R.string.title_undo_swipe_out_conversation;
154 }
155
156 final Snackbar snackbar = Snackbar.make(binding.list, title, 5000)
157 .setAction(R.string.undo, v -> {
158 pendingActionHelper.undo();
159 Conversation conversation = swipedConversation.pop();
160 conversationsAdapter.insert(conversation, position);
161 if (formerlySelected) {
162 if (activity instanceof OnConversationSelected) {
163 ((OnConversationSelected) activity).onConversationSelected(c);
164 }
165 }
166 LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
167 if (position > layoutManager.findLastVisibleItemPosition()) {
168 binding.list.smoothScrollToPosition(position);
169 }
170 })
171 .addCallback(new Snackbar.Callback() {
172 @Override
173 public void onDismissed(Snackbar transientBottomBar, int event) {
174 switch (event) {
175 case DISMISS_EVENT_SWIPE:
176 case DISMISS_EVENT_TIMEOUT:
177 pendingActionHelper.execute();
178 break;
179 }
180 }
181 });
182
183 pendingActionHelper.push(() -> {
184 if (snackbar.isShownOrQueued()) {
185 snackbar.dismiss();
186 }
187 final Conversation conversation = swipedConversation.pop();
188 if(conversation != null){
189 if (!conversation.isRead() && conversation.getMode() == Conversation.MODE_SINGLE) {
190 return;
191 }
192 activity.xmppConnectionService.archiveConversation(c);
193 }
194 });
195
196 ThemeHelper.fix(snackbar);
197 snackbar.show();
198 }
199 };
200
201 private ItemTouchHelper touchHelper;
202
203 public static Conversation getSuggestion(Activity activity) {
204 final Conversation exception;
205 Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
206 if (fragment instanceof ConversationsOverviewFragment) {
207 exception = ((ConversationsOverviewFragment) fragment).swipedConversation.peek();
208 } else {
209 exception = null;
210 }
211 return getSuggestion(activity, exception);
212 }
213
214 public static Conversation getSuggestion(Activity activity, Conversation exception) {
215 Fragment fragment = activity.getFragmentManager().findFragmentById(R.id.main_fragment);
216 if (fragment instanceof ConversationsOverviewFragment) {
217 List<Conversation> conversations = ((ConversationsOverviewFragment) fragment).conversations;
218 if (conversations.size() > 0) {
219 Conversation suggestion = conversations.get(0);
220 if (suggestion == exception) {
221 if (conversations.size() > 1) {
222 return conversations.get(1);
223 }
224 } else {
225 return suggestion;
226 }
227 }
228 }
229 return null;
230
231 }
232
233 @Override
234 public void onActivityCreated(Bundle savedInstanceState) {
235 super.onActivityCreated(savedInstanceState);
236 if (savedInstanceState == null) {
237 return;
238 }
239 pendingScrollState.push(savedInstanceState.getParcelable(STATE_SCROLL_POSITION));
240 }
241
242 @Override
243 public void onAttach(Activity activity) {
244 super.onAttach(activity);
245 if (activity instanceof XmppActivity) {
246 this.activity = (XmppActivity) activity;
247 } else {
248 throw new IllegalStateException("Trying to attach fragment to activity that is not an XmppActivity");
249 }
250 }
251 @Override
252 public void onDestroyView() {
253 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onDestroyView()");
254 super.onDestroyView();
255 this.binding = null;
256 this.conversationsAdapter = null;
257 this.touchHelper = null;
258 }
259 @Override
260 public void onDestroy() {
261 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onDestroy()");
262 super.onDestroy();
263
264 }
265 @Override
266 public void onPause() {
267 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.onPause()");
268 pendingActionHelper.execute();
269 super.onPause();
270 }
271
272 @Override
273 public void onDetach() {
274 super.onDetach();
275 this.activity = null;
276 }
277
278 @Override
279 public void onCreate(Bundle savedInstanceState) {
280 super.onCreate(savedInstanceState);
281 setHasOptionsMenu(true);
282 }
283
284 @Override
285 public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
286 this.mSwipeEscapeVelocity = getResources().getDimension(R.dimen.swipe_escape_velocity);
287 this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversations_overview, container, false);
288 this.binding.fab.setOnClickListener((view) -> StartConversationActivity.launch(getActivity()));
289
290 this.conversationsAdapter = new ConversationAdapter(this.activity, this.conversations);
291 this.conversationsAdapter.setConversationClickListener((view, conversation) -> {
292 if (activity instanceof OnConversationSelected) {
293 ((OnConversationSelected) activity).onConversationSelected(conversation);
294 } else {
295 Log.w(ConversationsOverviewFragment.class.getCanonicalName(), "Activity does not implement OnConversationSelected");
296 }
297 });
298 this.binding.list.setAdapter(this.conversationsAdapter);
299 this.binding.list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
300 this.touchHelper = new ItemTouchHelper(this.callback);
301 this.touchHelper.attachToRecyclerView(this.binding.list);
302 return binding.getRoot();
303 }
304
305 @Override
306 public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
307 menuInflater.inflate(R.menu.fragment_conversations_overview, menu);
308 AccountUtils.showHideMenuItems(menu);
309 final MenuItem easyOnboardInvite = menu.findItem(R.id.action_easy_invite);
310 easyOnboardInvite.setVisible(EasyOnboardingInvite.anyHasSupport(activity == null ? null : activity.xmppConnectionService));
311 }
312
313 @Override
314 public void onBackendConnected() {
315 refresh();
316 }
317
318 @Override
319 public void onSaveInstanceState(Bundle bundle) {
320 super.onSaveInstanceState(bundle);
321 ScrollState scrollState = getScrollState();
322 if (scrollState != null) {
323 bundle.putParcelable(STATE_SCROLL_POSITION, scrollState);
324 }
325 }
326
327 private ScrollState getScrollState() {
328 if (this.binding == null) {
329 return null;
330 }
331 LinearLayoutManager layoutManager = (LinearLayoutManager) this.binding.list.getLayoutManager();
332 int position = layoutManager.findFirstVisibleItemPosition();
333 final View view = this.binding.list.getChildAt(0);
334 if (view != null) {
335 return new ScrollState(position,view.getTop());
336 } else {
337 return new ScrollState(position, 0);
338 }
339 }
340
341 @Override
342 public void onStart() {
343 super.onStart();
344 Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onStart()");
345 if (activity.xmppConnectionService != null) {
346 refresh();
347 }
348 }
349
350 @Override
351 public void onResume() {
352 super.onResume();
353 Log.d(Config.LOGTAG, "ConversationsOverviewFragment.onResume()");
354 }
355
356 @Override
357 public boolean onOptionsItemSelected(final MenuItem item) {
358 if (MenuDoubleTabUtil.shouldIgnoreTap()) {
359 return false;
360 }
361 switch (item.getItemId()) {
362 case R.id.action_search:
363 startActivity(new Intent(getActivity(), SearchActivity.class));
364 return true;
365 case R.id.action_easy_invite:
366 selectAccountToStartEasyInvite();
367 return true;
368 }
369 return super.onOptionsItemSelected(item);
370 }
371
372 private void selectAccountToStartEasyInvite() {
373 final List<Account> accounts = EasyOnboardingInvite.getSupportingAccounts(activity.xmppConnectionService);
374 if (accounts.size() == 1) {
375 openEasyInviteScreen(accounts.get(0));
376 } else {
377 final AtomicReference<Account> selectedAccount = new AtomicReference<>(accounts.get(0));
378 final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
379 alertDialogBuilder.setTitle(R.string.choose_account);
380 final String[] asStrings = Collections2.transform(accounts, a -> a.getJid().asBareJid().toEscapedString()).toArray(new String[0]);
381 alertDialogBuilder.setSingleChoiceItems(asStrings, 0, (dialog, which) -> selectedAccount.set(accounts.get(which)));
382 alertDialogBuilder.setNegativeButton(R.string.cancel, null);
383 alertDialogBuilder.setPositiveButton(R.string.ok, (dialog, which) -> openEasyInviteScreen(selectedAccount.get()));
384 alertDialogBuilder.create().show();
385 }
386 }
387
388 private void openEasyInviteScreen(final Account account) {
389 EasyOnboardingInviteActivity.launch(account, activity);
390 }
391
392 @Override
393 void refresh() {
394 if (this.binding == null || this.activity == null) {
395 Log.d(Config.LOGTAG,"ConversationsOverviewFragment.refresh() skipped updated because view binding or activity was null");
396 return;
397 }
398 this.activity.xmppConnectionService.populateWithOrderedConversations(this.conversations);
399 Conversation removed = this.swipedConversation.peek();
400 if (removed != null) {
401 if (removed.isRead()) {
402 this.conversations.remove(removed);
403 } else {
404 pendingActionHelper.execute();
405 }
406 }
407 this.conversationsAdapter.notifyDataSetChanged();
408 ScrollState scrollState = pendingScrollState.pop();
409 if (scrollState != null) {
410 setScrollPosition(scrollState);
411 }
412 }
413
414 private void setScrollPosition(ScrollState scrollPosition) {
415 if (scrollPosition != null) {
416 LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
417 layoutManager.scrollToPositionWithOffset(scrollPosition.position, scrollPosition.offset);
418 }
419 }
420}