Tracker enhancements

This commit is contained in:
Koitharu
2020-04-02 20:23:20 +03:00
parent 3a79b4667b
commit 3f31bd5ad1
5 changed files with 31 additions and 14 deletions

View File

@@ -21,5 +21,6 @@ data class TrackEntity (
@ColumnInfo(name = "chapters_total") val totalChapters: Int, @ColumnInfo(name = "chapters_total") val totalChapters: Int,
@ColumnInfo(name = "last_chapter_id") val lastChapterId: Long, @ColumnInfo(name = "last_chapter_id") val lastChapterId: Long,
@ColumnInfo(name = "chapters_new") val newChapters: Int, @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
) )

View File

@@ -6,6 +6,6 @@ import androidx.sqlite.db.SupportSQLiteDatabase
object Migration3To4 : Migration(3, 4) { object Migration3To4 : Migration(3, 4) {
override fun migrate(database: SupportSQLiteDatabase) { 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 )")
} }
} }

View File

@@ -9,5 +9,6 @@ data class MangaTracking (
val manga: Manga, val manga: Manga,
val knownChaptersCount: Int, val knownChaptersCount: Int,
val lastChapterId: Long, val lastChapterId: Long,
val lastNotifiedChapterId: Long,
val lastCheck: Date? val lastCheck: Date?
): Parcelable ): Parcelable

View File

@@ -27,7 +27,8 @@ class TrackingRepository : KoinComponent {
MangaTracking( MangaTracking(
manga = m.toManga(), manga = m.toManga(),
knownChaptersCount = track?.totalChapters ?: -1, knownChaptersCount = track?.totalChapters ?: -1,
lastChapterId = track?.lastChapterId ?: 0, lastChapterId = track?.lastChapterId ?: 0L,
lastNotifiedChapterId = track?.lastNotifiedChapterId ?: 0L,
lastCheck = track?.lastCheck?.takeUnless { it == 0L }?.let(::Date) lastCheck = track?.lastCheck?.takeUnless { it == 0L }?.let(::Date)
) )
} }
@@ -37,14 +38,16 @@ class TrackingRepository : KoinComponent {
mangaId: Long, mangaId: Long,
knownChaptersCount: Int, knownChaptersCount: Int,
lastChapterId: Long, lastChapterId: Long,
newChapters: Int newChapters: Int,
lastNotifiedChapterId: Long
) { ) {
val entity = TrackEntity( val entity = TrackEntity(
mangaId = mangaId, mangaId = mangaId,
newChapters = newChapters, newChapters = newChapters,
lastCheck = System.currentTimeMillis(), lastCheck = System.currentTimeMillis(),
lastChapterId = lastChapterId, lastChapterId = lastChapterId,
totalChapters = knownChaptersCount totalChapters = knownChaptersCount,
lastNotifiedChapterId = lastNotifiedChapterId
) )
db.tracksDao.upsert(entity) db.tracksDao.upsert(entity)
} }
@@ -56,7 +59,8 @@ class TrackingRepository : KoinComponent {
totalChapters = chapters.size, totalChapters = chapters.size,
lastChapterId = chapters.lastOrNull()?.id ?: 0L, lastChapterId = chapters.lastOrNull()?.id ?: 0L,
newChapters = 0, newChapters = 0,
lastCheck = System.currentTimeMillis() lastCheck = System.currentTimeMillis(),
lastNotifiedChapterId = 0L
) )
db.tracksDao.insert(entity) db.tracksDao.insert(entity)
} }

View File

@@ -57,6 +57,7 @@ class TrackerJobService : BaseJobService() {
mangaId = track.manga.id, mangaId = track.manga.id,
knownChaptersCount = chapters.size, knownChaptersCount = chapters.size,
lastChapterId = chapters.lastOrNull()?.id ?: 0L, lastChapterId = chapters.lastOrNull()?.id ?: 0L,
lastNotifiedChapterId = 0L,
newChapters = 0 newChapters = 0
) )
} }
@@ -65,6 +66,7 @@ class TrackerJobService : BaseJobService() {
mangaId = track.manga.id, mangaId = track.manga.id,
knownChaptersCount = track.knownChaptersCount, knownChaptersCount = track.knownChaptersCount,
lastChapterId = 0L, lastChapterId = 0L,
lastNotifiedChapterId = chapters.lastOrNull()?.id ?: 0L,
newChapters = chapters.size newChapters = chapters.size
) )
showNotification(track.manga, chapters) showNotification(track.manga, chapters)
@@ -82,6 +84,7 @@ class TrackerJobService : BaseJobService() {
mangaId = track.manga.id, mangaId = track.manga.id,
knownChaptersCount = chapters.size, knownChaptersCount = chapters.size,
lastChapterId = chapters.lastOrNull()?.id ?: 0L, lastChapterId = chapters.lastOrNull()?.id ?: 0L,
lastNotifiedChapterId = chapters.lastOrNull()?.id ?: 0L,
newChapters = 0 newChapters = 0
) )
} else { } else {
@@ -90,9 +93,12 @@ class TrackerJobService : BaseJobService() {
mangaId = track.manga.id, mangaId = track.manga.id,
knownChaptersCount = knownChapter + 1, knownChaptersCount = knownChapter + 1,
lastChapterId = track.lastChapterId, lastChapterId = track.lastChapterId,
lastNotifiedChapterId = chapters.lastOrNull()?.id ?: 0L,
newChapters = newChapters 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, mangaId = track.manga.id,
knownChaptersCount = track.knownChaptersCount, knownChaptersCount = track.knownChaptersCount,
lastChapterId = track.lastChapterId, lastChapterId = track.lastChapterId,
lastNotifiedChapterId = chapters.lastOrNull()?.id ?: 0L,
newChapters = newChapters newChapters = newChapters
) )
showNotification(track.manga, chapters.takeLast(newChapters)) if (chapters.lastOrNull()?.id != track.lastNotifiedChapterId) {
showNotification(track.manga, chapters.takeLast(newChapters))
}
} }
} }
success++ success++
@@ -122,9 +131,10 @@ class TrackerJobService : BaseJobService() {
val id = manga.url.hashCode() val id = manga.url.hashCode()
val colorPrimary = ContextCompat.getColor(this@TrackerJobService, R.color.blue_primary) val colorPrimary = ContextCompat.getColor(this@TrackerJobService, R.color.blue_primary)
val builder = NotificationCompat.Builder(this, CHANNEL_ID) val builder = NotificationCompat.Builder(this, CHANNEL_ID)
val summary = resources.getQuantityString(R.plurals.new_chapters,
newChapters.size, newChapters.size)
with(builder) { with(builder) {
setContentText(resources.getQuantityString(R.plurals.new_chapters, setContentText(summary)
newChapters.size, newChapters.size))
setContentText(manga.title) setContentText(manga.title)
setNumber(newChapters.size) setNumber(newChapters.size)
setLargeIcon(safe { setLargeIcon(safe {
@@ -136,6 +146,7 @@ class TrackerJobService : BaseJobService() {
style.addLine(chapter.name) style.addLine(chapter.name)
} }
style.setSummaryText(manga.title) style.setSummaryText(manga.title)
style.setBigContentTitle(summary)
setStyle(style) setStyle(style)
val intent = MangaDetailsActivity.newIntent(this@TrackerJobService, manga) val intent = MangaDetailsActivity.newIntent(this@TrackerJobService, manga)
setContentIntent(PendingIntent.getActivity(this@TrackerJobService, id, setContentIntent(PendingIntent.getActivity(this@TrackerJobService, id,
@@ -176,9 +187,9 @@ class TrackerJobService : BaseJobService() {
createNotificationChannel(context) createNotificationChannel(context)
} }
val scheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler val scheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
// if (scheduler.allPendingJobs != null) { if (scheduler.allPendingJobs.any { it.id == JOB_ID }) {
// return return
// } }
val jobInfo = val jobInfo =
JobInfo.Builder(JOB_ID, ComponentName(context, TrackerJobService::class.java)) JobInfo.Builder(JOB_ID, ComponentName(context, TrackerJobService::class.java))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
@@ -191,7 +202,7 @@ class TrackerJobService : BaseJobService() {
} }
jobInfo.setRequiresDeviceIdle(true) jobInfo.setRequiresDeviceIdle(true)
jobInfo.setPersisted(true) jobInfo.setPersisted(true)
jobInfo.setPeriodic(TimeUnit.HOURS.toMillis(6)) jobInfo.setPeriodic(TimeUnit.HOURS.toMillis(4))
scheduler.schedule(jobInfo.build()) scheduler.schedule(jobInfo.build())
} }
} }