diff --git a/app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt b/app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt index 07f2674..1dacded 100644 --- a/app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt +++ b/app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt @@ -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 } @@ -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 diff --git a/app/src/main/java/com/rooster/rooster/AlarmHandler.kt b/app/src/main/java/com/rooster/rooster/AlarmHandler.kt index e9bd2e7..9b87283 100644 --- a/app/src/main/java/com/rooster/rooster/AlarmHandler.kt +++ b/app/src/main/java/com/rooster/rooster/AlarmHandler.kt @@ -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 + } + } \ No newline at end of file diff --git a/app/src/main/java/com/rooster/rooster/AlarmclockReceiver.kt b/app/src/main/java/com/rooster/rooster/AlarmclockReceiver.kt index 9ab2f25..bffb0f6 100644 --- a/app/src/main/java/com/rooster/rooster/AlarmclockReceiver.kt +++ b/app/src/main/java/com/rooster/rooster/AlarmclockReceiver.kt @@ -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)