Skip to content

Commit

Permalink
fix between calculation bug when 1 is past
Browse files Browse the repository at this point in the history
  • Loading branch information
Théophile Delmas committed Feb 8, 2024
1 parent 8194ae5 commit 4e9b4e5
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import java.util.Date
import java.util.Locale

class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", null, 1) {
companion object {
Expand Down Expand Up @@ -173,7 +174,7 @@ class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", nu
calculatedTime = getRelativeTime(alarm.relative1)
return calculatedTime
}
} else if (alarm.mode == "Between") {
}else if (alarm.mode == "Between") {
var time1 = alarm.time1
var time2 = alarm.time2
if (alarm.relative1 != "Pick Time") {
Expand All @@ -183,31 +184,41 @@ class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", nu
time2 = getRelativeTime(alarm.relative2)
}

val calendarNow = Calendar.getInstance()
val calendar1 = Calendar.getInstance()
val calendar2 = Calendar.getInstance()
calendar1.timeInMillis = time1
calendar2.timeInMillis = time2

// Ensure Date Time is future
while (calendar1.timeInMillis <= System.currentTimeMillis()) {
calendar1.add(Calendar.DAY_OF_MONTH, 1)
}

while (calendar2.timeInMillis <= System.currentTimeMillis()) {
calendar2.add(Calendar.DAY_OF_MONTH, 1)
}
// Ensure times are for today, adjust if in the past
if (calendar1.before(calendarNow)) {
calendar1.set(Calendar.YEAR, calendarNow.get(Calendar.YEAR))
calendar1.set(Calendar.MONTH, calendarNow.get(Calendar.MONTH))
calendar1.set(Calendar.DAY_OF_MONTH, calendarNow.get(Calendar.DAY_OF_MONTH)) }
if (calendar2.before(calendarNow)) {
calendar2.set(Calendar.YEAR, calendarNow.get(Calendar.YEAR))
calendar2.set(Calendar.MONTH, calendarNow.get(Calendar.MONTH))
calendar2.set(Calendar.DAY_OF_MONTH, calendarNow.get(Calendar.DAY_OF_MONTH)) }

calculatedTime = (calendar1.timeInMillis + calendar2.timeInMillis) / 2
// Calculate the middle point based only on time
val midTime = (calendar1.timeInMillis + calendar2.timeInMillis) / 2
val calculatedCalendar = Calendar.getInstance()
calculatedCalendar.timeInMillis = midTime

// Print the full date with the time
val fullDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm")
var formattedDate = fullDateFormat.format(calendar1.time)

Log.d("TIME1", "@ $formattedDate")
formattedDate = fullDateFormat.format(calendar2.time)
Log.d("TIME2", "@ $formattedDate")
return calculatedTime
} else if (alarm.mode == "After") {
if (calculatedCalendar.timeInMillis <= System.currentTimeMillis()) {
calculatedCalendar.add(Calendar.DAY_OF_MONTH, 1)
}
// Print the hours and minutes
val timeFormat = SimpleDateFormat("HH:mm", Locale.getDefault())
Log.d("TIME1", "@ ${timeFormat.format(calendar1.time)}")
Log.d("TIME2", "@ ${timeFormat.format(calendar2.time)}")
Log.d("CALCULATED_TIME", "@ ${timeFormat.format(calculatedCalendar.time)}")

return calculatedCalendar.timeInMillis
}
else if (alarm.mode == "After") {
var time1 = getRelativeTime(alarm.relative2)
var time2 = alarm.time1
calculatedTime = (time1 + time2)
Expand Down

0 comments on commit 4e9b4e5

Please sign in to comment.