Refactored removeTodayFromBitmask to not use bit operators

This commit is contained in:
Rawlin C 2023-07-05 10:16:46 +05:30
parent 9be4f122e8
commit 1e1666432f
2 changed files with 22 additions and 4 deletions

View File

@ -98,7 +98,7 @@ class AlarmsAdapter(
alarm_days.setTextColor(textColor)
alarm_label.text = alarm.label
alarm_label.setTextColor(textColor)
alarm_label.setTextColor(textColor) //test
alarm_label.beVisibleIf(alarm.label.isNotEmpty())
alarm_switch.isChecked = alarm.isEnabled

View File

@ -9,6 +9,7 @@ import com.simplemobiletools.clock.helpers.NOTIFICATION_ID
import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import java.util.Calendar
import kotlin.math.pow
class DismissAlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
@ -42,9 +43,26 @@ class DismissAlarmReceiver : BroadcastReceiver() {
private fun removeTodayFromBitmask(bitmask: Int): Int {
val calendar = Calendar.getInstance()
calendar.firstDayOfWeek = Calendar.MONDAY
val dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY
val dayOfWeek = (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7
// boolean array with each day of the week set as per the bitmask provided
val daysOfWeek = BooleanArray(7) { value ->
var bitValue = bitmask
for (i in 0 until value) {
bitValue /= 2
}
bitValue % 2 == 1
}
val todayBitmask = 1 shl dayOfWeek // This will left shift 0000001 by dayOfWeek places, creating a bitmask for today
return bitmask and todayBitmask.inv() // This will return a new bitmask without today included
daysOfWeek[dayOfWeek] = false // remove today
// Convert the boolean array back to an integer (bitmask)
var updatedBitmask = 0
for (i in 0..6) {
if (daysOfWeek[i]) {
updatedBitmask += 2.0.pow(i).toInt()
}
} // This will return a new bitmask without today included
return updatedBitmask
}
}