From f5d9fb1a97ffad5a5f78242c98b4e7b96a6fbad1 Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Wed, 31 Aug 2016 03:49:20 -0700 Subject: [PATCH] Change text color of time according to state of alarm --- .../clock2/alarms/BaseAlarmViewHolder.java | 25 ++++++++++++++++--- app/src/main/res/layout/alarm_time_layout.xml | 8 ++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/philliphsu/clock2/alarms/BaseAlarmViewHolder.java b/app/src/main/java/com/philliphsu/clock2/alarms/BaseAlarmViewHolder.java index cd36767..21431e2 100644 --- a/app/src/main/java/com/philliphsu/clock2/alarms/BaseAlarmViewHolder.java +++ b/app/src/main/java/com/philliphsu/clock2/alarms/BaseAlarmViewHolder.java @@ -1,11 +1,13 @@ package com.philliphsu.clock2.alarms; +import android.content.res.ColorStateList; import android.graphics.drawable.Drawable; import android.support.annotation.LayoutRes; import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; import android.support.v7.widget.SwitchCompat; import android.text.format.DateFormat; +import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; @@ -35,6 +37,7 @@ import static com.philliphsu.clock2.util.DateFormatUtils.formatTime; * Created by Phillip Hsu on 7/31/2016. */ public abstract class BaseAlarmViewHolder extends BaseViewHolder { + private static final String TAG = "BaseAlarmViewHolder"; private final AlarmController mAlarmController; // TODO: Should we use VectorDrawable type? @@ -60,7 +63,7 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder { @Override public void onBind(Alarm alarm) { super.onBind(alarm); - bindTime(new Date(alarm.ringsAt())); + bindTime(alarm); bindSwitch(alarm.isEnabled()); bindDismissButton(alarm); bindLabel(alarm.label()); @@ -157,13 +160,29 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder { } } - private void bindTime(Date date) { - String time = DateFormat.getTimeFormat(getContext()).format(date); + @OnClick(R.id.time) + void openTimePicker() { + Log.d(TAG, "Time clicked!"); + } + + private void bindTime(Alarm alarm) { + String time = DateFormat.getTimeFormat(getContext()).format(new Date(alarm.ringsAt())); if (DateFormat.is24HourFormat(getContext())) { mTime.setText(time); } else { TimeTextUtils.setText(time, mTime); } + + // Use a mock TextView to get our colors, because its ColorStateList is never + // mutated for the lifetime of this ViewHolder (even when reused). + // This solution is robust against dark/light theme changes, whereas using + // color resources is not. + TextView colorsSource = (TextView) itemView.findViewById(R.id.colors_source); + ColorStateList colors = colorsSource.getTextColors(); + int def = colors.getDefaultColor(); + int disabled = colors.getColorForState(new int[] {-android.R.attr.state_enabled}, def); + // We only have two states, so we don't care about losing the other state colors. + mTime.setTextColor(alarm.isEnabled() ? def : disabled); } private void bindSwitch(boolean enabled) { diff --git a/app/src/main/res/layout/alarm_time_layout.xml b/app/src/main/res/layout/alarm_time_layout.xml index 9aa19f7..16c09cf 100644 --- a/app/src/main/res/layout/alarm_time_layout.xml +++ b/app/src/main/res/layout/alarm_time_layout.xml @@ -25,4 +25,12 @@ android:layout_height="48dp" android:layout_gravity="center_vertical"/> + + + \ No newline at end of file