From d84ef8c47c5bebf92ad2064e9d2a260a9275ab70 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Sun, 8 Dec 2024 00:37:36 +0100 Subject: [PATCH] move notification toggle to ViewController --- deltachat-ios.xcodeproj/project.pbxproj | 4 + .../NotificationsViewController.swift | 89 +++++++++++++++++++ .../Settings/SettingsViewController.swift | 33 ++----- 3 files changed, 100 insertions(+), 26 deletions(-) create mode 100644 deltachat-ios/Controller/Settings/NotificationsViewController.swift diff --git a/deltachat-ios.xcodeproj/project.pbxproj b/deltachat-ios.xcodeproj/project.pbxproj index 666ea440e..ca3cbcc23 100644 --- a/deltachat-ios.xcodeproj/project.pbxproj +++ b/deltachat-ios.xcodeproj/project.pbxproj @@ -196,6 +196,7 @@ B20462E42440A4A600367A57 /* AutodelOverviewViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B20462E32440A4A600367A57 /* AutodelOverviewViewController.swift */; }; B20462E62440C99600367A57 /* AutodelOptionsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B20462E52440C99600367A57 /* AutodelOptionsViewController.swift */; }; B206C2AB2B7B8088003ACBE6 /* MapViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B206C2AA2B7B8088003ACBE6 /* MapViewController.swift */; }; + B20A907B2D05112800F44463 /* NotificationsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B20A907A2D05112700F44463 /* NotificationsViewController.swift */; }; B21005DB23383664004C70C5 /* EmailOptionsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B21005DA23383664004C70C5 /* EmailOptionsViewController.swift */; }; B2172F3C29C125F2002C289E /* AdvancedViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2172F3B29C125F2002C289E /* AdvancedViewController.swift */; }; B259D64329B771D5008FB706 /* BackupTransferViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B259D64229B771D5008FB706 /* BackupTransferViewController.swift */; }; @@ -516,6 +517,7 @@ B209B2042B10139700FBBECF /* vi */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/InfoPlist.strings; sourceTree = ""; }; B209B2052B10139700FBBECF /* vi */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = vi; path = vi.lproj/Localizable.strings; sourceTree = ""; }; B209B2062B10139700FBBECF /* vi */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = vi; path = vi.lproj/Localizable.stringsdict; sourceTree = ""; }; + B20A907A2D05112700F44463 /* NotificationsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationsViewController.swift; sourceTree = ""; }; B21005DA23383664004C70C5 /* EmailOptionsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EmailOptionsViewController.swift; sourceTree = ""; }; B2172F3B29C125F2002C289E /* AdvancedViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AdvancedViewController.swift; sourceTree = ""; }; B2537DD625E2F92F0010D739 /* ckb */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ckb; path = ckb.lproj/InfoPlist.strings; sourceTree = ""; }; @@ -1106,6 +1108,7 @@ D8CF2DDB2CDD110F001C2352 /* Proxy */, 78E45E3921D3CFBC00D4B15E /* SettingsViewController.swift */, B2D4B63A29C38D1900B47DA8 /* ChatsAndMediaViewController.swift */, + B20A907A2D05112700F44463 /* NotificationsViewController.swift */, B2172F3B29C125F2002C289E /* AdvancedViewController.swift */, AEE6EC472283045D00EDC689 /* SelfProfileViewController.swift */, B21005DA23383664004C70C5 /* EmailOptionsViewController.swift */, @@ -1699,6 +1702,7 @@ 70B8882E2091B8550074812E /* ContactCell.swift in Sources */, 305961CD2346125100C80F33 /* UIEdgeInsets+Extensions.swift in Sources */, 30EF7324252FF15F00E2C54A /* MessageLabel.swift in Sources */, + B20A907B2D05112800F44463 /* NotificationsViewController.swift in Sources */, 30C0D49D237C4908008E2A0E /* CertificateCheckController.swift in Sources */, 3080A034277DE30100E74565 /* NSNotification+Extensions.swift in Sources */, 3080A01B277DDB8A00E74565 /* InputPlugin.swift in Sources */, diff --git a/deltachat-ios/Controller/Settings/NotificationsViewController.swift b/deltachat-ios/Controller/Settings/NotificationsViewController.swift new file mode 100644 index 000000000..aba1fb835 --- /dev/null +++ b/deltachat-ios/Controller/Settings/NotificationsViewController.swift @@ -0,0 +1,89 @@ +import UIKit +import DcCore +import Intents + +internal final class NotificationsViewController: UITableViewController { + + private struct SectionConfigs { + let headerTitle: String? + let footerTitle: String? + let cells: [UITableViewCell] + } + + private enum CellTags: Int { + case notifications + } + + private var dcContext: DcContext + internal let dcAccounts: DcAccounts + + // MARK: - cells + private lazy var notificationsCell: SwitchCell = { + return SwitchCell( + textLabel: String.localized("pref_notifications"), + on: !dcContext.isMuted(), + action: { [weak self] cell in + guard let self else { return } + + dcContext.setMuted(!cell.isOn) + if cell.isOn { + if let appDelegate = UIApplication.shared.delegate as? AppDelegate { + appDelegate.registerForNotifications() + } + } else { + NotificationManager.removeAllNotifications() + } + + NotificationManager.updateBadgeCounters() + NotificationCenter.default.post(name: Event.messagesChanged, object: nil, userInfo: ["message_id": Int(0), "chat_id": Int(0)]) + }) + }() + + private lazy var sections: [SectionConfigs] = { + let preferencesSection = SectionConfigs( + headerTitle: nil, + footerTitle: nil, + cells: [notificationsCell] + ) + return [preferencesSection] + }() + + init(dcAccounts: DcAccounts) { + self.dcContext = dcAccounts.getSelected() + self.dcAccounts = dcAccounts + super.init(style: .grouped) + hidesBottomBarWhenPushed = true + } + + required init?(coder _: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + // MARK: - lifecycle + override func viewDidLoad() { + super.viewDidLoad() + title = String.localized("pref_notifications") + tableView.rowHeight = UITableView.automaticDimension + } + + // MARK: - UITableViewDelegate + UITableViewDatasource + override func numberOfSections(in tableView: UITableView) -> Int { + return sections.count + } + + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return sections[section].cells.count + } + + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + return sections[indexPath.section].cells[indexPath.row] + } + + override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { + return sections[section].headerTitle + } + + override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { + return sections[section].footerTitle + } +} diff --git a/deltachat-ios/Controller/Settings/SettingsViewController.swift b/deltachat-ios/Controller/Settings/SettingsViewController.swift index 490468781..b1af4bac9 100644 --- a/deltachat-ios/Controller/Settings/SettingsViewController.swift +++ b/deltachat-ios/Controller/Settings/SettingsViewController.swift @@ -52,22 +52,14 @@ internal final class SettingsViewController: UITableViewController { return cell }() - private lazy var notificationSwitch: UISwitch = { - let switchControl = UISwitch() - switchControl.isOn = !dcContext.isMuted() - switchControl.addTarget(self, action: #selector(handleNotificationToggle(_:)), for: .valueChanged) - return switchControl - }() - private lazy var notificationCell: UITableViewCell = { - let cell = UITableViewCell(style: .default, reuseIdentifier: nil) + let cell = UITableViewCell(style: .value1, reuseIdentifier: nil) cell.tag = CellTags.notifications.rawValue cell.textLabel?.text = String.localized("pref_notifications") if #available(iOS 16.0, *) { cell.imageView?.image = UIImage(systemName: "bell") } - cell.accessoryView = notificationSwitch - cell.selectionStyle = .none + cell.accessoryType = .disclosureIndicator return cell }() @@ -220,7 +212,7 @@ internal final class SettingsViewController: UITableViewController { case .profile: showEditSettingsController() case .chatsAndMedia: showChatsAndMedia() case .addAnotherDevice: showBackupProviderViewController() - case .notifications: break + case .notifications: showNotificationsViewController() case .advanced: showAdvanced() case .help: showHelp() case .connectivity: showConnectivity() @@ -248,21 +240,6 @@ internal final class SettingsViewController: UITableViewController { } } - // MARK: - actions - @objc private func handleNotificationToggle(_ sender: UISwitch) { - dcContext.setMuted(!sender.isOn) - if sender.isOn { - if let appDelegate = UIApplication.shared.delegate as? AppDelegate { - appDelegate.registerForNotifications() - } - } else { - NotificationManager.removeAllNotifications() - } - - NotificationManager.updateBadgeCounters() - NotificationCenter.default.post(name: Event.messagesChanged, object: nil, userInfo: ["message_id": Int(0), "chat_id": Int(0)]) - } - // MARK: - updates private func updateCells() { profileCell.updateCell(cellViewModel: ProfileViewModel(context: dcContext)) @@ -280,6 +257,10 @@ internal final class SettingsViewController: UITableViewController { navigationController?.pushViewController(ChatsAndMediaViewController(dcAccounts: dcAccounts), animated: true) } + private func showNotificationsViewController() { + navigationController?.pushViewController(NotificationsViewController(dcAccounts: dcAccounts), animated: true) + } + private func showBackupProviderViewController() { let alert = UIAlertController(title: String.localized("multidevice_title"), message: String.localized("multidevice_this_creates_a_qr_code"), preferredStyle: .alert) alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))