Skip to content

Commit

Permalink
Fix Swiftlint warnings in LocationDataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Petersson committed Sep 5, 2023
1 parent 7444ac2 commit 5d86ca3
Showing 1 changed file with 61 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,11 @@ final class LocationDataSource: UITableViewDiffableDataSource<Int, RelayLocation

func setRelays(_ response: REST.ServerRelaysResponse) {
let rootNode = Self.makeRootNode()
var nodeByLocation = [RelayLocation: Node]()
nodeByLocation.removeAll()

for relay in response.wireguard.relays {
guard case let .city(
countryCode,
cityCode
) = RelayLocation(dashSeparatedString: relay.location),
let serverLocation = response.locations[relay.location] else { continue }
guard case let .city(countryCode, cityCode) = RelayLocation(dashSeparatedString: relay.location),
let serverLocation = response.locations[relay.location] else { continue }

let relayLocation = RelayLocation.hostname(countryCode, cityCode, relay.hostname)

Expand All @@ -92,52 +89,21 @@ final class LocationDataSource: UITableViewDiffableDataSource<Int, RelayLocation
}

// Maintain the `showsChildren` state when transitioning between relay lists
let wasShowingChildren = nodeByLocation[ascendantOrSelf]?
.showsChildren ?? false

let node: Node
switch ascendantOrSelf {
case .country:
node = Node(
type: .country,
location: ascendantOrSelf,
displayName: serverLocation.country,
showsChildren: wasShowingChildren,
isActive: true,
children: []
)
rootNode.addChild(node)

case let .city(countryCode, _):
node = Node(
type: .city,
location: ascendantOrSelf,
displayName: serverLocation.city,
showsChildren: wasShowingChildren,
isActive: true,
children: []
)
nodeByLocation[.country(countryCode)]!.addChild(node)

case let .hostname(countryCode, cityCode, _):
node = Node(
type: .relay,
location: ascendantOrSelf,
displayName: relay.hostname,
showsChildren: false,
isActive: relay.active,
children: []
)
nodeByLocation[.city(countryCode, cityCode)]!.addChild(node)
}

let wasShowingChildren = nodeByLocation[ascendantOrSelf]?.showsChildren ?? false

let node = createNode(
ascendantOrSelf: ascendantOrSelf,
serverLocation: serverLocation,
relay: relay,
rootNode: rootNode,
wasShowingChildren: wasShowingChildren
)
nodeByLocation[ascendantOrSelf] = node
}
}

rootNode.sortChildrenRecursive()
rootNode.computeActiveChildrenRecursive()
self.nodeByLocation = nodeByLocation
locationList = rootNode.flatRelayLocationList()

filterRelays(by: currentSearchString)
Expand Down Expand Up @@ -192,6 +158,53 @@ final class LocationDataSource: UITableViewDiffableDataSource<Int, RelayLocation
}
}

private func createNode(
ascendantOrSelf: RelayLocation,
serverLocation: REST.ServerLocation,
relay: REST.ServerRelay,
rootNode: Node,
wasShowingChildren: Bool
) -> Node {
let node: Node

switch ascendantOrSelf {
case .country:
node = Node(
type: .country,
location: ascendantOrSelf,
displayName: serverLocation.country,
showsChildren: wasShowingChildren,
isActive: true,
children: []
)
rootNode.addChild(node)

case let .city(countryCode, _):
node = Node(
type: .city,
location: ascendantOrSelf,
displayName: serverLocation.city,
showsChildren: wasShowingChildren,
isActive: true,
children: []
)
nodeByLocation[.country(countryCode)]!.addChild(node)

case let .hostname(countryCode, cityCode, _):
node = Node(
type: .relay,
location: ascendantOrSelf,
displayName: relay.hostname,
showsChildren: false,
isActive: relay.active,
children: []
)
nodeByLocation[.city(countryCode, cityCode)]!.addChild(node)
}

return node
}

private func updateDataSnapshot(
with locations: [RelayLocation],
reloadExisting: Bool = false,
Expand Down Expand Up @@ -518,4 +531,6 @@ private extension [RelayLocation] {
locations.contains(location)
})
}

// swiftlint:disable:next file_length
}

0 comments on commit 5d86ca3

Please sign in to comment.