Implement Parcelable for Alarm, snackbar undo working when deleted
This commit is contained in:
parent
31385ad9b4
commit
5f138f2756
@ -25,7 +25,7 @@ dependencies {
|
|||||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||||
testCompile 'junit:junit:4.12'
|
testCompile 'junit:junit:4.12'
|
||||||
testCompile 'org.mockito:mockito-core:1.10.19'
|
testCompile 'org.mockito:mockito-core:1.10.19'
|
||||||
testCompile 'org.robolectric:robolectric:3.0'
|
testCompile 'org.robolectric:robolectric:3.0' // TODO: delete, not in use
|
||||||
provided 'com.google.auto.value:auto-value:1.2'
|
provided 'com.google.auto.value:auto-value:1.2'
|
||||||
apt 'com.google.auto.value:auto-value:1.2'
|
apt 'com.google.auto.value:auto-value:1.2'
|
||||||
compile 'com.android.support:appcompat-v7:23.2.1'
|
compile 'com.android.support:appcompat-v7:23.2.1'
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package com.philliphsu.clock2;
|
package com.philliphsu.clock2;
|
||||||
|
|
||||||
|
import android.os.Parcel;
|
||||||
|
import android.os.Parcelable;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
import com.google.auto.value.AutoValue;
|
import com.google.auto.value.AutoValue;
|
||||||
@ -18,7 +20,7 @@ import static com.philliphsu.clock2.DaysOfWeek.SUNDAY;
|
|||||||
* Created by Phillip Hsu on 5/26/2016.
|
* Created by Phillip Hsu on 5/26/2016.
|
||||||
*/
|
*/
|
||||||
@AutoValue
|
@AutoValue
|
||||||
public abstract class Alarm implements JsonSerializable {
|
public abstract class Alarm implements JsonSerializable, Parcelable {
|
||||||
private static final int MAX_MINUTES_CAN_SNOOZE = 30;
|
private static final int MAX_MINUTES_CAN_SNOOZE = 30;
|
||||||
|
|
||||||
// =================== MUTABLE =======================
|
// =================== MUTABLE =======================
|
||||||
@ -206,6 +208,63 @@ public abstract class Alarm implements JsonSerializable {
|
|||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ============================ PARCELABLE ==============================
|
||||||
|
// Unfortunately, we can't use the Parcelable extension for AutoValue because
|
||||||
|
// our model isn't totally immutable. Our mutable properties will be left
|
||||||
|
// out of the generated class.
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int describeContents() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeToParcel(Parcel dest, int flags) {
|
||||||
|
dest.writeInt(hour());
|
||||||
|
dest.writeInt(minutes());
|
||||||
|
dest.writeString(label());
|
||||||
|
dest.writeString(ringtone());
|
||||||
|
dest.writeInt(vibrates() ? 1 : 0);
|
||||||
|
// Mutable fields must be written after the immutable fields,
|
||||||
|
// because when we recreate the object, we can't initialize
|
||||||
|
// those mutable fields until after we call build(). Values
|
||||||
|
// in the parcel are read in the order they were written.
|
||||||
|
dest.writeLong(id);
|
||||||
|
dest.writeLong(snoozingUntilMillis);
|
||||||
|
dest.writeInt(enabled ? 1 : 0);
|
||||||
|
dest.writeBooleanArray(recurringDays);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Alarm create(Parcel in) {
|
||||||
|
Alarm alarm = Alarm.builder()
|
||||||
|
.hour(in.readInt())
|
||||||
|
.minutes(in.readInt())
|
||||||
|
.label(in.readString())
|
||||||
|
.ringtone(in.readString())
|
||||||
|
.vibrates(in.readInt() != 0)
|
||||||
|
.build();
|
||||||
|
alarm.id = in.readLong();
|
||||||
|
alarm.snoozingUntilMillis = in.readLong();
|
||||||
|
alarm.enabled = in.readInt() != 0;
|
||||||
|
in.readBooleanArray(alarm.recurringDays);
|
||||||
|
return alarm;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final Parcelable.Creator<Alarm> CREATOR
|
||||||
|
= new Parcelable.Creator<Alarm>() {
|
||||||
|
@Override
|
||||||
|
public Alarm createFromParcel(Parcel source) {
|
||||||
|
return Alarm.create(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Alarm[] newArray(int size) {
|
||||||
|
return new Alarm[size];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ======================================================================
|
||||||
|
|
||||||
@AutoValue.Builder
|
@AutoValue.Builder
|
||||||
public abstract static class Builder {
|
public abstract static class Builder {
|
||||||
public abstract Builder hour(int hour);
|
public abstract Builder hour(int hour);
|
||||||
|
|||||||
@ -127,10 +127,10 @@ public class AlarmsFragment extends Fragment implements LoaderCallbacks<Cursor>,
|
|||||||
case REQUEST_CREATE_ALARM:
|
case REQUEST_CREATE_ALARM:
|
||||||
getLoaderManager().restartLoader(0, null, this);
|
getLoaderManager().restartLoader(0, null, this);
|
||||||
case REQUEST_EDIT_ALARM:
|
case REQUEST_EDIT_ALARM:
|
||||||
if (data != null && data.getBooleanExtra(
|
Alarm deletedAlarm;
|
||||||
EditAlarmActivity.EXTRA_ALARM_DELETED, false)) {
|
if (data != null && (deletedAlarm = data.getParcelableExtra(
|
||||||
// TODO: Pass in the old alarm into the intent and access it here?
|
EditAlarmActivity.EXTRA_DELETED_ALARM)) != null) {
|
||||||
onListItemDeleted(null);
|
onListItemDeleted(deletedAlarm);
|
||||||
}
|
}
|
||||||
getLoaderManager().restartLoader(0, null, this);
|
getLoaderManager().restartLoader(0, null, this);
|
||||||
break;
|
break;
|
||||||
@ -149,6 +149,7 @@ public class AlarmsFragment extends Fragment implements LoaderCallbacks<Cursor>,
|
|||||||
|
|
||||||
// TODO: This doesn't need to be defined in the interface.
|
// TODO: This doesn't need to be defined in the interface.
|
||||||
// TODO: Rename to showDeletedSnackbar() or something
|
// TODO: Rename to showDeletedSnackbar() or something
|
||||||
|
// TODO: This needs to prompt a reload of the list.
|
||||||
@Override
|
@Override
|
||||||
public void onListItemDeleted(final Alarm item) {
|
public void onListItemDeleted(final Alarm item) {
|
||||||
Snackbar.make(getActivity().findViewById(R.id.main_content),
|
Snackbar.make(getActivity().findViewById(R.id.main_content),
|
||||||
|
|||||||
@ -58,7 +58,7 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
LoaderManager.LoaderCallbacks<Alarm> {
|
LoaderManager.LoaderCallbacks<Alarm> {
|
||||||
private static final String TAG = "EditAlarmActivity";
|
private static final String TAG = "EditAlarmActivity";
|
||||||
public static final String EXTRA_ALARM_ID = "com.philliphsu.clock2.editalarm.extra.ALARM_ID";
|
public static final String EXTRA_ALARM_ID = "com.philliphsu.clock2.editalarm.extra.ALARM_ID";
|
||||||
public static final String EXTRA_ALARM_DELETED = "com.philliphsu.clock2.editalarm.extra.ALARM_DELETED";
|
public static final String EXTRA_DELETED_ALARM = "com.philliphsu.clock2.editalarm.extra.DELETED_ALARM";
|
||||||
private static final RelativeSizeSpan AMPM_SIZE_SPAN = new RelativeSizeSpan(0.5f);
|
private static final RelativeSizeSpan AMPM_SIZE_SPAN = new RelativeSizeSpan(0.5f);
|
||||||
|
|
||||||
private static final int REQUEST_PICK_RINGTONE = 0;
|
private static final int REQUEST_PICK_RINGTONE = 0;
|
||||||
@ -260,13 +260,15 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
|||||||
if (mOldAlarm != null) {
|
if (mOldAlarm != null) {
|
||||||
if (mOldAlarm.isEnabled()) {
|
if (mOldAlarm.isEnabled()) {
|
||||||
cancelAlarm(mOldAlarm, false);
|
cancelAlarm(mOldAlarm, false);
|
||||||
|
// Re-enable in case this is restored so
|
||||||
|
// the alarm is scheduled again
|
||||||
|
mOldAlarm.setEnabled(true);
|
||||||
}
|
}
|
||||||
mDatabaseManager.deleteAlarm(mOldAlarm);
|
mDatabaseManager.deleteAlarm(mOldAlarm);
|
||||||
}
|
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
// TODO: Pass in the old alarm into the intent?
|
intent.putExtra(EXTRA_DELETED_ALARM, mOldAlarm);
|
||||||
intent.putExtra(EXTRA_ALARM_DELETED, true);
|
|
||||||
setResult(RESULT_OK, intent);
|
setResult(RESULT_OK, intent);
|
||||||
|
}
|
||||||
showEditorClosed();
|
showEditorClosed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user