-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from kirasok/boot_receiver
feat(ui): re-schedule alarms on boot
- Loading branch information
Showing
7 changed files
with
52 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
app/src/main/java/io/github/kirasok/alarmix/domain/repository/AlarmRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
app/src/main/java/io/github/kirasok/alarmix/presentation/receiver/BootReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.github.kirasok.alarmix.presentation.receiver | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import io.github.kirasok.alarmix.domain.use_case.AlarmUseCases | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.runBlocking | ||
import java.time.ZonedDateTime | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class BootReceiver : BroadcastReceiver() { | ||
|
||
@Inject | ||
lateinit var alarmUseCases: AlarmUseCases | ||
override fun onReceive(context: Context?, intent: Intent?) { | ||
if (intent?.action != Intent.ACTION_BOOT_COMPLETED) return | ||
// Get alarms | ||
val alarms = runBlocking(Dispatchers.IO) { alarmUseCases.getAlarms() } | ||
|
||
// Enable enabled alarms which timestamp is after now | ||
alarms.forEach { | ||
if (it.enabled && it.timestamp.isAfter(ZonedDateTime.now())) { | ||
runBlocking(Dispatchers.IO) { alarmUseCases.insertAlarm(it) } // On insert, alarm will be validated, replaced in DB and scheduled | ||
} else if (it.enabled) { // Disable alarms which timestamp is in past | ||
runBlocking(Dispatchers.IO) { alarmUseCases.insertAlarm(it.copy(enabled = false)) } | ||
} | ||
} | ||
} | ||
} |