浏览代码

basic skeletton

cyberta 2 年之前
父节点
当前提交
861e67abdb

+ 151 - 8
deltachat-ios/Controller/AccountSwitchViewController.swift

@@ -2,19 +2,132 @@ import UIKit
 import DcCore
 import DcCore
 
 
 class AccountSwitchViewController: UITableViewController {
 class AccountSwitchViewController: UITableViewController {
-    
+
+    private let dcAccounts: DcAccounts
+    private let accountSection = 0
+    private let addSection = 1
+    private let addAccountTag = -1
+
+    private lazy var accountIds: [Int] = {
+        return dcAccounts.getAll()
+    }()
+
+    private lazy var editButton: UIBarButtonItem = {
+        let btn = UIBarButtonItem(barButtonSystemItem: .edit,
+                                  target: self,
+                                  action: #selector(editAction))
+        return btn
+    }()
+
+    private lazy var cancelButton: UIBarButtonItem = {
+        let btn = UIBarButtonItem(barButtonSystemItem: .cancel,
+                                  target: self,
+                                  action: #selector(cancelAction))
+        return btn
+    }()
+
+    private lazy var addAccountCell: AccountCell = {
+        let cell = AccountCell()
+        cell.tag = -1
+        return cell
+    }()
+
+    init(dcAccounts: DcAccounts) {
+        self.dcAccounts = dcAccounts
+        super.init(style: .grouped)
+        setupSubviews()
+    }
+
+    required init?(coder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
 
 
     override func viewDidLoad() {
     override func viewDidLoad() {
         super.viewDidLoad()
         super.viewDidLoad()
-        setupSubviews()
+        navigationItem.setLeftBarButton(editButton, animated: false)
     }
     }
-    
+
     private func setupSubviews() {
     private func setupSubviews() {
         title = String.localized("switch_account")
         title = String.localized("switch_account")
         tableView.register(AccountCell.self, forCellReuseIdentifier: AccountCell.reuseIdentifier)
         tableView.register(AccountCell.self, forCellReuseIdentifier: AccountCell.reuseIdentifier)
         tableView.rowHeight = AccountCell.cellHeight
         tableView.rowHeight = AccountCell.cellHeight
         tableView.separatorStyle = .none
         tableView.separatorStyle = .none
     }
     }
+
+    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
+            return AccountCell.cellHeight
+    }
+
+    override func numberOfSections(in tableView: UITableView) -> Int {
+        return 2
+    }
+
+    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
+        if section == accountSection {
+            return accountIds.count
+        }
+        return 1
+    }
+
+    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
+        if indexPath.section == accountSection {
+            guard let cell: AccountCell = tableView.dequeueReusableCell(withIdentifier: AccountCell.reuseIdentifier, for: indexPath) as? AccountCell else {
+                safe_fatalError("unsupported cell type")
+                return UITableViewCell()
+            }
+
+            let selectedAccountId = dcAccounts.getSelected().id
+            cell.updateCell(selectedAccount: selectedAccountId, dcContext: dcAccounts.get(id: accountIds[indexPath.row]))
+            cell.accessoryType = selectedAccountId == accountIds[indexPath.row] ? .checkmark : .none
+            return cell
+        }
+        return addAccountCell
+    }
+
+    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+        guard let cell = tableView.cellForRow(at: indexPath) else {
+            safe_fatalError()
+            return
+        }
+
+        tableView.deselectRow(at: indexPath, animated: false)
+        let selectedAccountId = dcAccounts.getSelected().id
+        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
+        let prefs = UserDefaults.standard
+
+        if indexPath.section == accountSection {
+            let accountId = cell.tag
+            if selectedAccountId == accountId {
+                dismiss(animated: true)
+                return
+            }
+            _ = self.dcAccounts.select(id: accountId)
+        } else {
+            _ = self.dcAccounts.add()
+        }
+
+        appDelegate.reloadDcContext()
+        prefs.setValue(selectedAccountId, forKey: Constants.Keys.lastSelectedAccountKey)
+        dismiss(animated: true)
+    }
+
+    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
+        return nil
+    }
+
+    @objc private func editAction() {
+        logger.debug("edit Action")
+        navigationItem.setLeftBarButton(cancelButton, animated: false)
+        setEditing(true, animated: true)
+        tableView.reloadData()
+    }
+
+    @objc private func cancelAction() {
+        logger.debug("cancel Action")
+        navigationItem.setLeftBarButton(editButton, animated: false)
+        setEditing(false, animated: true)
+        tableView.reloadData()
+    }
 }
 }
 
 
 class AccountCell: UITableViewCell {
 class AccountCell: UITableViewCell {
@@ -40,16 +153,27 @@ class AccountCell: UITableViewCell {
     lazy var accountName: UILabel = {
     lazy var accountName: UILabel = {
         let label = UILabel()
         let label = UILabel()
         label.translatesAutoresizingMaskIntoConstraints = false
         label.translatesAutoresizingMaskIntoConstraints = false
+        label.font = UIFont.preferredFont(for: .body, weight: .bold)
+        label.textColor = DcColors.defaultTextColor
         return label
         return label
-    }
+    }()
 
 
     func setupSubviews() {
     func setupSubviews() {
-        addSubview(accountAvatar)
-        addSubview(label)
-        //addSubview(
+        contentView.addSubview(accountAvatar)
+        contentView.addSubview(accountName)
+        let margins = contentView.layoutMarginsGuide
+        addConstraints([
+            accountAvatar.constraintAlignTopToAnchor(margins.topAnchor),
+            accountAvatar.constraintAlignLeadingToAnchor(margins.leadingAnchor),
+            accountAvatar.constraintAlignBottomToAnchor(margins.bottomAnchor),
+            accountName.constraintAlignTopToAnchor(margins.topAnchor),
+            accountName.constraintToTrailingOf(accountAvatar),
+            accountName.constraintAlignBottomToAnchor(margins.bottomAnchor),
+            accountName.constraintAlignTrailingToAnchor(margins.trailingAnchor, paddingTrailing: 32)
+        ])
     }
     }
 
 
-    public func updateCell(dcContext: DcContext) {
+    public func updateCell(selectedAccount: Int, dcContext: DcContext) {
         let accountId = dcContext.id
         let accountId = dcContext.id
         let title = dcContext.displayname ?? dcContext.addr ?? ""
         let title = dcContext.displayname ?? dcContext.addr ?? ""
         let contact = dcContext.getContact(id: Int(DC_CONTACT_ID_SELF))
         let contact = dcContext.getContact(id: Int(DC_CONTACT_ID_SELF))
@@ -58,5 +182,24 @@ class AccountCell: UITableViewCell {
         if let image = contact.profileImage {
         if let image = contact.profileImage {
             accountAvatar.setImage(image)
             accountAvatar.setImage(image)
         }
         }
+        accountName.text = title
+        if isEditing {
+            accessoryType = .none
+        } else {
+            if selectedAccount == accountId {
+                accessoryType = .checkmark
+            } else {
+                accessoryType = .none
+            }
+        }
+        
+        tag = accountId
+    }
+
+    override func prepareForReuse() {
+        super.prepareForReuse()
+        accountAvatar.reset()
+        accountName.text = nil
+        tag = -1
     }
     }
 }
 }

+ 1 - 2
deltachat-ios/Controller/ChatListController.swift

@@ -574,8 +574,7 @@ class ChatListController: UITableViewController, AccountSwitcherHandler {
     }
     }
     
     
     @objc private func accountButtonTapped() {
     @objc private func accountButtonTapped() {
-        //showSwitchAccountMenu()
-        let viewController = AccountSwitchViewController()
+        let viewController = AccountSwitchViewController(dcAccounts: dcAccounts)
         let accountSwitchNavigationController = UINavigationController(rootViewController: viewController)
         let accountSwitchNavigationController = UINavigationController(rootViewController: viewController)
         if #available(iOS 15.0, *) {
         if #available(iOS 15.0, *) {
             if let sheet = accountSwitchNavigationController.sheetPresentationController {
             if let sheet = accountSwitchNavigationController.sheetPresentationController {