Skip to content

Commit

Permalink
fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavFitz committed Jun 4, 2021
1 parent f9c8fdf commit 3806b45
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let package = Package(
targets: ["InstantSearchInsights"])
],
dependencies: [
.package(name: "AlgoliaSearchClient", url:"https://github.com/algolia/algoliasearch-client-swift", from: "8.5.0")
.package(name: "AlgoliaSearchClient", url: "https://github.com/algolia/algoliasearch-client-swift", from: "8.5.0")
],
targets: [
.target(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,63 @@ import InstantSearchCore
import UIKit

public class DynamicFacetsTableViewController: UITableViewController, DynamicFacetsController {

public var orderedFacets: [AttributedFacets]
public var selections: [Attribute: Set<String>]
public var didSelect: ((Attribute, Facet) -> Void)?
public func apply(_ selections: [Attribute : Set<String>]) {

public func apply(_ selections: [Attribute: Set<String>]) {
self.selections = selections
tableView.reloadData()
}

public func apply(_ orderedFacets: [AttributedFacets]) {
self.orderedFacets = orderedFacets
tableView.reloadData()
}

public init(orderedFacets: [AttributedFacets] = [], selections: [Attribute: Set<String>] = [:]) {
self.orderedFacets = orderedFacets
self.selections = selections
super.init(style: .plain)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

public override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
public override func numberOfSections(in tableView: UITableView) -> Int {
return orderedFacets.count
}

public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return orderedFacets[section].facets.count
}

public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
}

public override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return orderedFacets[section].attribute.rawValue
}

public override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let attribute = orderedFacets[indexPath.section].attribute
let facet = orderedFacets[indexPath.section].facets[indexPath.row]
cell.textLabel?.text = facet.description
cell.accessoryType = (selections[attribute]?.contains(facet.value) ?? false) ? .checkmark : .none
}

public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let unit = orderedFacets[indexPath.section]
let facet = unit.facets[indexPath.row]
didSelect?(unit.attribute, facet)
}

}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import Foundation
Controller presenting the ordered list of facets
*/
public protocol DynamicFacetsController: AnyObject {

func apply(_ facetOrder: [AttributedFacets])
func apply(_ selections: [Attribute: Set<String>])

var didSelect: ((Attribute, Facet) -> Void)? { get set }

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import Foundation

public extension DynamicFacetsInteractor {

/// Connection between a dynamic facets business logic and a controller
struct ControllerConnection<Controller: DynamicFacetsController>: Connection {

/// Dynamic facets business logic
public let interactor: DynamicFacetsInteractor

///
public let controller: Controller

/**
- parameters:
- interactor: Dynamic facets business logic
Expand All @@ -28,7 +28,7 @@ public extension DynamicFacetsInteractor {
self.interactor = interactor
self.controller = controller
}

public func connect() {
controller.didSelect = { [weak interactor] attribute, facet in
guard let interactor = interactor else { return }
Expand All @@ -37,24 +37,24 @@ public extension DynamicFacetsInteractor {
interactor.onSelectionsChanged.subscribePast(with: controller) { (controller, selections) in
controller.apply(selections)
}.onQueue(.main)

interactor.onFacetOrderChanged.subscribePast(with: controller) { controller, facetOrder in
controller.apply(facetOrder)
}.onQueue(.main)
}

public func disconnect() {
controller.didSelect = nil
interactor.onSelectionsChanged.cancelSubscription(for: controller)
interactor.onFacetOrderChanged.cancelSubscription(for: controller)
}

}

@discardableResult func connectController<Controller: DynamicFacetsController>(_ controller: Controller) -> ControllerConnection<Controller> {
let connection = ControllerConnection(interactor: self, controller: controller)
connection.connect()
return connection
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import Foundation

public extension DynamicFacetsInteractor {

/// Connection between a dynamic facets business logic and a filter state
struct FilterStateConnection: Connection {

/// Dynamic facets business logic
public let interactor: DynamicFacetsInteractor

///
public let filterState: FilterState

///
public let groupIDForAttribute: [Attribute: FilterGroup.ID]

Expand All @@ -34,21 +34,21 @@ public extension DynamicFacetsInteractor {
self.filterState = filterState
self.groupIDForAttribute = groupIDForAttribute
}

private func groupID(for attribute: Attribute) -> FilterGroup.ID {
return groupIDForAttribute[attribute] ?? .and(name: attribute.rawValue)
}

public func connect() {
whenSelectionsComputedThenUpdateFilterState()
whenFilterStateChangedThenUpdateSelections()
}

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

private func whenSelectionsComputedThenUpdateFilterState() {
interactor.onSelectionsComputed.subscribePast(with: filterState) { filterState, selectionsPerAttribute in
selectionsPerAttribute.forEach { attribute, selections in
Expand All @@ -60,9 +60,9 @@ public extension DynamicFacetsInteractor {
filterState.notifyChange()
}
}

private func whenFilterStateChangedThenUpdateSelections() {
filterState.onChange.subscribePast(with: interactor) { interactor, filters in
filterState.onChange.subscribePast(with: interactor) { interactor, _ in
let selectionsPerAttribute: [(attribute: Attribute, values: Set<String>)] = interactor
.orderedFacets
.map(\.attribute)
Expand All @@ -77,13 +77,13 @@ public extension DynamicFacetsInteractor {
interactor.selections = Dictionary(uniqueKeysWithValues: selectionsPerAttribute)
}
}

}

@discardableResult func connectFilterState(_ filterState: FilterState) -> FilterStateConnection {
let connection = FilterStateConnection(interactor: self, filterState: filterState)
connection.connect()
return connection
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import Foundation

public extension DynamicFacetsInteractor {

/// Connection between a dynamic facets business logic and a searcher
struct SearcherConnection<Searcher: SearchResultObservable>: Connection where Searcher.SearchResult == SearchResponse {

/// Dynamic facets business logic
public let interactor: DynamicFacetsInteractor

Expand All @@ -27,7 +27,7 @@ public extension DynamicFacetsInteractor {
self.searcher = searcher
self.interactor = interactor
}

public func connect() {
searcher.onResults.subscribe(with: interactor) { (interactor, searchResponse) in
if let facetOrdering = searchResponse.renderingContent?.facetOrdering,
Expand All @@ -48,11 +48,11 @@ public extension DynamicFacetsInteractor {
}

}

@discardableResult func connectSearcher<Searcher: SearchResultObservable>(_ searcher: Searcher) -> SearcherConnection<Searcher> {
let connection = SearcherConnection(interactor: self, searcher: searcher)
connection.connect()
return connection
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,37 @@ import Foundation
- ...
*/
public class DynamicFacetsInteractor {

/// Mapping between a facet attribute and a set of its selected values
public typealias SelectionsPerAttribute = [Attribute: Set<String>]

public var orderedFacets: [AttributedFacets] = .init() {
didSet {
onFacetOrderChanged.fire(orderedFacets)
updateInteractors()
}
}

/// Facet selections per attribute
public var selections: SelectionsPerAttribute = .init() {
didSet {
guard oldValue != selections else { return }
onSelectionsChanged.fire(selections)
}
}

///
public let onFacetOrderChanged: Observer<[AttributedFacets]>

///
public let onSelectionsChanged: Observer<SelectionsPerAttribute>

///
public let onSelectionsComputed: Observer<SelectionsPerAttribute>

///
public let selectionModeForAttribute: [Attribute: SelectionMode]

///
private var facetListPerAttribute: [Attribute: SelectableListInteractor<String, Facet>]

Expand All @@ -75,7 +75,7 @@ public class DynamicFacetsInteractor {
onSelectionsChanged.fire(selections)
updateInteractors()
}

/**
Returns the selection state of facet value for attribute
- parameters:
Expand All @@ -86,7 +86,7 @@ public class DynamicFacetsInteractor {
for attribute: Attribute) -> Bool {
return selections[attribute]?.contains(facetValue) ?? false
}

/**
Toggle the selection state of facet value for attribute
- parameters:
Expand All @@ -97,28 +97,28 @@ public class DynamicFacetsInteractor {
for attribute: Attribute) {
facetListPerAttribute[attribute]?.computeSelections(selectingItemForKey: facetValue)
}

private func updateInteractors() {

for attributedFacet in orderedFacets {
let attribute = attributedFacet.attribute

let facetList: SelectableListInteractor<String, Facet>

if let existingFacetList = facetListPerAttribute[attribute] {
facetList = existingFacetList
} else {
facetList = createFacetList(for: attribute)
facetListPerAttribute[attribute] = facetList
}

facetList.items = attributedFacet.facets
facetList.selections = selections[attribute] ?? []

}

}

private func createFacetList(for attribute: Attribute) -> SelectableListInteractor<String, Facet> {
let selectionMode = selectionModeForAttribute[attribute] ?? .single
let facetList = SelectableListInteractor<String, Facet>(selectionMode: selectionMode)
Expand Down
Loading

0 comments on commit 3806b45

Please sign in to comment.