diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-04-09 18:19:14 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-04-09 18:19:14 +0300 |
commit | 7835fceb4aa1c2b0e0e79de14a7cbd5b1e9447e0 (patch) | |
tree | 227794d812ea548ee2db9c276ba5882392aa8848 /app/src/main/java/it/niedermann/nextcloud/deck/ui/view | |
parent | 99ebeb1a3555f1dd696208dccc631338f160c90a (diff) |
#298 new card layout for web and app?
Signed-off-by: Stefan Niedermann <info@niedermann.it>
Diffstat (limited to 'app/src/main/java/it/niedermann/nextcloud/deck/ui/view')
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/view/LabelChip.java | 59 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/ui/view/LabelLayout.java | 77 |
2 files changed, 136 insertions, 0 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/view/LabelChip.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/view/LabelChip.java new file mode 100644 index 000000000..6a4964dda --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/view/LabelChip.java @@ -0,0 +1,59 @@ +package it.niedermann.nextcloud.deck.ui.view; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.content.res.ColorStateList; +import android.graphics.Color; +import android.os.Build; +import android.text.TextUtils; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; + +import com.google.android.flexbox.FlexboxLayout; +import com.google.android.material.chip.Chip; + +import it.niedermann.nextcloud.deck.DeckLog; +import it.niedermann.nextcloud.deck.model.Label; +import it.niedermann.nextcloud.deck.util.ColorUtil; + +@SuppressLint("ViewConstructor") +public class LabelChip extends Chip { + + private final Label label; + + public LabelChip(@NonNull Context context, @NonNull Label label) { + super(context); + this.label = label; + + FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT + ); + + // TODO Margin right from dp values + params.setMargins(0, 0, 10, 0); + setLayoutParams(params); + + setEnsureMinTouchTargetSize(false); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + setTextAlignment(View.TEXT_ALIGNMENT_CENTER); + } + setText(label.getTitle()); + setEllipsize(TextUtils.TruncateAt.END); + + try { + int labelColor = Color.parseColor("#" + label.getColor()); + ColorStateList c = ColorStateList.valueOf(labelColor); + setChipBackgroundColor(c); + setTextColor(ColorUtil.getForegroundColorForBackgroundColor(labelColor)); + } catch (IllegalArgumentException e) { + DeckLog.logError(e); + } + } + + public Long getLabelLocalId() { + return this.label.getLocalId(); + } +}
\ No newline at end of file diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/view/LabelLayout.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/view/LabelLayout.java new file mode 100644 index 000000000..2e57c151a --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/view/LabelLayout.java @@ -0,0 +1,77 @@ +package it.niedermann.nextcloud.deck.ui.view; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.View; + +import androidx.annotation.NonNull; + +import com.google.android.flexbox.FlexboxLayout; + +import java.util.List; + +import it.niedermann.nextcloud.deck.DeckLog; +import it.niedermann.nextcloud.deck.model.Label; + +public class LabelLayout extends FlexboxLayout { + + public LabelLayout(Context context, AttributeSet attrs) { + super(context, attrs); + } + + /** + * Instead of clearing and adding all labels, one can use this method to avoid flickering + */ + public void updateLabels(@NonNull List<Label> labels) { + addNewLabels(labels); + removeObsoleteLabels(labels); + } + + /** + * Remove all labels from the view which are not in the labels list + */ + private void removeObsoleteLabels(List<Label> labels) { + for (int i = getChildCount() - 1; i >= 0; i--) { + final View existingView = getChildAt(i); + if (existingView instanceof LabelChip) { + final Long existingLabelLocalId = ((LabelChip) existingView).getLabelLocalId(); + boolean idExistsInList = false; + for (Label label : labels) { + if (existingLabelLocalId.equals(label.getLocalId())) { + idExistsInList = true; + break; + } + } + if (!idExistsInList) { + removeViewAt(i); + } + } else { + DeckLog.logError(new IllegalStateException("binding.labels should only contain child view of type " + LabelChip.class.getCanonicalName())); + } + } + } + + /** + * Add all labels to the view which are not yet in the view but in the labels list + */ + private void addNewLabels(List<Label> labels) { + for (Label label : labels) { + boolean viewContainsLabel = false; + for (int i = 0; i < getChildCount(); i++) { + final View existingView = getChildAt(i); + if (existingView instanceof LabelChip) { + final Long existingLabelLocalId = ((LabelChip) existingView).getLabelLocalId(); + if (existingLabelLocalId.equals(label.getLocalId())) { + viewContainsLabel = true; + break; + } + } else { + DeckLog.logError(new IllegalStateException("binding.labels should only contain child view of type " + LabelChip.class.getCanonicalName())); + } + } + if (!viewContainsLabel) { + addView(new LabelChip(getContext(), label)); + } + } + } +} |