-
Notifications
You must be signed in to change notification settings - Fork 339
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
Jul 3, 2023
1 parent
07c545e
commit d405196
Showing
37 changed files
with
1,502 additions
and
105 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.onApplyFilter = { [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.