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