Persist label change as it is made. Keep AlarmVH expanded as content change occurs.

This commit is contained in:
Phillip Hsu 2016-09-03 17:36:35 -07:00
parent 8171a663bc
commit abc849c243
3 changed files with 47 additions and 24 deletions

View File

@ -38,9 +38,9 @@ public class AlarmsCursorAdapter extends BaseCursorAdapter<Alarm, BaseAlarmViewH
@Override
public int getItemViewType(int position) {
// final long stableId = getItemId(position);
return /*stableId != RecyclerView.NO_ID && stableId == mExpandedId*/
position == mExpandedPosition
final long stableId = getItemId(position);
return stableId != RecyclerView.NO_ID && stableId == mExpandedId
// position == mExpandedPosition
? VIEW_TYPE_EXPANDED : VIEW_TYPE_COLLAPSED;
}
@ -50,10 +50,11 @@ public class AlarmsCursorAdapter extends BaseCursorAdapter<Alarm, BaseAlarmViewH
// }
public boolean expand(int position) {
final long stableId = getItemId(position);
if (mExpandedId == stableId) {
if (position == RecyclerView.NO_POSITION)
return false;
final long stableId = getItemId(position);
if (stableId == RecyclerView.NO_ID || mExpandedId == stableId)
return false;
}
mExpandedId = stableId;
// If we can call this, the item is in view, so we don't need to scroll to it?
// mScrollHandler.smoothScrollTo(position);

View File

@ -52,6 +52,10 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder<Alarm> {
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<Alarm> 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.
@ -79,6 +83,8 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder<Alarm> {
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.
@ -274,7 +280,12 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder<Alarm> {
return new AddLabelDialog.OnLabelSetListener() {
@Override
public void onLabelSet(String label) {
mLabel.setText(label);
final Alarm oldAlarm = getAlarm();
Alarm newAlarm = oldAlarm.toBuilder()
.label(label)
.build();
oldAlarm.copyMutableFieldsTo(newAlarm);
mAlarmController.save(newAlarm);
}
};
}
@ -295,4 +306,8 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder<Alarm> {
}
};
}
protected final OnListItemInteractionListener<Alarm> getInteractionListener() {
return mInteractionListener;
}
}

View File

@ -54,26 +54,13 @@ public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder {
listener.onListItemDeleted(getAlarm());
}
});
// TODO: We can now do method binding instead, because our superclass provides an API
// to retrieve the interaction listener.
mOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Alarm oldAlarm = getAlarm();
Alarm newAlarm = Alarm.builder()
.hour(oldAlarm.hour()/*TODO*/)
.minutes(oldAlarm.minutes()/*TODO*/)
.label(mLabel.getText().toString())
.ringtone(""/*TODO*/)
.vibrates(mVibrate.isChecked())
.build();
oldAlarm.copyMutableFieldsTo(newAlarm);
// ----------------------------------------------
// TOneverDO: precede copyMutableFieldsTo()
newAlarm.setEnabled(mSwitch.isChecked());
for (int i = SUNDAY; i <= SATURDAY; i++) {
newAlarm.setRecurring(i, isRecurringDay(i));
}
// ----------------------------------------------
listener.onListItemUpdate(newAlarm, getAdapterPosition());
createNewAlarmAndWriteToDb();
}
});
@ -204,4 +191,24 @@ public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder {
// Return the state of this day according to its button
return mDays[pos].isChecked();
}
private void createNewAlarmAndWriteToDb() {
final Alarm oldAlarm = getAlarm();
Alarm newAlarm = Alarm.builder()
.hour(oldAlarm.hour()/*TODO*/)
.minutes(oldAlarm.minutes()/*TODO*/)
.label(mLabel.getText().toString())
.ringtone(""/*TODO*/)
.vibrates(mVibrate.isChecked())
.build();
oldAlarm.copyMutableFieldsTo(newAlarm);
// ----------------------------------------------
// TOneverDO: precede copyMutableFieldsTo()
newAlarm.setEnabled(mSwitch.isChecked());
for (int i = SUNDAY; i <= SATURDAY; i++) {
newAlarm.setRecurring(i, isRecurringDay(i));
}
// ----------------------------------------------
getInteractionListener().onListItemUpdate(newAlarm, getAdapterPosition());
}
}