Skip to content

Commit

Permalink
fix(dynamic facets): recalculate selections on facets order change (#289
Browse files Browse the repository at this point in the history
)
  • Loading branch information
VladislavFitz authored May 30, 2023
1 parent dabaae0 commit 168505a
Showing 1 changed file with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ public extension DynamicFacetListInteractor {
public func connect() {
whenSelectionsComputedThenUpdateFilterState()
whenFilterStateChangedThenUpdateSelections()
whenFacetOrderChangedThenUpdateSelections()
}

public func disconnect() {
filterState.onChange.cancelSubscription(for: interactor)
interactor.onSelectionsChanged.cancelSubscription(for: filterState)
interactor.onFacetOrderChanged.cancelSubscription(for: filterState)
}

private func groupID(for attribute: Attribute) -> FilterGroup.ID {
Expand All @@ -63,6 +65,21 @@ public extension DynamicFacetListInteractor {
}
}

private func calculateSelections(facets: [AttributedFacets], filterState: FilterState) -> [Attribute: Set<String>] {
let selectionsPerAttribute: [(attribute: Attribute, values: Set<String>)] =
facets
.map(\.attribute)
.map { attribute in
let values = filterState
.getFilters(forGroupWithID: groupID(for: attribute))
.compactMap { $0.filter as? FacetFilter }
.filter { $0.attribute == attribute && !$0.isNegated }
.map(\.value.description)
return (attribute, Set(values))
}
return Dictionary(uniqueKeysWithValues: selectionsPerAttribute)
}

private func whenSelectionsComputedThenUpdateFilterState() {
interactor.onSelectionsComputed.subscribePast(with: filterState) { filterState, selectionsPerAttribute in
selectionsPerAttribute.forEach { attribute, selections in
Expand All @@ -75,20 +92,18 @@ public extension DynamicFacetListInteractor {
}
}

private func whenFacetOrderChangedThenUpdateSelections() {
interactor.onFacetOrderChanged.subscribePast(with: filterState) { filterState, orderedFacets in
interactor.selections = calculateSelections(facets: orderedFacets,
filterState: filterState)
}
}

private func whenFilterStateChangedThenUpdateSelections() {
filterState.onChange.subscribePast(with: interactor) { interactor, _ in
let selectionsPerAttribute: [(attribute: Attribute, values: Set<String>)] = interactor
.orderedFacets
.map(\.attribute)
.map { attribute in
let values = filterState
.getFilters(forGroupWithID: groupID(for: attribute))
.compactMap { $0.filter as? FacetFilter }
.filter { $0.attribute == attribute && !$0.isNegated }
.map(\.value.description)
return (attribute, Set(values))
}
interactor.selections = Dictionary(uniqueKeysWithValues: selectionsPerAttribute)
filterState.onChange.subscribePast(with: interactor) { [weak filterState] interactor, _ in
guard let filterState else { return }
interactor.selections = calculateSelections(facets: interactor.orderedFacets,
filterState: filterState)
}
}
}
Expand Down

0 comments on commit 168505a

Please sign in to comment.