-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UI for creating and editing a custom list
- Loading branch information
Jon Petersson
committed
Feb 16, 2024
1 parent
1a6cc86
commit 13dbec5
Showing
16 changed files
with
652 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
ios/MullvadVPN/Coordinators/CustomLists/AddCustomListCoordinator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// AddCustomListCoordinator.swift | ||
// MullvadVPN | ||
// | ||
// Created by Jon Petersson on 2024-02-14. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import MullvadSettings | ||
import Routing | ||
import UIKit | ||
|
||
class AddCustomListCoordinator: Coordinator, Presentable, Presenting { | ||
let navigationController: UINavigationController | ||
let customListInteractor: CustomListInteractorProtocol | ||
|
||
var presentedViewController: UIViewController { | ||
navigationController | ||
} | ||
|
||
var didFinish: (() -> Void)? | ||
|
||
init( | ||
navigationController: UINavigationController, | ||
customListInteractor: CustomListInteractorProtocol | ||
) { | ||
self.navigationController = navigationController | ||
self.customListInteractor = customListInteractor | ||
} | ||
|
||
func start() { | ||
let subject = CurrentValueSubject<CustomListViewModel, Never>( | ||
CustomListViewModel(id: UUID(), name: "", locations: [], tableSections: [.name, .addLocations]) | ||
) | ||
|
||
let controller = CustomListViewController( | ||
interactor: customListInteractor, | ||
subject: subject, | ||
alertPresenter: AlertPresenter(context: self) | ||
) | ||
controller.delegate = self | ||
|
||
controller.navigationItem.title = NSLocalizedString( | ||
"CUSTOM_LIST_NAVIGATION_EDIT_TITLE", | ||
tableName: "CustomLists", | ||
value: "New custom list", | ||
comment: "" | ||
) | ||
|
||
controller.saveBarButton.title = NSLocalizedString( | ||
"CUSTOM_LIST_NAVIGATION_CREATE_BUTTON", | ||
tableName: "CustomLists", | ||
value: "Create", | ||
comment: "" | ||
) | ||
|
||
navigationController.pushViewController(controller, animated: false) | ||
} | ||
} | ||
|
||
extension AddCustomListCoordinator: CustomListViewControllerDelegate { | ||
func customListDidSave() { | ||
didFinish?() | ||
} | ||
|
||
func customListDidDelete() { | ||
// No op. | ||
} | ||
|
||
func showLocations() { | ||
// TODO: Show view controller for locations. | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
ios/MullvadVPN/Coordinators/CustomLists/CustomListCellConfiguration.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// CustomListCellConfiguration.swift | ||
// MullvadVPN | ||
// | ||
// Created by Jon Petersson on 2024-02-14. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import UIKit | ||
|
||
struct CustomListCellConfiguration { | ||
let tableView: UITableView | ||
let subject: CurrentValueSubject<CustomListViewModel, Never> | ||
|
||
var onDelete: (() -> Void)? | ||
|
||
func dequeueCell(at indexPath: IndexPath, for itemIdentifier: CustomListItemIdentifier) -> UITableViewCell { | ||
let cell = tableView.dequeueReusableView(withIdentifier: itemIdentifier.cellIdentifier, for: indexPath) | ||
|
||
configureBackground(cell: cell, itemIdentifier: itemIdentifier) | ||
|
||
switch itemIdentifier { | ||
case .name: | ||
configureName(cell, itemIdentifier: itemIdentifier) | ||
case .addLocations, .editLocations: | ||
configureLocations(cell, itemIdentifier: itemIdentifier) | ||
case .deleteList: | ||
configureDelete(cell, itemIdentifier: itemIdentifier) | ||
} | ||
|
||
return cell | ||
} | ||
|
||
private func configureBackground(cell: UITableViewCell, itemIdentifier: CustomListItemIdentifier) { | ||
guard let cell = cell as? DynamicBackgroundConfiguration else { return } | ||
cell.setAutoAdaptingBackgroundConfiguration(.mullvadListGroupedCell(), selectionType: .dimmed) | ||
} | ||
|
||
private func configureName(_ cell: UITableViewCell, itemIdentifier: CustomListItemIdentifier) { | ||
var contentConfiguration = TextCellContentConfiguration() | ||
|
||
contentConfiguration.text = itemIdentifier.text | ||
contentConfiguration.setPlaceholder(type: .required) | ||
contentConfiguration.textFieldProperties = .withSmartFeaturesDisabled() | ||
contentConfiguration.inputText = subject.value.name | ||
contentConfiguration.editingEvents.onChange = subject.bindTextAction(to: \.name) | ||
|
||
cell.contentConfiguration = contentConfiguration | ||
} | ||
|
||
private func configureLocations(_ cell: UITableViewCell, itemIdentifier: CustomListItemIdentifier) { | ||
var contentConfiguration = UIListContentConfiguration.mullvadValueCell(tableStyle: tableView.style) | ||
|
||
contentConfiguration.text = itemIdentifier.text | ||
cell.contentConfiguration = contentConfiguration | ||
|
||
if let cell = cell as? CustomCellDisclosureHandling { | ||
cell.disclosureType = .chevron | ||
} | ||
} | ||
|
||
private func configureDelete(_ cell: UITableViewCell, itemIdentifier: CustomListItemIdentifier) { | ||
var contentConfiguration = ButtonCellContentConfiguration() | ||
|
||
contentConfiguration.style = .tableInsetGroupedDanger | ||
contentConfiguration.text = itemIdentifier.text | ||
contentConfiguration.primaryAction = UIAction { _ in | ||
onDelete?() | ||
} | ||
|
||
cell.contentConfiguration = contentConfiguration | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
ios/MullvadVPN/Coordinators/CustomLists/CustomListDataSourceConfiguration.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// CustomListDataSourceConfigurationv.swift | ||
// MullvadVPN | ||
// | ||
// Created by Jon Petersson on 2024-02-14. | ||
// Copyright © 2024 Mullvad VPN AB. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
class CustomListDataSourceConfiguration: NSObject { | ||
let dataSource: UITableViewDiffableDataSource<CustomListSectionIdentifier, CustomListItemIdentifier> | ||
|
||
var didSelectItem: ((CustomListItemIdentifier) -> Void)? | ||
|
||
init(dataSource: UITableViewDiffableDataSource<CustomListSectionIdentifier, CustomListItemIdentifier>) { | ||
self.dataSource = dataSource | ||
} | ||
|
||
func updateDataSource( | ||
sections: [CustomListSectionIdentifier], | ||
animated: Bool, | ||
completion: (() -> Void)? = nil | ||
) { | ||
var snapshot = NSDiffableDataSourceSnapshot<CustomListSectionIdentifier, CustomListItemIdentifier>() | ||
|
||
sections.forEach { section in | ||
switch section { | ||
case .name: | ||
snapshot.appendSections([.name]) | ||
snapshot.appendItems([.name], toSection: .name) | ||
case .addLocations: | ||
snapshot.appendSections([.addLocations]) | ||
snapshot.appendItems([.addLocations], toSection: .addLocations) | ||
case .editLocations: | ||
snapshot.appendSections([.editLocations]) | ||
snapshot.appendItems([.editLocations], toSection: .editLocations) | ||
case .deleteList: | ||
snapshot.appendSections([.deleteList]) | ||
snapshot.appendItems([.deleteList], toSection: .deleteList) | ||
} | ||
} | ||
|
||
dataSource.apply(snapshot, animatingDifferences: animated) | ||
} | ||
} | ||
|
||
extension CustomListDataSourceConfiguration: UITableViewDelegate { | ||
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { | ||
UIMetrics.SettingsCell.customListsCellHeight | ||
} | ||
|
||
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | ||
tableView.deselectRow(at: indexPath, animated: false) | ||
|
||
dataSource.itemIdentifier(for: indexPath).flatMap { item in | ||
didSelectItem?(item) | ||
} | ||
} | ||
} |
Oops, something went wrong.