From c0c7d521dfcede95442e04d8b6f7820b10dcb534 Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Sat, 3 Sep 2016 17:57:19 -0700 Subject: [PATCH] Persist recurring day change as it is made --- .../clock2/alarms/BaseAlarmViewHolder.java | 11 -------- .../alarms/ExpandedAlarmViewHolder.java | 26 ++++++++++++++----- 2 files changed, 20 insertions(+), 17 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 9a1f11c..639f251 100644 --- a/app/src/main/java/com/philliphsu/clock2/alarms/BaseAlarmViewHolder.java +++ b/app/src/main/java/com/philliphsu/clock2/alarms/BaseAlarmViewHolder.java @@ -54,10 +54,6 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder { private final Drawable mCancelSnoozeDrawable; private final FragmentManager mFragmentManager; - // So far, Alarms are the only VH type that requires saving a reference to the listener, - // so that they may do extra stuff with it. - private final OnListItemInteractionListener mInteractionListener; - // These should only be changed from the OnTimeSet callback. // If we had initialized these in onBind(), they would be reset to their original values // from the Alarm each time the ViewHolder binds. @@ -85,8 +81,6 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder { AppCompatActivity act = (AppCompatActivity) getContext(); mFragmentManager = act.getSupportFragmentManager(); - mInteractionListener = listener; - // Are we recreating this because of a rotation? // If so, try finding any dialog that was last shown in our backstack, // and restore the callback. @@ -308,9 +302,4 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder { } }; } - - @Deprecated - protected final OnListItemInteractionListener getInteractionListener() { - return mInteractionListener; - } } diff --git a/app/src/main/java/com/philliphsu/clock2/alarms/ExpandedAlarmViewHolder.java b/app/src/main/java/com/philliphsu/clock2/alarms/ExpandedAlarmViewHolder.java index 15df7ff..3b29c5a 100644 --- a/app/src/main/java/com/philliphsu/clock2/alarms/ExpandedAlarmViewHolder.java +++ b/app/src/main/java/com/philliphsu/clock2/alarms/ExpandedAlarmViewHolder.java @@ -12,6 +12,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; +import android.widget.CompoundButton; import android.widget.ToggleButton; import com.philliphsu.clock2.Alarm; @@ -60,7 +61,12 @@ public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder { mOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - createNewAlarmAndWriteToDb(); + // Since changes are persisted as soon as they are made, there's really + // nothing we have to persist here. This just lets the listener know + // we should collapse this VH. + // TODO: Verify that the VH is collapsed. + // Alternatively, I think we can just performClick() on the itemView. + listener.onListItemUpdate(getAlarm(), getAdapterPosition()); } }); @@ -139,9 +145,19 @@ public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder { } @OnCheckedChanged({ R.id.day0, R.id.day1, R.id.day2, R.id.day3, R.id.day4, R.id.day5, R.id.day6 }) - void onDayToggled() { - // TODO - Log.d("yooo", "Hello!"); + void onDayToggled(CompoundButton buttonView, boolean isChecked) { + final Alarm oldAlarm = getAlarm(); + Alarm newAlarm = oldAlarm.toBuilder().build(); + oldAlarm.copyMutableFieldsTo(newAlarm); + // --------------------------------------------------------------------------------- + // TOneverDO: precede copyMutableFieldsTo() + int position = ((ViewGroup) buttonView.getParent()).indexOfChild(buttonView); + int weekDayAtPosition = DaysOfWeek.getInstance(getContext()).weekDayAt(position); + Log.d(TAG, "Day toggle #" + position + " checked changed. This is weekday #" + + weekDayAtPosition + " relative to a week starting on Sunday"); + newAlarm.setRecurring(weekDayAtPosition, isChecked); + // --------------------------------------------------------------------------------- + mAlarmController.save(newAlarm); } /////////////////////////////////////////////////////////////////////////////////////////// @@ -203,7 +219,6 @@ public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder { .hour(oldAlarm.hour()/*TODO*/) .minutes(oldAlarm.minutes()/*TODO*/) .ringtone(""/*TODO*/) - .vibrates(mVibrate.isChecked()) .build(); oldAlarm.copyMutableFieldsTo(newAlarm); // ---------------------------------------------- @@ -213,6 +228,5 @@ public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder { newAlarm.setRecurring(i, isRecurringDay(i)); } // ---------------------------------------------- - getInteractionListener().onListItemUpdate(newAlarm, getAdapterPosition()); } }