summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2021-04-08 12:50:31 +0200
committerAndrej Mihajlov <and@mullvad.net>2021-04-12 11:14:52 +0200
commit37ff3be94b7cff0b0479ada5d3b4c45cd6727774 (patch)
treee853fd4d2f1e48542296a4d1b74a47e0326a7066
parenta104ef42cccbe56db9402495fb8a4edaa4836ca5 (diff)
downloadmullvadvpn-37ff3be94b7cff0b0479ada5d3b4c45cd6727774.tar.xz
mullvadvpn-37ff3be94b7cff0b0479ada5d3b4c45cd6727774.zip
SelectLocation: add delegate
-rw-r--r--ios/MullvadVPN/SelectLocationViewController.swift62
1 files changed, 26 insertions, 36 deletions
diff --git a/ios/MullvadVPN/SelectLocationViewController.swift b/ios/MullvadVPN/SelectLocationViewController.swift
index 39464bce5c..e308f0d26b 100644
--- a/ios/MullvadVPN/SelectLocationViewController.swift
+++ b/ios/MullvadVPN/SelectLocationViewController.swift
@@ -9,7 +9,11 @@
import UIKit
import Logging
-class SelectLocationViewController: UIViewController, RelayCacheObserver, UITableViewDelegate {
+protocol SelectLocationViewControllerDelegate: class {
+ func selectLocationViewController(_ controller: SelectLocationViewController, didSelectRelayLocation relayLocation: RelayLocation)
+}
+
+class SelectLocationViewController: UIViewController, UITableViewDelegate {
private enum ReuseIdentifiers: String {
case cell
@@ -37,9 +41,10 @@ class SelectLocationViewController: UIViewController, RelayCacheObserver, UITabl
private var dataSource: LocationDataSource?
private var setCachedRelaysOnViewDidLoad: CachedRelays?
private var setRelayLocationOnViewDidLoad: RelayLocation?
+ private var setScrollPositionOnViewDidLoad: UITableView.ScrollPosition = .none
private var isViewAppeared = false
- var didSelectRelayLocation: ((SelectLocationViewController, RelayLocation) -> Void)?
+ weak var delegate: SelectLocationViewControllerDelegate?
var scrollToSelectedRelayOnViewWillAppear = true
init() {
@@ -91,11 +96,9 @@ class SelectLocationViewController: UIViewController, RelayCacheObserver, UITabl
setRelayLocationOnViewDidLoad,
showHiddenParents: true,
animated: false,
- scrollPosition: .none
+ scrollPosition: setScrollPositionOnViewDidLoad
)
}
-
- RelayCache.shared.addObserver(self)
}
override func viewWillAppear(_ animated: Bool) {
@@ -120,6 +123,16 @@ class SelectLocationViewController: UIViewController, RelayCacheObserver, UITabl
isViewAppeared = false
}
+ override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
+ super.viewWillTransition(to: size, with: coordinator)
+
+ coordinator.animate { (context) in
+ if let indexPath = self.dataSource?.indexPathForSelectedRelay() {
+ self.tableView.scrollToRow(at: indexPath, at: .middle, animated: false)
+ }
+ }
+ }
+
// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
@@ -145,7 +158,8 @@ class SelectLocationViewController: UIViewController, RelayCacheObserver, UITabl
animated: false,
scrollPosition: .none
)
- didSelectRelayLocation?(self, item.location)
+
+ self.delegate?.selectLocationViewController(self, didSelectRelayLocation: item.location)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
@@ -162,34 +176,20 @@ class SelectLocationViewController: UIViewController, RelayCacheObserver, UITabl
return view
}
- // MARK: - RelayCacheObserver
-
- func relayCache(_ relayCache: RelayCache, didUpdateCachedRelays cachedRelays: CachedRelays) {
- DispatchQueue.main.async {
- self.didReceiveCachedRelays(cachedRelays)
- }
- }
-
// MARK: - Public
- func prefetchData(completionHandler: @escaping (RelayCacheError?) -> Void) {
- RelayCache.shared.read { (result) in
- DispatchQueue.main.async {
- switch result {
- case .success(let cachedRelays):
- self.didReceiveCachedRelays(cachedRelays)
- completionHandler(nil)
-
- case .failure(let error):
- completionHandler(error)
- }
- }
+ func setCachedRelays(_ cachedRelays: CachedRelays) {
+ guard isViewLoaded else {
+ self.setCachedRelaysOnViewDidLoad = cachedRelays
+ return
}
+ self.dataSource?.setRelays(cachedRelays.relays)
}
func setSelectedRelayLocation(_ relayLocation: RelayLocation?, animated: Bool, scrollPosition: UITableView.ScrollPosition) {
guard isViewLoaded else {
self.setRelayLocationOnViewDidLoad = relayLocation
+ self.setScrollPositionOnViewDidLoad = scrollPosition
return
}
@@ -201,16 +201,6 @@ class SelectLocationViewController: UIViewController, RelayCacheObserver, UITabl
)
}
- // MARK: - Relay list handling
-
- private func didReceiveCachedRelays(_ cachedRelays: CachedRelays) {
- guard isViewLoaded else {
- self.setCachedRelaysOnViewDidLoad = cachedRelays
- return
- }
- self.dataSource?.setRelays(cachedRelays.relays)
- }
-
// MARK: - Collapsible cells
private func collapseCell(_ cell: SelectLocationCell) {