Created AlarmUtilsHelper interface passed into EditAlarmPresenter
This commit is contained in:
parent
5fb8448ce3
commit
a4b54bd935
@ -16,6 +16,6 @@ interface AlarmContract {
|
|||||||
|
|
||||||
interface Presenter {
|
interface Presenter {
|
||||||
void dismissNow();
|
void dismissNow();
|
||||||
void endSnoozing();
|
void stopSnoozing();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -45,7 +45,7 @@ public final class AlarmUtils {
|
|||||||
am.setExact(AlarmManager.RTC_WAKEUP, ringAt, alarmIntent(context, alarm, false));
|
am.setExact(AlarmManager.RTC_WAKEUP, ringAt, alarmIntent(context, alarm, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void unscheduleAlarm(Context c, Alarm a) {
|
public static void cancelAlarm(Context c, Alarm a) {
|
||||||
AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
|
AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
|
||||||
|
|
||||||
PendingIntent pi = alarmIntent(c, a, true);
|
PendingIntent pi = alarmIntent(c, a, true);
|
||||||
|
|||||||
@ -0,0 +1,11 @@
|
|||||||
|
package com.philliphsu.clock2.editalarm;
|
||||||
|
|
||||||
|
import com.philliphsu.clock2.Alarm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Phillip Hsu on 6/3/2016.
|
||||||
|
*/
|
||||||
|
public interface AlarmUtilsHelper {
|
||||||
|
void scheduleAlarm(Alarm alarm);
|
||||||
|
void cancelAlarm(Alarm alarm);
|
||||||
|
}
|
||||||
@ -39,6 +39,7 @@ import static com.philliphsu.clock2.util.Preconditions.checkNotNull;
|
|||||||
|
|
||||||
public class EditAlarmActivity extends BaseActivity implements
|
public class EditAlarmActivity extends BaseActivity implements
|
||||||
EditAlarmContract.View,
|
EditAlarmContract.View,
|
||||||
|
AlarmUtilsHelper,
|
||||||
AlarmNumpad.KeyListener {
|
AlarmNumpad.KeyListener {
|
||||||
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";
|
||||||
@ -66,7 +67,7 @@ public class EditAlarmActivity extends BaseActivity implements
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setWeekDaysText();
|
setWeekDaysText();
|
||||||
mNumpad.setKeyListener(this);
|
mNumpad.setKeyListener(this);
|
||||||
mPresenter = new EditAlarmPresenter(this, AlarmsRepository.getInstance(this));
|
mPresenter = new EditAlarmPresenter(this, AlarmsRepository.getInstance(this), this);
|
||||||
mPresenter.loadAlarm(getIntent().getLongExtra(EXTRA_ALARM_ID, -1));
|
mPresenter.loadAlarm(getIntent().getLongExtra(EXTRA_ALARM_ID, -1));
|
||||||
mPresenter.setTimeTextHint();
|
mPresenter.setTimeTextHint();
|
||||||
}
|
}
|
||||||
@ -84,7 +85,7 @@ public class EditAlarmActivity extends BaseActivity implements
|
|||||||
mPresenter.dismissNow();
|
mPresenter.dismissNow();
|
||||||
return true;
|
return true;
|
||||||
case R.id.action_done_snoozing:
|
case R.id.action_done_snoozing:
|
||||||
mPresenter.endSnoozing();
|
mPresenter.stopSnoozing();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
@ -392,4 +393,9 @@ public class EditAlarmActivity extends BaseActivity implements
|
|||||||
public void scheduleAlarm(Alarm alarm) {
|
public void scheduleAlarm(Alarm alarm) {
|
||||||
AlarmUtils.scheduleAlarm(this, alarm);
|
AlarmUtils.scheduleAlarm(this, alarm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cancelAlarm(Alarm alarm) {
|
||||||
|
AlarmUtils.cancelAlarm(this, alarm);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
package com.philliphsu.clock2.editalarm;
|
package com.philliphsu.clock2.editalarm;
|
||||||
|
|
||||||
import com.philliphsu.clock2.Alarm;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Phillip Hsu on 6/2/2016.
|
* Created by Phillip Hsu on 6/2/2016.
|
||||||
*/
|
*/
|
||||||
@ -18,7 +16,6 @@ public interface EditAlarmContract {
|
|||||||
void setTimeTextHint();
|
void setTimeTextHint();
|
||||||
void showTimeText(String timeText);
|
void showTimeText(String timeText);
|
||||||
void showTimeTextPostBackspace(String newStr);
|
void showTimeTextPostBackspace(String newStr);
|
||||||
void scheduleAlarm(Alarm alarm);
|
|
||||||
int getHour();
|
int getHour();
|
||||||
int getMinutes();
|
int getMinutes();
|
||||||
boolean isEnabled();
|
boolean isEnabled();
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import java.util.Date;
|
|||||||
import static com.philliphsu.clock2.DaysOfWeek.NUM_DAYS;
|
import static com.philliphsu.clock2.DaysOfWeek.NUM_DAYS;
|
||||||
import static com.philliphsu.clock2.DaysOfWeek.SATURDAY;
|
import static com.philliphsu.clock2.DaysOfWeek.SATURDAY;
|
||||||
import static com.philliphsu.clock2.DaysOfWeek.SUNDAY;
|
import static com.philliphsu.clock2.DaysOfWeek.SUNDAY;
|
||||||
|
import static com.philliphsu.clock2.util.Preconditions.checkNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Phillip Hsu on 6/3/2016.
|
* Created by Phillip Hsu on 6/3/2016.
|
||||||
@ -21,12 +22,15 @@ public class EditAlarmPresenter implements EditAlarmContract.Presenter {
|
|||||||
|
|
||||||
@NonNull private final EditAlarmContract.View mView;
|
@NonNull private final EditAlarmContract.View mView;
|
||||||
@NonNull private final Repository<Alarm> mRepository;
|
@NonNull private final Repository<Alarm> mRepository;
|
||||||
|
@NonNull private final AlarmUtilsHelper mAlarmUtilsHelper;
|
||||||
@Nullable private Alarm mAlarm;
|
@Nullable private Alarm mAlarm;
|
||||||
|
|
||||||
public EditAlarmPresenter(@NonNull EditAlarmContract.View view,
|
public EditAlarmPresenter(@NonNull EditAlarmContract.View view,
|
||||||
@NonNull Repository<Alarm> repository) {
|
@NonNull Repository<Alarm> repository,
|
||||||
|
@NonNull AlarmUtilsHelper helper) {
|
||||||
mView = view;
|
mView = view;
|
||||||
mRepository = repository;
|
mRepository = repository;
|
||||||
|
mAlarmUtilsHelper = helper;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -66,19 +70,14 @@ public class EditAlarmPresenter implements EditAlarmContract.Presenter {
|
|||||||
.build();
|
.build();
|
||||||
a.setEnabled(mView.isEnabled());
|
a.setEnabled(mView.isEnabled());
|
||||||
if (mAlarm != null) {
|
if (mAlarm != null) {
|
||||||
// TODO: Cancel any alarm scheduled with the old alarm's ID
|
mAlarmUtilsHelper.cancelAlarm(mAlarm);
|
||||||
// TODO: Schedule the new alarm
|
|
||||||
mRepository.updateItem(mAlarm, a);
|
mRepository.updateItem(mAlarm, a);
|
||||||
} else {
|
} else {
|
||||||
// TODO: Schedule the new alarm
|
|
||||||
mRepository.addItem(a);
|
mRepository.addItem(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a.isEnabled()) {
|
if (a.isEnabled()) {
|
||||||
// TODO: Consider passing in some interface during construction that abstracts away the
|
mAlarmUtilsHelper.scheduleAlarm(a);
|
||||||
// Context required to call AlarmUtils.scheduleAlarm(), so we can call it here instead.
|
|
||||||
// It doesn't seem right that this task is delegated to the View.
|
|
||||||
mView.scheduleAlarm(a);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mView.showEditorClosed();
|
mView.showEditorClosed();
|
||||||
@ -87,20 +86,21 @@ public class EditAlarmPresenter implements EditAlarmContract.Presenter {
|
|||||||
@Override
|
@Override
|
||||||
public void delete() {
|
public void delete() {
|
||||||
if (mAlarm != null) {
|
if (mAlarm != null) {
|
||||||
mRepository.deleteItem(mAlarm);
|
mAlarmUtilsHelper.cancelAlarm(mAlarm); // (1)
|
||||||
|
mRepository.deleteItem(mAlarm); // TOneverDO: before (1)
|
||||||
}
|
}
|
||||||
mView.showEditorClosed();
|
mView.showEditorClosed();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dismissNow() {
|
public void dismissNow() {
|
||||||
// TODO: Cancel the alarm scheduled
|
mAlarmUtilsHelper.cancelAlarm(checkNotNull(mAlarm));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void endSnoozing() {
|
public void stopSnoozing() {
|
||||||
// TODO: Write method in ALarm class called endSnoozing()
|
dismissNow(); // MUST be first, see AlarmUtils.notifyUpcomingAlarmIntent()
|
||||||
// Cancel the alarm scheduled
|
mAlarm.stopSnoozing(); // TOneverDO: move this from last line
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -85,7 +85,6 @@ public class RingtoneActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void snooze() {
|
private void snooze() {
|
||||||
// Schedule another launch
|
|
||||||
AlarmUtils.scheduleAlarm(this, mAlarm);
|
AlarmUtils.scheduleAlarm(this, mAlarm);
|
||||||
/*
|
/*
|
||||||
Intent intent = new Intent(this, RingtoneActivity.class)
|
Intent intent = new Intent(this, RingtoneActivity.class)
|
||||||
|
|||||||
@ -131,6 +131,6 @@ public class RingtoneService extends Service { // TODO: abstract this, make subc
|
|||||||
/*int minutes = Integer.parseInt(pref.getString(
|
/*int minutes = Integer.parseInt(pref.getString(
|
||||||
getString(R.string.key_silence_after),
|
getString(R.string.key_silence_after),
|
||||||
"15"));*/
|
"15"));*/
|
||||||
mSilenceHandler.postDelayed(mSilenceRunnable, 10000);
|
mSilenceHandler.postDelayed(mSilenceRunnable, 20000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user