Skip to content

Commit b248cb3

Browse files
committed
Apply filtering on custom lists
1 parent fc7a0c2 commit b248cb3

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

ios/MullvadVPN/View controllers/SelectLocation/CustomListsDataSource.swift

+18-8
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@ class CustomListsDataSource: LocationDataSourceProtocol {
2525

2626
/// Constructs a collection of node trees by copying each matching counterpart
2727
/// from the complete list of nodes created in ``AllLocationDataSource``.
28-
func reload(allLocationNodes: [LocationNode]) {
29-
nodes = repository.fetchAll().map { list in
28+
func reload(allLocationNodes: [LocationNode], isFiltered: Bool) {
29+
nodes.removeAll()
30+
let customLists = repository.fetchAll()
31+
for customList in customLists {
3032
let listNode = CustomListLocationNode(
31-
name: list.name,
32-
code: list.name.lowercased(),
33-
locations: list.locations,
34-
customList: list
33+
name: customList.name,
34+
code: customList.name.lowercased(),
35+
locations: customList.locations,
36+
customList: customList
3537
)
3638

37-
listNode.children = list.locations.compactMap { location in
39+
listNode.children = customList.locations.compactMap { location in
3840
copy(location, from: allLocationNodes, withParent: listNode)
3941
}
4042

@@ -46,7 +48,15 @@ class CustomListsDataSource: LocationDataSourceProtocol {
4648
node.code = LocationNode.combineNodeCodes([listNode.code, node.code])
4749
}
4850

49-
return listNode
51+
// Indicates if the relays are filtered by ``Ownership`` or ``Provider``, node should be added while it has children
52+
if isFiltered {
53+
guard !listNode.children.isEmpty else {
54+
continue
55+
}
56+
nodes.append(listNode)
57+
} else {
58+
nodes.append(listNode)
59+
}
5060
}
5161
}
5262

ios/MullvadVPN/View controllers/SelectLocation/LocationDataSource.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ final class LocationDataSource: UITableViewDiffableDataSource<LocationSection, L
1717
private let tableView: UITableView
1818
private var dataSources: [LocationDataSourceProtocol] = []
1919
private var selectedItem: LocationCellViewModel?
20+
private var hasFilter = false
2021

2122
var didSelectRelayLocations: ((UserSelectedRelays) -> Void)?
2223
var didTapEditCustomLists: (() -> Void)?
@@ -47,6 +48,8 @@ final class LocationDataSource: UITableViewDiffableDataSource<LocationSection, L
4748
}
4849

4950
func setRelays(_ response: REST.ServerRelaysResponse, selectedRelays: UserSelectedRelays?, filter: RelayFilter) {
51+
hasFilter = filter.providers != .any || filter.ownership != .any
52+
5053
let allLocationsDataSource =
5154
dataSources.first(where: { $0 is AllLocationDataSource }) as? AllLocationDataSource
5255

@@ -58,7 +61,7 @@ final class LocationDataSource: UITableViewDiffableDataSource<LocationSection, L
5861
}
5962

6063
allLocationsDataSource?.reload(response, relays: relays)
61-
customListsDataSource?.reload(allLocationNodes: allLocationsDataSource?.nodes ?? [])
64+
customListsDataSource?.reload(allLocationNodes: allLocationsDataSource?.nodes ?? [], isFiltered: hasFilter)
6265

6366
mapSelectedItem(from: selectedRelays)
6467
filterRelays(by: currentSearchString)
@@ -101,7 +104,7 @@ final class LocationDataSource: UITableViewDiffableDataSource<LocationSection, L
101104
let customListsDataSource =
102105
dataSources.first(where: { $0 is CustomListsDataSource }) as? CustomListsDataSource
103106

104-
customListsDataSource?.reload(allLocationNodes: allLocationsDataSource?.nodes ?? [])
107+
customListsDataSource?.reload(allLocationNodes: allLocationsDataSource?.nodes ?? [], isFiltered: hasFilter)
105108

106109
mapSelectedItem(from: selectedRelays)
107110
filterRelays(by: currentSearchString, scrollToSelected: false)

ios/MullvadVPNTests/Location/CustomListsDataSourceTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class CustomListsDataSourceTests: XCTestCase {
6363
extension CustomListsDataSourceTests {
6464
private func setUpDataSource() {
6565
dataSource = CustomListsDataSource(repository: CustomListsRepositoryStub(customLists: customLists))
66-
dataSource.reload(allLocationNodes: allLocationNodes)
66+
dataSource.reload(allLocationNodes: allLocationNodes, isFiltered: false)
6767
}
6868

6969
private func createAllLocationNodes() {

0 commit comments

Comments
 (0)