|
| 1 | +// |
| 2 | +// CustomListCellConfiguration.swift |
| 3 | +// MullvadVPN |
| 4 | +// |
| 5 | +// Created by Jon Petersson on 2024-02-14. |
| 6 | +// Copyright © 2024 Mullvad VPN AB. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Combine |
| 10 | +import UIKit |
| 11 | + |
| 12 | +struct CustomListCellConfiguration { |
| 13 | + let tableView: UITableView |
| 14 | + let subject: CurrentValueSubject<CustomListViewModel, Never> |
| 15 | + |
| 16 | + var onDelete: (() -> Void)? |
| 17 | + |
| 18 | + func dequeueCell( |
| 19 | + at indexPath: IndexPath, |
| 20 | + for itemIdentifier: CustomListItemIdentifier, |
| 21 | + validationErrors: Set<CustomListFieldValidationError> |
| 22 | + ) -> UITableViewCell { |
| 23 | + let cell = tableView.dequeueReusableView(withIdentifier: itemIdentifier.cellIdentifier, for: indexPath) |
| 24 | + |
| 25 | + configureBackground(cell: cell, itemIdentifier: itemIdentifier, validationErrors: validationErrors) |
| 26 | + |
| 27 | + switch itemIdentifier { |
| 28 | + case .name: |
| 29 | + configureName(cell, itemIdentifier: itemIdentifier) |
| 30 | + case .addLocations, .editLocations: |
| 31 | + configureLocations(cell, itemIdentifier: itemIdentifier) |
| 32 | + case .deleteList: |
| 33 | + configureDelete(cell, itemIdentifier: itemIdentifier) |
| 34 | + } |
| 35 | + |
| 36 | + return cell |
| 37 | + } |
| 38 | + |
| 39 | + private func configureBackground( |
| 40 | + cell: UITableViewCell, |
| 41 | + itemIdentifier: CustomListItemIdentifier, |
| 42 | + validationErrors: Set<CustomListFieldValidationError> |
| 43 | + ) { |
| 44 | + configureErrorState( |
| 45 | + cell: cell, |
| 46 | + itemIdentifier: itemIdentifier, |
| 47 | + contentValidationErrors: validationErrors |
| 48 | + ) |
| 49 | + |
| 50 | + guard let cell = cell as? DynamicBackgroundConfiguration else { return } |
| 51 | + |
| 52 | + cell.setAutoAdaptingBackgroundConfiguration(.mullvadListGroupedCell(), selectionType: .dimmed) |
| 53 | + } |
| 54 | + |
| 55 | + private func configureErrorState( |
| 56 | + cell: UITableViewCell, |
| 57 | + itemIdentifier: CustomListItemIdentifier, |
| 58 | + contentValidationErrors: Set<CustomListFieldValidationError> |
| 59 | + ) { |
| 60 | + let itemsWithErrors = CustomListItemIdentifier.fromFieldValidationErrors(contentValidationErrors) |
| 61 | + |
| 62 | + if itemsWithErrors.contains(itemIdentifier) { |
| 63 | + cell.layer.cornerRadius = 10 |
| 64 | + cell.layer.borderWidth = 1 |
| 65 | + cell.layer.borderColor = UIColor.Cell.validationErrorBorderColor.cgColor |
| 66 | + } else { |
| 67 | + cell.layer.borderWidth = 0 |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private func configureName(_ cell: UITableViewCell, itemIdentifier: CustomListItemIdentifier) { |
| 72 | + var contentConfiguration = TextCellContentConfiguration() |
| 73 | + |
| 74 | + contentConfiguration.text = itemIdentifier.text |
| 75 | + contentConfiguration.setPlaceholder(type: .required) |
| 76 | + contentConfiguration.textFieldProperties = .withSmartFeaturesDisabled() |
| 77 | + contentConfiguration.inputText = subject.value.name |
| 78 | + contentConfiguration.editingEvents.onChange = subject.bindTextAction(to: \.name) |
| 79 | + |
| 80 | + cell.contentConfiguration = contentConfiguration |
| 81 | + } |
| 82 | + |
| 83 | + private func configureLocations(_ cell: UITableViewCell, itemIdentifier: CustomListItemIdentifier) { |
| 84 | + var contentConfiguration = UIListContentConfiguration.mullvadValueCell(tableStyle: tableView.style) |
| 85 | + |
| 86 | + contentConfiguration.text = itemIdentifier.text |
| 87 | + cell.contentConfiguration = contentConfiguration |
| 88 | + |
| 89 | + if let cell = cell as? CustomCellDisclosureHandling { |
| 90 | + cell.disclosureType = .chevron |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private func configureDelete(_ cell: UITableViewCell, itemIdentifier: CustomListItemIdentifier) { |
| 95 | + var contentConfiguration = ButtonCellContentConfiguration() |
| 96 | + |
| 97 | + contentConfiguration.style = .tableInsetGroupedDanger |
| 98 | + contentConfiguration.text = itemIdentifier.text |
| 99 | + contentConfiguration.primaryAction = UIAction { _ in |
| 100 | + onDelete?() |
| 101 | + } |
| 102 | + |
| 103 | + cell.contentConfiguration = contentConfiguration |
| 104 | + } |
| 105 | +} |
0 commit comments