mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-11-11 15:30:49 +01:00
Refactor notification builder extension
This commit is contained in:
parent
a5fadcda15
commit
8aa48effaa
6 changed files with 66 additions and 51 deletions
|
@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.data.notification.NotificationHandler
|
|||
import eu.kanade.tachiyomi.data.notification.NotificationReceiver
|
||||
import eu.kanade.tachiyomi.data.notification.Notifications
|
||||
import eu.kanade.tachiyomi.util.lang.chop
|
||||
import eu.kanade.tachiyomi.util.system.notificationBuilder
|
||||
import eu.kanade.tachiyomi.util.system.notificationManager
|
||||
import java.util.regex.Pattern
|
||||
|
||||
|
@ -19,12 +20,9 @@ import java.util.regex.Pattern
|
|||
* @param context context of application
|
||||
*/
|
||||
internal class DownloadNotifier(private val context: Context) {
|
||||
/**
|
||||
* Notification builder.
|
||||
*/
|
||||
private val notification by lazy {
|
||||
NotificationCompat.Builder(context, Notifications.CHANNEL_DOWNLOADER)
|
||||
.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher))
|
||||
|
||||
private val notificationBuilder = context.notificationBuilder(Notifications.CHANNEL_DOWNLOADER) {
|
||||
setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher))
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -70,9 +68,10 @@ internal class DownloadNotifier(private val context: Context) {
|
|||
/**
|
||||
* Clear old actions if they exist.
|
||||
*/
|
||||
private fun clearActions() = with(notification) {
|
||||
if (!mActions.isEmpty())
|
||||
private fun clearActions() = with(notificationBuilder) {
|
||||
if (mActions.isNotEmpty()) {
|
||||
mActions.clear()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,7 +89,7 @@ internal class DownloadNotifier(private val context: Context) {
|
|||
*/
|
||||
fun onProgressChange(download: Download) {
|
||||
// Create notification
|
||||
with(notification) {
|
||||
with(notificationBuilder) {
|
||||
// Check if first call.
|
||||
if (!isDownloading) {
|
||||
setSmallIcon(android.R.drawable.stat_sys_download)
|
||||
|
@ -114,14 +113,14 @@ internal class DownloadNotifier(private val context: Context) {
|
|||
setProgress(download.pages!!.size, download.downloadedImages, false)
|
||||
}
|
||||
// Displays the progress bar on notification
|
||||
notification.show()
|
||||
notificationBuilder.show()
|
||||
}
|
||||
|
||||
/**
|
||||
* Show notification when download is paused.
|
||||
*/
|
||||
fun onDownloadPaused() {
|
||||
with(notification) {
|
||||
with(notificationBuilder) {
|
||||
setContentTitle(context.getString(R.string.chapter_paused))
|
||||
setContentText(context.getString(R.string.download_notifier_download_paused))
|
||||
setSmallIcon(R.drawable.ic_pause_white_24dp)
|
||||
|
@ -141,7 +140,7 @@ internal class DownloadNotifier(private val context: Context) {
|
|||
}
|
||||
|
||||
// Show notification.
|
||||
notification.show()
|
||||
notificationBuilder.show()
|
||||
|
||||
// Reset initial values
|
||||
isDownloading = false
|
||||
|
@ -159,7 +158,7 @@ internal class DownloadNotifier(private val context: Context) {
|
|||
return
|
||||
}
|
||||
// Create notification.
|
||||
with(notification) {
|
||||
with(notificationBuilder) {
|
||||
val title = download.manga.title.chop(15)
|
||||
val quotedTitle = Pattern.quote(title)
|
||||
val chapter = download.chapter.name.replaceFirst("$quotedTitle[\\s]*[-]*[\\s]*".toRegex(RegexOption.IGNORE_CASE), "")
|
||||
|
@ -173,7 +172,7 @@ internal class DownloadNotifier(private val context: Context) {
|
|||
}
|
||||
|
||||
// Show notification.
|
||||
notification.show()
|
||||
notificationBuilder.show()
|
||||
|
||||
// Reset initial values
|
||||
isDownloading = false
|
||||
|
@ -186,7 +185,7 @@ internal class DownloadNotifier(private val context: Context) {
|
|||
* @param reason the text to show.
|
||||
*/
|
||||
fun onWarning(reason: String) {
|
||||
with(notification) {
|
||||
with(notificationBuilder) {
|
||||
setContentTitle(context.getString(R.string.download_notifier_downloader_title))
|
||||
setContentText(reason)
|
||||
setSmallIcon(android.R.drawable.stat_sys_warning)
|
||||
|
@ -195,7 +194,7 @@ internal class DownloadNotifier(private val context: Context) {
|
|||
setContentIntent(NotificationHandler.openDownloadManagerPendingActivity(context))
|
||||
setProgress(0, 0, false)
|
||||
}
|
||||
notification.show()
|
||||
notificationBuilder.show()
|
||||
|
||||
// Reset download information
|
||||
isDownloading = false
|
||||
|
@ -210,7 +209,7 @@ internal class DownloadNotifier(private val context: Context) {
|
|||
*/
|
||||
fun onError(error: String? = null, chapter: String? = null) {
|
||||
// Create notification
|
||||
with(notification) {
|
||||
with(notificationBuilder) {
|
||||
setContentTitle(chapter ?: context.getString(R.string.download_notifier_downloader_title))
|
||||
setContentText(error ?: context.getString(R.string.download_notifier_unkown_error))
|
||||
setSmallIcon(android.R.drawable.stat_sys_warning)
|
||||
|
@ -219,7 +218,7 @@ internal class DownloadNotifier(private val context: Context) {
|
|||
setContentIntent(NotificationHandler.openDownloadManagerPendingActivity(context))
|
||||
setProgress(0, 0, false)
|
||||
}
|
||||
notification.show(Notifications.ID_DOWNLOAD_CHAPTER_ERROR)
|
||||
notificationBuilder.show(Notifications.ID_DOWNLOAD_CHAPTER_ERROR)
|
||||
|
||||
// Reset download information
|
||||
errorThrown = true
|
||||
|
|
|
@ -9,7 +9,6 @@ import android.net.NetworkInfo.State.DISCONNECTED
|
|||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
import android.os.PowerManager
|
||||
import androidx.core.app.NotificationCompat
|
||||
import com.github.pwittchen.reactivenetwork.library.Connectivity
|
||||
import com.github.pwittchen.reactivenetwork.library.ReactiveNetwork
|
||||
import com.jakewharton.rxrelay.BehaviorRelay
|
||||
|
@ -18,6 +17,7 @@ import eu.kanade.tachiyomi.data.notification.Notifications
|
|||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||
import eu.kanade.tachiyomi.util.lang.plusAssign
|
||||
import eu.kanade.tachiyomi.util.system.connectivityManager
|
||||
import eu.kanade.tachiyomi.util.system.notification
|
||||
import eu.kanade.tachiyomi.util.system.powerManager
|
||||
import eu.kanade.tachiyomi.util.system.toast
|
||||
import rx.android.schedulers.AndroidSchedulers
|
||||
|
@ -187,9 +187,9 @@ class DownloadService : Service() {
|
|||
}
|
||||
|
||||
private fun getPlaceholderNotification(): Notification {
|
||||
return NotificationCompat.Builder(this, Notifications.CHANNEL_DOWNLOADER)
|
||||
.setContentTitle(getString(R.string.download_notifier_downloader_title))
|
||||
.build()
|
||||
return notification(Notifications.CHANNEL_DOWNLOADER) {
|
||||
setContentTitle(getString(R.string.download_notifier_downloader_title))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
|
|||
import eu.kanade.tachiyomi.util.lang.chop
|
||||
import eu.kanade.tachiyomi.util.system.isServiceRunning
|
||||
import eu.kanade.tachiyomi.util.system.notification
|
||||
import eu.kanade.tachiyomi.util.system.notificationBuilder
|
||||
import eu.kanade.tachiyomi.util.system.notificationManager
|
||||
import rx.Observable
|
||||
import rx.Subscription
|
||||
|
@ -86,13 +87,15 @@ class LibraryUpdateService(
|
|||
/**
|
||||
* Cached progress notification to avoid creating a lot.
|
||||
*/
|
||||
private val progressNotification by lazy { NotificationCompat.Builder(this, Notifications.CHANNEL_LIBRARY)
|
||||
.setContentTitle(getString(R.string.app_name))
|
||||
.setSmallIcon(R.drawable.ic_refresh_white_24dp)
|
||||
.setLargeIcon(notificationBitmap)
|
||||
.setOngoing(true)
|
||||
.setOnlyAlertOnce(true)
|
||||
.addAction(R.drawable.ic_close_white_24dp, getString(android.R.string.cancel), cancelIntent)
|
||||
private val progressNotificationBuilder by lazy {
|
||||
notificationBuilder(Notifications.CHANNEL_LIBRARY) {
|
||||
setContentTitle(getString(R.string.app_name))
|
||||
setSmallIcon(R.drawable.ic_refresh_white_24dp)
|
||||
setLargeIcon(notificationBitmap)
|
||||
setOngoing(true)
|
||||
setOnlyAlertOnce(true)
|
||||
addAction(R.drawable.ic_close_white_24dp, getString(android.R.string.cancel), cancelIntent)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -165,7 +168,7 @@ class LibraryUpdateService(
|
|||
*/
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
startForeground(Notifications.ID_LIBRARY_PROGRESS, progressNotification.build())
|
||||
startForeground(Notifications.ID_LIBRARY_PROGRESS, progressNotificationBuilder.build())
|
||||
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock(
|
||||
PowerManager.PARTIAL_WAKE_LOCK, "LibraryUpdateService:WakeLock")
|
||||
wakeLock.acquire()
|
||||
|
@ -430,7 +433,7 @@ class LibraryUpdateService(
|
|||
* @param total the total progress.
|
||||
*/
|
||||
private fun showProgressNotification(manga: Manga, current: Int, total: Int) {
|
||||
notificationManager.notify(Notifications.ID_LIBRARY_PROGRESS, progressNotification
|
||||
notificationManager.notify(Notifications.ID_LIBRARY_PROGRESS, progressNotificationBuilder
|
||||
.setContentTitle(manga.title)
|
||||
.setProgress(total, current, false)
|
||||
.build())
|
||||
|
|
|
@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.R
|
|||
import eu.kanade.tachiyomi.data.notification.NotificationHandler
|
||||
import eu.kanade.tachiyomi.data.notification.NotificationReceiver
|
||||
import eu.kanade.tachiyomi.data.notification.Notifications
|
||||
import eu.kanade.tachiyomi.util.system.notificationBuilder
|
||||
import eu.kanade.tachiyomi.util.system.notificationManager
|
||||
|
||||
/**
|
||||
|
@ -16,12 +17,7 @@ import eu.kanade.tachiyomi.util.system.notificationManager
|
|||
*/
|
||||
internal class UpdaterNotifier(private val context: Context) {
|
||||
|
||||
/**
|
||||
* Builder to manage notifications.
|
||||
*/
|
||||
private val notification by lazy {
|
||||
NotificationCompat.Builder(context, Notifications.CHANNEL_COMMON)
|
||||
}
|
||||
private val notificationBuilder = context.notificationBuilder(Notifications.CHANNEL_COMMON)
|
||||
|
||||
/**
|
||||
* Call to show notification.
|
||||
|
@ -38,13 +34,13 @@ internal class UpdaterNotifier(private val context: Context) {
|
|||
* @param title tile of notification.
|
||||
*/
|
||||
fun onDownloadStarted(title: String) {
|
||||
with(notification) {
|
||||
with(notificationBuilder) {
|
||||
setContentTitle(title)
|
||||
setContentText(context.getString(R.string.update_check_notification_download_in_progress))
|
||||
setSmallIcon(android.R.drawable.stat_sys_download)
|
||||
setOngoing(true)
|
||||
}
|
||||
notification.show()
|
||||
notificationBuilder.show()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -53,11 +49,11 @@ internal class UpdaterNotifier(private val context: Context) {
|
|||
* @param progress progress of download (xx%/100).
|
||||
*/
|
||||
fun onProgressChange(progress: Int) {
|
||||
with(notification) {
|
||||
with(notificationBuilder) {
|
||||
setProgress(100, progress, false)
|
||||
setOnlyAlertOnce(true)
|
||||
}
|
||||
notification.show()
|
||||
notificationBuilder.show()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,7 +62,7 @@ internal class UpdaterNotifier(private val context: Context) {
|
|||
* @param uri path location of apk.
|
||||
*/
|
||||
fun onDownloadFinished(uri: Uri) {
|
||||
with(notification) {
|
||||
with(notificationBuilder) {
|
||||
setContentText(context.getString(R.string.update_check_notification_download_complete))
|
||||
setSmallIcon(android.R.drawable.stat_sys_download_done)
|
||||
setOnlyAlertOnce(false)
|
||||
|
@ -81,7 +77,7 @@ internal class UpdaterNotifier(private val context: Context) {
|
|||
context.getString(R.string.action_cancel),
|
||||
NotificationReceiver.dismissNotificationPendingBroadcast(context, Notifications.ID_UPDATER))
|
||||
}
|
||||
notification.show()
|
||||
notificationBuilder.show()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,7 +86,7 @@ internal class UpdaterNotifier(private val context: Context) {
|
|||
* @param url web location of apk to download.
|
||||
*/
|
||||
fun onDownloadError(url: String) {
|
||||
with(notification) {
|
||||
with(notificationBuilder) {
|
||||
setContentText(context.getString(R.string.update_check_notification_download_error))
|
||||
setSmallIcon(android.R.drawable.stat_sys_warning)
|
||||
setOnlyAlertOnce(false)
|
||||
|
@ -104,6 +100,6 @@ internal class UpdaterNotifier(private val context: Context) {
|
|||
context.getString(R.string.action_cancel),
|
||||
NotificationReceiver.dismissNotificationPendingBroadcast(context, Notifications.ID_UPDATER))
|
||||
}
|
||||
notification.show(Notifications.ID_UPDATER)
|
||||
notificationBuilder.show(Notifications.ID_UPDATER)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import eu.kanade.tachiyomi.data.glide.GlideApp
|
|||
import eu.kanade.tachiyomi.data.notification.NotificationHandler
|
||||
import eu.kanade.tachiyomi.data.notification.NotificationReceiver
|
||||
import eu.kanade.tachiyomi.data.notification.Notifications
|
||||
import eu.kanade.tachiyomi.util.system.notificationBuilder
|
||||
import eu.kanade.tachiyomi.util.system.notificationManager
|
||||
import java.io.File
|
||||
|
||||
|
@ -20,7 +21,7 @@ class SaveImageNotifier(private val context: Context) {
|
|||
/**
|
||||
* Notification builder.
|
||||
*/
|
||||
private val notificationBuilder = NotificationCompat.Builder(context, Notifications.CHANNEL_COMMON)
|
||||
private val notificationBuilder = context.notificationBuilder(Notifications.CHANNEL_COMMON)
|
||||
|
||||
/**
|
||||
* Id of the notification.
|
||||
|
@ -59,8 +60,9 @@ class SaveImageNotifier(private val context: Context) {
|
|||
setAutoCancel(true)
|
||||
|
||||
// Clear old actions if they exist
|
||||
if (mActions.isNotEmpty())
|
||||
if (mActions.isNotEmpty()) {
|
||||
mActions.clear()
|
||||
}
|
||||
|
||||
setContentIntent(NotificationHandler.openImagePendingActivity(context, file))
|
||||
// Share action
|
||||
|
|
|
@ -43,16 +43,31 @@ fun Context.toast(text: String?, duration: Int = Toast.LENGTH_SHORT) {
|
|||
Toast.makeText(this, text.orEmpty(), duration).show()
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a notification builder.
|
||||
*
|
||||
* @param id the channel id.
|
||||
* @param block the function that will execute inside the builder.
|
||||
* @return a notification to be displayed or updated.
|
||||
*/
|
||||
fun Context.notificationBuilder(channelId: String, block: (NotificationCompat.Builder.() -> Unit)? = null): NotificationCompat.Builder {
|
||||
val builder = NotificationCompat.Builder(this, channelId)
|
||||
.setColor(ContextCompat.getColor(this, R.color.colorPrimary))
|
||||
if (block != null) {
|
||||
builder.block()
|
||||
}
|
||||
return builder
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a notification.
|
||||
*
|
||||
* @param id the channel id.
|
||||
* @param func the function that will execute inside the builder.
|
||||
* @param block the function that will execute inside the builder.
|
||||
* @return a notification to be displayed or updated.
|
||||
*/
|
||||
inline fun Context.notification(channelId: String, func: NotificationCompat.Builder.() -> Unit): Notification {
|
||||
val builder = NotificationCompat.Builder(this, channelId)
|
||||
builder.func()
|
||||
fun Context.notification(channelId: String, block: (NotificationCompat.Builder.() -> Unit)?): Notification {
|
||||
val builder = notificationBuilder(channelId, block)
|
||||
return builder.build()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue