refactor: store notification ids in an enum

This commit is contained in:
Bnyro 2024-01-22 14:12:00 +01:00
parent 1387eba344
commit 6616ad40bf
6 changed files with 24 additions and 21 deletions

View File

@ -0,0 +1,7 @@
package com.github.libretube.enums
enum class NotificationId(val id: Int) {
PLAYER_PLAYBACK(1),
DOWNLOAD_IN_PROGRESS(2),
ENQUEUE_PLAYLIST_DOWNLOAD(3)
}

View File

@ -25,6 +25,7 @@ import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.db.obj.Download import com.github.libretube.db.obj.Download
import com.github.libretube.db.obj.DownloadItem import com.github.libretube.db.obj.DownloadItem
import com.github.libretube.enums.FileType import com.github.libretube.enums.FileType
import com.github.libretube.enums.NotificationId
import com.github.libretube.extensions.formatAsFileSize import com.github.libretube.extensions.formatAsFileSize
import com.github.libretube.extensions.getContentLength import com.github.libretube.extensions.getContentLength
import com.github.libretube.extensions.parcelableExtra import com.github.libretube.extensions.parcelableExtra
@ -411,7 +412,7 @@ class DownloadService : LifecycleService() {
.setOnlyAlertOnce(true) .setOnlyAlertOnce(true)
.setGroupSummary(true) .setGroupSummary(true)
startForeground(DOWNLOAD_PROGRESS_NOTIFICATION_ID, summaryNotificationBuilder.build()) startForeground(NotificationId.DOWNLOAD_IN_PROGRESS.id, summaryNotificationBuilder.build())
} }
private fun getNotificationBuilder(item: DownloadItem): NotificationCompat.Builder { private fun getNotificationBuilder(item: DownloadItem): NotificationCompat.Builder {
@ -534,7 +535,6 @@ class DownloadService : LifecycleService() {
} }
companion object { companion object {
private const val DOWNLOAD_PROGRESS_NOTIFICATION_ID = 2
private const val DOWNLOAD_NOTIFICATION_GROUP = "download_notification_group" private const val DOWNLOAD_NOTIFICATION_GROUP = "download_notification_group"
const val ACTION_SERVICE_STARTED = const val ACTION_SERVICE_STARTED =
"com.github.libretube.services.DownloadService.ACTION_SERVICE_STARTED" "com.github.libretube.services.DownloadService.ACTION_SERVICE_STARTED"

View File

@ -19,12 +19,12 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.DownloadWithItems import com.github.libretube.db.obj.DownloadWithItems
import com.github.libretube.enums.FileType import com.github.libretube.enums.FileType
import com.github.libretube.enums.NotificationId
import com.github.libretube.extensions.toAndroidUri import com.github.libretube.extensions.toAndroidUri
import com.github.libretube.extensions.updateParameters import com.github.libretube.extensions.updateParameters
import com.github.libretube.helpers.PlayerHelper import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.obj.PlayerNotificationData import com.github.libretube.obj.PlayerNotificationData
import com.github.libretube.util.NowPlayingNotification import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.NowPlayingNotification.Companion.PLAYER_NOTIFICATION_ID
import kotlin.io.path.exists import kotlin.io.path.exists
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -63,7 +63,7 @@ class OfflinePlayerService : LifecycleService() {
.setSmallIcon(R.drawable.ic_launcher_lockscreen) .setSmallIcon(R.drawable.ic_launcher_lockscreen)
.build() .build()
startForeground(PLAYER_NOTIFICATION_ID, notification) startForeground(NotificationId.PLAYER_PLAYBACK.id, notification)
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

View File

@ -30,6 +30,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.db.DatabaseHelper import com.github.libretube.db.DatabaseHelper
import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.db.obj.WatchPosition import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.enums.NotificationId
import com.github.libretube.extensions.parcelableExtra import com.github.libretube.extensions.parcelableExtra
import com.github.libretube.extensions.setMetadata import com.github.libretube.extensions.setMetadata
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
@ -40,7 +41,6 @@ import com.github.libretube.helpers.ProxyHelper
import com.github.libretube.obj.PlayerNotificationData import com.github.libretube.obj.PlayerNotificationData
import com.github.libretube.parcelable.PlayerData import com.github.libretube.parcelable.PlayerData
import com.github.libretube.util.NowPlayingNotification import com.github.libretube.util.NowPlayingNotification
import com.github.libretube.util.NowPlayingNotification.Companion.PLAYER_NOTIFICATION_ID
import com.github.libretube.util.PlayingQueue import com.github.libretube.util.PlayingQueue
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -159,7 +159,7 @@ class OnlinePlayerService : LifecycleService() {
.setSmallIcon(R.drawable.ic_launcher_lockscreen) .setSmallIcon(R.drawable.ic_launcher_lockscreen)
.build() .build()
startForeground(PLAYER_NOTIFICATION_ID, notification) startForeground(NotificationId.PLAYER_PLAYBACK.id, notification)
} }
/** /**

View File

@ -15,6 +15,7 @@ import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.PipedStream import com.github.libretube.api.obj.PipedStream
import com.github.libretube.api.obj.StreamItem import com.github.libretube.api.obj.StreamItem
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.enums.NotificationId
import com.github.libretube.enums.PlaylistType import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.getWhileDigit import com.github.libretube.extensions.getWhileDigit
import com.github.libretube.extensions.serializableExtra import com.github.libretube.extensions.serializableExtra
@ -24,7 +25,6 @@ import com.github.libretube.helpers.DownloadHelper
import com.github.libretube.parcelable.DownloadData import com.github.libretube.parcelable.DownloadData
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class PlaylistDownloadEnqueueService : LifecycleService() { class PlaylistDownloadEnqueueService : LifecycleService() {
private lateinit var nManager: NotificationManager private lateinit var nManager: NotificationManager
@ -42,7 +42,7 @@ class PlaylistDownloadEnqueueService : LifecycleService() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
startForeground(ENQUEUE_PROGRESS_NOTIFICATION_ID, buildNotification()) startForeground(NotificationId.ENQUEUE_PLAYLIST_DOWNLOAD.id, buildNotification())
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@ -56,7 +56,7 @@ class PlaylistDownloadEnqueueService : LifecycleService() {
captionLanguage = intent.getStringExtra(IntentData.captionLanguage) captionLanguage = intent.getStringExtra(IntentData.captionLanguage)
audioLanguage = intent.getStringExtra(IntentData.audioLanguage) audioLanguage = intent.getStringExtra(IntentData.audioLanguage)
nManager.notify(ENQUEUE_PROGRESS_NOTIFICATION_ID, buildNotification()) nManager.notify(NotificationId.ENQUEUE_PLAYLIST_DOWNLOAD.id, buildNotification())
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
if (playlistType != PlaylistType.PUBLIC) { if (playlistType != PlaylistType.PUBLIC) {
@ -82,7 +82,7 @@ class PlaylistDownloadEnqueueService : LifecycleService() {
val playlist = try { val playlist = try {
PlaylistsHelper.getPlaylist(playlistId) PlaylistsHelper.getPlaylist(playlistId)
} catch (e: Exception) { } catch (e: Exception) {
toastFromMainDispatcher(e.localizedMessage) toastFromMainDispatcher(e.localizedMessage.orEmpty())
stopSelf() stopSelf()
return return
} }
@ -94,7 +94,7 @@ class PlaylistDownloadEnqueueService : LifecycleService() {
val playlist = try { val playlist = try {
RetrofitInstance.api.getPlaylist(playlistId) RetrofitInstance.api.getPlaylist(playlistId)
} catch (e: Exception) { } catch (e: Exception) {
toastFromMainDispatcher(e.localizedMessage) toastFromMainDispatcher(e.localizedMessage.orEmpty())
stopSelf() stopSelf()
return return
} }
@ -130,7 +130,7 @@ class PlaylistDownloadEnqueueService : LifecycleService() {
} }
private suspend fun enqueueStreams(streams: List<StreamItem>) { private suspend fun enqueueStreams(streams: List<StreamItem>) {
nManager.notify(ENQUEUE_PROGRESS_NOTIFICATION_ID, buildNotification()) nManager.notify(NotificationId.ENQUEUE_PLAYLIST_DOWNLOAD.id, buildNotification())
for (stream in streams) { for (stream in streams) {
val videoInfo = runCatching { val videoInfo = runCatching {
@ -157,7 +157,7 @@ class PlaylistDownloadEnqueueService : LifecycleService() {
DownloadHelper.startDownloadService(this, downloadData) DownloadHelper.startDownloadService(this, downloadData)
amountOfVideosDone++ amountOfVideosDone++
nManager.notify(ENQUEUE_PROGRESS_NOTIFICATION_ID, buildNotification()) nManager.notify(NotificationId.ENQUEUE_PLAYLIST_DOWNLOAD.id, buildNotification())
} }
if (amountOfVideos == amountOfVideosDone) stopSelf() if (amountOfVideos == amountOfVideosDone) stopSelf()
@ -183,8 +183,4 @@ class PlaylistDownloadEnqueueService : LifecycleService() {
super.onDestroy() super.onDestroy()
} }
companion object {
private const val ENQUEUE_PROGRESS_NOTIFICATION_ID = 3
}
} }

View File

@ -24,6 +24,7 @@ import coil.request.ImageRequest
import com.github.libretube.LibreTubeApp.Companion.PLAYER_CHANNEL_NAME import com.github.libretube.LibreTubeApp.Companion.PLAYER_CHANNEL_NAME
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.enums.NotificationId
import com.github.libretube.extensions.seekBy import com.github.libretube.extensions.seekBy
import com.github.libretube.extensions.toMediaMetadataCompat import com.github.libretube.extensions.toMediaMetadataCompat
import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.BackgroundHelper
@ -370,7 +371,7 @@ class NowPlayingNotification(
} }
.build() .build()
updateSessionMetadata() updateSessionMetadata()
nManager.notify(PLAYER_NOTIFICATION_ID, notification) nManager.notify(NotificationId.PLAYER_PLAYBACK.id, notification)
} }
private val notificationActionReceiver = object : BroadcastReceiver() { private val notificationActionReceiver = object : BroadcastReceiver() {
@ -398,11 +399,11 @@ class NowPlayingNotification(
context.unregisterReceiver(notificationActionReceiver) context.unregisterReceiver(notificationActionReceiver)
} }
nManager.cancel(PLAYER_NOTIFICATION_ID) nManager.cancel(NotificationId.PLAYER_PLAYBACK.id)
} }
fun cancelNotification() { fun cancelNotification() {
nManager.cancel(PLAYER_NOTIFICATION_ID) nManager.cancel(NotificationId.PLAYER_PLAYBACK.id)
} }
fun refreshNotification() { fun refreshNotification() {
@ -410,7 +411,6 @@ class NowPlayingNotification(
} }
companion object { companion object {
const val PLAYER_NOTIFICATION_ID = 1
private const val PREV = "prev" private const val PREV = "prev"
private const val NEXT = "next" private const val NEXT = "next"
private const val REWIND = "rewind" private const val REWIND = "rewind"