diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt index dc16f6cc..5b10e581 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/DbUtils.kt @@ -3,31 +3,34 @@ package com.pitchedapps.frost.utils import android.content.Context import com.raizlabs.android.dbflow.config.FlowManager import com.raizlabs.android.dbflow.kotlinextensions.processInTransactionAsync -import com.raizlabs.android.dbflow.structure.BaseModel +import com.raizlabs.android.dbflow.kotlinextensions.save import com.raizlabs.android.dbflow.structure.database.transaction.Transaction /** * Created by Allan Wang on 2017-05-30. */ + object DbUtils { fun db(name: String) = FlowManager.getDatabase(name) fun dbName(name: String) = "$name.db" + fun deleteDatabase(c: Context, name: String) = c.deleteDatabase(dbName(name)) - inline fun <reified T : BaseModel> replace( - context: Context, dbName: String, type: Class<T>, data: List<T>, - crossinline callback: ((successful: Boolean) -> Unit)) { - db(dbName).reset(context) - data.processInTransactionAsync({ - t, databaseWrapper -> - t.save(databaseWrapper) - }, - Transaction.Success { - callback.invoke(true) - }, - Transaction.Error { _, error -> - L.e(error.message ?: "DbReplace error") - callback.invoke(false) - }) - } } + +inline fun <reified T : Any> List<T>.replace(context: Context, dbName: String, + crossinline callback: ((successful: Boolean) -> Unit)) { + L.d("Replacing $dbName.db") + DbUtils.db(dbName).reset(context) + this.processInTransactionAsync({ + t, databaseWrapper -> + t.save(databaseWrapper) + }, + Transaction.Success { + callback.invoke(true) + }, + Transaction.Error { _, error -> + L.e(error.message ?: "DbReplace error") + callback.invoke(false) + }) +}
\ No newline at end of file |