blob: 9a1d60021002ba3ec5943b1d27480a090ae51b6c (
plain)
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
|
package it.niedermann.nextcloud.deck.ui.view.labellayout;
import android.content.Context;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Px;
import com.google.android.flexbox.FlexboxLayout;
import java.util.LinkedList;
import java.util.List;
import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.model.Label;
import it.niedermann.nextcloud.deck.ui.view.labelchip.LabelChip;
import static it.niedermann.nextcloud.deck.util.DimensionUtil.dpToPx;
public abstract class LabelLayout extends FlexboxLayout {
@Px
final protected int gutter;
@NonNull
final private List<LabelChip> chipList = new LinkedList<>();
public LabelLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.gutter = dpToPx(context, R.dimen.spacer_1hx);
}
/**
* Instead of clearing and adding all labels, one can use this method to avoid flickering
*/
public void updateLabels(@NonNull List<Label> labels) {
removeObsoleteLabels(labels);
addNewLabels(labels);
}
@Override
public void removeAllViews() {
super.removeAllViews();
this.chipList.clear();
}
@Override
public void removeViewAt(int index) {
// TODO this should also remove the item from chipList
DeckLog.logError(new UnsupportedOperationException("Not implemented yet"));
super.removeViewAt(index);
}
/**
* Remove all labels from the view which are not in the labels list
*/
private void removeObsoleteLabels(List<Label> labels) {
chipList:
for (int i = 0; i < chipList.size(); i++) {
LabelChip currentChip = chipList.get(i);
final Label existingLabel = currentChip.getLabel();
for (Label label : labels) {
if (existingLabel.equals(label)) {
continue chipList;
}
}
super.removeViewAt(i);
chipList.remove(currentChip);
i--;
}
}
/**
* Add all labels to the view which are not yet in the view but in the labels list
*/
private void addNewLabels(List<Label> labels) {
int oldLabelSize = chipList.size();
labelList:
for (Label label : labels) {
for (int i = 0; i < oldLabelSize; i++) {
final LabelChip currentChip = chipList.get(i);
final Label existingLabel = currentChip.getLabel();
if (existingLabel.equals(label)) {
continue labelList;
}
}
final LabelChip chip = createLabelChip(label);
addView(chip);
chipList.add(chip);
}
}
protected abstract LabelChip createLabelChip(@NonNull Label label);
}
|