Refactor HistoryLastReadPutResolver

This commit is contained in:
Jays2Kings 2021-04-19 13:02:05 -04:00
parent d0ceac73f5
commit 0492b2e1dd

View File

@ -1,7 +1,7 @@
package eu.kanade.tachiyomi.data.database.resolvers package eu.kanade.tachiyomi.data.database.resolvers
import android.content.ContentValues
import androidx.annotation.NonNull import androidx.annotation.NonNull
import androidx.core.content.contentValuesOf
import com.pushtorefresh.storio.sqlite.StorIOSQLite import com.pushtorefresh.storio.sqlite.StorIOSQLite
import com.pushtorefresh.storio.sqlite.operations.put.PutResult import com.pushtorefresh.storio.sqlite.operations.put.PutResult
import com.pushtorefresh.storio.sqlite.queries.Query import com.pushtorefresh.storio.sqlite.queries.Query
@ -27,19 +27,15 @@ class HistoryLastReadPutResolver : HistoryPutResolver() {
.build() .build()
) )
val putResult: PutResult val putResult = cursor.use { putCursor ->
if (putCursor.count == 0) {
try {
if (cursor.count == 0) {
val insertQuery = mapToInsertQuery(history) val insertQuery = mapToInsertQuery(history)
val insertedId = db.lowLevel().insert(insertQuery, mapToContentValues(history)) val insertedId = db.lowLevel().insert(insertQuery, mapToContentValues(history))
putResult = PutResult.newInsertResult(insertedId, insertQuery.table()) PutResult.newInsertResult(insertedId, insertQuery.table())
} else { } else {
val numberOfRowsUpdated = db.lowLevel().update(updateQuery, mapToUpdateContentValues(history)) val numberOfRowsUpdated = db.lowLevel().update(updateQuery, mapToUpdateContentValues(history))
putResult = PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.table()) PutResult.newUpdateResult(numberOfRowsUpdated, updateQuery.table())
} }
} finally {
cursor.close()
} }
putResult putResult
@ -59,7 +55,8 @@ class HistoryLastReadPutResolver : HistoryPutResolver() {
* Create content query * Create content query
* @param history object * @param history object
*/ */
fun mapToUpdateContentValues(history: History) = ContentValues(1).apply { private fun mapToUpdateContentValues(history: History) =
put(HistoryTable.COL_LAST_READ, history.last_read) contentValuesOf(
} HistoryTable.COL_LAST_READ to history.last_read
)
} }