Skip to content

Commit

Permalink
Show custom lists even if no filter constraints match
Browse files Browse the repository at this point in the history
  • Loading branch information
rablador committed Apr 29, 2024
1 parent 0454d16 commit 8943caa
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct CustomListLocationNodeBuilder {
name: customList.name,
code: customList.name.lowercased(),
locations: customList.locations,
isActive: !customList.locations.isEmpty,
isActive: true, // Defaults to true, updated after children have been populated.
customList: customList
)

Expand Down Expand Up @@ -47,7 +47,9 @@ struct CustomListLocationNodeBuilder {
}
}

listNode.isActive = !listNode.children.isEmpty
listNode.sort()

return listNode
}
}
Expand All @@ -66,7 +68,7 @@ private extension CustomListLocationNode {
})
.sorted(by: { $0.key < $1.key })
.reduce([]) {
return $0 + $1.value.sorted(by: { $0.name < $1.name })
$0 + $1.value.sorted(by: { $0.name < $1.name })
}

children = sortedChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class CustomListsDataSource: LocationDataSourceProtocol {

/// Constructs a collection of node trees by copying each matching counterpart
/// from the complete list of nodes created in ``AllLocationDataSource``.
func reload(allLocationNodes: [LocationNode], isFiltered: Bool) {
nodes = repository.fetchAll().compactMap { list in
func reload(allLocationNodes: [LocationNode]) {
nodes = repository.fetchAll().map { list in
let customListWrapper = CustomListLocationNodeBuilder(customList: list, allLocations: allLocationNodes)
let listNode = customListWrapper.customListLocationNode

Expand All @@ -38,7 +38,7 @@ class CustomListsDataSource: LocationDataSourceProtocol {
node.code = LocationNode.combineNodeCodes([listNode.code, node.code])
}

return (isFiltered && listNode.children.isEmpty) ? nil : listNode
return listNode
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ final class LocationDataSource:
private var currentSearchString = ""
private var dataSources: [LocationDataSourceProtocol] = []
private var selectedItem: LocationCellViewModel?
private var hasFilter = false
let tableView: UITableView
let sections: [LocationSection]

Expand Down Expand Up @@ -52,8 +51,6 @@ final class LocationDataSource:
}

func setRelays(_ response: REST.ServerRelaysResponse, selectedRelays: UserSelectedRelays?, filter: RelayFilter) {
hasFilter = filter.providers != .any || filter.ownership != .any

let allLocationsDataSource =
dataSources.first(where: { $0 is AllLocationDataSource }) as? AllLocationDataSource

Expand All @@ -65,7 +62,7 @@ final class LocationDataSource:
}

allLocationsDataSource?.reload(response, relays: relays)
customListsDataSource?.reload(allLocationNodes: allLocationsDataSource?.nodes ?? [], isFiltered: hasFilter)
customListsDataSource?.reload(allLocationNodes: allLocationsDataSource?.nodes ?? [])

mapSelectedItem(from: selectedRelays)
filterRelays(by: currentSearchString)
Expand Down Expand Up @@ -110,7 +107,7 @@ final class LocationDataSource:
.filter { $0.showsChildren }

// Reload data source with (possibly) updated custom lists.
customListsDataSource.reload(allLocationNodes: allLocationsDataSource.nodes, isFiltered: hasFilter)
customListsDataSource.reload(allLocationNodes: allLocationsDataSource.nodes)

// Reapply current selection.
mapSelectedItem(from: selectedRelays)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class CustomListsDataSourceTests: XCTestCase {
extension CustomListsDataSourceTests {
private func setUpDataSource() {
dataSource = CustomListsDataSource(repository: CustomListsRepositoryStub(customLists: customLists))
dataSource.reload(allLocationNodes: allLocationNodes, isFiltered: false)
dataSource.reload(allLocationNodes: allLocationNodes)
}

private func createAllLocationNodes() {
Expand Down

0 comments on commit 8943caa

Please sign in to comment.