From b1722ac1b2eea19d6cb3073c0662b20960858aab Mon Sep 17 00:00:00 2001 From: mojganii Date: Thu, 11 Apr 2024 11:04:51 +0200 Subject: [PATCH] Add alphabetical sorting for custom list locations --- .../CustomListLocationNodeBuilder.swift | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/ios/MullvadVPN/View controllers/SelectLocation/CustomListLocationNodeBuilder.swift b/ios/MullvadVPN/View controllers/SelectLocation/CustomListLocationNodeBuilder.swift index 66e4ddbb5a6c..e92a8bda8de7 100644 --- a/ios/MullvadVPN/View controllers/SelectLocation/CustomListLocationNodeBuilder.swift +++ b/ios/MullvadVPN/View controllers/SelectLocation/CustomListLocationNodeBuilder.swift @@ -8,6 +8,7 @@ import Foundation import MullvadSettings +import MullvadTypes struct CustomListLocationNodeBuilder { let customList: CustomList @@ -18,6 +19,7 @@ struct CustomListLocationNodeBuilder { name: customList.name, code: customList.name.lowercased(), locations: customList.locations, + isActive: !customList.locations.isEmpty, customList: customList ) @@ -44,6 +46,33 @@ struct CustomListLocationNodeBuilder { .copy(withParent: listNode) } } + + listNode.sort() return listNode } } + +private extension CustomListLocationNode { + func sort() { + let sortedChildren = Dictionary(grouping: children, by: { + return switch RelayLocation(dashSeparatedString: $0.code)! { + case .country: + LocationGroup.country + case .city: + LocationGroup.city + case .hostname: + LocationGroup.host + } + }) + .sorted(by: { $0.key < $1.key }) + .reduce([]) { + return $0 + $1.value.sorted(by: { $0.name < $1.name }) + } + + children = sortedChildren + } +} + +private enum LocationGroup: CaseIterable, Comparable { + case country, city, host +}