From 94ad86cd666f5a978edb04f8be0115e1b21e439b Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Mon, 8 Jul 2024 12:13:29 +0200 Subject: [PATCH] Dependencyinject ViewModel based on downloaded policy-data (IOS-241) --- .../NotificationPolicyViewController.swift | 25 +++++++++++++------ .../NotificationViewController.swift | 5 +++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Mastodon/Scene/Notification/Notification Filtering/NotificationPolicyViewController.swift b/Mastodon/Scene/Notification/Notification Filtering/NotificationPolicyViewController.swift index 50645897b..d9e2b8761 100644 --- a/Mastodon/Scene/Notification/Notification Filtering/NotificationPolicyViewController.swift +++ b/Mastodon/Scene/Notification/Notification Filtering/NotificationPolicyViewController.swift @@ -60,12 +60,10 @@ class NotificationPolicyViewController: UIViewController { let tableView: UITableView var dataSource: UITableViewDiffableDataSource? let items: [NotificationFilterItem] - let viewModel: NotificationFilterViewModel + var viewModel: NotificationFilterViewModel - - init() { - //TODO: Dependency Inject Policy ViewModel - viewModel = NotificationFilterViewModel(notFollowing: false, noFollower: false, newAccount: false, privateMentions: false) + init(viewModel: NotificationFilterViewModel) { + self.viewModel = viewModel items = NotificationFilterItem.allCases tableView = UITableView(frame: .zero, style: .insetGrouped) @@ -128,7 +126,7 @@ class NotificationPolicyViewController: UIViewController { // MARK: - Action @objc private func save(_ sender: UIBarButtonItem) { - //TODO: Save + //TODO: Save aka PATH viewModel to API and dismiss } @objc private func cancel(_ sender: UIBarButtonItem) { @@ -137,11 +135,22 @@ class NotificationPolicyViewController: UIViewController { } extension NotificationPolicyViewController: UITableViewDelegate { - + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + tableView.deselectRow(at: indexPath, animated: true) + } } extension NotificationPolicyViewController: NotificationPolicyFilterTableViewCellDelegate { func toggleValueChanged(_ tableViewCell: NotificationPolicyFilterTableViewCell, filterItem: NotificationFilterItem, newValue: Bool) { - //TODO: Update ViewModel + switch filterItem { + case .notFollowing: + viewModel.notFollowing = newValue + case .noFollower: + viewModel.noFollower = newValue + case .newAccount: + viewModel.newAccount = newValue + case .privateMentions: + viewModel.privateMentions = newValue + } } } diff --git a/Mastodon/Scene/Notification/NotificationViewController.swift b/Mastodon/Scene/Notification/NotificationViewController.swift index bcfeaf01e..12dd976fc 100644 --- a/Mastodon/Scene/Notification/NotificationViewController.swift +++ b/Mastodon/Scene/Notification/NotificationViewController.swift @@ -118,8 +118,11 @@ extension NotificationViewController { //MARK: - Actions @objc private func showNotificationPolicySettings(_ sender: Any) { + guard let policy = viewModel?.notificationPolicy else { return } + + let policyViewModel = NotificationFilterViewModel(notFollowing: policy.filterNotFollowing, noFollower: policy.filterNotFollowers, newAccount: policy.filterNewAccounts, privateMentions: policy.filterPrivateMentions) //TODO: Move to SceneCoordinator - let notificationPolicyViewController = NotificationPolicyViewController() + let notificationPolicyViewController = NotificationPolicyViewController(viewModel: policyViewModel) notificationPolicyViewController.modalPresentationStyle = .formSheet let navigationController = UINavigationController(rootViewController: notificationPolicyViewController)