mirror of
https://github.com/aniyomiorg/aniyomi.git
synced 2024-11-28 09:15:12 +03:00
Update history custom put resolver
This commit is contained in:
parent
6999fa858e
commit
8f83f497d5
2 changed files with 28 additions and 16 deletions
|
@ -22,7 +22,7 @@ class HistoryTypeMapping : SQLiteTypeMapping<History>(
|
|||
HistoryDeleteResolver()
|
||||
)
|
||||
|
||||
class HistoryPutResolver : DefaultPutResolver<History>() {
|
||||
open class HistoryPutResolver : DefaultPutResolver<History>() {
|
||||
|
||||
override fun mapToInsertQuery(obj: History) = InsertQuery.builder()
|
||||
.table(TABLE)
|
||||
|
|
|
@ -3,49 +3,61 @@ package eu.kanade.tachiyomi.data.database.resolvers
|
|||
import android.content.ContentValues
|
||||
import android.support.annotation.NonNull
|
||||
import com.pushtorefresh.storio.sqlite.StorIOSQLite
|
||||
import com.pushtorefresh.storio.sqlite.operations.put.PutResolver
|
||||
import com.pushtorefresh.storio.sqlite.operations.put.PutResult
|
||||
import com.pushtorefresh.storio.sqlite.queries.Query
|
||||
import com.pushtorefresh.storio.sqlite.queries.UpdateQuery
|
||||
import eu.kanade.tachiyomi.data.database.inTransactionReturn
|
||||
import eu.kanade.tachiyomi.data.database.mappers.HistoryPutResolver
|
||||
import eu.kanade.tachiyomi.data.database.models.History
|
||||
import eu.kanade.tachiyomi.data.database.tables.HistoryTable
|
||||
|
||||
class HistoryLastReadPutResolver : PutResolver<History>() {
|
||||
class HistoryLastReadPutResolver : HistoryPutResolver() {
|
||||
|
||||
/**
|
||||
* Updates last_read time of chapter
|
||||
*/
|
||||
override fun performPut(@NonNull db: StorIOSQLite, @NonNull history: History): PutResult = db.inTransactionReturn {
|
||||
// Create put query
|
||||
val updateQuery = mapToUpdateQuery(history)
|
||||
val contentValues = mapToContentValues(history)
|
||||
|
||||
// Execute query
|
||||
val numberOfRowsUpdated = db.lowLevel().update(updateQuery, contentValues)
|
||||
val cursor = db.lowLevel().query(Query.builder()
|
||||
.table(updateQuery.table())
|
||||
.where(updateQuery.where())
|
||||
.whereArgs(updateQuery.whereArgs())
|
||||
.build())
|
||||
|
||||
// If chapter not found in history insert into database
|
||||
if (numberOfRowsUpdated == 0) {
|
||||
db.put().`object`(history).prepare().executeAsBlocking()
|
||||
val putResult: PutResult
|
||||
|
||||
try {
|
||||
if (cursor.count == 0) {
|
||||
val insertQuery = mapToInsertQuery(history)
|
||||
val insertedId = db.lowLevel().insert(insertQuery, mapToContentValues(history))
|
||||
putResult = PutResult.newInsertResult(insertedId, insertQuery.table())
|
||||
} else {
|
||||
val numberOfRowsUpdated = db.lowLevel().update(updateQuery, mapToUpdateContentValues(history))
|
||||
putResult = PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.table())
|
||||
}
|
||||
} finally {
|
||||
cursor.close()
|
||||
}
|
||||
// Update result
|
||||
PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.table())
|
||||
|
||||
putResult
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates update query
|
||||
* @param history object
|
||||
* @param obj history object
|
||||
*/
|
||||
fun mapToUpdateQuery(history: History) = UpdateQuery.builder()
|
||||
override fun mapToUpdateQuery(obj: History) = UpdateQuery.builder()
|
||||
.table(HistoryTable.TABLE)
|
||||
.where("${HistoryTable.COL_CHAPTER_ID} = ?")
|
||||
.whereArgs(history.chapter_id)
|
||||
.whereArgs(obj.chapter_id)
|
||||
.build()
|
||||
|
||||
/**
|
||||
* Create content query
|
||||
* @param history object
|
||||
*/
|
||||
fun mapToContentValues(history: History) = ContentValues(1).apply {
|
||||
fun mapToUpdateContentValues(history: History) = ContentValues(1).apply {
|
||||
put(HistoryTable.COL_LAST_READ, history.last_read)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue