@@ -15,75 +15,73 @@ class AllLocationDataSource: LocationDataSourceProtocol {
15
15
private var locationList = [ RelayLocation] ( )
16
16
17
17
func search( by text: String ) -> [ RelayLocation ] {
18
- if text. isEmpty {
18
+ guard ! text. isEmpty else {
19
19
return locationList
20
- } else {
21
- var filteredLocations : [ RelayLocation ] = [ ]
22
- locationList. forEach { location in
23
- guard let countryNode = nodeByLocation [ location] else { return }
24
- countryNode. showsChildren = false
25
-
26
- if text. isEmpty || countryNode. displayName. fuzzyMatch ( text) {
27
- filteredLocations. append ( countryNode. location)
28
- }
20
+ }
21
+
22
+ var filteredLocations : [ RelayLocation ] = [ ]
23
+ locationList. forEach { location in
24
+ guard let countryNode = nodeByLocation [ location] else { return }
25
+ countryNode. showsChildren = false
26
+
27
+ if countryNode. displayName. fuzzyMatch ( text) {
28
+ filteredLocations. append ( countryNode. location)
29
+ }
29
30
30
- for cityNode in countryNode. children {
31
- cityNode. showsChildren = false
31
+ countryNode. children. forEach { cityNode in
32
+ cityNode. showsChildren = false
32
33
33
- let relaysContainSearchString = cityNode. children
34
- . contains ( where: { $0. displayName. fuzzyMatch ( text) } )
34
+ let relaysContainSearchString = cityNode. children
35
+ . contains ( where: { $0. displayName. fuzzyMatch ( text) } )
35
36
36
- if cityNode. displayName. fuzzyMatch ( text) || relaysContainSearchString {
37
- if !filteredLocations. contains ( where : { $0 == countryNode. location } ) {
38
- filteredLocations. append ( countryNode. location)
39
- }
37
+ if cityNode. displayName. fuzzyMatch ( text) || relaysContainSearchString {
38
+ if !filteredLocations. contains ( countryNode. location) {
39
+ filteredLocations. append ( countryNode. location)
40
+ }
40
41
41
- filteredLocations. append ( cityNode. location)
42
- countryNode. showsChildren = true
42
+ filteredLocations. append ( cityNode. location)
43
+ countryNode. showsChildren = true
43
44
44
- if relaysContainSearchString {
45
- cityNode. children. map { $0. location } . forEach {
46
- filteredLocations. append ( $0)
47
- }
48
- cityNode. showsChildren = true
49
- }
45
+ if relaysContainSearchString {
46
+ filteredLocations. append ( contentsOf: cityNode. children. map { $0. location } )
47
+ cityNode. showsChildren = true
50
48
}
51
49
}
52
50
}
53
-
54
- return filteredLocations
55
51
}
52
+
53
+ return filteredLocations
56
54
}
57
55
58
56
func reload(
59
- _ response: MullvadREST . REST . ServerRelaysResponse ,
60
- relays: [ MullvadREST . REST . ServerRelay ]
57
+ _ response: REST . ServerRelaysResponse ,
58
+ relays: [ REST . ServerRelay ]
61
59
) -> [ RelayLocation ] {
62
60
nodeByLocation. removeAll ( )
63
- let rootNode = self . makeRootNode ( name: SelectLocationGroup . allLocations. description)
61
+ let rootNode = self . makeRootNode ( name: SelectLocationSection . allLocations. description)
64
62
65
63
for relay in relays {
66
64
guard case let . city( countryCode, cityCode) = RelayLocation ( dashSeparatedString: relay. location) ,
67
65
let serverLocation = response. locations [ relay. location] else { continue }
68
66
69
67
let relayLocation = RelayLocation . hostname ( countryCode, cityCode, relay. hostname)
70
68
71
- for ascendantOrSelf in relayLocation. ascendants + [ relayLocation] {
72
- guard !nodeByLocation. keys. contains ( ascendantOrSelf ) else {
69
+ for ancestorOrSelf in relayLocation. ancestors + [ relayLocation] {
70
+ guard !nodeByLocation. keys. contains ( ancestorOrSelf ) else {
73
71
continue
74
72
}
75
73
76
74
// Maintain the `showsChildren` state when transitioning between relay lists
77
- let wasShowingChildren = nodeByLocation [ ascendantOrSelf ] ? . showsChildren ?? false
75
+ let wasShowingChildren = nodeByLocation [ ancestorOrSelf ] ? . showsChildren ?? false
78
76
79
77
let node = createNode (
80
78
root: rootNode,
81
- ascendantOrSelf : ascendantOrSelf ,
79
+ ancestorOrSelf : ancestorOrSelf ,
82
80
serverLocation: serverLocation,
83
81
relay: relay,
84
82
wasShowingChildren: wasShowingChildren
85
83
)
86
- nodeByLocation [ ascendantOrSelf ] = node
84
+ nodeByLocation [ ancestorOrSelf ] = node
87
85
}
88
86
}
89
87
0 commit comments