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() private val enabledWidgets = widgetsService.getWidgets()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(100), emptyList()) .stateIn(viewModelScope, SharingStarted.WhileSubscribed(100), emptyList())
private val allBuiltInWidgets = enabledWidgets.map { w -> private val allBuiltInWidgets =
widgetsService.getBuiltInWidgets().filter { b -> !w.any { it::class == b::class } } widgetsService.getAvailableBuiltInWidgets()
}.shareIn(viewModelScope, SharingStarted.WhileSubscribed(100)) .shareIn(viewModelScope, SharingStarted.WhileSubscribed(100))
val builtInWidgets = allBuiltInWidgets val builtInWidgets = allBuiltInWidgets
.combine(searchQuery) { widgets, query -> .combine(searchQuery) { widgets, query ->

View File

@ -23,7 +23,8 @@ class WidgetsService(
) { ) {
suspend fun getAppWidgetProviders(): List<AppWidgetProviderInfo> = withContext(Dispatchers.IO) { suspend fun getAppWidgetProviders(): List<AppWidgetProviderInfo> = withContext(Dispatchers.IO) {
val appWidgetManager = AppWidgetManager.getInstance(context) 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 profiles = launcherApps.profiles
val widgets = mutableListOf<AppWidgetProviderInfo>() val widgets = mutableListOf<AppWidgetProviderInfo>()
for (profile in profiles) { for (profile in profiles) {
@ -32,6 +33,18 @@ class WidgetsService(
widgets 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> { fun getBuiltInWidgets(): List<BuiltInWidgetInfo> {
return listOf( return listOf(
BuiltInWidgetInfo( BuiltInWidgetInfo(