diff options
| author | mojganii <mojgan.jelodar@codic.se> | 2024-03-11 16:11:33 +0100 |
|---|---|---|
| committer | Bug Magnet <marco.nikic@mullvad.net> | 2024-03-12 08:31:23 +0100 |
| commit | 8506380b6f7453720faccf692b60ffd8a1bc112e (patch) | |
| tree | d6b462d48943d756e9e29cbce2a2c3dfc5633331 /ios/MullvadVPN/View controllers/SelectLocation/LocationDataSource.swift | |
| parent | 1c89cd07fb90e23dd52a5e9441a2d144e913a7c3 (diff) | |
| download | mullvadvpn-8506380b6f7453720faccf692b60ffd8a1bc112e.tar.xz mullvadvpn-8506380b6f7453720faccf692b60ffd8a1bc112e.zip | |
adding section header view in select location
- each section should have its own distinct header view.
- custom list section has an action that should take user to add/edit custom list.it will be coming during upcoming changes.
- refactoring location cell
- removing LocationCellFactory
Diffstat (limited to 'ios/MullvadVPN/View controllers/SelectLocation/LocationDataSource.swift')
| -rw-r--r-- | ios/MullvadVPN/View controllers/SelectLocation/LocationDataSource.swift | 59 |
1 files changed, 41 insertions, 18 deletions
diff --git a/ios/MullvadVPN/View controllers/SelectLocation/LocationDataSource.swift b/ios/MullvadVPN/View controllers/SelectLocation/LocationDataSource.swift index 34b78737a7..77da5c69b9 100644 --- a/ios/MullvadVPN/View controllers/SelectLocation/LocationDataSource.swift +++ b/ios/MullvadVPN/View controllers/SelectLocation/LocationDataSource.swift @@ -15,11 +15,11 @@ import UIKit final class LocationDataSource: UITableViewDiffableDataSource<LocationSection, LocationCellViewModel> { private var currentSearchString = "" private let tableView: UITableView - private let locationCellFactory: LocationCellFactory private var dataSources: [LocationDataSourceProtocol] = [] private var selectedItem: LocationCellViewModel? var didSelectRelayLocations: ((RelayLocations) -> Void)? + var didTapEditCustomLists: (() -> Void)? init( tableView: UITableView, @@ -33,19 +33,18 @@ final class LocationDataSource: UITableViewDiffableDataSource<LocationSection, L #endif self.dataSources.append(allLocations) - let locationCellFactory = LocationCellFactory( - tableView: tableView, - reuseIdentifier: LocationSection.Cell.locationCell.reuseIdentifier - ) - self.locationCellFactory = locationCellFactory - super.init(tableView: tableView) { _, indexPath, itemIdentifier in - locationCellFactory.makeCell(for: itemIdentifier, indexPath: indexPath) + let reuseIdentifier = LocationSection.Cell.locationCell.reuseIdentifier + let cell = tableView.dequeueReusableCell( + withIdentifier: reuseIdentifier, + for: indexPath + // swiftlint:disable:next force_cast + ) as! LocationCell + cell.configureCell(item: itemIdentifier) + return cell } tableView.delegate = self - locationCellFactory.delegate = self - defaultRowAnimation = .fade registerClasses() } @@ -218,21 +217,44 @@ final class LocationDataSource: UITableViewDiffableDataSource<LocationSection, L return viewModels } + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + // swiftlint:disable:next force_cast + let cell = super.tableView(tableView, cellForRowAt: indexPath) as! LocationCell + cell.delegate = self + return cell + } } extension LocationDataSource: UITableViewDelegate { + func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { + switch LocationSection.allCases[section] { + case .allLocations: + return LocationSectionHeaderView( + configuration: LocationSectionHeaderView.Configuration(name: LocationSection.allLocations.description) + ) + case .customLists: + return LocationSectionHeaderView(configuration: LocationSectionHeaderView.Configuration( + name: LocationSection.customLists.description, + primaryAction: UIAction( + handler: { [weak self] _ in + self?.didTapEditCustomLists?() + } + ) + )) + } + } + func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { nil } func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { - let section = snapshot().sectionIdentifiers[section] - - switch section { + switch LocationSection.allCases[section] { + case .allLocations: + return .zero case .customLists: return 24 - case .allLocations: - return 0 } } @@ -263,9 +285,10 @@ extension LocationDataSource: UITableViewDelegate { } } -extension LocationDataSource: LocationCellEventHandler { - func toggleCell(for item: LocationCellViewModel) { - guard let indexPath = indexPath(for: item) else { return } +extension LocationDataSource: LocationCellDelegate { + func toggle(cell: LocationCell) { + guard let indexPath = tableView.indexPath(for: cell), + let item = itemIdentifier(for: indexPath) else { return } let sections = LocationSection.allCases let section = sections[indexPath.section] |
