@@ -46,11 +46,13 @@ public extension DynamicFacetListInteractor {
4646 public func connect( ) {
4747 whenSelectionsComputedThenUpdateFilterState ( )
4848 whenFilterStateChangedThenUpdateSelections ( )
49+ whenFacetOrderChangedThenUpdateSelections ( )
4950 }
5051
5152 public func disconnect( ) {
5253 filterState. onChange. cancelSubscription ( for: interactor)
5354 interactor. onSelectionsChanged. cancelSubscription ( for: filterState)
55+ interactor. onFacetOrderChanged. cancelSubscription ( for: filterState)
5456 }
5557
5658 private func groupID( for attribute: Attribute ) -> FilterGroup . ID {
@@ -63,6 +65,21 @@ public extension DynamicFacetListInteractor {
6365 }
6466 }
6567
68+ private func calculateSelections( facets: [ AttributedFacets ] , filterState: FilterState ) -> [ Attribute : Set < String > ] {
69+ let selectionsPerAttribute : [ ( attribute: Attribute , values: Set < String > ) ] =
70+ facets
71+ . map ( \. attribute)
72+ . map { attribute in
73+ let values = filterState
74+ . getFilters ( forGroupWithID: groupID ( for: attribute) )
75+ . compactMap { $0. filter as? FacetFilter }
76+ . filter { $0. attribute == attribute && !$0. isNegated }
77+ . map ( \. value. description)
78+ return ( attribute, Set ( values) )
79+ }
80+ return Dictionary ( uniqueKeysWithValues: selectionsPerAttribute)
81+ }
82+
6683 private func whenSelectionsComputedThenUpdateFilterState( ) {
6784 interactor. onSelectionsComputed. subscribePast ( with: filterState) { filterState, selectionsPerAttribute in
6885 selectionsPerAttribute. forEach { attribute, selections in
@@ -75,20 +92,18 @@ public extension DynamicFacetListInteractor {
7592 }
7693 }
7794
95+ private func whenFacetOrderChangedThenUpdateSelections( ) {
96+ interactor. onFacetOrderChanged. subscribePast ( with: filterState) { filterState, orderedFacets in
97+ interactor. selections = calculateSelections ( facets: orderedFacets,
98+ filterState: filterState)
99+ }
100+ }
101+
78102 private func whenFilterStateChangedThenUpdateSelections( ) {
79- filterState. onChange. subscribePast ( with: interactor) { interactor, _ in
80- let selectionsPerAttribute : [ ( attribute: Attribute , values: Set < String > ) ] = interactor
81- . orderedFacets
82- . map ( \. attribute)
83- . map { attribute in
84- let values = filterState
85- . getFilters ( forGroupWithID: groupID ( for: attribute) )
86- . compactMap { $0. filter as? FacetFilter }
87- . filter { $0. attribute == attribute && !$0. isNegated }
88- . map ( \. value. description)
89- return ( attribute, Set ( values) )
90- }
91- interactor. selections = Dictionary ( uniqueKeysWithValues: selectionsPerAttribute)
103+ filterState. onChange. subscribePast ( with: interactor) { [ weak filterState] interactor, _ in
104+ guard let filterState else { return }
105+ interactor. selections = calculateSelections ( facets: interactor. orderedFacets,
106+ filterState: filterState)
92107 }
93108 }
94109 }
0 commit comments