Explorar el Código

implemented PortsettingsController - used for ImapPort

Bastian van de Wetering hace 6 años
padre
commit
e6801e5cb2

+ 12 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -87,6 +87,7 @@
 		7AE0A5491FC42F65005ECB4B /* NewChatViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7AE0A5481FC42F65005ECB4B /* NewChatViewController.swift */; };
 		8B6D425BC604F7C43B65D436 /* Pods_deltachat_ios.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6241BE1534A653E79AD5D01D /* Pods_deltachat_ios.framework */; };
 		AE0D26FD1FB1FE88002FAFCE /* ChatListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE0D26FC1FB1FE88002FAFCE /* ChatListController.swift */; };
+		AE18F292228C17BC0007B1BE /* PortSettingsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE18F291228C17BC0007B1BE /* PortSettingsController.swift */; };
 		AE25F09022807AD800CDEA66 /* GroupNameCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE25F08F22807AD800CDEA66 /* GroupNameCell.swift */; };
 		AE38B31822672DFC00EC37A1 /* ActionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE38B31722672DFC00EC37A1 /* ActionCell.swift */; };
 		AE38B31A2267328200EC37A1 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE38B3192267328200EC37A1 /* Colors.swift */; };
@@ -281,6 +282,7 @@
 		8DE110C607A0E4485C43B5FA /* Pods-deltachat-ios.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-deltachat-ios.debug.xcconfig"; path = "Pods/Target Support Files/Pods-deltachat-ios/Pods-deltachat-ios.debug.xcconfig"; sourceTree = "<group>"; };
 		A8615D4600859851E53CAA9C /* Pods-deltachat-ios.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-deltachat-ios.release.xcconfig"; path = "Pods/Target Support Files/Pods-deltachat-ios/Pods-deltachat-ios.release.xcconfig"; sourceTree = "<group>"; };
 		AE0D26FC1FB1FE88002FAFCE /* ChatListController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatListController.swift; sourceTree = "<group>"; };
+		AE18F291228C17BC0007B1BE /* PortSettingsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PortSettingsController.swift; sourceTree = "<group>"; };
 		AE25F08F22807AD800CDEA66 /* GroupNameCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupNameCell.swift; sourceTree = "<group>"; };
 		AE38B31722672DFC00EC37A1 /* ActionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionCell.swift; sourceTree = "<group>"; };
 		AE38B3192267328200EC37A1 /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = "<group>"; };
@@ -567,6 +569,14 @@
 			name = Pods;
 			sourceTree = "<group>";
 		};
+		AE18F28B228C17630007B1BE /* AccountSetup */ = {
+			isa = PBXGroup;
+			children = (
+				AE18F291228C17BC0007B1BE /* PortSettingsController.swift */,
+			);
+			path = AccountSetup;
+			sourceTree = "<group>";
+		};
 		AE851A02227AECDE00ED86F0 /* deltachat-iosTests */ = {
 			isa = PBXGroup;
 			children = (
@@ -589,6 +599,7 @@
 		AE851AC0227C693B00ED86F0 /* Controller */ = {
 			isa = PBXGroup;
 			children = (
+				AE18F28B228C17630007B1BE /* AccountSetup */,
 				78E45E3D21D3D28C00D4B15E /* NavigationController.swift */,
 				785BE16721E247F1003BE98C /* MessageInfoViewController.swift */,
 				7A0052C71FBE6CB40048C3BF /* NewContactController.swift */,
@@ -1014,6 +1025,7 @@
 				7A451D941FB1B1DB00177250 /* wrapper.c in Sources */,
 				7092474120B3869500AF8799 /* ContactDetailViewController.swift in Sources */,
 				7070FB5E20FF345F000DC258 /* dc_token.c in Sources */,
+				AE18F292228C17BC0007B1BE /* PortSettingsController.swift in Sources */,
 				AE851AD0227DF50900ED86F0 /* SingleChatDetailViewController.swift in Sources */,
 				7070FB6C20FF345F000DC258 /* dc_keyring.c in Sources */,
 				7A451DB01FB1F84900177250 /* AppCoordinator.swift in Sources */,

+ 165 - 0
deltachat-ios/Controller/AccountSetup/PortSettingsController.swift

@@ -0,0 +1,165 @@
+//
+//  PortSettingsController.swift
+//  deltachat-ios
+//
+//  Created by Bastian van de Wetering on 15.05.19.
+//  Copyright © 2019 Jonas Reinsch. All rights reserved.
+//
+
+import UIKit
+
+class PortSettingsController: UITableViewController {
+
+	var ports: [Int]
+
+	private var sectionTitle: String?
+
+	var resetButton: UIBarButtonItem!
+
+	var currentPort: Int {
+		didSet {
+			// activate resetButton
+			resetButton.isEnabled = true
+		}
+	}
+
+	var selectedIndex: Int?
+
+	let backupValue: Int
+
+	var staticCells: [UITableViewCell] {
+		return ports.map({
+			let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
+			cell.textLabel?.text = "\($0)"
+			cell.selectionStyle = .none
+			return cell
+		})
+	}
+
+	lazy var customCell: TextFieldCell = {
+		let cell = TextFieldCell(description: "", placeholder: "\(self.currentPort)")
+		cell.selectionStyle = .none
+		cell.textLabel?.text = nil
+		cell.textField.keyboardType = .numberPad
+		cell.onTextFieldChange = textFieldDidChange
+		return cell
+	}()
+
+	init(sectionTitle: String?, ports: [Int], currentPort: Int?) {
+		self.ports = ports
+		self.sectionTitle = sectionTitle
+		self.currentPort = currentPort ?? 0
+		for (index, port) in ports.enumerated() where currentPort == port {
+			selectedIndex = index
+		}
+		backupValue = self.currentPort
+		super.init(style: .grouped)
+		self.title = sectionTitle
+	}
+
+	required init?(coder aDecoder: NSCoder) {
+		fatalError("init(coder:) has not been implemented")
+	}
+
+	override func viewDidLoad() {
+		super.viewDidLoad()
+
+		resetButton = UIBarButtonItem(title: "Reset", style: .plain, target: self, action: #selector(resetButtonPressed))
+		navigationItem.rightBarButtonItem = resetButton
+		resetButton.isEnabled = false
+	}
+
+	// MARK: - Table view data source
+
+	override func numberOfSections(in tableView: UITableView) -> Int {
+		// #warning Incomplete implementation, return the number of sections
+		return 2
+	}
+
+	override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
+		if section == 0 {
+			return ports.count
+		} else {
+			return 1
+		}
+	}
+
+	override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+		if indexPath.section == 0 {
+			let row = indexPath.row
+			selectItem(at: row)
+		}
+	}
+
+	override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
+
+		let section = indexPath.section
+		let row = indexPath.row
+		if section == 0 {
+			let cell = staticCells[row]
+			if row == selectedIndex || cell.textLabel?.text == "\(currentPort)" {
+				cell.accessoryType = .checkmark
+			} else {
+				cell.accessoryType = .none
+			}
+			return cell
+		} else {
+			// section == 1
+			return customCell
+		}
+	}
+
+	override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
+		if section == 0 {
+			return sectionTitle
+		} else {
+			return "Custom Port"
+		}
+	}
+
+	private func textFieldDidChange(_ textField: UITextField) {
+		selectItem(at: nil)
+		if let text = textField.text, let port = Int(text) {
+			self.currentPort = port
+		}
+	}
+
+	private func selectItem(at index: Int? ) {
+		// unselect old cell
+		// select new cell
+		// update port
+		if let oldIndex = selectedIndex {
+			let cell = tableView.cellForRow(at: IndexPath.init(row: oldIndex, section: 0))
+			cell?.accessoryType = .none
+		}
+		if let newIndex = index {
+			// activate accesoryType on selected cell
+			let cell = tableView.cellForRow(at: IndexPath.init(row: newIndex, section: 0))
+			cell?.accessoryType = .checkmark
+			currentPort = ports[newIndex]
+			// update customCell
+			customCell.textField.placeholder = "\(currentPort)"
+			customCell.textField.resignFirstResponder()
+			customCell.textField.text = nil // will display currentValue as placeholder
+		}
+		selectedIndex = index
+	}
+
+	@objc private func resetButtonPressed() {
+
+		if let index = ports.index(of: backupValue) {
+			selectItem(at: index)
+		} else {
+			selectItem(at: nil)
+		}
+		self.currentPort = backupValue
+		customCell.textField.placeholder = "\(currentPort)"
+		customCell.textField.resignFirstResponder()
+		customCell.textField.text = nil // will display currentValue as placeholder
+
+		tableView.reloadData()
+	}
+
+}
+
+

+ 13 - 1
deltachat-ios/Controller/AccountSetupController.swift

@@ -249,7 +249,19 @@ class AccountSetupController: UITableViewController {
 
     if tappedCell.accessibilityIdentifier == "restoreCell" {
       restoreBackup()
-    }
+		} else if tappedCell.accessibilityIdentifier == "IMAPPortCell" {
+			coordinator?.showImapPortOptions()
+		} else if tappedCell.accessibilityIdentifier == "SMTPPortCell" {
+			coordinator?.showSmtpPortsOptions()
+		} else if tappedCell.accessibilityIdentifier == "IMAPSecurity" {
+			coordinator?.showImapSecurityOptions()
+		} else if tappedCell.accessibilityIdentifier == "SMTPSecurity" {
+			coordinator?.showSmptpSecurityOptions()
+		}
+
+
+
+
   }
 
   private func toggleAdvancedSection(button: UILabel) {

+ 8 - 2
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -209,7 +209,10 @@ class AccountSetupCoordinator: Coordinator {
 	}
 
 	func showImapPortOptions() {
-
+		let currentMailPort = MRConfig.mailPort ?? MRConfig.configuredMailPort
+		let currentPort = Int(currentMailPort)
+		let portSettingsController = PortSettingsController(sectionTitle: "IMAP Port", ports: [143, 993], currentPort: currentPort)
+		navigationController.pushViewController(portSettingsController, animated: true)
 	}
 
 	func showImapSecurityOptions() {
@@ -217,7 +220,10 @@ class AccountSetupCoordinator: Coordinator {
 	}
 
 	func showSmtpPortsOptions() {
-
+		let currentMailPort = MRConfig.sendPort ?? MRConfig.configuredSendPort
+		let currentPort = Int(currentMailPort)
+		let portSettingsController = PortSettingsController(sectionTitle: "SMTP Port", ports: [25, 465, 587], currentPort: currentPort)
+		navigationController.pushViewController(portSettingsController, animated: true)
 	}
 
 	func showSmptpSecurityOptions() {

+ 7 - 0
deltachat-ios/View/TextFieldCell.swift

@@ -11,11 +11,14 @@ import UIKit
 class TextFieldCell: UITableViewCell {
   private let placeholder: String
 
+	var onTextFieldChange:((_:UITextField)->Void)?	// set this from outside to get notified about textfield changes
+
   lazy var textField: UITextField = {
     let textField = UITextField()
     textField.textAlignment = .right
     // textField.enablesReturnKeyAutomatically = true
     textField.placeholder = self.placeholder
+		textField.addTarget(self, action: #selector(textFieldChanged), for: .editingChanged)
     return textField
   }()
 
@@ -56,6 +59,10 @@ class TextFieldCell: UITableViewCell {
     }
   }
 
+	@objc func textFieldChanged() {
+		onTextFieldChange?(self.textField)
+	}
+
   func getText() -> String? {
     if let text = textField.text {
       if text.isEmpty {