Show pinned calendar events in favorites if calendar widget is disabled
This commit is contained in:
parent
164219fe93
commit
a86d453f4e
@ -18,8 +18,8 @@ interface SearchDao {
|
|||||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
fun insertSkipExisting(items: FavoritesItemEntity)
|
fun insertSkipExisting(items: FavoritesItemEntity)
|
||||||
|
|
||||||
@Query("SELECT * FROM Searchable WHERE pinned > 0 AND NOT `key` LIKE 'calendar://%' ORDER BY pinned DESC, launchCount DESC")
|
@Query("SELECT * FROM Searchable WHERE pinned > 0 AND (NOT :excludeCalendarEvents OR NOT `key` LIKE 'calendar://%') ORDER BY pinned DESC, launchCount DESC")
|
||||||
fun getFavorites(): Flow<List<FavoritesItemEntity>>
|
fun getFavorites(excludeCalendarEvents: Boolean = false): Flow<List<FavoritesItemEntity>>
|
||||||
|
|
||||||
@Query("SELECT * FROM Searchable WHERE pinned > 0 AND `key` LIKE 'calendar://%' ORDER BY pinned DESC, launchCount DESC")
|
@Query("SELECT * FROM Searchable WHERE pinned > 0 AND `key` LIKE 'calendar://%' ORDER BY pinned DESC, launchCount DESC")
|
||||||
fun getPinnedCalendarEvents(): Flow<List<FavoritesItemEntity>>
|
fun getPinnedCalendarEvents(): Flow<List<FavoritesItemEntity>>
|
||||||
|
|||||||
@ -33,4 +33,7 @@ interface WidgetDao {
|
|||||||
|
|
||||||
@Query("UPDATE Widget SET height = :newHeight WHERE data = :data AND type = :type")
|
@Query("UPDATE Widget SET height = :newHeight WHERE data = :data AND type = :type")
|
||||||
fun updateHeight(type: String, data: String, newHeight: Int)
|
fun updateHeight(type: String, data: String, newHeight: Int)
|
||||||
|
|
||||||
|
@Query("SELECT EXISTS(SELECT 1 FROM Widget WHERE type = :type AND data = :data)")
|
||||||
|
fun exists(type: String, data: String) : Flow<Boolean>
|
||||||
}
|
}
|
||||||
@ -9,15 +9,13 @@ import de.mm20.launcher2.search.SearchableDeserializer
|
|||||||
import de.mm20.launcher2.search.data.CalendarEvent
|
import de.mm20.launcher2.search.data.CalendarEvent
|
||||||
import de.mm20.launcher2.search.data.Searchable
|
import de.mm20.launcher2.search.data.Searchable
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.channels.awaitClose
|
|
||||||
import kotlinx.coroutines.channels.trySendBlocking
|
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
import org.koin.core.component.KoinComponent
|
import org.koin.core.component.KoinComponent
|
||||||
import org.koin.core.component.get
|
import org.koin.core.component.get
|
||||||
import org.koin.core.parameter.parametersOf
|
import org.koin.core.parameter.parametersOf
|
||||||
|
|
||||||
interface FavoritesRepository {
|
interface FavoritesRepository {
|
||||||
fun getFavorites(): Flow<List<Searchable>>
|
fun getFavorites(excludeCalendarEvents: Boolean = false): Flow<List<Searchable>>
|
||||||
fun getPinnedCalendarEvents(): Flow<List<Searchable>>
|
fun getPinnedCalendarEvents(): Flow<List<Searchable>>
|
||||||
fun isPinned(searchable: Searchable): Flow<Boolean>
|
fun isPinned(searchable: Searchable): Flow<Boolean>
|
||||||
fun pinItem(searchable: Searchable)
|
fun pinItem(searchable: Searchable)
|
||||||
@ -39,21 +37,15 @@ internal class FavoritesRepositoryImpl(
|
|||||||
|
|
||||||
private val scope = CoroutineScope(Job() + Dispatchers.Default)
|
private val scope = CoroutineScope(Job() + Dispatchers.Default)
|
||||||
|
|
||||||
override fun getFavorites(): Flow<List<Searchable>> = channelFlow {
|
override fun getFavorites(excludeCalendarEvents: Boolean): Flow<List<Searchable>> =
|
||||||
|
channelFlow {
|
||||||
|
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
|
|
||||||
val gridColumns = dataStore.data.map { it.grid.columnCount }.distinctUntilChanged()
|
val gridColumns = dataStore.data.map { it.grid.columnCount }.distinctUntilChanged()
|
||||||
val enableFavorites = dataStore.data.map { it.favorites.enabled}.distinctUntilChanged()
|
val dao = database.searchDao()
|
||||||
val dao = database.searchDao()
|
|
||||||
|
|
||||||
enableFavorites.collectLatest {
|
val pinnedFavorites = dao.getFavorites(excludeCalendarEvents).map {
|
||||||
if (!it) {
|
|
||||||
send(emptyList())
|
|
||||||
return@collectLatest
|
|
||||||
}
|
|
||||||
|
|
||||||
val pinnedFavorites = dao.getFavorites().map {
|
|
||||||
it.mapNotNull {
|
it.mapNotNull {
|
||||||
val item = fromDatabaseEntity(it).searchable
|
val item = fromDatabaseEntity(it).searchable
|
||||||
if (item == null) {
|
if (item == null) {
|
||||||
@ -79,7 +71,6 @@ internal class FavoritesRepositoryImpl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
override fun getPinnedCalendarEvents(): Flow<List<CalendarEvent>> {
|
override fun getPinnedCalendarEvents(): Flow<List<CalendarEvent>> {
|
||||||
return database.searchDao().getPinnedCalendarEvents().map {
|
return database.searchDao().getPinnedCalendarEvents().map {
|
||||||
|
|||||||
@ -19,6 +19,7 @@ import de.mm20.launcher2.search.WebsearchRepository
|
|||||||
import de.mm20.launcher2.search.data.*
|
import de.mm20.launcher2.search.data.*
|
||||||
import de.mm20.launcher2.unitconverter.UnitConverterRepository
|
import de.mm20.launcher2.unitconverter.UnitConverterRepository
|
||||||
import de.mm20.launcher2.websites.WebsiteRepository
|
import de.mm20.launcher2.websites.WebsiteRepository
|
||||||
|
import de.mm20.launcher2.widgets.WidgetRepository
|
||||||
import de.mm20.launcher2.wikipedia.WikipediaRepository
|
import de.mm20.launcher2.wikipedia.WikipediaRepository
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
@ -31,6 +32,7 @@ import org.koin.core.component.inject
|
|||||||
class SearchVM : ViewModel(), KoinComponent {
|
class SearchVM : ViewModel(), KoinComponent {
|
||||||
|
|
||||||
private val favoritesRepository: FavoritesRepository by inject()
|
private val favoritesRepository: FavoritesRepository by inject()
|
||||||
|
private val widgetRepository: WidgetRepository by inject()
|
||||||
private val permissionsManager: PermissionsManager by inject()
|
private val permissionsManager: PermissionsManager by inject()
|
||||||
private val dataStore: LauncherDataStore by inject()
|
private val dataStore: LauncherDataStore by inject()
|
||||||
|
|
||||||
@ -49,9 +51,7 @@ class SearchVM : ViewModel(), KoinComponent {
|
|||||||
val searchQuery = MutableLiveData("")
|
val searchQuery = MutableLiveData("")
|
||||||
val isSearchEmpty = MutableLiveData(true)
|
val isSearchEmpty = MutableLiveData(true)
|
||||||
|
|
||||||
val favorites by lazy {
|
val favorites = MutableLiveData<List<Searchable>>(emptyList())
|
||||||
favoritesRepository.getFavorites().asLiveData()
|
|
||||||
}
|
|
||||||
|
|
||||||
val appResults = MutableLiveData<List<Application>>(emptyList())
|
val appResults = MutableLiveData<List<Application>>(emptyList())
|
||||||
val appShortcutResults = MutableLiveData<List<AppShortcut>>(emptyList())
|
val appShortcutResults = MutableLiveData<List<AppShortcut>>(emptyList())
|
||||||
@ -68,6 +68,19 @@ class SearchVM : ViewModel(), KoinComponent {
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
search("")
|
search("")
|
||||||
|
viewModelScope.launch {
|
||||||
|
dataStore.data.map { it.favorites.enabled }.collectLatest { enabled ->
|
||||||
|
if (!enabled) {
|
||||||
|
favorites.value = emptyList()
|
||||||
|
return@collectLatest
|
||||||
|
}
|
||||||
|
widgetRepository.isCalendarWidgetEnabled().collectLatest { excludeCalendar ->
|
||||||
|
favoritesRepository.getFavorites(excludeCalendarEvents = excludeCalendar).collectLatest {
|
||||||
|
favorites.value = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var searchJob: Job? = null
|
var searchJob: Job? = null
|
||||||
|
|||||||
@ -13,6 +13,7 @@ interface WidgetRepository {
|
|||||||
fun addWidget(widget: Widget, position: Int)
|
fun addWidget(widget: Widget, position: Int)
|
||||||
fun removeWidget(widget: Widget)
|
fun removeWidget(widget: Widget)
|
||||||
fun setWidgetHeight(widget: Widget, newHeight: Int)
|
fun setWidgetHeight(widget: Widget, newHeight: Int)
|
||||||
|
fun isCalendarWidgetEnabled(): Flow<Boolean>
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class WidgetRepositoryImpl(
|
internal class WidgetRepositoryImpl(
|
||||||
@ -76,4 +77,8 @@ internal class WidgetRepositoryImpl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun isCalendarWidgetEnabled(): Flow<Boolean> {
|
||||||
|
return database.widgetDao().exists("internal", "calendar")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user