Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LabelLayout.java « labellayout « view « ui « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ce7e38262d302d7feb345ac5d1f9f73352cfdb37 (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
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;

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 = context.getResources().getDimensionPixelSize(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++) {
            final var currentChip = chipList.get(i);
            final var existingLabel = currentChip.getLabel();
            for (final var 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 (final var label : labels) {
            for (int i = 0; i < oldLabelSize; i++) {
                final var currentChip = chipList.get(i);
                final var existingLabel = currentChip.getLabel();
                if (existingLabel.equals(label)) {
                    continue labelList;
                }
            }
            final var chip = createLabelChip(label);
            addView(chip);
            chipList.add(chip);
        }
    }

    protected abstract LabelChip createLabelChip(@NonNull Label label);
}