Skip to content

Commit

Permalink
chore(done): Support compact mode
Browse files Browse the repository at this point in the history
Refs: #1556

Signed-off-by: Stefan Niedermann <info@niedermann.it>
  • Loading branch information
stefan-niedermann committed Jan 16, 2024
1 parent 8405834 commit b8d1a2a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,49 @@

public class DueDateChip extends Chip {

protected @ColorInt int colorOnSurface;
@ColorInt
protected final int colorOnSurface;
protected final boolean compactMode;

public DueDateChip(Context context) {
super(context);
initialize();
this(context, null);
}

public DueDateChip(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
this(context, attrs, R.attr.chipStyle);
}

public DueDateChip(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize();
}

private void initialize() {
final var typedValue = new TypedValue();
final var theme = getContext().getTheme();
theme.resolveAttribute(R.attr.colorOnSurface, typedValue, true);
this.colorOnSurface = typedValue.data;

final var styles = context.obtainStyledAttributes(attrs, R.styleable.DueDateChip, defStyleAttr, 0);
this.compactMode = styles.getBoolean(R.styleable.DueDateChip_compactMode, false);
styles.recycle();

setEnsureMinTouchTargetSize(false);
setMinHeight(0);
setChipMinHeight(0);
@Px final var padding = DimensionUtil.INSTANCE.dpToPx(getContext(), R.dimen.spacer_1x);
setPadding(padding, padding, padding, padding);
setClickable(false);

final var typedValue = new TypedValue();
final var theme = getContext().getTheme();
theme.resolveAttribute(R.attr.colorOnSurface, typedValue, true);
this.colorOnSurface = typedValue.data;
if (compactMode) {
setChipEndPadding(0);
setTextEndPadding(0);
}
}

public void setDueDate(@NonNull Instant date, boolean isDone) {
setText(DateUtil.getRelativeDateTimeString(getContext(), date.toEpochMilli()));
if (compactMode) {
setText(null);
} else {
setText(DateUtil.getRelativeDateTimeString(getContext(), date.toEpochMilli()));
}

@DrawableRes final int chipIconRes;
@Nullable @ColorRes final Integer textColorRes;
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/item_card_compact.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
android:id="@+id/card_due_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:compactMode="true"
tools:chipIcon="@drawable/ic_time_24"
tools:text="tomorrow" />

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
<attr name="description" format="string" />
<attr name="image" format="reference" />
</declare-styleable>
<declare-styleable name="DueDateChip">
<attr name="compactMode" format="boolean" />
</declare-styleable>
</resources>

0 comments on commit b8d1a2a

Please sign in to comment.