Created AlarmUtilsHelper interface passed into EditAlarmPresenter
This commit is contained in:
parent
5fb8448ce3
commit
a4b54bd935
@ -16,6 +16,6 @@ interface AlarmContract {
|
||||
|
||||
interface Presenter {
|
||||
void dismissNow();
|
||||
void endSnoozing();
|
||||
void stopSnoozing();
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,7 +45,7 @@ public final class AlarmUtils {
|
||||
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);
|
||||
|
||||
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
|
||||
EditAlarmContract.View,
|
||||
AlarmUtilsHelper,
|
||||
AlarmNumpad.KeyListener {
|
||||
private static final String TAG = "EditAlarmActivity";
|
||||
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);
|
||||
setWeekDaysText();
|
||||
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.setTimeTextHint();
|
||||
}
|
||||
@ -84,7 +85,7 @@ public class EditAlarmActivity extends BaseActivity implements
|
||||
mPresenter.dismissNow();
|
||||
return true;
|
||||
case R.id.action_done_snoozing:
|
||||
mPresenter.endSnoozing();
|
||||
mPresenter.stopSnoozing();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
@ -392,4 +393,9 @@ public class EditAlarmActivity extends BaseActivity implements
|
||||
public void scheduleAlarm(Alarm alarm) {
|
||||
AlarmUtils.scheduleAlarm(this, alarm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelAlarm(Alarm alarm) {
|
||||
AlarmUtils.cancelAlarm(this, alarm);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
package com.philliphsu.clock2.editalarm;
|
||||
|
||||
import com.philliphsu.clock2.Alarm;
|
||||
|
||||
/**
|
||||
* Created by Phillip Hsu on 6/2/2016.
|
||||
*/
|
||||
@ -18,7 +16,6 @@ public interface EditAlarmContract {
|
||||
void setTimeTextHint();
|
||||
void showTimeText(String timeText);
|
||||
void showTimeTextPostBackspace(String newStr);
|
||||
void scheduleAlarm(Alarm alarm);
|
||||
int getHour();
|
||||
int getMinutes();
|
||||
boolean isEnabled();
|
||||
|
||||
@ -12,6 +12,7 @@ import java.util.Date;
|
||||
import static com.philliphsu.clock2.DaysOfWeek.NUM_DAYS;
|
||||
import static com.philliphsu.clock2.DaysOfWeek.SATURDAY;
|
||||
import static com.philliphsu.clock2.DaysOfWeek.SUNDAY;
|
||||
import static com.philliphsu.clock2.util.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* 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 Repository<Alarm> mRepository;
|
||||
@NonNull private final AlarmUtilsHelper mAlarmUtilsHelper;
|
||||
@Nullable private Alarm mAlarm;
|
||||
|
||||
public EditAlarmPresenter(@NonNull EditAlarmContract.View view,
|
||||
@NonNull Repository<Alarm> repository) {
|
||||
@NonNull Repository<Alarm> repository,
|
||||
@NonNull AlarmUtilsHelper helper) {
|
||||
mView = view;
|
||||
mRepository = repository;
|
||||
mAlarmUtilsHelper = helper;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -66,19 +70,14 @@ public class EditAlarmPresenter implements EditAlarmContract.Presenter {
|
||||
.build();
|
||||
a.setEnabled(mView.isEnabled());
|
||||
if (mAlarm != null) {
|
||||
// TODO: Cancel any alarm scheduled with the old alarm's ID
|
||||
// TODO: Schedule the new alarm
|
||||
mAlarmUtilsHelper.cancelAlarm(mAlarm);
|
||||
mRepository.updateItem(mAlarm, a);
|
||||
} else {
|
||||
// TODO: Schedule the new alarm
|
||||
mRepository.addItem(a);
|
||||
}
|
||||
|
||||
if (a.isEnabled()) {
|
||||
// TODO: Consider passing in some interface during construction that abstracts away the
|
||||
// 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);
|
||||
mAlarmUtilsHelper.scheduleAlarm(a);
|
||||
}
|
||||
|
||||
mView.showEditorClosed();
|
||||
@ -87,20 +86,21 @@ public class EditAlarmPresenter implements EditAlarmContract.Presenter {
|
||||
@Override
|
||||
public void delete() {
|
||||
if (mAlarm != null) {
|
||||
mRepository.deleteItem(mAlarm);
|
||||
mAlarmUtilsHelper.cancelAlarm(mAlarm); // (1)
|
||||
mRepository.deleteItem(mAlarm); // TOneverDO: before (1)
|
||||
}
|
||||
mView.showEditorClosed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dismissNow() {
|
||||
// TODO: Cancel the alarm scheduled
|
||||
mAlarmUtilsHelper.cancelAlarm(checkNotNull(mAlarm));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endSnoozing() {
|
||||
// TODO: Write method in ALarm class called endSnoozing()
|
||||
// Cancel the alarm scheduled
|
||||
public void stopSnoozing() {
|
||||
dismissNow(); // MUST be first, see AlarmUtils.notifyUpcomingAlarmIntent()
|
||||
mAlarm.stopSnoozing(); // TOneverDO: move this from last line
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -85,7 +85,6 @@ public class RingtoneActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
private void snooze() {
|
||||
// Schedule another launch
|
||||
AlarmUtils.scheduleAlarm(this, mAlarm);
|
||||
/*
|
||||
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(
|
||||
getString(R.string.key_silence_after),
|
||||
"15"));*/
|
||||
mSilenceHandler.postDelayed(mSilenceRunnable, 10000);
|
||||
mSilenceHandler.postDelayed(mSilenceRunnable, 20000);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user