فهرست منبع

Update history custom put resolver

len 8 سال پیش
والد
کامیت
8f83f497d5

+ 1 - 1
app/src/main/java/eu/kanade/tachiyomi/data/database/mappers/HistoryTypeMapping.kt

@@ -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)

+ 27 - 15
app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/HistoryLastReadPutResolver.kt

@@ -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)
     }