|
| 1 | +// |
| 2 | +// AddCustomListViewController.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 MullvadSettings |
| 11 | +import UIKit |
| 12 | + |
| 13 | +protocol AddCustomListViewControllerDelegate: AnyObject { |
| 14 | + func customListDidSave() |
| 15 | + func showLocations() |
| 16 | +} |
| 17 | + |
| 18 | +class AddCustomListViewController: UIViewController { |
| 19 | + typealias DataSource = UITableViewDiffableDataSource<CustomListSectionIdentifier, CustomListItemIdentifier> |
| 20 | + |
| 21 | + private let interactor: CustomListInteractorProtocol |
| 22 | + private let tableView = UITableView(frame: .zero, style: .insetGrouped) |
| 23 | + private let subject: CurrentValueSubject<CustomListViewModel, Never> |
| 24 | + private var cancellables = Set<AnyCancellable>() |
| 25 | + private var dataSource: DataSource? |
| 26 | + |
| 27 | + private lazy var cellConfiguration: CustomListCellConfiguration = { |
| 28 | + CustomListCellConfiguration(tableView: tableView, subject: subject) |
| 29 | + }() |
| 30 | + |
| 31 | + private lazy var dataSourceConfiguration: CustomListDataSourceConfiguration? = { |
| 32 | + dataSource.flatMap { dataSource in |
| 33 | + CustomListDataSourceConfiguration(dataSource: dataSource) |
| 34 | + } |
| 35 | + }() |
| 36 | + |
| 37 | + lazy var saveBarButton: UIBarButtonItem = { |
| 38 | + let barButtonItem = UIBarButtonItem( |
| 39 | + title: NSLocalizedString("SAVE_NAVIGATION_BUTTON", tableName: "CustomLists", value: "Create", comment: ""), |
| 40 | + primaryAction: UIAction { [weak self] _ in |
| 41 | + self?.onSave() |
| 42 | + } |
| 43 | + ) |
| 44 | + barButtonItem.style = .done |
| 45 | + return barButtonItem |
| 46 | + }() |
| 47 | + |
| 48 | + weak var delegate: AddCustomListViewControllerDelegate? |
| 49 | + |
| 50 | + init( |
| 51 | + interactor: CustomListInteractorProtocol, |
| 52 | + subject: CurrentValueSubject<CustomListViewModel, Never> |
| 53 | + ) { |
| 54 | + self.subject = subject |
| 55 | + self.interactor = interactor |
| 56 | + |
| 57 | + super.init(nibName: nil, bundle: nil) |
| 58 | + } |
| 59 | + |
| 60 | + required init?(coder: NSCoder) { |
| 61 | + fatalError("init(coder:) has not been implemented") |
| 62 | + } |
| 63 | + |
| 64 | + override func viewDidLoad() { |
| 65 | + super.viewDidLoad() |
| 66 | + |
| 67 | + view.directionalLayoutMargins = UIMetrics.contentLayoutMargins |
| 68 | + view.backgroundColor = .secondaryColor |
| 69 | + isModalInPresentation = true |
| 70 | + |
| 71 | + addSubviews() |
| 72 | + configureNavigationItem() |
| 73 | + configureDataSource() |
| 74 | + configureTableView() |
| 75 | + |
| 76 | + subject.sink { [weak self] viewModel in |
| 77 | + self?.saveBarButton.isEnabled = !viewModel.name.isEmpty |
| 78 | + } |
| 79 | + .store(in: &cancellables) |
| 80 | + } |
| 81 | + |
| 82 | + private func configureNavigationItem() { |
| 83 | + navigationItem.title = NSLocalizedString( |
| 84 | + "CUSTOM_LIST_NAVIGATION_ADD_TITLE", |
| 85 | + tableName: "CustomLists", |
| 86 | + value: "New custom list", |
| 87 | + comment: "" |
| 88 | + ) |
| 89 | + |
| 90 | + navigationItem.leftBarButtonItem = UIBarButtonItem( |
| 91 | + systemItem: .cancel, |
| 92 | + primaryAction: UIAction(handler: { _ in |
| 93 | + self.dismiss(animated: true) |
| 94 | + }) |
| 95 | + ) |
| 96 | + |
| 97 | + navigationItem.rightBarButtonItem = saveBarButton |
| 98 | + } |
| 99 | + |
| 100 | + private func configureTableView() { |
| 101 | + tableView.delegate = dataSourceConfiguration |
| 102 | + tableView.backgroundColor = .secondaryColor |
| 103 | + tableView.registerReusableViews(from: CustomListItemIdentifier.CellIdentifier.self) |
| 104 | + } |
| 105 | + |
| 106 | + private func configureDataSource() { |
| 107 | + dataSource = DataSource( |
| 108 | + tableView: tableView, |
| 109 | + cellProvider: { [weak self] _, indexPath, itemIdentifier in |
| 110 | + self?.cellConfiguration.dequeueCell(at: indexPath, for: itemIdentifier) |
| 111 | + } |
| 112 | + ) |
| 113 | + |
| 114 | + dataSourceConfiguration?.didSelectItem = { item in |
| 115 | + switch item { |
| 116 | + case .name: |
| 117 | + break |
| 118 | + case .locations: |
| 119 | + self.showLocations() |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + dataSourceConfiguration?.updateDataSource(animated: false) |
| 124 | + } |
| 125 | + |
| 126 | + private func addSubviews() { |
| 127 | + view.addConstrainedSubviews([tableView]) { |
| 128 | + tableView.pinEdgesToSuperview() |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private func showLocations() { |
| 133 | + view.endEditing(false) |
| 134 | + delegate?.showLocations() |
| 135 | + } |
| 136 | + |
| 137 | + private func onSave() { |
| 138 | + do { |
| 139 | + try interactor.createCustomList(viewModel: subject.value) |
| 140 | + delegate?.customListDidSave() |
| 141 | + } catch { |
| 142 | + // Todo: Show error dialog. |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments