From 497fe1e68a845da9021b89bfb71d1e00d19388b1 Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 17 Feb 2020 11:30:13 -0500 Subject: [PATCH] Minor cleanup --- .../data/download/DownloadNotifier.kt | 2 +- .../data/library/LibraryUpdateService.kt | 19 ++++---- .../data/notification/NotificationReceiver.kt | 44 +++++++++---------- .../kanade/tachiyomi/ui/main/MainActivity.kt | 7 +-- .../tachiyomi/ui/reader/ReaderActivity.kt | 17 ++++--- .../tachiyomi/ui/reader/ReaderPresenter.kt | 4 +- app/src/main/res/values/strings.xml | 4 +- 7 files changed, 50 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt index c0a632941..5791b26f4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt @@ -5,7 +5,6 @@ import android.graphics.BitmapFactory import androidx.core.app.NotificationCompat import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.download.model.Download -import eu.kanade.tachiyomi.data.download.model.DownloadQueue import eu.kanade.tachiyomi.data.notification.NotificationHandler import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.notification.Notifications @@ -96,6 +95,7 @@ internal class DownloadNotifier(private val context: Context) { .format(download.downloadedImages, download.pages!!.size)) setProgress(download.pages!!.size, download.downloadedImages, false) } + // Displays the progress bar on notification notificationBuilder.show() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index e3d9dfd73..07ee035b5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -12,7 +12,6 @@ import android.os.PowerManager import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat.GROUP_ALERT_SUMMARY import androidx.core.app.NotificationManagerCompat -import androidx.core.content.ContextCompat import com.bumptech.glide.Glide import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.DatabaseHelper @@ -281,7 +280,7 @@ class LibraryUpdateService( val count = AtomicInteger(0) // List containing new updates val newUpdates = ArrayList>>() - // list containing failed updates + // List containing failed updates val failedUpdates = ArrayList() // List containing categories that get included in downloads. val categoriesToDownload = preferences.downloadNewCategories().getOrDefault().map(String::toInt) @@ -313,8 +312,12 @@ class LibraryUpdateService( } } // Convert to the manga that contains new chapters. - .map { Pair(manga, (it.first.sortedByDescending { ch -> ch - .source_order }.toTypedArray())) } + .map { + Pair( + manga, + (it.first.sortedByDescending { ch -> ch.source_order }.toTypedArray()) + ) + } } // Add manga with new chapters to the list. .doOnNext { manga -> @@ -491,7 +494,6 @@ class LibraryUpdateService( } NotificationManagerCompat.from(this).apply { - notify(Notifications.ID_NEW_CHAPTERS, notification(Notifications.CHANNEL_NEW_CHAPTERS) { setSmallIcon(R.drawable.ic_tachi) setLargeIcon(notificationBitmap) @@ -532,9 +534,10 @@ class LibraryUpdateService( * Returns an intent to open the main activity. */ private fun getNotificationIntent(): PendingIntent { - val intent = Intent(this, MainActivity::class.java) - intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP - intent.action = MainActivity.SHORTCUT_RECENTLY_UPDATED + val intent = Intent(this, MainActivity::class.java).apply { + flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP + action = MainActivity.SHORTCUT_RECENTLY_UPDATED + } return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt index 825310b50..002540bc1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt @@ -34,9 +34,7 @@ import eu.kanade.tachiyomi.BuildConfig.APPLICATION_ID as ID * NOTE: Use local broadcasts if possible. */ class NotificationReceiver : BroadcastReceiver() { - /** - * Download manager. - */ + private val downloadManager: DownloadManager by injectLazy() override fun onReceive(context: Context, intent: Intent) { @@ -67,14 +65,17 @@ class NotificationReceiver : BroadcastReceiver() { openChapter(context, intent.getLongExtra(EXTRA_MANGA_ID, -1), intent.getLongExtra(EXTRA_CHAPTER_ID, -1)) } + // Mark updated manga chapters as read ACTION_MARK_AS_READ -> { val notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1) - if (notificationId > -1) dismissNotification( - context, notificationId, intent.getIntExtra(EXTRA_GROUP_ID, 0) - ) + if (notificationId > -1) { + dismissNotification(context, notificationId, intent.getIntExtra(EXTRA_GROUP_ID, 0)) + } val urls = intent.getStringArrayExtra(EXTRA_CHAPTER_URL) ?: return val mangaId = intent.getLongExtra(EXTRA_MANGA_ID, -1) - markAsRead(urls, mangaId) + if (mangaId > -1) { + markAsRead(urls, mangaId) + } } } } @@ -159,10 +160,10 @@ class NotificationReceiver : BroadcastReceiver() { } /** - * Method called when user wants to mark as read + * Method called when user wants to mark manga chapters as read * - * @param context context of application - * @param notificationId id of notification + * @param chapterUrls URLs of chapter to mark as read + * @param mangaId id of manga */ private fun markAsRead(chapterUrls: Array, mangaId: Long) { val db: DatabaseHelper = Injekt.get() @@ -192,10 +193,10 @@ class NotificationReceiver : BroadcastReceiver() { // Called to cancel library update. private const val ACTION_CANCEL_LIBRARY_UPDATE = "$ID.$NAME.CANCEL_LIBRARY_UPDATE" - // Called to mark as read + // Called to mark manga chapters as read. private const val ACTION_MARK_AS_READ = "$ID.$NAME.MARK_AS_READ" - // Called to open chapter + // Called to open chapter. private const val ACTION_OPEN_CHAPTER = "$ID.$NAME.ACTION_OPEN_CHAPTER" // Value containing file location. @@ -299,22 +300,24 @@ class NotificationReceiver : BroadcastReceiver() { * @param notificationId id of notification * @return [PendingIntent] */ - internal fun dismissNotification(context: Context, notificationId: Int, groupId: Int? = - null) { + internal fun dismissNotification(context: Context, notificationId: Int, groupId: Int? = null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { val groupKey = context.notificationManager.activeNotifications.find { it.id == notificationId }?.groupKey + if (groupId != null && groupId != 0 && groupKey != null && groupKey.isNotEmpty()) { val notifications = context.notificationManager.activeNotifications.filter { it.groupKey == groupKey } + if (notifications.size == 2) { context.notificationManager.cancel(groupId) return } } } + context.notificationManager.cancel(notificationId) } @@ -359,11 +362,9 @@ class NotificationReceiver : BroadcastReceiver() { * @param manga manga of chapter * @param chapter chapter that needs to be opened */ - internal fun openChapterPendingActivity(context: Context, manga: Manga, chapter: - Chapter): PendingIntent { + internal fun openChapterPendingActivity(context: Context, manga: Manga, chapter: Chapter): PendingIntent { val newIntent = ReaderActivity.newIntent(context, manga, chapter) - return PendingIntent.getActivity(context, manga.id.hashCode(), newIntent, PendingIntent - .FLAG_UPDATE_CURRENT) + return PendingIntent.getActivity(context, manga.id.hashCode(), newIntent, PendingIntent.FLAG_UPDATE_CURRENT) } /** @@ -372,17 +373,14 @@ class NotificationReceiver : BroadcastReceiver() { * @param context context of application * @param manga manga of chapter */ - internal fun openChapterPendingActivity(context: Context, manga: Manga, groupId: Int): - PendingIntent { + internal fun openChapterPendingActivity(context: Context, manga: Manga, groupId: Int): PendingIntent { val newIntent = Intent(context, MainActivity::class.java).setAction(MainActivity.SHORTCUT_MANGA) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) .putExtra(MangaController.MANGA_EXTRA, manga.id) .putExtra("notificationId", manga.id.hashCode()) .putExtra("groupId", groupId) - return PendingIntent.getActivity( - context, manga.id.hashCode(), newIntent, PendingIntent.FLAG_UPDATE_CURRENT - ) + return PendingIntent.getActivity(context, manga.id.hashCode(), newIntent, PendingIntent.FLAG_UPDATE_CURRENT) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index 60b30cc2c..bd90d0689 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -138,9 +138,10 @@ class MainActivity : BaseActivity() { private fun handleIntentAction(intent: Intent): Boolean { val notificationId = intent.getIntExtra("notificationId", -1) - if (notificationId > -1) NotificationReceiver.dismissNotification( - applicationContext, notificationId, intent.getIntExtra("groupId", 0) - ) + if (notificationId > -1) { + NotificationReceiver.dismissNotification(applicationContext, notificationId, intent.getIntExtra("groupId", 0)) + } + when (intent.action) { SHORTCUT_LIBRARY -> setSelectedDrawerItem(R.id.nav_drawer_library) SHORTCUT_RECENTLY_UPDATED -> setSelectedDrawerItem(R.id.nav_drawer_recent_updates) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index f83aff55a..caa413b68 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -106,15 +106,14 @@ class ReaderActivity : BaseRxActivity() { const val VERTICAL = 3 const val WEBTOON = 4 - fun newIntent(context: Context, manga: Manga, chapter: Chapter): - Intent { - val intent = Intent(context, ReaderActivity::class.java) - intent.putExtra("manga", manga.id) - intent.putExtra("chapter", chapter.id) - // chapters just added from library updates don't have an id yet - intent.putExtra("chapterUrl", chapter.url) - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - return intent + fun newIntent(context: Context, manga: Manga, chapter: Chapter): Intent { + return Intent(context, ReaderActivity::class.java).apply { + putExtra("manga", manga.id) + putExtra("chapter", chapter.id) + // chapters just added from library updates don't have an id yet + putExtra("chapterUrl", chapter.url) + addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt index f9298ab60..43f7b9a66 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt @@ -191,11 +191,13 @@ class ReaderPresenter( */ fun init(mangaId: Long, chapterUrl: String) { if (!needsInit()) return + val context = Injekt.get() val db = DatabaseHelper(context) val chapterId = db.getChapter(chapterUrl, mangaId).executeAsBlocking()?.id - if (chapterId != null) + if (chapterId != null) { init(mangaId, chapterId) + } } /** diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8c59a2244..4fdffe923 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -542,8 +542,8 @@ Common - Updating Library + Library Downloader - New Chapters + Chapter updates