summaryrefslogtreecommitdiffhomepage
path: root/ios
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2022-08-12 16:02:28 +0200
committerAndrej Mihajlov <and@mullvad.net>2022-08-16 10:54:15 +0200
commita67c3acc58fbe1b26a0395065534f767fd2d97a0 (patch)
treebb9bac718327546927d133244bf42906cb7dd9aa /ios
parentbab4c7a70f65c4bdcb0b106f46a42d6eb396b35a (diff)
downloadmullvadvpn-a67c3acc58fbe1b26a0395065534f767fd2d97a0.tar.xz
mullvadvpn-a67c3acc58fbe1b26a0395065534f767fd2d97a0.zip
Extract cell configuration from provider to configurator
Diffstat (limited to 'ios')
-rw-r--r--ios/MullvadVPN/LocationDataSource.swift35
-rw-r--r--ios/MullvadVPN/SelectLocationViewController.swift19
2 files changed, 27 insertions, 27 deletions
diff --git a/ios/MullvadVPN/LocationDataSource.swift b/ios/MullvadVPN/LocationDataSource.swift
index 4269eaefc7..df70afef72 100644
--- a/ios/MullvadVPN/LocationDataSource.swift
+++ b/ios/MullvadVPN/LocationDataSource.swift
@@ -25,11 +25,12 @@ class LocationDataSource: NSObject, UITableViewDataSource {
typealias CellProviderBlock = (UITableView, IndexPath, LocationDataSourceItemProtocol)
-> UITableViewCell?
- typealias CellUpdaterBlock = (UITableView, IndexPath, LocationDataSourceItemProtocol) -> Void
+ typealias CellConfiguratorBlock = (UITableViewCell, IndexPath, LocationDataSourceItemProtocol)
+ -> Void
private let tableView: UITableView
private let cellProvider: CellProviderBlock
- private let cellUpdater: CellUpdaterBlock
+ private let cellConfigurator: CellConfiguratorBlock
private(set) var selectedRelayLocation: RelayLocation?
private var lastShowHiddenParents = false
@@ -49,11 +50,12 @@ class LocationDataSource: NSObject, UITableViewDataSource {
init(
tableView: UITableView,
cellProvider: @escaping CellProviderBlock,
- cellUpdater: @escaping CellUpdaterBlock
+ cellConfigurator: @escaping CellConfiguratorBlock
) {
self.tableView = tableView
self.cellProvider = cellProvider
- self.cellUpdater = cellUpdater
+ self.cellConfigurator = cellConfigurator
+
super.init()
tableView.dataSource = self
@@ -354,18 +356,18 @@ class LocationDataSource: NSObject, UITableViewDataSource {
return
}
if changeSet.insertIndexPaths.count >= visibleIndexPaths.count {
- tableView?.scrollToRow(at: lastUpdatedIndexPath, at: .top, animated: true)
+ tableView?.scrollToRow(at: lastUpdatedIndexPath, at: .top, animated: animated)
} else {
- tableView?.scrollToRow(
- at: lastInsertedIndexPath,
- at: .bottom,
- animated: true
- )
+ tableView?.scrollToRow(at: lastInsertedIndexPath, at: .bottom, animated: animated)
}
}
if animated {
- guard let changeSet = applyChanges() else { return }
+ guard let changeSet = applyChanges() else {
+ completion?()
+ return
+ }
+
tableView.performBatchUpdates {
tableView.insertRows(at: changeSet.insertIndexPaths, with: .fade)
tableView.deleteRows(at: changeSet.deleteIndexPaths, with: .fade)
@@ -374,7 +376,10 @@ class LocationDataSource: NSObject, UITableViewDataSource {
assertionFailure()
return
}
- cellUpdater(tableView, indexPath, item)
+
+ if let cell = tableView.cellForRow(at: indexPath) {
+ cellConfigurator(cell, indexPath, item)
+ }
}
} completion: { finished in
scrollToInsertedIndexPaths(changeSet)
@@ -426,7 +431,11 @@ class LocationDataSource: NSObject, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
assert(indexPath.section == 0)
let item = item(for: indexPath)!
- return cellProvider(tableView, indexPath, item)!
+ let cell = cellProvider(tableView, indexPath, item)!
+
+ cellConfigurator(cell, indexPath, item)
+
+ return cell
}
}
diff --git a/ios/MullvadVPN/SelectLocationViewController.swift b/ios/MullvadVPN/SelectLocationViewController.swift
index 777dc0ac48..b9d52107fa 100644
--- a/ios/MullvadVPN/SelectLocationViewController.swift
+++ b/ios/MullvadVPN/SelectLocationViewController.swift
@@ -89,14 +89,14 @@ class SelectLocationViewController: UIViewController, UITableViewDelegate {
dataSource = LocationDataSource(
tableView: tableView,
- cellProvider: { [weak self] tableView, indexPath, item -> UITableViewCell? in
- guard let self = self else { return nil }
-
- let cell = tableView.dequeueReusableCell(
+ cellProvider: { tableView, indexPath, item in
+ return tableView.dequeueReusableCell(
withIdentifier: Self.cellReuseIdentifier,
for: indexPath
)
- as! SelectLocationCell
+ },
+ cellConfigurator: { [weak self] cell, indexPath, item in
+ guard let cell = cell as? SelectLocationCell else { return }
cell.accessibilityIdentifier = item.location.stringRepresentation
cell.isDisabled = !item.isActive
@@ -106,15 +106,6 @@ class SelectLocationViewController: UIViewController, UITableViewDelegate {
cell.didCollapseHandler = { [weak self] cell in
self?.collapseCell(cell)
}
-
- return cell
- },
- cellUpdater: { tableView, indexPath, item in
- guard let cell = tableView.cellForRow(at: indexPath) as? SelectLocationCell else {
- assertionFailure()
- return
- }
- cell.isExpanded = item.showsChildren
}
)