Skip to content

Commit

Permalink
Major bugfix: crash when adding repeat alarms (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
WrichikBasu authored Dec 20, 2020
1 parent 3f327fc commit b637481
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
13 changes: 8 additions & 5 deletions app/src/main/java/in/basulabs/shakealarmclock/AlarmData.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package in.basulabs.shakealarmclock;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.time.LocalDateTime;
import java.time.LocalTime;
Expand Down Expand Up @@ -39,8 +40,7 @@ public AlarmData(boolean isSwitchedOn, @NonNull LocalDateTime alarmDateTime, int
* @param soundVibrateSetting
* @param repeatDays
*/
public AlarmData(boolean isSwitchedOn, @NonNull LocalTime alarmTime, int soundVibrateSetting,
@NonNull ArrayList<Integer> repeatDays) {
public AlarmData(boolean isSwitchedOn, @NonNull LocalTime alarmTime, int soundVibrateSetting, @NonNull ArrayList<Integer> repeatDays) {
this.isSwitchedOn = isSwitchedOn;
this.alarmTime = alarmTime;
this.soundVibrateSetting = soundVibrateSetting;
Expand All @@ -57,11 +57,12 @@ public void setRepeatOn(boolean repeatOn) {
isRepeatOn = repeatOn;
}

@Nullable
public ArrayList<Integer> getRepeatDays() {
return repeatDays;
}

public void setRepeatDays(ArrayList<Integer> repeatDays) {
public void setRepeatDays(@Nullable ArrayList<Integer> repeatDays) {
this.repeatDays = repeatDays;
}

Expand All @@ -73,19 +74,21 @@ public void setSwitchedOn(boolean switchedOn) {
isSwitchedOn = switchedOn;
}

@Nullable
public LocalDateTime getAlarmDateTime() {
return alarmDateTime;
}

public void setAlarmDateTime(LocalDateTime alarmDateTime) {
public void setAlarmDateTime(@NonNull LocalDateTime alarmDateTime) {
this.alarmDateTime = alarmDateTime;
}

@NonNull
public LocalTime getAlarmTime() {
return alarmTime;
}

public void setAlarmTime(LocalTime alarmTime) {
public void setAlarmTime(@NonNull LocalTime alarmTime) {
this.alarmTime = alarmTime;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public LiveData<Integer> getLiveAlarmsCount() {

//--------------------------------------------------------------------------------------------------

public void incrementAlarmsCount() {
private void incrementAlarmsCount() {
if (alarmsCount == null || alarmsCount.getValue() == null) {
alarmsCount = new MutableLiveData<>();
alarmsCount.setValue(1);
Expand All @@ -39,7 +39,7 @@ public void incrementAlarmsCount() {

//--------------------------------------------------------------------------------------------------

public void decrementAlarmsCount() {
private void decrementAlarmsCount() {
if ((alarmsCount != null) && (alarmsCount.getValue() != null) && (alarmsCount.getValue() > 0)) {
alarmsCount.setValue(alarmsCount.getValue() - 1);
}
Expand Down Expand Up @@ -216,11 +216,8 @@ public int[] addAlarm(@NonNull AlarmDatabase alarmDatabase, @NonNull AlarmEntity
});
thread.start();

///////////////////////////////////////////////////////////////
// Now, change the data in alarmDataArrayList.
///////////////////////////////////////////////////////////////
LocalDateTime alarmDateTime = LocalDateTime.of(alarmEntity.alarmYear, alarmEntity.alarmMonth, alarmEntity.alarmDay, alarmEntity.alarmHour,
alarmEntity.alarmMinutes);
LocalDateTime alarmDateTime = LocalDateTime.of(alarmEntity.alarmYear, alarmEntity.alarmMonth,
alarmEntity.alarmDay, alarmEntity.alarmHour, alarmEntity.alarmMinutes);

int scrollToPosition = 0;

Expand All @@ -233,18 +230,20 @@ public int[] addAlarm(@NonNull AlarmDatabase alarmDatabase, @NonNull AlarmEntity

} else {

// Check if the array list already has an alarm with same time, and remove it:
int index = isAlarmInTheList(alarmEntity.alarmHour, alarmEntity.alarmMinutes);
if (index != - 1) {
alarmDataArrayList.getValue().remove(index);
}

// Insert the new alarm at the correct position:
for (int i = 0; i < Objects.requireNonNull(alarmDataArrayList.getValue()).size(); i++) {

if (alarmDataArrayList.getValue().get(i).getAlarmDateTime().toLocalTime().isBefore(alarmDateTime.toLocalTime())) {
if (alarmDataArrayList.getValue().get(i).getAlarmTime().isBefore(alarmDateTime.toLocalTime())) {

if ((i + 1) < alarmDataArrayList.getValue().size()) {

if (alarmDataArrayList.getValue().get(i + 1).getAlarmDateTime().toLocalTime().isAfter(alarmDateTime.toLocalTime())) {
if (alarmDataArrayList.getValue().get(i + 1).getAlarmTime().isAfter(alarmDateTime.toLocalTime())) {
alarmDataArrayList.getValue().add(i + 1, newAlarmData);
scrollToPosition = i + 1;
break;
Expand Down

0 comments on commit b637481

Please sign in to comment.