forked from github/wulkanowy-mirror
Add option to make upcoming lesson notification not persistent (#1537)
This commit is contained in:
parent
e10e530dee
commit
5bc54c12f1
@ -107,6 +107,14 @@ class PreferencesRepository @Inject constructor(
|
||||
R.bool.pref_default_notification_upcoming_lessons_enable
|
||||
)
|
||||
|
||||
val isUpcomingLessonsNotificationsPersistentKey =
|
||||
context.getString(R.string.pref_key_notifications_upcoming_lessons_persistent)
|
||||
val isUpcomingLessonsNotificationsPersistent: Boolean
|
||||
get() = getBoolean(
|
||||
isUpcomingLessonsNotificationsPersistentKey,
|
||||
R.bool.pref_default_notification_upcoming_lessons_persistent
|
||||
)
|
||||
|
||||
val isNotificationPiggybackEnabledKey =
|
||||
context.getString(R.string.pref_key_notifications_piggyback)
|
||||
val isNotificationPiggybackEnabled: Boolean
|
||||
|
@ -11,6 +11,7 @@ import androidx.core.app.NotificationManagerCompat
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import io.github.wulkanowy.R
|
||||
import io.github.wulkanowy.data.Status
|
||||
import io.github.wulkanowy.data.repositories.PreferencesRepository
|
||||
import io.github.wulkanowy.data.repositories.StudentRepository
|
||||
import io.github.wulkanowy.services.HiltBroadcastReceiver
|
||||
import io.github.wulkanowy.services.sync.channels.UpcomingLessonsChannel.Companion.CHANNEL_ID
|
||||
@ -32,6 +33,9 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
|
||||
@Inject
|
||||
lateinit var studentRepository: StudentRepository
|
||||
|
||||
@Inject
|
||||
lateinit var preferencesRepository: PreferencesRepository
|
||||
|
||||
companion object {
|
||||
const val NOTIFICATION_TYPE_CURRENT = 1
|
||||
const val NOTIFICATION_TYPE_UPCOMING = 2
|
||||
@ -68,6 +72,7 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
|
||||
private fun prepareNotification(context: Context, intent: Intent) {
|
||||
val type = intent.getIntExtra(LESSON_TYPE, 0)
|
||||
val notificationId = intent.getIntExtra(NOTIFICATION_ID, MainView.Section.TIMETABLE.id)
|
||||
val isPersistent = preferencesRepository.isUpcomingLessonsNotificationsPersistent
|
||||
|
||||
if (type == NOTIFICATION_TYPE_LAST_LESSON_CANCELLATION) {
|
||||
return NotificationManagerCompat.from(context).cancel(notificationId)
|
||||
@ -87,33 +92,57 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
|
||||
|
||||
Timber.d("TimetableNotification receive: type: $type, subject: $subject, start: ${start.toLocalDateTime()}, student: $studentId")
|
||||
|
||||
showNotification(context, notificationId, studentName,
|
||||
showNotification(context, notificationId, isPersistent, studentName,
|
||||
if (type == NOTIFICATION_TYPE_CURRENT) end else start, end - start,
|
||||
context.getString(if (type == NOTIFICATION_TYPE_CURRENT) R.string.timetable_now else R.string.timetable_next, "($room) $subject".removePrefix("()")),
|
||||
nextSubject?.let { context.getString(R.string.timetable_later, "($nextRoom) $nextSubject".removePrefix("()")) }
|
||||
context.getString(
|
||||
if (type == NOTIFICATION_TYPE_CURRENT) R.string.timetable_now else R.string.timetable_next,
|
||||
"($room) $subject".removePrefix("()")
|
||||
),
|
||||
nextSubject?.let {
|
||||
context.getString(
|
||||
R.string.timetable_later,
|
||||
"($nextRoom) $nextSubject".removePrefix("()")
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun showNotification(context: Context, notificationId: Int, studentName: String?, countDown: Long, timeout: Long, title: String, next: String?) {
|
||||
NotificationManagerCompat.from(context).notify(notificationId, NotificationCompat.Builder(context, CHANNEL_ID)
|
||||
.setContentTitle(title)
|
||||
.setContentText(next)
|
||||
.setAutoCancel(false)
|
||||
.setOngoing(true)
|
||||
.setWhen(countDown)
|
||||
.apply {
|
||||
if (Build.VERSION.SDK_INT >= N) setUsesChronometer(true)
|
||||
}
|
||||
.setTimeoutAfter(timeout)
|
||||
.setSmallIcon(R.drawable.ic_stat_timetable)
|
||||
.setColor(context.getCompatColor(R.color.colorPrimary))
|
||||
.setStyle(NotificationCompat.InboxStyle().also {
|
||||
it.setSummaryText(studentName)
|
||||
it.addLine(next)
|
||||
})
|
||||
.setContentIntent(PendingIntent.getActivity(context, MainView.Section.TIMETABLE.id,
|
||||
MainActivity.getStartIntent(context, MainView.Section.TIMETABLE, true), FLAG_UPDATE_CURRENT))
|
||||
.build()
|
||||
)
|
||||
private fun showNotification(
|
||||
context: Context,
|
||||
notificationId: Int,
|
||||
isPersistent: Boolean,
|
||||
studentName: String?,
|
||||
countDown: Long,
|
||||
timeout: Long,
|
||||
title: String,
|
||||
next: String?
|
||||
) {
|
||||
NotificationManagerCompat.from(context)
|
||||
.notify(notificationId, NotificationCompat.Builder(context, CHANNEL_ID)
|
||||
.setContentTitle(title)
|
||||
.setContentText(next)
|
||||
.setAutoCancel(false)
|
||||
.setWhen(countDown)
|
||||
.setOngoing(isPersistent)
|
||||
.apply {
|
||||
if (Build.VERSION.SDK_INT >= N) setUsesChronometer(true)
|
||||
}
|
||||
.setTimeoutAfter(timeout)
|
||||
.setSmallIcon(R.drawable.ic_stat_timetable)
|
||||
.setColor(context.getCompatColor(R.color.colorPrimary))
|
||||
.setStyle(NotificationCompat.InboxStyle().also {
|
||||
it.setSummaryText(studentName)
|
||||
it.addLine(next)
|
||||
})
|
||||
.setContentIntent(
|
||||
PendingIntent.getActivity(
|
||||
context,
|
||||
MainView.Section.TIMETABLE.id,
|
||||
MainActivity.getStartIntent(context, MainView.Section.TIMETABLE, true),
|
||||
FLAG_UPDATE_CURRENT
|
||||
)
|
||||
)
|
||||
.build()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ class NotificationsPresenter @Inject constructor(
|
||||
|
||||
preferencesRepository.apply {
|
||||
when (key) {
|
||||
isUpcomingLessonsNotificationsEnableKey -> {
|
||||
isUpcomingLessonsNotificationsEnableKey, isUpcomingLessonsNotificationsPersistentKey -> {
|
||||
if (!isUpcomingLessonsNotificationsEnable) {
|
||||
timetableNotificationHelper.cancelNotification()
|
||||
}
|
||||
|
@ -14,6 +14,7 @@
|
||||
<bool name="pref_default_services_wifi_only">false</bool>
|
||||
<bool name="pref_default_notifications_enable">true</bool>
|
||||
<bool name="pref_default_notification_upcoming_lessons_enable">false</bool>
|
||||
<bool name="pref_default_notification_upcoming_lessons_persistent">true</bool>
|
||||
<bool name="pref_default_notification_debug">false</bool>
|
||||
<string name="pref_default_grade_modifier_plus">0.33</string>
|
||||
<string name="pref_default_grade_modifier_minus">0.33</string>
|
||||
|
@ -18,6 +18,7 @@
|
||||
<string name="pref_key_notifications_system_settings">notifications_system_settings</string>
|
||||
<string name="pref_key_notifications_enable">notifications_enable</string>
|
||||
<string name="pref_key_notifications_upcoming_lessons_enable">notifications_upcoming_lessons_enable</string>
|
||||
<string name="pref_key_notifications_upcoming_lessons_persistent">notifications_upcoming_lessons_persistent</string>
|
||||
<string name="pref_key_notification_debug">notification_debug</string>
|
||||
<string name="pref_key_grade_modifier_plus">grade_modifier_plus</string>
|
||||
<string name="pref_key_grade_modifier_minus">grade_modifier_minus</string>
|
||||
|
@ -621,6 +621,8 @@
|
||||
<string name="pref_notify_header">Notifications</string>
|
||||
<string name="pref_notify_switch">Show notifications</string>
|
||||
<string name="pref_notify_upcoming_lessons_switch">Show upcoming lesson notifications</string>
|
||||
<string name="pref_notify_upcoming_lessons_persistent_switch">Make upcoming lesson notification persistent</string>
|
||||
<string name="pref_notify_upcoming_lessons_persistent_summary">Turn off when notification is not showing in your watch/band</string>
|
||||
<string name="pref_notify_open_system_settings">Open system notification settings</string>
|
||||
<string name="pref_notify_fix_sync_issues">Fix synchronization & notifications issues</string>
|
||||
<string name="pref_notify_fix_sync_issues_message">Your device may have data synchronization issues and with notifications.\n\nTo fix them, you need to add Wulkanowy to the autostart and turn off battery optimization/saving in the phone settings.</string>
|
||||
|
@ -14,6 +14,14 @@
|
||||
app:key="@string/pref_key_notifications_upcoming_lessons_enable"
|
||||
app:singleLineTitle="false"
|
||||
app:title="@string/pref_notify_upcoming_lessons_switch" />
|
||||
<SwitchPreferenceCompat
|
||||
app:defaultValue="@bool/pref_default_notification_upcoming_lessons_persistent"
|
||||
app:iconSpaceReserved="false"
|
||||
app:key="@string/pref_key_notifications_upcoming_lessons_persistent"
|
||||
app:dependency="@string/pref_key_notifications_upcoming_lessons_enable"
|
||||
app:singleLineTitle="false"
|
||||
app:summary="@string/pref_notify_upcoming_lessons_persistent_summary"
|
||||
app:title="@string/pref_notify_upcoming_lessons_persistent_switch" />
|
||||
<SwitchPreferenceCompat
|
||||
app:defaultValue="@bool/pref_default_notification_piggyback"
|
||||
app:iconSpaceReserved="false"
|
||||
|
Loading…
x
Reference in New Issue
Block a user