Fix #22
Refresh widgets in widget view model after widgets have been changed
This commit is contained in:
parent
fb407afe21
commit
e8a5c1f24b
@ -3,11 +3,12 @@ package de.mm20.launcher2.database
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.room.*
|
||||
import de.mm20.launcher2.database.entities.WidgetEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface WidgetDao {
|
||||
@Query("SELECT * FROM Widget ORDER BY position ASC")
|
||||
fun getWidgets(): List<WidgetEntity>
|
||||
fun getWidgets(): Flow<List<WidgetEntity>>
|
||||
|
||||
@Transaction
|
||||
fun updateWidgets(widgets: List<WidgetEntity>) {
|
||||
|
||||
@ -2,6 +2,7 @@ package de.mm20.launcher2.ui.launcher.widgets
|
||||
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.asLiveData
|
||||
import androidx.lifecycle.liveData
|
||||
import de.mm20.launcher2.widgets.Widget
|
||||
import de.mm20.launcher2.widgets.WidgetRepository
|
||||
@ -13,9 +14,7 @@ class WidgetsVM : ViewModel(), KoinComponent {
|
||||
|
||||
val isEditMode = MutableLiveData(false)
|
||||
|
||||
val widgets = liveData<List<Widget>?> {
|
||||
emit(widgetRepository.getWidgets())
|
||||
}
|
||||
val widgets = widgetRepository.getWidgets().asLiveData()
|
||||
|
||||
fun setEditMode(editMode: Boolean) {
|
||||
isEditMode.value = editMode
|
||||
|
||||
@ -4,6 +4,9 @@ import android.content.Context
|
||||
import de.mm20.launcher2.widgets.R
|
||||
import de.mm20.launcher2.database.AppDatabase
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
class WidgetRepository(
|
||||
@ -12,10 +15,10 @@ class WidgetRepository(
|
||||
|
||||
private val scope = CoroutineScope(Job() + Dispatchers.Default)
|
||||
|
||||
suspend fun getWidgets(): List<Widget> {
|
||||
return withContext(Dispatchers.IO) {
|
||||
AppDatabase.getInstance(context).widgetDao().getWidgets().map { Widget(it) }
|
||||
}
|
||||
fun getWidgets(): Flow<List<Widget>> {
|
||||
return AppDatabase.getInstance(context).widgetDao()
|
||||
.getWidgets()
|
||||
.map { it.map { Widget(it) } }
|
||||
}
|
||||
|
||||
fun getInternalWidgets(): List<Widget> {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user