summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/SelectLocation/LocationNode.swift
blob: 53c5f0dfd65d0843f564843d47cc68ee8b6ce1d3 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
//
//  LocationNode.swift
//  MullvadVPN
//
//  Created by Jon Petersson on 2024-02-21.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import MullvadSettings
import MullvadTypes

class LocationNode: @unchecked Sendable {
    let name: String
    var code: String
    var locations: [RelayLocation]
    var isActive: Bool
    weak var parent: LocationNode?
    var children: [LocationNode]
    var showsChildren: Bool
    var isHiddenFromSearch: Bool

    init(
        name: String,
        code: String,
        locations: [RelayLocation] = [],
        isActive: Bool = true,
        parent: LocationNode? = nil,
        children: [LocationNode] = [],
        showsChildren: Bool = false,
        isHiddenFromSearch: Bool = false
    ) {
        self.name = name
        self.code = code
        self.locations = locations
        self.isActive = isActive
        self.parent = parent
        self.children = children
        self.showsChildren = showsChildren
        self.isHiddenFromSearch = isHiddenFromSearch
    }
}

extension LocationNode {
    var root: LocationNode {
        parent?.root ?? self
    }

    var hierarchyLevel: Int {
        var level = 0
        forEachAncestor { _ in level += 1 }
        return level
    }

    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: "-")
    }

    var flattened: [LocationNode] {
        children + children.flatMap { $0.flattened }
    }
}

extension LocationNode {
    /// Recursively copies a node, its parent and its descendants from another
    /// node (tree), with an optional custom root parent.
    func copy(withParent parent: LocationNode? = nil) -> LocationNode {
        let node = LocationNode(
            name: name,
            code: code,
            locations: locations,
            isActive: isActive,
            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.lowercased() < rhs.name.lowercased()
    }
}

/// Proxy class for building and/or searching node trees.
class RootLocationNode: LocationNode, @unchecked Sendable {
    init(name: String = "", code: String = "", children: [LocationNode] = []) {
        super.init(name: name, code: code, children: children)
    }
}

class CustomListLocationNode: LocationNode, @unchecked Sendable {
    let customList: CustomList

    init(
        name: String,
        code: String,
        locations: [RelayLocation] = [],
        isActive: Bool = true,
        parent: LocationNode? = nil,
        children: [LocationNode] = [],
        showsChildren: Bool = false,
        isHiddenFromSearch: Bool = false,
        customList: CustomList
    ) {
        self.customList = customList

        super.init(
            name: name,
            code: code,
            locations: locations,
            isActive: isActive,
            parent: parent,
            children: children,
            showsChildren: showsChildren,
            isHiddenFromSearch: isHiddenFromSearch
        )
    }
}