Save widgets in WidgetRepository coroutine scope
This commit is contained in:
@@ -3,15 +3,15 @@ package de.mm20.launcher2.widgets
|
||||
import android.content.Context
|
||||
import de.mm20.launcher2.widgets.R
|
||||
import de.mm20.launcher2.database.AppDatabase
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.*
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
class WidgetRepository(
|
||||
val context: Context
|
||||
) {
|
||||
|
||||
private val scope = CoroutineScope(Job() + Dispatchers.Default)
|
||||
|
||||
suspend fun getWidgets(): List<Widget> {
|
||||
return withContext(Dispatchers.IO) {
|
||||
AppDatabase.getInstance(context).widgetDao().getWidgets().map { Widget(it) }
|
||||
@@ -27,9 +27,11 @@ class WidgetRepository(
|
||||
}
|
||||
|
||||
|
||||
suspend fun saveWidgets(widgets: List<Widget>) {
|
||||
withContext(Dispatchers.IO) {
|
||||
AppDatabase.getInstance(context).widgetDao().updateWidgets(widgets.mapIndexed { i, widget -> widget.toDatabaseEntity(i) })
|
||||
fun saveWidgets(widgets: List<Widget>) {
|
||||
scope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
AppDatabase.getInstance(context).widgetDao().updateWidgets(widgets.mapIndexed { i, widget -> widget.toDatabaseEntity(i) })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user