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

github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Niedermann <info@niedermann.it>2020-07-08 14:47:21 +0300
committerNiedermann IT-Dienstleistungen <stefan-niedermann@users.noreply.github.com>2020-07-08 15:21:08 +0300
commit7d349fae5fce71762dcfa1aa7844ccc2c759834d (patch)
tree65e029b588aad709776d39e6d39446c6390e38be /app/src/main/java/it/niedermann/nextcloud/deck/ui/view/labellayout/LabelLayout.java
parent93123c828101aae745812a3583bbd2365a0cede2 (diff)
#579 Compact mode - refactoring
Diffstat (limited to 'app/src/main/java/it/niedermann/nextcloud/deck/ui/view/labellayout/LabelLayout.java')
-rw-r--r--app/src/main/java/it/niedermann/nextcloud/deck/ui/view/labellayout/LabelLayout.java94
1 files changed, 94 insertions, 0 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/view/labellayout/LabelLayout.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/view/labellayout/LabelLayout.java
new file mode 100644
index 000000000..9a1d60021
--- /dev/null
+++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/view/labellayout/LabelLayout.java
@@ -0,0 +1,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);
+}