diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackEntity.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackEntity.kt index d9ee04df9..561ff2f46 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackEntity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/entity/TrackEntity.kt @@ -21,5 +21,6 @@ data class TrackEntity ( @ColumnInfo(name = "chapters_total") val totalChapters: Int, @ColumnInfo(name = "last_chapter_id") val lastChapterId: Long, @ColumnInfo(name = "chapters_new") val newChapters: Int, - @ColumnInfo(name = "last_check") val lastCheck: Long + @ColumnInfo(name = "last_check") val lastCheck: Long, + @ColumnInfo(name = "last_notified_id") val lastNotifiedChapterId: Long ) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/db/migrations/Migration3To4.kt b/app/src/main/java/org/koitharu/kotatsu/core/db/migrations/Migration3To4.kt index 3e8dc8702..786e27ade 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/db/migrations/Migration3To4.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/db/migrations/Migration3To4.kt @@ -6,6 +6,6 @@ import androidx.sqlite.db.SupportSQLiteDatabase object Migration3To4 : Migration(3, 4) { override fun migrate(database: SupportSQLiteDatabase) { - database.execSQL("CREATE TABLE IF NOT EXISTS tracks (manga_id INTEGER NOT NULL, chapters_total INTEGER NOT NULL, last_chapter_id INTEGER NOT NULL, chapters_new INTEGER NOT NULL, last_check INTEGER NOT NULL, PRIMARY KEY(manga_id), FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE )") + database.execSQL("CREATE TABLE IF NOT EXISTS tracks (manga_id INTEGER NOT NULL, chapters_total INTEGER NOT NULL, last_chapter_id INTEGER NOT NULL, chapters_new INTEGER NOT NULL, last_check INTEGER NOT NULL, last_notified_id INTEGER NOT NULL, PRIMARY KEY(manga_id), FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE )") } } \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/core/model/MangaTracking.kt b/app/src/main/java/org/koitharu/kotatsu/core/model/MangaTracking.kt index e9a0629c9..d5a1ccb39 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/model/MangaTracking.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/model/MangaTracking.kt @@ -9,5 +9,6 @@ data class MangaTracking ( val manga: Manga, val knownChaptersCount: Int, val lastChapterId: Long, + val lastNotifiedChapterId: Long, val lastCheck: Date? ): Parcelable \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/domain/tracking/TrackingRepository.kt b/app/src/main/java/org/koitharu/kotatsu/domain/tracking/TrackingRepository.kt index 95ca283dc..7893254ff 100644 --- a/app/src/main/java/org/koitharu/kotatsu/domain/tracking/TrackingRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/domain/tracking/TrackingRepository.kt @@ -27,7 +27,8 @@ class TrackingRepository : KoinComponent { MangaTracking( manga = m.toManga(), knownChaptersCount = track?.totalChapters ?: -1, - lastChapterId = track?.lastChapterId ?: 0, + lastChapterId = track?.lastChapterId ?: 0L, + lastNotifiedChapterId = track?.lastNotifiedChapterId ?: 0L, lastCheck = track?.lastCheck?.takeUnless { it == 0L }?.let(::Date) ) } @@ -37,14 +38,16 @@ class TrackingRepository : KoinComponent { mangaId: Long, knownChaptersCount: Int, lastChapterId: Long, - newChapters: Int + newChapters: Int, + lastNotifiedChapterId: Long ) { val entity = TrackEntity( mangaId = mangaId, newChapters = newChapters, lastCheck = System.currentTimeMillis(), lastChapterId = lastChapterId, - totalChapters = knownChaptersCount + totalChapters = knownChaptersCount, + lastNotifiedChapterId = lastNotifiedChapterId ) db.tracksDao.upsert(entity) } @@ -56,7 +59,8 @@ class TrackingRepository : KoinComponent { totalChapters = chapters.size, lastChapterId = chapters.lastOrNull()?.id ?: 0L, newChapters = 0, - lastCheck = System.currentTimeMillis() + lastCheck = System.currentTimeMillis(), + lastNotifiedChapterId = 0L ) db.tracksDao.insert(entity) } diff --git a/app/src/main/java/org/koitharu/kotatsu/ui/tracker/TrackerJobService.kt b/app/src/main/java/org/koitharu/kotatsu/ui/tracker/TrackerJobService.kt index 30acde27d..39501aa67 100644 --- a/app/src/main/java/org/koitharu/kotatsu/ui/tracker/TrackerJobService.kt +++ b/app/src/main/java/org/koitharu/kotatsu/ui/tracker/TrackerJobService.kt @@ -57,6 +57,7 @@ class TrackerJobService : BaseJobService() { mangaId = track.manga.id, knownChaptersCount = chapters.size, lastChapterId = chapters.lastOrNull()?.id ?: 0L, + lastNotifiedChapterId = 0L, newChapters = 0 ) } @@ -65,6 +66,7 @@ class TrackerJobService : BaseJobService() { mangaId = track.manga.id, knownChaptersCount = track.knownChaptersCount, lastChapterId = 0L, + lastNotifiedChapterId = chapters.lastOrNull()?.id ?: 0L, newChapters = chapters.size ) showNotification(track.manga, chapters) @@ -82,6 +84,7 @@ class TrackerJobService : BaseJobService() { mangaId = track.manga.id, knownChaptersCount = chapters.size, lastChapterId = chapters.lastOrNull()?.id ?: 0L, + lastNotifiedChapterId = chapters.lastOrNull()?.id ?: 0L, newChapters = 0 ) } else { @@ -90,9 +93,12 @@ class TrackerJobService : BaseJobService() { mangaId = track.manga.id, knownChaptersCount = knownChapter + 1, lastChapterId = track.lastChapterId, + lastNotifiedChapterId = chapters.lastOrNull()?.id ?: 0L, newChapters = newChapters ) - showNotification(track.manga, chapters.takeLast(newChapters)) + if (chapters.lastOrNull()?.id != track.lastNotifiedChapterId) { + showNotification(track.manga, chapters.takeLast(newChapters)) + } } } } @@ -102,9 +108,12 @@ class TrackerJobService : BaseJobService() { mangaId = track.manga.id, knownChaptersCount = track.knownChaptersCount, lastChapterId = track.lastChapterId, + lastNotifiedChapterId = chapters.lastOrNull()?.id ?: 0L, newChapters = newChapters ) - showNotification(track.manga, chapters.takeLast(newChapters)) + if (chapters.lastOrNull()?.id != track.lastNotifiedChapterId) { + showNotification(track.manga, chapters.takeLast(newChapters)) + } } } success++ @@ -122,9 +131,10 @@ class TrackerJobService : BaseJobService() { val id = manga.url.hashCode() val colorPrimary = ContextCompat.getColor(this@TrackerJobService, R.color.blue_primary) val builder = NotificationCompat.Builder(this, CHANNEL_ID) + val summary = resources.getQuantityString(R.plurals.new_chapters, + newChapters.size, newChapters.size) with(builder) { - setContentText(resources.getQuantityString(R.plurals.new_chapters, - newChapters.size, newChapters.size)) + setContentText(summary) setContentText(manga.title) setNumber(newChapters.size) setLargeIcon(safe { @@ -136,6 +146,7 @@ class TrackerJobService : BaseJobService() { style.addLine(chapter.name) } style.setSummaryText(manga.title) + style.setBigContentTitle(summary) setStyle(style) val intent = MangaDetailsActivity.newIntent(this@TrackerJobService, manga) setContentIntent(PendingIntent.getActivity(this@TrackerJobService, id, @@ -176,9 +187,9 @@ class TrackerJobService : BaseJobService() { createNotificationChannel(context) } val scheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler - // if (scheduler.allPendingJobs != null) { - // return - // } + if (scheduler.allPendingJobs.any { it.id == JOB_ID }) { + return + } val jobInfo = JobInfo.Builder(JOB_ID, ComponentName(context, TrackerJobService::class.java)) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { @@ -191,7 +202,7 @@ class TrackerJobService : BaseJobService() { } jobInfo.setRequiresDeviceIdle(true) jobInfo.setPersisted(true) - jobInfo.setPeriodic(TimeUnit.HOURS.toMillis(6)) + jobInfo.setPeriodic(TimeUnit.HOURS.toMillis(4)) scheduler.schedule(jobInfo.build()) } }