Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use 'Cancel' instead of 'Back arrow' for server settings #2247

Merged
merged 9 commits into from
Aug 1, 2024
88 changes: 52 additions & 36 deletions deltachat-ios/Controller/AccountSetup/AccountSetupController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
return cell
}()

let imapSecurityValue: AccountSetupSecurityValue

lazy var smtpServerCell: TextFieldCell = {
let cell = TextFieldCell(
descriptionID: "login_smtp_server",
Expand Down Expand Up @@ -229,8 +231,10 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
return cell
}()

let smtpSecurityValue: AccountSetupSecurityValue

lazy var certCheckCell: UITableViewCell = {
let certCheckType = CertificateCheckController.ValueConverter.convertHexToString(value: dcContext.certificateChecks)
let certCheckType = CertificateCheckController.ValueConverter.convertHexToString(value: certValue)
let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
cell.textLabel?.text = String.localized("login_certificate_checks")
cell.detailTextLabel?.text = certCheckType
Expand All @@ -239,6 +243,8 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
return cell
}()

lazy var certValue: Int = dcContext.certificateChecks

lazy var viewLogCell: UITableViewCell = {
let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
cell.textLabel?.text = String.localized("pref_view_log")
Expand All @@ -247,12 +253,13 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
return cell
}()

private var cancelButton: UIBarButtonItem {
let button = UIBarButtonItem(title: String.localized("cancel"), style: .plain, target: self, action: #selector(cancelButtonPressed))
return button
}

private lazy var loginButton: UIBarButtonItem = {
let button = UIBarButtonItem(
title: String.localized("login_title"),
style: .done,
target: self,
action: #selector(loginButtonPressed))
let button = UIBarButtonItem(title: String.localized("login_title"), style: .done, target: self, action: #selector(loginButtonPressed))
button.isEnabled = !dcContext.isConfigured()
return button
}()
Expand All @@ -266,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
}
Expand All @@ -282,6 +292,7 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
} else {
title = String.localized("login_header")
}
navigationItem.leftBarButtonItem = cancelButton
navigationItem.rightBarButtonItem = loginButton
emailCell.setText(text: dcContext.addr ?? nil)
passwordCell.setText(text: dcContext.mailPw ?? nil)
Expand Down Expand Up @@ -333,23 +344,7 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
if sections[section] == basicSection {
return String.localized("login_no_servers_hint")
} else if sections[section] == advancedSection {
if advancedSectionShowing && dcContext.isConfigured() {
var info = String.localized("used_settings") + "\n"
info += "IMAP "
info += SecurityConverter.getSocketName(value: Int32(dcContext.getConfigInt("mail_security"))) + " "
info += (dcContext.getConfig("configured_mail_user") ?? "unset") + ":***@"
info += (dcContext.getConfig("configured_mail_server") ?? "unset") + ":"
info += (dcContext.getConfig("configured_mail_port") ?? "unset") + "\n"
info += "SMTP "
info += SecurityConverter.getSocketName(value: Int32(dcContext.getConfigInt("send_security"))) + " "
info += (dcContext.getConfig("configured_send_user") ?? "unset") + ":***@"
info += (dcContext.getConfig("configured_send_server") ?? "unset") + ":"
info += (dcContext.getConfig("configured_send_port") ?? "unset") + "\n\n"
info += String.localized("login_subheader")
return info
} else {
return String.localized("login_subheader")
}
return String.localized("login_subheader")
} else {
return nil
}
Expand Down Expand Up @@ -414,6 +409,10 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
tableView.reloadData() // needed to force a redraw
}

@objc private func cancelButtonPressed() {
navigationController?.popViewController(animated: true)
}

@objc private func loginButtonPressed() {
guard let emailAddress = emailCell.getText() else {
return // handle case when either email or pw fields are empty
Expand Down Expand Up @@ -501,10 +500,6 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
showProgressAlert(title: String.localized("login_header"), dcContext: dcContext)
}

@objc func closeButtonPressed() {
dismiss(animated: true, completion: nil)
}

// returns true if needed
private func showOAuthAlertIfNeeded(emailAddress: String, handleCancel: (() -> Void)?) -> Bool {
return false
Expand Down Expand Up @@ -587,6 +582,9 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
}
}
}
dcContext.setConfigInt("mail_security", imapSecurityValue.value)
dcContext.setConfigInt("send_security", smtpSecurityValue.value)
dcContext.certificateChecks = certValue
}

private func handleLoginSuccess() {
Expand All @@ -603,9 +601,9 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
}

private func initSelectionCells() {
imapSecurityCell.detailTextLabel?.text = SecurityConverter.getSocketName(value: Int32(dcContext.getConfigInt("mail_security")))
smtpSecurityCell.detailTextLabel?.text = SecurityConverter.getSocketName(value: Int32(dcContext.getConfigInt("send_security")))
certCheckCell.detailTextLabel?.text = CertificateCheckController.ValueConverter.convertHexToString(value: dcContext.certificateChecks)
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)
}

private func resignFirstResponderOnAllCells() {
Expand Down Expand Up @@ -675,20 +673,20 @@ class AccountSetupController: UITableViewController, ProgressAlertHandler {
}

private func showCertCheckOptions() {
let certificateCheckController = CertificateCheckController(dcContext: dcContext, sectionTitle: String.localized("login_certificate_checks"))
let certificateCheckController = CertificateCheckController(initValue: certValue, sectionTitle: String.localized("login_certificate_checks"))
certificateCheckController.delegate = self
navigationController?.pushViewController(certificateCheckController, animated: true)
}

private func showImapSecurityOptions() {
let securitySettingsController = SecuritySettingsController(dcContext: dcContext, title: String.localized("login_imap_security"),
type: SecurityType.IMAPSecurity)
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(dcContext: dcContext,
title: String.localized("login_smtp_security"),
type: SecurityType.SMTPSecurity)
let securitySettingsController = SecuritySettingsController(initValue: smtpSecurityValue.value, title: String.localized("login_smtp_security"))
securitySettingsController.delegate = smtpSecurityValue
navigationController?.pushViewController(securitySettingsController, animated: true)
}

Expand Down Expand Up @@ -729,3 +727,21 @@ extension AccountSetupController: UITextFieldDelegate {
}
}
}

extension AccountSetupController: CertificateCheckDelegate {
func onCertificateCheckChanged(newValue: Int) {
certValue = newValue
}
}

class AccountSetupSecurityValue: SecuritySettingsDelegate {
var value: Int

init(initValue: Int) {
value = initValue
}

func onSecuritySettingsChanged(newValue: Int) {
value = newValue
}
}
Comment on lines +737 to +747
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason this implementation of SecuritySettingsDelegate is a dedicated class, while CertificateCheckDelegate is implemented as extension of AccountSetupController?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need two different delegates for SecuritySettingsDelegate - i did not find out a smart way to do that as an extension. one could add some state to the delegate - but that seems to work against the idea of the delegate

Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import UIKit
import DcCore

protocol CertificateCheckDelegate: AnyObject {
func onCertificateCheckChanged(newValue: Int)
}

class CertificateCheckController: UITableViewController {

var options = [Int(DC_CERTCK_AUTO),
Expand All @@ -9,17 +13,7 @@ class CertificateCheckController: UITableViewController {

var currentValue: Int
var selectedIndex: Int?
let dcContext: DcContext

var okButton: UIBarButtonItem {
let button = UIBarButtonItem(title: String.localized("ok"), style: .done, target: self, action: #selector(okButtonPressed))
return button
}

var cancelButton: UIBarButtonItem {
let button = UIBarButtonItem(title: String.localized("cancel"), style: .plain, target: self, action: #selector(cancelButtonPressed))
return button
}
weak var delegate: CertificateCheckDelegate?

var staticCells: [UITableViewCell] {
return options.map({
Expand All @@ -29,9 +23,8 @@ class CertificateCheckController: UITableViewController {
})
}

init(dcContext: DcContext, sectionTitle: String?) {
self.dcContext = dcContext
self.currentValue = dcContext.certificateChecks
init(initValue: Int, sectionTitle: String?) {
self.currentValue = initValue
for (index, value) in options.enumerated() where currentValue == value {
selectedIndex = index
}
Expand All @@ -43,13 +36,6 @@ class CertificateCheckController: UITableViewController {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()

navigationItem.rightBarButtonItem = okButton
navigationItem.leftBarButtonItem = cancelButton
}

// MARK: - Table view data source

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
Expand Down Expand Up @@ -86,18 +72,10 @@ class CertificateCheckController: UITableViewController {
currentValue = options[newIndex]
}
selectedIndex = index
delegate?.onCertificateCheckChanged(newValue: currentValue)
}

@objc private func okButtonPressed() {
dcContext.certificateChecks = currentValue
navigationController?.popViewController(animated: true)
}

@objc private func cancelButtonPressed() {
navigationController?.popViewController(animated: true)
}

class ValueConverter {
class ValueConverter {
static func convertHexToString(value: Int) -> String {
switch value {
case Int(DC_CERTCK_AUTO):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import UIKit
import DcCore

protocol SecuritySettingsDelegate: AnyObject {
func onSecuritySettingsChanged(newValue: Int)
}

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
private let dcContext: DcContext

private var okButton: UIBarButtonItem {
let button = UIBarButtonItem(title: String.localized("ok"), style: .done, target: self, action: #selector(okButtonPressed))
return button
}

private var cancelButton: UIBarButtonItem {
let button = UIBarButtonItem(title: String.localized("cancel"), style: .plain, target: self, action: #selector(cancelButtonPressed))
return button
}
weak var delegate: SecuritySettingsDelegate?

private var staticCells: [UITableViewCell] {
return options.map {
Expand All @@ -28,15 +20,8 @@ class SecuritySettingsController: UITableViewController {
}
}

init(dcContext: DcContext, title: String, type: SecurityType) {
self.securityType = type
self.dcContext = dcContext
switch securityType {
case .IMAPSecurity:
selectedIndex = options.firstIndex(of: Int32(dcContext.getConfigInt("mail_security"))) ?? 0
case .SMTPSecurity:
selectedIndex = options.firstIndex(of: Int32(dcContext.getConfigInt("send_security"))) ?? 0
}
init(initValue: Int, title: String) {
selectedIndex = options.firstIndex(of: Int32(initValue)) ?? 0
super.init(style: .grouped)
self.title = title
}
Expand All @@ -45,12 +30,6 @@ class SecuritySettingsController: UITableViewController {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = okButton
navigationItem.leftBarButtonItem = cancelButton
}

// MARK: - Table view data source

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
Expand All @@ -76,26 +55,8 @@ class SecuritySettingsController: UITableViewController {
cell.accessoryType = .checkmark
}
selectedIndex = indexPath.row
delegate?.onSecuritySettingsChanged(newValue: Int(options[selectedIndex]))
}

@objc func okButtonPressed() {
switch securityType {
case .IMAPSecurity:
dcContext.setConfigInt("mail_security", Int(options[selectedIndex]))
case .SMTPSecurity:
dcContext.setConfigInt("send_security", Int(options[selectedIndex]))
}
navigationController?.popViewController(animated: true)
}

@objc func cancelButtonPressed() {
navigationController?.popViewController(animated: true)
}
}

enum SecurityType {
case IMAPSecurity
case SMTPSecurity
}

class SecurityConverter {
Expand Down