Refactor search, favorites and calendar widget

This commit is contained in:
MM20
2021-12-31 20:16:08 +01:00
parent 0193d4e495
commit a2ccef64ce
81 changed files with 1335 additions and 1671 deletions
@@ -5,16 +5,34 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import de.mm20.launcher2.database.AppDatabase
import de.mm20.launcher2.search.data.Searchable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
/**
* A low level repository for hidden items. This can only be used to retrieve keys and to check
* whether an item is hidden. To retrieve actual Searchable objects, use FavoritesRepository.
*/
class HiddenItemsRepository(val context: Context) {
class HiddenItemsRepository(val context: Context, database: AppDatabase) {
val hiddenItemsKeys : LiveData<List<String>> = AppDatabase.getInstance(context).searchDao().getHiddenItemKeys()
val scope = CoroutineScope(Job() + Dispatchers.Main)
val hiddenItemsKeys = MutableStateFlow<List<String>>(emptyList())
fun isHidden(item: Searchable): LiveData<Boolean> {
init {
scope.launch {
AppDatabase.getInstance(context).searchDao().getHiddenItemKeys().collectLatest {
hiddenItemsKeys.value = it
}
}
}
fun isHidden(item: Searchable): Flow<Boolean> {
return AppDatabase.getInstance(context).searchDao().isHidden(item.key)
}
}
@@ -5,6 +5,6 @@ import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
val hiddenItemsModule = module {
single { HiddenItemsRepository(androidContext()) }
single { HiddenItemsRepository(androidContext(), get()) }
viewModel { HiddenItemsViewModel(get()) }
}