Skip to content

Commit

Permalink
Fix NextAlarm Calc
Browse files Browse the repository at this point in the history
  • Loading branch information
Théophile Delmas committed Feb 20, 2024
1 parent 9c2db50 commit 6d018ab
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
7 changes: 3 additions & 4 deletions app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,15 @@ class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", nu
alarmHandler.setNextAlarm(context)
}

private fun calculateTime(alarm: Alarm): Long {
internal fun calculateTime(alarm: Alarm): Long {
Log.d("Rooster", "---\nAlarm: ${alarm.label} - id: ${alarm.id}")
alarm.calculatedTime = calculateTimeInner(alarm)
alarm.calculatedTime = addDays(alarm, alarm.calculatedTime)
val calendar = Calendar.getInstance()
val fullDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm")
calendar.timeInMillis = alarm.calculatedTime
var formattedDate = fullDateFormat.format(calendar.time)
Log.d("Rooster", "Calculated time\n@ $formattedDate")
Log.d("Rooster", "Next Iteration: $formattedDate\n---")
return alarm.calculatedTime
}

Expand Down Expand Up @@ -251,9 +252,7 @@ class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", nu
alarm.saturday
)
// Start searching from the current day and go up to 7 days (a full week)
Log.e("Day Check", alarmDayOfWeek.toString())
for (i in 0 until 7) {
Log.e("Day Check", i.toString())
val dayToCheck = (alarmDayOfWeek + i) % 7 // Ensure it wraps around the days of the week
if (weekdays[dayToCheck]) {
// Calculate the difference in days between the current day and the day with a true value
Expand Down
58 changes: 28 additions & 30 deletions app/src/main/java/com/rooster/rooster/AlarmHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,46 +74,44 @@ class AlarmHandler {

var closestAlarm: Alarm? = null
var timeDifference: Long = Long.MAX_VALUE
val dayFormat = SimpleDateFormat("EEEE", Locale.getDefault())

Log.i(TAG, "Current Time: $currentTime")

for (alarm in alarms) {
if (!alarm.enabled) continue // Skip disabled alarms

// Update the calculatedTime for each alarm
alarmDbHelper.calculateTime(alarm) // Assuming this updates alarm.calculatedTime and logs the time

val alarmTime = Calendar.getInstance()
alarmTime.timeInMillis = alarm.calculatedTime
val alarmDay: String = dayFormat.format(alarmTime.time)
var alarmMillis = alarmTime.timeInMillis

// Check if alarm is enabled for today and in the future, or if it's in the past
if (alarm.enabled && (alarm.getDayEnabled(alarmDay) || alarmMillis < currentMillis)) {
var diff = alarmMillis - currentMillis

// If alarm is in the past, find next occurrence on enabled days
if (diff <= 0) {
val today = currentTime.get(Calendar.DAY_OF_WEEK)
var dayIncrement = 1
// Find next enabled day
while (!alarm.getDayEnabled(dayFormat.format(alarmTime.time))) {
alarmTime.add(Calendar.DAY_OF_WEEK, dayIncrement)
dayIncrement++
}
alarmMillis = alarmTime.timeInMillis
diff = alarmMillis - currentMillis
}

Log.i(TAG, "Alarm: ${alarm.label}, Day: $alarmDay, Difference: $diff")

// Update closestAlarm if the alarm time is closer than the previous closest alarm
if (diff > 0 && diff < timeDifference) {
closestAlarm = alarm
timeDifference = diff
}
val alarmMillis = alarm.calculatedTime

// Calculate the difference between current time and the alarm time
var diff = alarmMillis - currentMillis

if (diff < 0) {
// If the calculated time is in the past, calculate for the next occurrence
continue // Or adjust logic to calculate for the next valid day
}

// Update closestAlarm if this alarm is closer than the previously found closest alarm
if (diff < timeDifference) {
closestAlarm = alarm
timeDifference = diff
}
}

closestAlarm?.let {
Log.i(TAG, "Closest Alarm Set: $it")
setAlarm(context, it)
Log.i(TAG, "Closest Alarm Set: ${it.label}")
setAlarm(context, it) // Assuming setAlarm is a method to actually set the alarm
}
}


fun dayOfWeek(index: Int): String {
val days = arrayOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
return days[index % days.size] // Use modulo to safely wrap around the array
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class AlarmclockReceiver : BroadcastReceiver() {
val notificationBuilder = NotificationCompat.Builder(context, notificationChannel.id)
.setContentTitle("Rooster")
.setContentText("Click here to stop the alarm")
.setCategory(NotificationCompat.CATEGORY_STOPWATCH)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_launcher_foreground)
Expand Down

0 comments on commit 6d018ab

Please sign in to comment.