Commit 5323c2ad authored by Robert Hitt's avatar Robert Hitt
Browse files

NotificationSettings are now saved to Realm

- Really not sure why I decided to save them to
SharedPreferences in the first place. Was pretty hacky
- Also changed some wording, and maybe some other stuff
parent a79578de
package srct.whatsopen.model;
public class NotificationSettings {
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
public class NotificationSettings extends RealmObject {
public NotificationSettings() {
}
public NotificationSettings(boolean opening, boolean closing, boolean interval_on, boolean interval_15, boolean interval_30, boolean interval_hour) {
public NotificationSettings(String name, boolean opening, boolean closing, boolean interval_on, boolean interval_15, boolean interval_30, boolean interval_hour) {
this.name = name;
this.opening = opening;
this.closing = closing;
this.interval_on = interval_on;
......@@ -15,6 +19,9 @@ public class NotificationSettings {
this.interval_hour = interval_hour;
}
@PrimaryKey
public String name;
public boolean opening;
public boolean closing;
public boolean interval_on;
......
......@@ -125,11 +125,13 @@ public class MainPresenter {
final String name = facility.getName();
realm.executeTransactionAsync((bgRealm) -> {
RealmResults<Facility> results = realm.where(Facility.class).equalTo("mName", name)
RealmResults<Facility> results = bgRealm.where(Facility.class).equalTo("mName", name)
.findAll();
results.deleteAllFromRealm();
});
realm.close();
}
// Sets the open status of each facility in the Realm instance
......
......@@ -19,6 +19,7 @@ import java.util.Set;
import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmResults;
import srct.whatsopen.model.Facility;
import srct.whatsopen.model.NotificationSettings;
import srct.whatsopen.model.OpenTimes;
......@@ -29,12 +30,9 @@ import srct.whatsopen.views.NotificationView;
public class NotificationPresenter {
private NotificationView mNotificationView;
private SharedPreferences pref;
public void attachView(NotificationView view) {
mNotificationView = view;
pref = PreferenceManager
.getDefaultSharedPreferences(mNotificationView.getContext());
}
public void detachView() {
......@@ -44,64 +42,35 @@ public class NotificationPresenter {
// Gets the notification settings from SharedPreferences, parses them to booleans,
// and passes them to the View
public void presentNotifications(String name) {
Set<String> notificationSettingsSet = pref.getStringSet(name+"NotificationSettings", null);
NotificationSettings n = getNotificationSettingsFromSet(notificationSettingsSet);
mNotificationView.setNotificationChecks(n);
}
Realm realm = Realm.getDefaultInstance();
NotificationSettings n = realm.where(NotificationSettings.class).equalTo("name", name)
.findFirst();
realm.close();
public NotificationSettings getNotificationSettingsFromSet(Set<String> set) {
NotificationSettings n = new NotificationSettings();
if(set != null) {
for (String s : set) {
switch (s) {
case "opening": n.opening = true; break;
case "closing": n.closing = true; break;
case "interval_on": n.interval_on = true; break;
case "interval_15": n.interval_15 = true; break;
case "interval_30": n.interval_30 = true; break;
case "interval_hour": n.interval_hour = true; break;
}
}
if(n != null) {
mNotificationView.setNotificationChecks(n);
}
return n;
}
// Returns a String set parsed from the given NotificationSettings
public Set<String> getSetFromNotificationSettings(NotificationSettings n) {
Set<String> set = new HashSet<>(6);
if(n.opening) set.add("opening");
if(n.closing) set.add("closing");
if(n.interval_on) set.add("interval_on");
if(n.interval_15) set.add("interval_15");
if(n.interval_30) set.add("interval_30");
if(n.interval_hour) set.add("interval_hour");
return set;
}
// Saves the notification settings to SharedPreferences and schedules the Notifications
public void saveNotifications(String name, boolean inEditMode, NotificationSettings n) {
public void saveNotifications(boolean inEditMode, NotificationSettings n) {
if(inEditMode) {
editNotifications(name, n);
editNotifications(n);
} else {
setNotifications("Notifications set", name, n);
setNotifications("Notifications set", n);
}
}
// If no checkboxes are set, removes Notifications. Else, sets new Notifications
private void editNotifications(String name, NotificationSettings n) {
private void editNotifications(NotificationSettings n) {
if(!n.opening && !n.closing && !n.interval_on && !n.interval_15 && !n.interval_30 &&
!n.interval_hour) {
removeNotifications(name, true);
removeNotifications(n.name, true);
} else {
removeNotifications(name, false);
setNotifications("Notifications edited", name, n);
removeNotifications(n.name, false);
setNotifications("Notifications edited", n);
}
}
......@@ -109,42 +78,49 @@ public class NotificationPresenter {
// Removes the Notification settings from SharedPreferences
public void removeNotifications(String name, boolean dismiss) {
// Remove the set Notifications
Set<String> notificationSettingsSet = pref.getStringSet(name+"NotificationSettings", null);
NotificationSettings n = getNotificationSettingsFromSet(notificationSettingsSet);
deleteNotificationsForFacility(name, n);
deleteNotificationsForFacility(name);
// Remove the NotificationSettings
SharedPreferences.Editor editor = pref.edit();
editor.putStringSet(name + "NotificationSettings", null);
editor.apply();
Realm realm = Realm.getDefaultInstance();
realm.executeTransactionAsync((bgRealm) -> {
NotificationSettings results = bgRealm.where(NotificationSettings.class)
.equalTo("name", name)
.findFirst();
if(dismiss) {
Toast.makeText(mNotificationView.getContext(), "Notifications removed",
Toast.LENGTH_SHORT).show();
if(results != null) {
results.deleteFromRealm();
}
}, () -> { // on success
if(dismiss) {
Toast.makeText(mNotificationView.getContext(), "Notifications removed",
Toast.LENGTH_SHORT).show();
mNotificationView.dismiss();
}
mNotificationView.dismiss();
}
});
realm.close();
}
// Saves the Notification settings to SharedPreferences
private void setNotifications(String message, String name, NotificationSettings n) {
private void setNotifications(String message, NotificationSettings n) {
// if the Notifications are valid (i.e. one of each category is checked)
if((n.opening || n.closing) &&
(n.interval_on || n.interval_15 || n.interval_30 || n.interval_hour)) {
Set<String> set = getSetFromNotificationSettings(n);
SharedPreferences.Editor editor = pref.edit();
editor.putStringSet(name + "NotificationSettings", set);
editor.apply();
Realm realm = Realm.getDefaultInstance();
realm.executeTransactionAsync(
bgRealm -> bgRealm.copyToRealmOrUpdate(n),
() -> {
// TODO: make async
createAlarmsForFacility(n);
// TODO: make async
createAlarmsForFacility(name, n);
Toast.makeText(mNotificationView.getContext(),
message, Toast.LENGTH_SHORT).show();
Toast.makeText(mNotificationView.getContext(), message, Toast.LENGTH_SHORT).show();
mNotificationView.dismiss();
mNotificationView.dismiss();
});
realm.close();
} else {
// Show error message
Toast.makeText(mNotificationView.getContext(),
......@@ -154,23 +130,25 @@ public class NotificationPresenter {
}
// Sets Alarms for the Facility with the given name
private void createAlarmsForFacility(String name, NotificationSettings notificationSettings) {
private void createAlarmsForFacility(NotificationSettings notificationSettings) {
Realm realm = Realm.getDefaultInstance();
Facility facility = realm.where(Facility.class).equalTo("mName", name).findFirst();
Facility facility = realm.where(Facility.class)
.equalTo("mName", notificationSettings.name).findFirst();
RealmList<OpenTimes> openTimesList = getActiveSchedule(facility, Calendar.getInstance());
if(openTimesList == null || openTimesList.size() == 0)
return;
for(OpenTimes o : openTimesList) {
setAlarmsFromOpenTimes(name, o, notificationSettings);
setAlarmsFromOpenTimes(o, notificationSettings);
}
realm.close();
}
private void setAlarmsFromOpenTimes(String name, OpenTimes openTimes,
private void setAlarmsFromOpenTimes(OpenTimes openTimes,
NotificationSettings n) {
String name = n.name;
for(int i = openTimes.getStartDay(); i <= openTimes.getEndDay(); i++) {
// Parse Python day of week int to Calendar day of week
......@@ -234,33 +212,41 @@ public class NotificationPresenter {
alarm.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 7*1440*60000, pendingIntent);
}
private void deleteNotificationsForFacility(String name, NotificationSettings n) {
private void deleteNotificationsForFacility(String name) {
Realm realm = Realm.getDefaultInstance();
NotificationSettings n = realm.where(NotificationSettings.class).equalTo("name", name)
.findFirst();
realm.close();
if(n == null)
return;
for(int i = 1; i <= 7; i++) {
if(n.opening) {
if(n.interval_on)
deleteNotification(name, i, "Op_on");
cancelNotificationAlarms(name, i, "Op_on");
if(n.interval_15)
deleteNotification(name, i, "Op_15");
cancelNotificationAlarms(name, i, "Op_15");
if(n.interval_30)
deleteNotification(name, i, "Op_30");
cancelNotificationAlarms(name, i, "Op_30");
if(n.interval_hour)
deleteNotification(name, i, "Op_hour");
cancelNotificationAlarms(name, i, "Op_hour");
}
if(n.closing) {
if(n.interval_on)
deleteNotification(name, i, "Cl_on");
cancelNotificationAlarms(name, i, "Cl_on");
if(n.interval_15)
deleteNotification(name, i, "Cl_15");
cancelNotificationAlarms(name, i, "Cl_15");
if(n.interval_30)
deleteNotification(name, i, "Cl_30");
cancelNotificationAlarms(name, i, "Cl_30");
if(n.interval_hour)
deleteNotification(name, i, "Cl_hour");
cancelNotificationAlarms(name, i, "Cl_hour");
}
}
}
private void deleteNotification(String name, int day, String type) {
private void cancelNotificationAlarms(String name, int day, String type) {
int id = (name+type+day).hashCode(); // unique id for editing the Notification later
Log.i("Delete hash for " + name + day + type, ""+id);
......@@ -276,7 +262,7 @@ public class NotificationPresenter {
// Returns the time in ms from epoch for the given time on the next Day
// of the week relative to the given Calendar
public Long parseTimeStringToMs(String timeString, int day, Calendar alarmCalendar) {
private Long parseTimeStringToMs(String timeString, int day, Calendar alarmCalendar) {
// Determine if the time is in the past
boolean hasPassed = timeHasPassed(timeString, day, alarmCalendar);
......
......@@ -25,6 +25,7 @@ import butterknife.OnClick;
import io.realm.Realm;
import srct.whatsopen.R;
import srct.whatsopen.model.Facility;
import srct.whatsopen.model.NotificationSettings;
import srct.whatsopen.views.FacilityView;
import srct.whatsopen.presenters.FacilityPresenter;
import srct.whatsopen.views.fragments.NotificationDialogFragment;
......@@ -157,9 +158,10 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
// Sets the notification button text to edit if a Notification exists
private void setNotificationStatus() {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
Set<String> notificationSettings = pref.getStringSet(mFacility.getName()
+ "NotificationSettings", null);
Realm realm = Realm.getDefaultInstance();
NotificationSettings notificationSettings = realm.where(NotificationSettings.class)
.equalTo("name", mFacility.getName()).findFirst();
realm.close();
if (notificationSettings != null) {
inEditMode = true;
......
......@@ -81,6 +81,11 @@ public class NotificationDialogFragment extends DialogFragment implements Notifi
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
// Changes the Notification button text in DetailActivity
NotificationDialogListener listener = (NotificationDialogListener) getActivity();
if(listener != null) {
listener.onSetNotification();
}
}
@Nullable
......@@ -107,15 +112,11 @@ public class NotificationDialogFragment extends DialogFragment implements Notifi
@OnClick(R.id.save_button)
public void onSave() {
mPresenter.saveNotifications(mName, inEditMode, new NotificationSettings(
mPresenter.saveNotifications(inEditMode, new NotificationSettings(mName,
typeOpeningCheckBox.isChecked(), typeClosingCheckBox.isChecked(),
intervalOnCheckBox.isChecked(), interval15CheckBox.isChecked(),
interval30CheckBox.isChecked(), intervalHourCheckBox.isChecked()
));
// Changes the Notification button text in DetailActivity
NotificationDialogListener listener = (NotificationDialogListener) getActivity();
listener.onSetNotification();
}
@OnClick(R.id.cancel_button)
......@@ -127,10 +128,6 @@ public class NotificationDialogFragment extends DialogFragment implements Notifi
@OnClick(R.id.remove_button)
public void onRemove() {
mPresenter.removeNotifications(mName, true);
// Changes the Notification button text in DetailActivity
NotificationDialogListener listener = (NotificationDialogListener) getActivity();
listener.onSetNotification();
}
@Override
......
......@@ -85,7 +85,7 @@
<Button
android:id="@+id/remove_button"
android:text="Remove"
android:text="Remove All"
android:textColor="@color/colorPrimary"
android:layout_gravity="center_horizontal"
android:visibility="gone"
......
......@@ -131,7 +131,7 @@
<Button
android:id="@+id/remove_button"
android:text="Remove"
android:text="Remove All"
android:textColor="@color/colorPrimary"
android:layout_toRightOf="@id/save_button"
android:layout_toEndOf="@id/save_button"
......
......@@ -40,14 +40,6 @@ public class NotificationPresenterUnitTest {
assertFalse(hasPassed);
}
@Test
public void testParseTimeStringToMs() {
now.setTimeZone(TimeZone.getTimeZone("EST"));
Long timeInMs = mPresenter.parseTimeStringToMs("14:00:00", 3, now);
assertEquals(1484074800000L, timeInMs.longValue());
}
@Test
public void testSetFromNotificationSettings() {
NotificationSettings n = new NotificationSettings(true, true, true, true, true, true);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment