1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
package it.niedermann.nextcloud.deck.ui.card;
import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupMenu;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import it.niedermann.android.crosstabdnd.DragAndDropAdapter;
import it.niedermann.android.crosstabdnd.DraggedItemLocalState;
import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.databinding.ItemCardBinding;
import it.niedermann.nextcloud.deck.model.Account;
import it.niedermann.nextcloud.deck.model.Card;
import it.niedermann.nextcloud.deck.model.Label;
import it.niedermann.nextcloud.deck.model.Stack;
import it.niedermann.nextcloud.deck.model.User;
import it.niedermann.nextcloud.deck.model.enums.DBStatus;
import it.niedermann.nextcloud.deck.model.full.FullCard;
import it.niedermann.nextcloud.deck.model.full.FullStack;
import it.niedermann.nextcloud.deck.persistence.sync.SyncManager;
import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper;
import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.WrappedLiveData;
import it.niedermann.nextcloud.deck.ui.branding.Branded;
import it.niedermann.nextcloud.deck.ui.branding.BrandedActivity;
import it.niedermann.nextcloud.deck.ui.branding.BrandedAlertDialogBuilder;
import it.niedermann.nextcloud.deck.ui.exception.ExceptionDialogFragment;
import it.niedermann.nextcloud.deck.util.DateUtil;
import it.niedermann.nextcloud.deck.util.ViewUtil;
import static it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper.observeOnce;
import static it.niedermann.nextcloud.deck.util.MimeTypeUtil.TEXT_PLAIN;
public class CardAdapter extends RecyclerView.Adapter<ItemCardViewHolder> implements DragAndDropAdapter<FullCard>, Branded {
protected final SyncManager syncManager;
private final FragmentManager fragmentManager;
private final Account account;
@Nullable
private final Long currentBoardRemoteId;
private final long boardId;
private final long stackId;
private final boolean canEdit;
@NonNull
private final Context context;
@Nullable
private final SelectCardListener selectCardListener;
private List<FullCard> cardList = new LinkedList<>();
private LifecycleOwner lifecycleOwner;
private List<FullStack> availableStacks = new ArrayList<>();
private String counterMaxValue;
private int mainColor;
@StringRes
private int shareLinkRes;
public CardAdapter(@NonNull Context context, @NonNull FragmentManager fragmentManager, @NonNull Account account, long boardId, @Nullable Long currentBoardRemoteId, long stackId, boolean canEdit, @NonNull SyncManager syncManager, @NonNull LifecycleOwner lifecycleOwner, @Nullable SelectCardListener selectCardListener) {
this.context = context;
this.fragmentManager = fragmentManager;
this.lifecycleOwner = lifecycleOwner;
this.account = account;
this.shareLinkRes = account.getServerDeckVersionAsObject().getShareLinkResource();
this.boardId = boardId;
this.currentBoardRemoteId = currentBoardRemoteId;
this.stackId = stackId;
this.canEdit = canEdit;
this.syncManager = syncManager;
this.selectCardListener = selectCardListener;
this.mainColor = context.getResources().getColor(R.color.primary);
syncManager.getStacksForBoard(account.getId(), boardId).observe(this.lifecycleOwner, (stacks) -> {
availableStacks.clear();
availableStacks.addAll(stacks);
});
setHasStableIds(true);
}
@Override
public long getItemId(int position) {
return cardList.get(position).getLocalId();
}
@NonNull
@Override
public ItemCardViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
final Context context = viewGroup.getContext();
counterMaxValue = context.getString(R.string.counter_max_value);
LayoutInflater layoutInflater = LayoutInflater.from(context);
ItemCardBinding binding = ItemCardBinding.inflate(layoutInflater, viewGroup, false);
return new ItemCardViewHolder(binding);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull ItemCardViewHolder viewHolder, int position) {
final Context context = viewHolder.itemView.getContext();
final FullCard card = cardList.get(position);
viewHolder.binding.card.setOnClickListener((v) -> {
if (selectCardListener == null) {
context.startActivity(EditActivity.createEditCardIntent(context, account, boardId, card.getLocalId()));
} else {
selectCardListener.onCardSelected(card);
}
});
if (canEdit && selectCardListener == null) {
viewHolder.binding.card.setOnLongClickListener((v) -> {
DeckLog.log("Starting drag and drop");
v.startDrag(ClipData.newPlainText("cardid", String.valueOf(card.getLocalId())),
new View.DragShadowBuilder(v),
new DraggedItemLocalState<>(card, viewHolder.binding.card, this, position),
0
);
return true;
});
} else {
viewHolder.binding.cardMenu.setVisibility(View.GONE);
}
viewHolder.binding.cardTitle.setText(card.getCard().getTitle().trim());
if (card.getAssignedUsers() != null && card.getAssignedUsers().size() > 0) {
viewHolder.binding.overlappingAvatars.setAvatars(account, card.getAssignedUsers());
viewHolder.binding.overlappingAvatars.setVisibility(View.VISIBLE);
} else {
viewHolder.binding.overlappingAvatars.setVisibility(View.GONE);
}
DrawableCompat.setTint(viewHolder.binding.notSyncedYet.getDrawable(), mainColor);
viewHolder.binding.notSyncedYet.setVisibility(DBStatus.LOCAL_EDITED.equals(card.getStatusEnum()) ? View.VISIBLE : View.GONE);
if (card.getCard().getDueDate() != null) {
setupDueDate(viewHolder.binding.cardDueDate, card.getCard());
viewHolder.binding.cardDueDate.setVisibility(View.VISIBLE);
} else {
viewHolder.binding.cardDueDate.setVisibility(View.GONE);
}
final int attachmentsCount = card.getAttachments().size();
if (attachmentsCount == 0) {
viewHolder.binding.cardCountAttachments.setVisibility(View.GONE);
} else {
setupCounter(viewHolder.binding.cardCountAttachments, attachmentsCount);
viewHolder.binding.cardCountAttachments.setVisibility(View.VISIBLE);
}
final int commentsCount = card.getCommentCount();
if (commentsCount == 0) {
viewHolder.binding.cardCountComments.setVisibility(View.GONE);
} else {
setupCounter(viewHolder.binding.cardCountComments, commentsCount);
viewHolder.binding.cardCountComments.setVisibility(View.VISIBLE);
}
List<Label> labels = card.getLabels();
if (labels != null && labels.size() > 0) {
viewHolder.binding.labels.updateLabels(labels);
viewHolder.binding.labels.setVisibility(View.VISIBLE);
} else {
viewHolder.binding.labels.removeAllViews();
viewHolder.binding.labels.setVisibility(View.GONE);
}
Card.TaskStatus taskStatus = card.getCard().getTaskStatus();
if (taskStatus.taskCount > 0) {
viewHolder.binding.cardCountTasks.setText(context.getResources().getString(R.string.task_count, String.valueOf(taskStatus.doneCount), String.valueOf(taskStatus.taskCount)));
viewHolder.binding.cardCountTasks.setVisibility(View.VISIBLE);
} else {
viewHolder.binding.cardCountTasks.setVisibility(View.GONE);
}
viewHolder.binding.cardMenu.setOnClickListener(v -> onOverflowIconClicked(v, card));
}
private void setupCounter(@NonNull TextView textView, int count) {
if (count > 99) {
textView.setText(counterMaxValue);
} else if (count > 1) {
textView.setText(String.valueOf(count));
} else if (count == 1) {
textView.setText("");
}
}
private void setupDueDate(@NonNull TextView cardDueDate, @NotNull Card card) {
final Context context = cardDueDate.getContext();
cardDueDate.setText(DateUtil.getRelativeDateTimeString(context, card.getDueDate().getTime()));
ViewUtil.themeDueDate(context, cardDueDate, card.getDueDate());
}
@Override
public int getItemCount() {
return cardList == null ? 0 : cardList.size();
}
public void insertItem(FullCard fullCard, int position) {
cardList.add(position, fullCard);
notifyItemInserted(position);
}
@Override
public List<FullCard> getItemList() {
return this.cardList;
}
@Override
public void moveItem(int fromPosition, int toPosition) {
cardList.add(toPosition, cardList.remove(fromPosition));
notifyItemMoved(fromPosition, toPosition);
}
@Override
public void removeItem(int position) {
cardList.remove(position);
notifyItemRemoved(position);
}
protected void onOverflowIconClicked(@NotNull View view, FullCard card) {
final Context context = view.getContext();
final PopupMenu popup = new PopupMenu(context, view);
popup.inflate(R.menu.card_menu);
prepareOptionsMenu(popup.getMenu(), card);
popup.setOnMenuItemClickListener(item -> optionsItemSelected(context, item, card));
popup.show();
}
protected void prepareOptionsMenu(Menu menu, @NotNull FullCard card) {
if (containsUser(card.getAssignedUsers(), account.getUserName())) {
menu.removeItem(menu.findItem(R.id.action_card_assign).getItemId());
} else {
menu.removeItem(menu.findItem(R.id.action_card_unassign).getItemId());
}
if (currentBoardRemoteId == null || card.getCard().getId() == null) {
menu.removeItem(R.id.share_link);
}
}
public void setCardList(@NonNull List<FullCard> cardList) {
this.cardList.clear();
this.cardList.addAll(cardList);
notifyDataSetChanged();
}
@Contract("null, _ -> false")
private boolean containsUser(List<User> userList, String username) {
if (userList != null) {
for (User user : userList) {
if (user.getPrimaryKey().equals(username)) {
return true;
}
}
}
return false;
}
protected boolean optionsItemSelected(@NonNull Context context, @NotNull MenuItem item, FullCard fullCard) {
switch (item.getItemId()) {
case R.id.share_link: {
Intent shareIntent = new Intent()
.setAction(Intent.ACTION_SEND)
.setType(TEXT_PLAIN)
.putExtra(Intent.EXTRA_SUBJECT, fullCard.getCard().getTitle())
.putExtra(Intent.EXTRA_TITLE, fullCard.getCard().getTitle())
.putExtra(Intent.EXTRA_TEXT, account.getUrl() + context.getString(shareLinkRes, currentBoardRemoteId, fullCard.getCard().getId()));
context.startActivity(Intent.createChooser(shareIntent, fullCard.getCard().getTitle()));
}
case R.id.action_card_assign: {
new Thread(() -> syncManager.assignUserToCard(syncManager.getUserByUidDirectly(fullCard.getCard().getAccountId(), account.getUserName()), fullCard.getCard())).start();
return true;
}
case R.id.action_card_unassign: {
new Thread(() -> syncManager.unassignUserFromCard(syncManager.getUserByUidDirectly(fullCard.getCard().getAccountId(), account.getUserName()), fullCard.getCard())).start();
return true;
}
case R.id.action_card_move: {
int currentStackItem = 0;
CharSequence[] items = new CharSequence[availableStacks.size()];
for (int i = 0; i < availableStacks.size(); i++) {
final Stack stack = availableStacks.get(i).getStack();
items[i] = stack.getTitle();
if (stack.getLocalId().equals(stackId)) {
currentStackItem = i;
}
}
final FullCard newCard = fullCard;
new BrandedAlertDialogBuilder(context)
.setSingleChoiceItems(items, currentStackItem, (dialog, which) -> {
dialog.cancel();
newCard.getCard().setStackId(availableStacks.get(which).getStack().getLocalId());
LiveDataHelper.observeOnce(syncManager.updateCard(newCard), lifecycleOwner, (c) -> {
// Nothing to do here...
});
DeckLog.log("Moved card \"" + fullCard.getCard().getTitle() + "\" to \"" + availableStacks.get(which).getStack().getTitle() + "\"");
})
.setNeutralButton(android.R.string.cancel, null)
.setTitle(context.getString(R.string.action_card_move_title, fullCard.getCard().getTitle()))
.show();
return true;
}
case R.id.action_card_archive: {
final WrappedLiveData<FullCard> archiveLiveData = syncManager.archiveCard(fullCard);
observeOnce(archiveLiveData, lifecycleOwner, (v) -> {
if (archiveLiveData.hasError()) {
ExceptionDialogFragment.newInstance(archiveLiveData.getError(), account).show(fragmentManager, ExceptionDialogFragment.class.getSimpleName());
}
});
return true;
}
case R.id.action_card_delete: {
final WrappedLiveData<Void> deleteLiveData = syncManager.deleteCard(fullCard.getCard());
observeOnce(deleteLiveData, lifecycleOwner, (v) -> {
if (deleteLiveData.hasError()) {
ExceptionDialogFragment.newInstance(deleteLiveData.getError(), account).show(fragmentManager, ExceptionDialogFragment.class.getSimpleName());
}
});
return true;
}
}
return true;
}
@Override
public void applyBrand(int mainColor) {
this.mainColor = BrandedActivity.getSecondaryForegroundColorDependingOnTheme(context, mainColor);
notifyDataSetChanged();
}
}
|