Hide widgets that are already in use from widget picker

This commit is contained in:
MM20 2023-04-10 14:09:29 +02:00
parent f927ed8e82
commit 4db54fc038
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
2 changed files with 17 additions and 4 deletions

View File

@ -42,9 +42,9 @@ class WidgetPickerSheetVM(
private val enabledWidgets = widgetsService.getWidgets()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(100), emptyList())
private val allBuiltInWidgets = enabledWidgets.map { w ->
widgetsService.getBuiltInWidgets().filter { b -> !w.any { it::class == b::class } }
}.shareIn(viewModelScope, SharingStarted.WhileSubscribed(100))
private val allBuiltInWidgets =
widgetsService.getAvailableBuiltInWidgets()
.shareIn(viewModelScope, SharingStarted.WhileSubscribed(100))
val builtInWidgets = allBuiltInWidgets
.combine(searchQuery) { widgets, query ->

View File

@ -23,7 +23,8 @@ class WidgetsService(
) {
suspend fun getAppWidgetProviders(): List<AppWidgetProviderInfo> = withContext(Dispatchers.IO) {
val appWidgetManager = AppWidgetManager.getInstance(context)
val launcherApps = context.getSystemService<LauncherApps>() ?: return@withContext emptyList()
val launcherApps =
context.getSystemService<LauncherApps>() ?: return@withContext emptyList()
val profiles = launcherApps.profiles
val widgets = mutableListOf<AppWidgetProviderInfo>()
for (profile in profiles) {
@ -32,6 +33,18 @@ class WidgetsService(
widgets
}
fun getAvailableBuiltInWidgets(): Flow<List<BuiltInWidgetInfo>> {
return widgetRepository.get().map { widgets ->
getBuiltInWidgets().filter {
it.type == FavoritesWidget.Type && !widgets.any { it is FavoritesWidget } ||
it.type == WeatherWidget.Type && !widgets.any { it is WeatherWidget } ||
it.type == MusicWidget.Type && !widgets.any { it is MusicWidget } ||
it.type == CalendarWidget.Type && !widgets.any { it is CalendarWidget }
}
}
}
fun getBuiltInWidgets(): List<BuiltInWidgetInfo> {
return listOf(
BuiltInWidgetInfo(