-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathLocationNode.swift
158 lines (133 loc) · 4.18 KB
/
LocationNode.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// LocationNode.swift
// MullvadVPN
//
// Created by Jon Petersson on 2024-02-21.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//
import MullvadSettings
import MullvadTypes
class LocationNode {
let name: String
var code: String
var locations: [RelayLocation]
var parent: LocationNode?
var children: [LocationNode]
var showsChildren: Bool
var isHiddenFromSearch: Bool
init(
name: String,
code: String,
locations: [RelayLocation] = [],
parent: LocationNode? = nil,
children: [LocationNode] = [],
showsChildren: Bool = false,
isHiddenFromSearch: Bool = false
) {
self.name = name
self.code = code
self.locations = locations
self.parent = parent
self.children = children
self.showsChildren = showsChildren
self.isHiddenFromSearch = isHiddenFromSearch
}
}
extension LocationNode {
var root: LocationNode {
parent?.root ?? self
}
func countryFor(code: String) -> LocationNode? {
self.code == code ? self : children.first(where: { $0.code == code })
}
func cityFor(codes: [String]) -> LocationNode? {
let combinedCode = Self.combineNodeCodes(codes)
return self.code == combinedCode ? self : children.first(where: { $0.code == combinedCode })
}
func hostFor(code: String) -> LocationNode? {
self.code == code ? self : children.first(where: { $0.code == code })
}
func descendantNodeFor(codes: [String]) -> LocationNode? {
let combinedCode = Self.combineNodeCodes(codes)
return self.code == combinedCode ? self : children.compactMap { $0.descendantNodeFor(codes: codes) }.first
}
func forEachDescendant(do callback: (LocationNode) -> Void) {
children.forEach { child in
callback(child)
child.forEachDescendant(do: callback)
}
}
func forEachAncestor(do callback: (LocationNode) -> Void) {
if let parent = parent {
callback(parent)
parent.forEachAncestor(do: callback)
}
}
static func combineNodeCodes(_ codes: [String]) -> String {
codes.joined(separator: "-")
}
}
extension LocationNode {
func copy(withParent parent: LocationNode? = nil) -> LocationNode {
let node = LocationNode(
name: name,
code: code,
locations: locations,
parent: parent,
children: [],
showsChildren: showsChildren,
isHiddenFromSearch: isHiddenFromSearch
)
node.children = recursivelyCopyChildren(withParent: node)
return node
}
private func recursivelyCopyChildren(withParent parent: LocationNode) -> [LocationNode] {
children.map { $0.copy(withParent: parent) }
}
}
extension LocationNode: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(code)
}
static func == (lhs: LocationNode, rhs: LocationNode) -> Bool {
lhs.code == rhs.code
}
}
extension LocationNode: Comparable {
static func < (lhs: LocationNode, rhs: LocationNode) -> Bool {
lhs.name < rhs.name
}
}
/// Proxy class for building and/or searching node trees.
class RootLocationNode: LocationNode {
init(name: String = "", code: String = "", children: [LocationNode] = []) {
super.init(name: name, code: code, children: children)
}
}
class CustomListLocationNode: LocationNode {
let customList: CustomList
init(
name: String,
code: String,
locations: [RelayLocation] = [],
parent: LocationNode? = nil,
children: [LocationNode] = [],
showsChildren: Bool = false,
isHiddenFromSearch: Bool = false,
customList: CustomList
) {
self.customList = customList
super.init(
name: name,
code: code,
locations: locations,
parent: parent,
children: children,
showsChildren: showsChildren,
isHiddenFromSearch: isHiddenFromSearch
)
}
}
class CountryLocationNode: LocationNode {}
class CityLocationNode: LocationNode {}
class HostLocationNode: LocationNode {}