Log manga tracking

This commit is contained in:
Koitharu
2020-05-22 19:20:08 +03:00
parent 7cf57535ab
commit 140a0f4d66
7 changed files with 56 additions and 16 deletions

View File

@@ -19,8 +19,6 @@ android {
versionCode gitCommits
versionName '0.4'
buildConfigField 'String', 'GIT_BRANCH', "\"${gitBranch}\""
kapt {
arguments {
arg 'room.schemaLocation', "$projectDir/schemas".toString()
@@ -73,7 +71,7 @@ dependencies {
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha03'
implementation 'androidx.viewpager2:viewpager2:1.1.0-alpha01'
implementation 'androidx.preference:preference-ktx:1.1.1'
implementation 'androidx.work:work-runtime-ktx:2.4.0-beta1'
implementation 'androidx.work:work-runtime-ktx:2.4.0-beta01'
implementation 'com.google.android.material:material:1.2.0-alpha06'
implementation 'androidx.room:room-runtime:2.2.5'

View File

@@ -17,8 +17,8 @@ import androidx.room.PrimaryKey
)
data class TrackLogEntity(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id") val id: Long,
@ColumnInfo(name = "manga_id") val mangaId: Long,
@ColumnInfo(name = "id") val id: Long = 0L,
@ColumnInfo(name = "manga_id", index = true) val mangaId: Long,
@ColumnInfo(name = "chapters") val chapters: String,
@ColumnInfo(name = "created_at") val createdAt: Long = System.currentTimeMillis()
)

View File

@@ -3,6 +3,8 @@ package org.koitharu.kotatsu.core.db.entity
import androidx.room.Embedded
import androidx.room.Junction
import androidx.room.Relation
import org.koitharu.kotatsu.core.model.TrackingLogItem
import java.util.*
data class TrackLogWithManga(
@Embedded val trackLog: TrackLogEntity,
@@ -17,4 +19,12 @@ data class TrackLogWithManga(
associateBy = Junction(MangaTagsEntity::class)
)
val tags: List<TagEntity>
)
) {
fun toTrackingLogItem() = TrackingLogItem(
id = trackLog.id,
chapters = trackLog.chapters.split('\n'),
manga = manga.toManga(tags.map { x -> x.toMangaTag() }.toSet()),
createdAt = Date(trackLog.createdAt)
)
}

View File

@@ -7,5 +7,6 @@ object Migration5To6 : Migration(4, 5) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE IF NOT EXISTS track_logs (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, manga_id INTEGER NOT NULL, chapters TEXT NOT NULL, created_at INTEGER NOT NULL, FOREIGN KEY(manga_id) REFERENCES manga(manga_id) ON UPDATE NO ACTION ON DELETE CASCADE)")
database.execSQL("CREATE INDEX IF NOT EXISTS index_track_logs_manga_id ON track_logs (manga_id)")
}
}

View File

@@ -0,0 +1,13 @@
package org.koitharu.kotatsu.core.model
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
import java.util.*
@Parcelize
data class TrackingLogItem (
val id: Long,
val manga: Manga,
val chapters: List<String>,
val createdAt: Date
): Parcelable

View File

@@ -1,11 +1,15 @@
package org.koitharu.kotatsu.domain.tracking
import androidx.room.withTransaction
import org.koin.core.KoinComponent
import org.koin.core.inject
import org.koitharu.kotatsu.core.db.MangaDatabase
import org.koitharu.kotatsu.core.db.entity.TrackEntity
import org.koitharu.kotatsu.core.db.entity.TrackLogEntity
import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.core.model.MangaChapter
import org.koitharu.kotatsu.core.model.MangaTracking
import org.koitharu.kotatsu.core.model.TrackingLogItem
import java.util.*
class TrackingRepository : KoinComponent {
@@ -34,22 +38,36 @@ class TrackingRepository : KoinComponent {
}
}
suspend fun getTrackingLog(offset: Int, limit: Int): List<TrackingLogItem> {
return db.trackLogsDao.findAll(offset, limit).map { x ->
x.toTrackingLogItem()
}
}
suspend fun storeTrackResult(
mangaId: Long,
knownChaptersCount: Int,
lastChapterId: Long,
newChapters: Int,
newChapters: List<MangaChapter>,
lastNotifiedChapterId: Long
) {
val entity = TrackEntity(
mangaId = mangaId,
newChapters = newChapters,
newChapters = newChapters.size,
lastCheck = System.currentTimeMillis(),
lastChapterId = lastChapterId,
totalChapters = knownChaptersCount,
lastNotifiedChapterId = lastNotifiedChapterId
)
db.tracksDao.upsert(entity)
val logEntity = TrackLogEntity(
mangaId = mangaId,
chapters = newChapters.joinToString("\n") { x -> x.name },
createdAt = System.currentTimeMillis()
)
db.withTransaction {
db.tracksDao.upsert(entity)
db.trackLogsDao.insert(logEntity)
}
}
suspend fun upsert(manga: Manga) {

View File

@@ -56,7 +56,7 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
knownChaptersCount = chapters.size,
lastChapterId = chapters.lastOrNull()?.id ?: 0L,
lastNotifiedChapterId = 0L,
newChapters = 0
newChapters = emptyList()
)
}
track.knownChaptersCount == 0 && track.lastChapterId == 0L -> { //manga was empty on last check
@@ -65,7 +65,7 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
knownChaptersCount = track.knownChaptersCount,
lastChapterId = 0L,
lastNotifiedChapterId = chapters.lastOrNull()?.id ?: 0L,
newChapters = chapters.size
newChapters = chapters
)
showNotification(track.manga, chapters)
}
@@ -83,10 +83,10 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
knownChaptersCount = chapters.size,
lastChapterId = chapters.lastOrNull()?.id ?: 0L,
lastNotifiedChapterId = chapters.lastOrNull()?.id ?: 0L,
newChapters = 0
newChapters = emptyList()
)
} else {
val newChapters = chapters.size - knownChapter + 1
val newChapters = chapters.takeLast(chapters.size - knownChapter + 1)
repo.storeTrackResult(
mangaId = track.manga.id,
knownChaptersCount = knownChapter + 1,
@@ -95,13 +95,13 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
newChapters = newChapters
)
if (chapters.lastOrNull()?.id != track.lastNotifiedChapterId) {
showNotification(track.manga, chapters.takeLast(newChapters))
showNotification(track.manga, newChapters)
}
}
}
}
else -> {
val newChapters = chapters.size - track.knownChaptersCount
val newChapters = chapters.takeLast(chapters.size - track.knownChaptersCount)
repo.storeTrackResult(
mangaId = track.manga.id,
knownChaptersCount = track.knownChaptersCount,
@@ -110,7 +110,7 @@ class TrackWorker(context: Context, workerParams: WorkerParameters) :
newChapters = newChapters
)
if (chapters.lastOrNull()?.id != track.lastNotifiedChapterId) {
showNotification(track.manga, chapters.takeLast(newChapters))
showNotification(track.manga, newChapters)
}
}
}