-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jon Petersson
committed
Jun 21, 2023
1 parent
152dfd8
commit 214446c
Showing
36 changed files
with
1,435 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// RelayFilter.swift | ||
// MullvadVPN | ||
// | ||
// Created by Jon Petersson on 2023-06-08. | ||
// Copyright © 2023 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct RelayFilter: Codable, Equatable { | ||
public enum Ownership: Codable { | ||
case any | ||
case owned | ||
case rented | ||
} | ||
|
||
public var ownership: Ownership | ||
public var providers: RelayConstraint<[String]> | ||
|
||
public init(ownership: Ownership = .any, providers: RelayConstraint<[String]> = .any) { | ||
self.ownership = ownership | ||
self.providers = providers | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
88 changes: 88 additions & 0 deletions
88
ios/MullvadVPN/Coordinators/App/RelayFilterCoordinator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// | ||
// RelayFilterCoordinator.swift | ||
// MullvadVPN | ||
// | ||
// Created by Jon Petersson on 2023-06-14. | ||
// Copyright © 2023 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import MullvadTypes | ||
import RelayCache | ||
import UIKit | ||
|
||
class RelayFilterCoordinator: Coordinator, Presentable, RelayCacheTrackerObserver { | ||
private let tunnelManager: TunnelManager | ||
private let relayCacheTracker: RelayCacheTracker | ||
private var cachedRelays: CachedRelays? | ||
|
||
let navigationController: UINavigationController | ||
|
||
var presentedViewController: UIViewController { | ||
return navigationController | ||
} | ||
|
||
var relayFilterViewController: RelayFilterViewController? { | ||
return navigationController.viewControllers.first { | ||
$0 is RelayFilterViewController | ||
} as? RelayFilterViewController | ||
} | ||
|
||
var relayFilter: RelayFilter { | ||
switch tunnelManager.settings.relayConstraints.filter { | ||
case .any: | ||
return RelayFilter() | ||
case let .only(filter): | ||
return filter | ||
} | ||
} | ||
|
||
var didFinish: ((RelayFilterCoordinator, RelayFilter?) -> Void)? | ||
|
||
init( | ||
navigationController: UINavigationController, | ||
tunnelManager: TunnelManager, | ||
relayCacheTracker: RelayCacheTracker | ||
) { | ||
self.navigationController = navigationController | ||
self.tunnelManager = tunnelManager | ||
self.relayCacheTracker = relayCacheTracker | ||
} | ||
|
||
func start() { | ||
let relayFilterViewController = RelayFilterViewController() | ||
|
||
relayFilterViewController.didApplyFilter = { [weak self] filter in | ||
guard let self else { return } | ||
|
||
var relayConstraints = tunnelManager.settings.relayConstraints | ||
relayConstraints.filter = .only(filter) | ||
|
||
tunnelManager.setRelayConstraints(relayConstraints) | ||
|
||
didFinish?(self, filter) | ||
} | ||
|
||
relayFilterViewController.didFinish = { [weak self] in | ||
guard let self else { return } | ||
|
||
didFinish?(self, nil) | ||
} | ||
|
||
relayCacheTracker.addObserver(self) | ||
|
||
if let cachedRelays = try? relayCacheTracker.getCachedRelays() { | ||
self.cachedRelays = cachedRelays | ||
relayFilterViewController.setCachedRelays(cachedRelays, filter: relayFilter) | ||
} | ||
|
||
navigationController.pushViewController(relayFilterViewController, animated: false) | ||
} | ||
|
||
func relayCacheTracker( | ||
_ tracker: RelayCacheTracker, | ||
didUpdateCachedRelays cachedRelays: CachedRelays | ||
) { | ||
self.cachedRelays = cachedRelays | ||
relayFilterViewController?.setCachedRelays(cachedRelays, filter: relayFilter) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// Collection+Sorting.swift | ||
// MullvadVPN | ||
// | ||
// Created by Jon Petersson on 2023-06-14. | ||
// Copyright © 2023 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Collection where Element: StringProtocol { | ||
public func caseInsensitiveSorted() -> [Element] { | ||
sorted { $0.caseInsensitiveCompare($1) == .orderedAscending } | ||
} | ||
} | ||
|
||
extension MutableCollection where Element: StringProtocol, Self: RandomAccessCollection { | ||
public mutating func caseInsensitiveSort() { | ||
sort { $0.caseInsensitiveCompare($1) == .orderedAscending } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.