forked from EventFahrplan/EventFahrplan
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fosdem-2024
- Loading branch information
Showing
15 changed files
with
545 additions
and
102 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
69 changes: 69 additions & 0 deletions
69
app/src/main/java/nerd/tuxmobil/fahrplan/congress/alarms/SessionAlarmViewModelDelegate.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,69 @@ | ||
package nerd.tuxmobil.fahrplan.congress.alarms | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.channels.SendChannel | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import kotlinx.coroutines.launch | ||
import nerd.tuxmobil.fahrplan.congress.models.Session | ||
import nerd.tuxmobil.fahrplan.congress.notifications.NotificationHelper | ||
|
||
/** | ||
* Wraps all concerns related to adding and deleting sessions alarms | ||
* and the related permission checks. This handling is centralized here | ||
* to avoid code duplication in the individual view models. | ||
*/ | ||
internal class SessionAlarmViewModelDelegate( | ||
private val viewModelScope: CoroutineScope, | ||
private val notificationHelper: NotificationHelper, | ||
private val alarmServices: AlarmServices, | ||
private val runsAtLeastOnAndroidTiramisu: Boolean, | ||
) { | ||
|
||
private val mutableShowAlarmTimePicker = Channel<Unit>() | ||
val showAlarmTimePicker = mutableShowAlarmTimePicker | ||
.receiveAsFlow() | ||
|
||
private val mutableRequestPostNotificationsPermission = Channel<Unit>() | ||
val requestPostNotificationsPermission = mutableRequestPostNotificationsPermission | ||
.receiveAsFlow() | ||
|
||
private val mutableNotificationsDisabled = Channel<Unit>() | ||
val notificationsDisabled = mutableNotificationsDisabled | ||
.receiveAsFlow() | ||
|
||
private val mutableRequestScheduleExactAlarmsPermission = Channel<Unit>() | ||
val requestScheduleExactAlarmsPermission = mutableRequestScheduleExactAlarmsPermission | ||
.receiveAsFlow() | ||
|
||
fun canAddAlarms() = alarmServices.canScheduleExactAlarms | ||
|
||
fun addAlarmWithChecks() { | ||
if (notificationHelper.notificationsEnabled) { | ||
if (alarmServices.canScheduleExactAlarms) { | ||
mutableShowAlarmTimePicker.sendOneTimeEvent(Unit) | ||
} else { | ||
mutableRequestScheduleExactAlarmsPermission.sendOneTimeEvent(Unit) | ||
} | ||
} else { | ||
// Check runtime version here because requesting the POST_NOTIFICATION permission | ||
// before Android 13 (Tiramisu) has no effect nor error message. | ||
when (runsAtLeastOnAndroidTiramisu) { | ||
true -> mutableRequestPostNotificationsPermission.sendOneTimeEvent(Unit) | ||
false -> mutableNotificationsDisabled.sendOneTimeEvent(Unit) | ||
} | ||
} | ||
} | ||
|
||
fun addAlarm(session: Session, alarmTimesIndex: Int) = | ||
alarmServices.addSessionAlarm(session, alarmTimesIndex) | ||
|
||
fun deleteAlarm(session: Session) = alarmServices.deleteSessionAlarm(session) | ||
|
||
private fun <E> SendChannel<E>.sendOneTimeEvent(event: E) { | ||
viewModelScope.launch { | ||
send(event) | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.