Browse Source

basic autodel-selector

B. Petersen 5 years ago
parent
commit
05998ae760

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

@@ -160,6 +160,7 @@
 		AEFBE22F23FEF23D0045327A /* ProviderInfoCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEFBE22E23FEF23D0045327A /* ProviderInfoCell.swift */; };
 		AEFBE23123FF09B20045327A /* TypeAlias.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEFBE23023FF09B20045327A /* TypeAlias.swift */; };
 		B20462E42440A4A600367A57 /* SettingsAutodelOverviewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B20462E32440A4A600367A57 /* SettingsAutodelOverviewController.swift */; };
+		B20462E62440C99600367A57 /* SettingsAutodelSetController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B20462E52440C99600367A57 /* SettingsAutodelSetController.swift */; };
 		B21005DB23383664004C70C5 /* SettingsClassicViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B21005DA23383664004C70C5 /* SettingsClassicViewController.swift */; };
 		B26B3BC7236DC3DC008ED35A /* SwitchCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B26B3BC6236DC3DC008ED35A /* SwitchCell.swift */; };
 /* End PBXBuildFile section */
@@ -400,6 +401,7 @@
 		B20462E12440805C00367A57 /* id */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = id; path = id.lproj/Localizable.strings; sourceTree = "<group>"; };
 		B20462E22440805C00367A57 /* id */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = id; path = id.lproj/Localizable.stringsdict; sourceTree = "<group>"; };
 		B20462E32440A4A600367A57 /* SettingsAutodelOverviewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SettingsAutodelOverviewController.swift; sourceTree = "<group>"; };
+		B20462E52440C99600367A57 /* SettingsAutodelSetController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SettingsAutodelSetController.swift; sourceTree = "<group>"; };
 		B21005DA23383664004C70C5 /* SettingsClassicViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SettingsClassicViewController.swift; sourceTree = "<group>"; };
 		B253ED992336E759004DD215 /* ja */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/InfoPlist.strings; sourceTree = "<group>"; };
 		B253ED9A2336E759004DD215 /* ja */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/Localizable.strings; sourceTree = "<group>"; };
@@ -776,6 +778,7 @@
 				B21005DA23383664004C70C5 /* SettingsClassicViewController.swift */,
 				78E45E3921D3CFBC00D4B15E /* SettingsController.swift */,
 				B20462E32440A4A600367A57 /* SettingsAutodelOverviewController.swift */,
+				B20462E52440C99600367A57 /* SettingsAutodelSetController.swift */,
 				AE76E5ED242BF2EA003CF461 /* WelcomeViewController.swift */,
 			);
 			path = Controller;
@@ -1183,6 +1186,7 @@
 				7AE0A5491FC42F65005ECB4B /* NewChatViewController.swift in Sources */,
 				305961E52346125100C80F33 /* LabelAlignment.swift in Sources */,
 				AE77838F23E4276D0093EABD /* ContactCellViewModel.swift in Sources */,
+				B20462E62440C99600367A57 /* SettingsAutodelSetController.swift in Sources */,
 				305961E82346125100C80F33 /* Sender.swift in Sources */,
 				305961EE2346125100C80F33 /* AvatarPosition.swift in Sources */,
 				3015634423A003BA00E9DEF4 /* AudioRecorderController.swift in Sources */,

+ 16 - 0
deltachat-ios/Controller/SettingsAutodelOverviewController.swift

@@ -84,5 +84,21 @@ class SettingsAutodelOverviewController: UITableViewController {
     }
 
     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+        guard let cell = tableView.cellForRow(at: indexPath), let cellTag = CellTags(rawValue: cell.tag) else {
+            safe_fatalError()
+            return
+        }
+        tableView.deselectRow(at: indexPath, animated: false) // to achieve highlight effect
+
+        switch cellTag {
+        case .autodelDevice:
+            let controller = SettingsAutodelSetController(dcContext: dcContext, fromServer: false)
+            navigationController?.pushViewController(controller, animated: true)
+
+        case .autodelServer:
+            let controller = SettingsAutodelSetController(dcContext: dcContext, fromServer: true)
+            navigationController?.pushViewController(controller, animated: true)
+        }
+
     }
 }

+ 130 - 0
deltachat-ios/Controller/SettingsAutodelSetController.swift

@@ -0,0 +1,130 @@
+import UIKit
+
+class SettingsAutodelSetController: UITableViewController {
+
+    var dcContext: DcContext
+
+    private struct Options {
+        let value: Int
+        let descr: String
+    }
+
+    private lazy var autodelDeviceOptions: [Options] = {
+        return [
+            Options(value: 0, descr: "off"),
+            Options(value: 3600, descr: "autodel_after_1_hour"),
+            Options(value: 86400, descr: "autodel_after_1_day"),
+            Options(value: 604800, descr: "autodel_after_1_week"),
+            Options(value: 2419200, descr: "autodel_after_4_weeks"),
+        ]
+    }()
+
+    private lazy var autodelServerOptions: [Options] = {
+        return [
+            Options(value: 0, descr: "off"),
+            Options(value: 1, descr: "autodel_at_once"),
+            Options(value: 3600, descr: "autodel_after_1_hour"),
+            Options(value: 86400, descr: "autodel_after_1_day"),
+            Options(value: 604800, descr: "autodel_after_1_week"),
+            Options(value: 2419200, descr: "autodel_after_4_weeks"),
+        ]
+    }()
+
+    private lazy var autodelOptions: [Options] = {
+        return fromServer ? autodelServerOptions : autodelDeviceOptions
+    }()
+
+    var fromServer: Bool
+    var enteringVal: Int
+    var currVal: Int
+
+    private var cancelButton: UIBarButtonItem {
+        let button =  UIBarButtonItem(title: String.localized("cancel"), style: .plain, target: self, action: #selector(cancelButtonPressed))
+        return button
+    }
+
+    private var okButton: UIBarButtonItem {
+        let button =  UIBarButtonItem(title: String.localized("ok"), style: .done, target: self, action: #selector(okButtonPressed))
+        return button
+    }
+
+    var staticCells: [UITableViewCell] {
+        return autodelOptions.map({
+            let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
+            cell.textLabel?.text = String.localized($0.descr)
+            cell.selectionStyle = .none
+            cell.accessoryType = $0.value==currVal ? .checkmark : .none
+            return cell
+        })
+    }
+
+    init(dcContext: DcContext, fromServer: Bool) {
+        self.dcContext = dcContext
+        self.fromServer = fromServer
+        self.enteringVal = dcContext.getConfigInt(fromServer ? "delete_server_after" :  "delete_device_after")
+        self.currVal = enteringVal
+        super.init(style: .grouped)
+        self.title = String.localized(fromServer ? "autodel_server_title" : "autodel_device_title")
+        hidesBottomBarWhenPushed = true
+    }
+
+    required init?(coder aDecoder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
+    override func viewDidLoad() {
+        super.viewDidLoad()
+        navigationItem.leftBarButtonItem = cancelButton
+        navigationItem.rightBarButtonItem = okButton
+    }
+
+    static public func getAutodelString(fromServer: Bool, val: Int) -> String {
+        // TODO
+        return "Err"
+    }
+
+    func valToIndex(val: Int) -> Int {
+        var index = 0
+        for option in autodelOptions {
+            if option.value == val {
+                return index
+            }
+            index += 1
+        }
+        return 0 // default to "off"
+    }
+
+    // MARK: - Table view data source
+
+    override func numberOfSections(in tableView: UITableView) -> Int {
+        return 1
+    }
+
+    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
+        return autodelOptions.count
+    }
+
+    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+        let oldSelectedCell = tableView.cellForRow(at: IndexPath.init(row: valToIndex(val: currVal), section: 0))
+        oldSelectedCell?.accessoryType = .none
+
+        let newSelectedCell = tableView.cellForRow(at: IndexPath.init(row: indexPath.row, section: 0))
+        newSelectedCell?.accessoryType = .checkmark
+
+        currVal = autodelOptions[indexPath.row].value
+    }
+
+    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
+        return staticCells[indexPath.row]
+    }
+
+    // MARK: - actions
+
+    @objc private func cancelButtonPressed() {
+        navigationController?.popViewController(animated: true)
+    }
+
+    @objc private func okButtonPressed() {
+        navigationController?.popViewController(animated: true)
+    }
+}