Add option to make upcoming lesson notification not persistent (#1537)

This commit is contained in:
Mateusz Idziejczak 2021-09-28 11:48:25 +02:00 committed by GitHub
parent e10e530dee
commit 5bc54c12f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 25 deletions

View File

@ -107,6 +107,14 @@ class PreferencesRepository @Inject constructor(
R.bool.pref_default_notification_upcoming_lessons_enable 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 = val isNotificationPiggybackEnabledKey =
context.getString(R.string.pref_key_notifications_piggyback) context.getString(R.string.pref_key_notifications_piggyback)
val isNotificationPiggybackEnabled: Boolean val isNotificationPiggybackEnabled: Boolean

View File

@ -11,6 +11,7 @@ import androidx.core.app.NotificationManagerCompat
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import io.github.wulkanowy.R import io.github.wulkanowy.R
import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.Status
import io.github.wulkanowy.data.repositories.PreferencesRepository
import io.github.wulkanowy.data.repositories.StudentRepository import io.github.wulkanowy.data.repositories.StudentRepository
import io.github.wulkanowy.services.HiltBroadcastReceiver import io.github.wulkanowy.services.HiltBroadcastReceiver
import io.github.wulkanowy.services.sync.channels.UpcomingLessonsChannel.Companion.CHANNEL_ID import io.github.wulkanowy.services.sync.channels.UpcomingLessonsChannel.Companion.CHANNEL_ID
@ -32,6 +33,9 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
@Inject @Inject
lateinit var studentRepository: StudentRepository lateinit var studentRepository: StudentRepository
@Inject
lateinit var preferencesRepository: PreferencesRepository
companion object { companion object {
const val NOTIFICATION_TYPE_CURRENT = 1 const val NOTIFICATION_TYPE_CURRENT = 1
const val NOTIFICATION_TYPE_UPCOMING = 2 const val NOTIFICATION_TYPE_UPCOMING = 2
@ -68,6 +72,7 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
private fun prepareNotification(context: Context, intent: Intent) { private fun prepareNotification(context: Context, intent: Intent) {
val type = intent.getIntExtra(LESSON_TYPE, 0) val type = intent.getIntExtra(LESSON_TYPE, 0)
val notificationId = intent.getIntExtra(NOTIFICATION_ID, MainView.Section.TIMETABLE.id) val notificationId = intent.getIntExtra(NOTIFICATION_ID, MainView.Section.TIMETABLE.id)
val isPersistent = preferencesRepository.isUpcomingLessonsNotificationsPersistent
if (type == NOTIFICATION_TYPE_LAST_LESSON_CANCELLATION) { if (type == NOTIFICATION_TYPE_LAST_LESSON_CANCELLATION) {
return NotificationManagerCompat.from(context).cancel(notificationId) 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") 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, 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("()")), context.getString(
nextSubject?.let { context.getString(R.string.timetable_later, "($nextRoom) $nextSubject".removePrefix("()")) } 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?) { private fun showNotification(
NotificationManagerCompat.from(context).notify(notificationId, NotificationCompat.Builder(context, CHANNEL_ID) context: Context,
.setContentTitle(title) notificationId: Int,
.setContentText(next) isPersistent: Boolean,
.setAutoCancel(false) studentName: String?,
.setOngoing(true) countDown: Long,
.setWhen(countDown) timeout: Long,
.apply { title: String,
if (Build.VERSION.SDK_INT >= N) setUsesChronometer(true) next: String?
} ) {
.setTimeoutAfter(timeout) NotificationManagerCompat.from(context)
.setSmallIcon(R.drawable.ic_stat_timetable) .notify(notificationId, NotificationCompat.Builder(context, CHANNEL_ID)
.setColor(context.getCompatColor(R.color.colorPrimary)) .setContentTitle(title)
.setStyle(NotificationCompat.InboxStyle().also { .setContentText(next)
it.setSummaryText(studentName) .setAutoCancel(false)
it.addLine(next) .setWhen(countDown)
}) .setOngoing(isPersistent)
.setContentIntent(PendingIntent.getActivity(context, MainView.Section.TIMETABLE.id, .apply {
MainActivity.getStartIntent(context, MainView.Section.TIMETABLE, true), FLAG_UPDATE_CURRENT)) if (Build.VERSION.SDK_INT >= N) setUsesChronometer(true)
.build() }
) .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()
)
} }
} }

View File

@ -42,7 +42,7 @@ class NotificationsPresenter @Inject constructor(
preferencesRepository.apply { preferencesRepository.apply {
when (key) { when (key) {
isUpcomingLessonsNotificationsEnableKey -> { isUpcomingLessonsNotificationsEnableKey, isUpcomingLessonsNotificationsPersistentKey -> {
if (!isUpcomingLessonsNotificationsEnable) { if (!isUpcomingLessonsNotificationsEnable) {
timetableNotificationHelper.cancelNotification() timetableNotificationHelper.cancelNotification()
} }

View File

@ -14,6 +14,7 @@
<bool name="pref_default_services_wifi_only">false</bool> <bool name="pref_default_services_wifi_only">false</bool>
<bool name="pref_default_notifications_enable">true</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_enable">false</bool>
<bool name="pref_default_notification_upcoming_lessons_persistent">true</bool>
<bool name="pref_default_notification_debug">false</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_plus">0.33</string>
<string name="pref_default_grade_modifier_minus">0.33</string> <string name="pref_default_grade_modifier_minus">0.33</string>

View File

@ -18,6 +18,7 @@
<string name="pref_key_notifications_system_settings">notifications_system_settings</string> <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_enable">notifications_enable</string>
<string name="pref_key_notifications_upcoming_lessons_enable">notifications_upcoming_lessons_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_notification_debug">notification_debug</string>
<string name="pref_key_grade_modifier_plus">grade_modifier_plus</string> <string name="pref_key_grade_modifier_plus">grade_modifier_plus</string>
<string name="pref_key_grade_modifier_minus">grade_modifier_minus</string> <string name="pref_key_grade_modifier_minus">grade_modifier_minus</string>

View File

@ -621,6 +621,8 @@
<string name="pref_notify_header">Notifications</string> <string name="pref_notify_header">Notifications</string>
<string name="pref_notify_switch">Show 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_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_open_system_settings">Open system notification settings</string>
<string name="pref_notify_fix_sync_issues">Fix synchronization &amp; notifications issues</string> <string name="pref_notify_fix_sync_issues">Fix synchronization &amp; 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> <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>

View File

@ -14,6 +14,14 @@
app:key="@string/pref_key_notifications_upcoming_lessons_enable" app:key="@string/pref_key_notifications_upcoming_lessons_enable"
app:singleLineTitle="false" app:singleLineTitle="false"
app:title="@string/pref_notify_upcoming_lessons_switch" /> 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 <SwitchPreferenceCompat
app:defaultValue="@bool/pref_default_notification_piggyback" app:defaultValue="@bool/pref_default_notification_piggyback"
app:iconSpaceReserved="false" app:iconSpaceReserved="false"