Skip to content

Commit

Permalink
[suggested addon finder] update candidates on load
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Fiddian-Green <[email protected]>
  • Loading branch information
andrewfg committed Oct 31, 2023
1 parent 81c044e commit 2bfa9d1
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openhab.core.addon.AddonInfo;
import org.openhab.core.addon.AddonInfoProvider;
import org.openhab.core.config.discovery.addon.finders.AddonSuggestionFinder;
import org.openhab.core.i18n.LocaleProvider;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
Expand All @@ -46,18 +47,28 @@ public class AddonSuggestionFinderService implements AutoCloseable {

private final Set<AddonInfoProvider> addonInfoProviders = ConcurrentHashMap.newKeySet();
private final List<AddonSuggestionFinder> addonSuggestionFinders = Collections.synchronizedList(new ArrayList<>());
private final LocaleProvider localeProvider;

@Activate
public AddonSuggestionFinderService() {
public AddonSuggestionFinderService(@Reference LocaleProvider localeProvider) {
this.localeProvider = localeProvider;
}

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
public void addAddonInfoProvider(AddonInfoProvider addonInfoProvider) {
addonInfoProviders.add(addonInfoProvider);
addonInfoProvidersChanged();
}

public void removeAddonInfoProvider(AddonInfoProvider addonInfoProvider) {
addonInfoProviders.remove(addonInfoProvider);
addonInfoProvidersChanged();
}

private void addonInfoProvidersChanged() {
List<AddonInfo> candidates = addonInfoProviders.stream().map(p -> p.getAddonInfos(localeProvider.getLocale()))
.flatMap(Collection::stream).toList();
addonSuggestionFinders.forEach(f -> f.setAddonCandidates(candidates));
}

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
Expand All @@ -77,9 +88,6 @@ public void close() throws Exception {
}

public Set<AddonInfo> getSuggestedAddons(@Nullable Locale locale) {
List<AddonInfo> candidates = addonInfoProviders.stream().map(p -> p.getAddonInfos(locale))
.flatMap(Collection::stream).toList();
addonSuggestionFinders.forEach(f -> f.setAddonCandidates(candidates));
return addonSuggestionFinders.stream().map(f -> f.getSuggestedAddons()).flatMap(Collection::stream)
.collect(Collectors.toSet());
}
Expand Down

0 comments on commit 2bfa9d1

Please sign in to comment.