From c79725f80ac78c21a02d68086552fcb17a120828 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 26 Jul 2024 10:52:19 +0200 Subject: [PATCH] make SecuritySettingsController dumber and leave details to the delegate --- .../AccountSetup/AccountSetupController.swift | 38 +++++++++++-------- .../SecuritySettingsController.swift | 13 ++----- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/deltachat-ios/Controller/AccountSetup/AccountSetupController.swift b/deltachat-ios/Controller/AccountSetup/AccountSetupController.swift index e459ef95c..df2e14eb8 100644 --- a/deltachat-ios/Controller/AccountSetup/AccountSetupController.swift +++ b/deltachat-ios/Controller/AccountSetup/AccountSetupController.swift @@ -165,7 +165,7 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler { return cell }() - lazy var imapSecurityValue: Int = dcContext.getConfigInt("mail_security") + let imapSecurityValue: AccountSetupSecurityValue lazy var smtpServerCell: TextFieldCell = { let cell = TextFieldCell( @@ -231,7 +231,7 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler { return cell }() - lazy var smtpSecurityValue: Int = dcContext.getConfigInt("send_security") + let smtpSecurityValue: AccountSetupSecurityValue lazy var certCheckCell: UITableViewCell = { let certCheckType = CertificateCheckController.ValueConverter.convertHexToString(value: certValue) @@ -273,6 +273,9 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler { self.sections.append(basicSection) self.sections.append(advancedSection) + self.imapSecurityValue = AccountSetupSecurityValue(initValue: dcContext.getConfigInt("mail_security")) + self.smtpSecurityValue = AccountSetupSecurityValue(initValue: dcContext.getConfigInt("send_security")) + super.init(style: .grouped) hidesBottomBarWhenPushed = true } @@ -579,8 +582,8 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler { } } } - dcContext.setConfigInt("mail_security", imapSecurityValue) - dcContext.setConfigInt("send_security", smtpSecurityValue) + dcContext.setConfigInt("mail_security", imapSecurityValue.value) + dcContext.setConfigInt("send_security", smtpSecurityValue.value) dcContext.certificateChecks = certValue } @@ -598,8 +601,8 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler { } private func initSelectionCells() { - imapSecurityCell.detailTextLabel?.text = SecurityConverter.getSocketName(value: Int32(imapSecurityValue)) - smtpSecurityCell.detailTextLabel?.text = SecurityConverter.getSocketName(value: Int32(smtpSecurityValue)) + imapSecurityCell.detailTextLabel?.text = SecurityConverter.getSocketName(value: Int32(imapSecurityValue.value)) + smtpSecurityCell.detailTextLabel?.text = SecurityConverter.getSocketName(value: Int32(smtpSecurityValue.value)) certCheckCell.detailTextLabel?.text = CertificateCheckController.ValueConverter.convertHexToString(value: certValue) } @@ -676,14 +679,14 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler { } private func showImapSecurityOptions() { - let securitySettingsController = SecuritySettingsController(initValue: imapSecurityValue, title: String.localized("login_imap_security"), type: SecurityType.IMAPSecurity) - securitySettingsController.delegate = self + let securitySettingsController = SecuritySettingsController(initValue: imapSecurityValue.value, title: String.localized("login_imap_security")) + securitySettingsController.delegate = imapSecurityValue navigationController?.pushViewController(securitySettingsController, animated: true) } private func showSmtpSecurityOptions() { - let securitySettingsController = SecuritySettingsController(initValue: smtpSecurityValue, title: String.localized("login_smtp_security"), type: SecurityType.SMTPSecurity) - securitySettingsController.delegate = self + let securitySettingsController = SecuritySettingsController(initValue: smtpSecurityValue.value, title: String.localized("login_smtp_security")) + securitySettingsController.delegate = smtpSecurityValue navigationController?.pushViewController(securitySettingsController, animated: true) } @@ -731,11 +734,14 @@ extension AccountSetupController: CertificateCheckDelegate { } } -extension AccountSetupController: SecuritySettingsDelegate { - func onSecuritySettingsChanged(type: SecurityType, newValue: Int) { - switch type { - case .IMAPSecurity: imapSecurityValue = newValue - case .SMTPSecurity: smtpSecurityValue = newValue - } +class AccountSetupSecurityValue: SecuritySettingsDelegate { + var value: Int + + init(initValue: Int) { + value = initValue + } + + func onSecuritySettingsChanged(newValue: Int) { + value = newValue } } diff --git a/deltachat-ios/Controller/AccountSetup/SecuritySettingsController.swift b/deltachat-ios/Controller/AccountSetup/SecuritySettingsController.swift index 21f3f6503..8d4c301a1 100644 --- a/deltachat-ios/Controller/AccountSetup/SecuritySettingsController.swift +++ b/deltachat-ios/Controller/AccountSetup/SecuritySettingsController.swift @@ -2,7 +2,7 @@ import UIKit import DcCore protocol SecuritySettingsDelegate: AnyObject { - func onSecuritySettingsChanged(type: SecurityType, newValue: Int) + func onSecuritySettingsChanged(newValue: Int) } class SecuritySettingsController: UITableViewController { @@ -10,7 +10,6 @@ class SecuritySettingsController: UITableViewController { private var options: [Int32] = [DC_SOCKET_AUTO, DC_SOCKET_SSL, DC_SOCKET_STARTTLS, DC_SOCKET_PLAIN] private var selectedIndex: Int - private var securityType: SecurityType weak var delegate: SecuritySettingsDelegate? private var okButton: UIBarButtonItem { @@ -31,8 +30,7 @@ class SecuritySettingsController: UITableViewController { } } - init(initValue: Int, title: String, type: SecurityType) { - self.securityType = type + init(initValue: Int, title: String) { selectedIndex = options.firstIndex(of: Int32(initValue)) ?? 0 super.init(style: .grouped) self.title = title @@ -76,7 +74,7 @@ class SecuritySettingsController: UITableViewController { } @objc func okButtonPressed() { - delegate?.onSecuritySettingsChanged(type: securityType, newValue: Int(options[selectedIndex])) + delegate?.onSecuritySettingsChanged(newValue: Int(options[selectedIndex])) navigationController?.popViewController(animated: true) } @@ -85,11 +83,6 @@ class SecuritySettingsController: UITableViewController { } } -enum SecurityType { - case IMAPSecurity - case SMTPSecurity -} - class SecurityConverter { static func getSocketName(value: Int32) -> String { switch value {