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
|
package it.niedermann.nextcloud.deck.ui.card.attachments;
import static androidx.lifecycle.Transformations.distinctUntilChanged;
import static androidx.recyclerview.widget.RecyclerView.NO_ID;
import static it.niedermann.nextcloud.deck.util.AttachmentUtil.openAttachmentInBrowser;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityOptionsCompat;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.databinding.ItemAttachmentDefaultBinding;
import it.niedermann.nextcloud.deck.databinding.ItemAttachmentImageBinding;
import it.niedermann.nextcloud.deck.model.Account;
import it.niedermann.nextcloud.deck.model.Attachment;
import it.niedermann.nextcloud.deck.ui.attachments.AttachmentsActivity;
import it.niedermann.nextcloud.deck.ui.branding.Branded;
import it.niedermann.nextcloud.deck.util.MimeTypeUtil;
@SuppressWarnings("WeakerAccess")
public class CardAttachmentAdapter extends RecyclerView.Adapter<AttachmentViewHolder> implements Branded {
public static final int VIEW_TYPE_DEFAULT = 2;
public static final int VIEW_TYPE_IMAGE = 1;
@NonNull
private final MutableLiveData<Boolean> isEmpty = new MutableLiveData<>(true);
@NonNull
private final MenuInflater menuInflater;
@ColorInt
private int mainColor;
private final Account account;
@Nullable
private Long cardRemoteId = null;
private final long cardLocalId;
@NonNull
private final FragmentManager fragmentManager;
@NonNull
private final List<Attachment> attachments = new ArrayList<>();
@NonNull
private final AttachmentClickedListener attachmentClickedListener;
CardAttachmentAdapter(
@NonNull FragmentManager fragmentManager,
@NonNull MenuInflater menuInflater,
@NonNull AttachmentClickedListener attachmentClickedListener,
@NonNull Account account,
@Nullable Long cardLocalId
) {
super();
this.fragmentManager = fragmentManager;
this.menuInflater = menuInflater;
this.attachmentClickedListener = attachmentClickedListener;
this.account = account;
this.cardLocalId = cardLocalId == null ? NO_ID : cardLocalId;
setHasStableIds(true);
}
@Override
public long getItemId(int position) {
Long id = attachments.get(position).getLocalId();
return id == null ? NO_ID : id;
}
@NonNull
@Override
public AttachmentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final var context = parent.getContext();
switch (viewType) {
case VIEW_TYPE_IMAGE:
return new ImageAttachmentViewHolder(ItemAttachmentImageBinding.inflate(LayoutInflater.from(context), parent, false));
case VIEW_TYPE_DEFAULT:
default:
return new DefaultAttachmentViewHolder(ItemAttachmentDefaultBinding.inflate(LayoutInflater.from(context), parent, false));
}
}
@Override
public void onBindViewHolder(@NonNull AttachmentViewHolder holder, int position) {
final var attachment = attachments.get(position);
final var context = holder.itemView.getContext();
final View.OnClickListener onClickListener;
switch (getItemViewType(position)) {
case VIEW_TYPE_IMAGE: {
onClickListener = (event) -> {
attachmentClickedListener.onAttachmentClicked(position);
final var intent = AttachmentsActivity.createIntent(context, account, cardLocalId, attachment.getLocalId());
if (context instanceof Activity) {
String transitionName = context.getString(R.string.transition_attachment_preview, String.valueOf(attachment.getLocalId()));
holder.getPreview().setTransitionName(transitionName);
context.startActivity(intent, ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context, holder.getPreview(), transitionName).toBundle());
} else {
context.startActivity(intent);
}
};
break;
}
case VIEW_TYPE_DEFAULT:
default: {
onClickListener = (event) -> openAttachmentInBrowser(account, context, cardRemoteId, attachment);
break;
}
}
holder.bind(account, menuInflater, fragmentManager, cardRemoteId, attachment, onClickListener, mainColor);
}
@Override
public int getItemViewType(int position) {
return MimeTypeUtil.isImage(attachments.get(position).getMimetype()) ? VIEW_TYPE_IMAGE : VIEW_TYPE_DEFAULT;
}
@Override
public int getItemCount() {
return attachments.size();
}
private void updateIsEmpty() {
this.isEmpty.postValue(getItemCount() <= 0);
}
@NonNull
public LiveData<Boolean> isEmpty() {
return distinctUntilChanged(this.isEmpty);
}
public void setAttachments(@NonNull List<Attachment> attachments, @Nullable Long cardRemoteId) {
this.cardRemoteId = cardRemoteId;
this.attachments.clear();
this.attachments.addAll(attachments);
notifyDataSetChanged();
this.updateIsEmpty();
}
public void addAttachment(Attachment a) {
this.attachments.add(0, a);
notifyItemInserted(this.attachments.size());
this.updateIsEmpty();
}
public void removeAttachment(Attachment a) {
final int index = this.attachments.indexOf(a);
this.attachments.remove(a);
notifyItemRemoved(index);
this.updateIsEmpty();
}
public void replaceAttachment(Attachment toReplace, Attachment with) {
final int index = this.attachments.indexOf(toReplace);
this.attachments.remove(toReplace);
this.attachments.add(index, with);
notifyItemChanged(index);
}
@Override
public void applyBrand(@ColorInt int mainColor) {
this.mainColor = mainColor;
notifyDataSetChanged();
}
}
|