IOS-230 FolderListViewController builds IOS-230
authorDirk Zimmermann <dirk@pep-project.org>
Mon, 24 Oct 2016 17:15:14 +0200
branchIOS-230
changeset 86153c7f9ec0dec
parent 860 668701d8188f
child 867 89ccccb16970
IOS-230 FolderListViewController builds
pEpForiOS/UI/AccountsAndFolders/FolderListViewController.swift
pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
     1.1 --- a/pEpForiOS/UI/AccountsAndFolders/FolderListViewController.swift	Mon Oct 24 16:07:07 2016 +0200
     1.2 +++ b/pEpForiOS/UI/AccountsAndFolders/FolderListViewController.swift	Mon Oct 24 17:15:14 2016 +0200
     1.3 @@ -11,7 +11,9 @@
     1.4  
     1.5  import MessageModel
     1.6  
     1.7 -class FolderListViewController: FetchTableViewController {
     1.8 +class FolderListViewController: UITableViewController {
     1.9 +    let comp = "FolderListViewController"
    1.10 +
    1.11      struct FolderListConfig {
    1.12          let account: Account
    1.13          let appConfig: AppConfig
    1.14 @@ -33,20 +35,16 @@
    1.15  
    1.16      var state = UIState()
    1.17  
    1.18 +    var folders = [Folder]()
    1.19 +
    1.20      override func viewDidLoad() {
    1.21          super.viewDidLoad()
    1.22 -
    1.23          tableView.register(UITableViewCell.self, forCellReuseIdentifier: standardCell)
    1.24 -
    1.25 -        let refreshController = UIRefreshControl.init()
    1.26 -        refreshController.addTarget(self, action: #selector(self.refreshFoldersControl),
    1.27 -                                    for: UIControlEvents.valueChanged)
    1.28 -        self.refreshControl = refreshController
    1.29      }
    1.30  
    1.31      override func viewWillAppear(_ animated: Bool) {
    1.32          super.viewWillAppear(animated)
    1.33 -        prepareFetchRequest()
    1.34 +        updateModel()
    1.35      }
    1.36  
    1.37      override func didReceiveMemoryWarning() {
    1.38 @@ -54,44 +52,8 @@
    1.39          // Dispose of any resources that can be recreated.
    1.40      }
    1.41  
    1.42 -    func prepareFetchRequest() {
    1.43 -        let fetchRequest = NSFetchRequest<NSManagedObject>.init(entityName: CdFolder.entityName())
    1.44 -
    1.45 -        let predicateAccount = NSPredicate.init(
    1.46 -            format: "account.email = %@", config.account.user.address)
    1.47 -        let predicateNotDeleted = NSPredicate.init(
    1.48 -            format: "shouldDelete = false")
    1.49 -        let predicate = NSCompoundPredicate.init(
    1.50 -            andPredicateWithSubpredicates: [predicateAccount, predicateNotDeleted])
    1.51 -
    1.52 -        fetchRequest.predicate = predicate
    1.53 -        fetchRequest.sortDescriptors = [NSSortDescriptor.init(key: "name", ascending: true)]
    1.54 -        fetchController = NSFetchedResultsController.init(
    1.55 -            fetchRequest: fetchRequest,
    1.56 -            managedObjectContext: config.appConfig.coreDataUtil.managedObjectContext,
    1.57 -            sectionNameKeyPath: nil, cacheName: nil)
    1.58 -        fetchController?.delegate = self
    1.59 -        do {
    1.60 -            try fetchController?.performFetch()
    1.61 -        } catch let err as NSError {
    1.62 -            Log.errorComponent(comp, error: err)
    1.63 -        }
    1.64 -    }
    1.65 -
    1.66 -    func refreshFoldersControl(_ refreshControl: UIRefreshControl? = nil) {
    1.67 -        state.isUpdating = true
    1.68 -        updateUI()
    1.69 -        if let connectInfo = config.account.connectInfo {
    1.70 -            config.appConfig.grandOperator.fetchFolders(
    1.71 -                connectInfo, completionBlock: { error in
    1.72 -                    self.state.isUpdating = false
    1.73 -                    self.updateUI()
    1.74 -                    self.tableView.reloadData()
    1.75 -            })
    1.76 -        } else {
    1.77 -            Log.errorComponent(comp,
    1.78 -                               errorString: "No connectInfo for account \(config.account.user.address)")
    1.79 -        }
    1.80 +    func updateModel() {
    1.81 +        folders = config.account.rootFolders
    1.82      }
    1.83  
    1.84      func updateUI() {
    1.85 @@ -110,39 +72,30 @@
    1.86          return cell
    1.87      }
    1.88  
    1.89 -    override func configureCell(_ cell: UITableViewCell, indexPath: IndexPath) {
    1.90 -        if let folder = fetchController?.object(at: indexPath) as? CdFolder {
    1.91 -            cell.textLabel?.text = "\(folder.name)"
    1.92 -            cell.accessoryType = .disclosureIndicator
    1.93 -        }
    1.94 +    func configureCell(_ cell: UITableViewCell, indexPath: IndexPath) {
    1.95 +        let folder = folderAt(indexPath: indexPath)
    1.96 +        cell.textLabel?.text = "\(folder.name)"
    1.97 +        cell.accessoryType = .disclosureIndicator
    1.98      }
    1.99  
   1.100      override func tableView(_ tableView: UITableView,
   1.101                              canEditRowAt indexPath: IndexPath) -> Bool {
   1.102 -        if let folder = fetchController?.object(at: indexPath) as? CdFolder {
   1.103 -            switch folder.folderType.intValue {
   1.104 -            case FolderType.localOutbox.rawValue, FolderType.inbox.rawValue: return false
   1.105 -            default: return true
   1.106 -            }
   1.107 +        let folder = folderAt(indexPath: indexPath)
   1.108 +        switch folder.folderType {
   1.109 +        case FolderType.localOutbox, FolderType.inbox: return false
   1.110 +        default: return true
   1.111          }
   1.112 -        return false
   1.113      }
   1.114  
   1.115      override func tableView(
   1.116          _ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle,
   1.117          forRowAt indexPath: IndexPath) {
   1.118 -        if let folder = fetchController?.object(at: indexPath) as? CdFolder {
   1.119 -            folder.shouldDelete = true
   1.120 -            config.appConfig.model.save()
   1.121 -            state.isUpdating = true
   1.122 -            updateUI()
   1.123 +        let folder = folderAt(indexPath: indexPath)
   1.124 +        folder.delete()
   1.125 +    }
   1.126  
   1.127 -            config.appConfig.grandOperator.deleteFolder(folder as CdFolder) { error in
   1.128 -                UIHelper.displayError(error, controller: self)
   1.129 -                self.state.isUpdating = false
   1.130 -                self.updateUI()
   1.131 -            }
   1.132 -        }
   1.133 +    func folderAt(indexPath: IndexPath) -> Folder {
   1.134 +        return folders[indexPath.row]
   1.135      }
   1.136  
   1.137      // MARK: - Table view delegate
   1.138 @@ -150,47 +103,23 @@
   1.139      override func tableView(
   1.140          _ tableView: UITableView,
   1.141          indentationLevelForRowAt indexPath: IndexPath) -> Int {
   1.142 -        if var folder = fetchController?.object(at: indexPath) as? CdFolder {
   1.143 -            var count = 0
   1.144 -            while folder.parent != nil {
   1.145 -                count += 1
   1.146 -                folder = folder.parent!
   1.147 -            }
   1.148 -            return count
   1.149 +        var folder = folderAt(indexPath: indexPath) as MessageFolder
   1.150 +        var count = 0
   1.151 +        while folder.parent != nil {
   1.152 +            count += 1
   1.153 +            folder = folder.parent!
   1.154          }
   1.155 -        return 0
   1.156 +        return count
   1.157      }
   1.158  
   1.159      override func tableView(_ tableView: UITableView,
   1.160                              didSelectRowAt indexPath: IndexPath) {
   1.161 -        if let fi = fetchController?.object(at: indexPath) as? CdFolder {
   1.162 -            let predicateBasic = config.appConfig.model.basicMessagePredicate()
   1.163 -            let predicateAccount = NSPredicate.init(
   1.164 -                format: "folder.account.email = %@", config.account.user.address)
   1.165 -            let predicateFolder = NSPredicate.init(
   1.166 -                format: "folder.name = %@", fi.name)
   1.167 +        let fi = folderAt(indexPath: indexPath)
   1.168 +        emailListConfig = EmailListViewController.EmailListConfig.init(
   1.169 +            appConfig: config.appConfig,
   1.170 +            account: config.account, folder: fi)
   1.171  
   1.172 -            // If the folder is just local, then don't let the email list view sync.
   1.173 -            var account: Account? = nil
   1.174 -            if let ft = FolderType.fromInt(fi.folderType.intValue) {
   1.175 -                if ft.isRemote() {
   1.176 -                    account = config.account
   1.177 -                }
   1.178 -                // Start syncing emails when it's not an inbox (which was just synced already)
   1.179 -                let syncOnAppear = ft != .inbox
   1.180 -
   1.181 -                emailListConfig = EmailListViewController.EmailListConfig.init(
   1.182 -                    appConfig: config.appConfig,
   1.183 -                    predicate: NSCompoundPredicate.init(
   1.184 -                        andPredicateWithSubpredicates: [predicateBasic, predicateAccount,
   1.185 -                            predicateFolder]),
   1.186 -                    sortDescriptors: [NSSortDescriptor.init(
   1.187 -                        key: "receivedDate", ascending: false)],
   1.188 -                    account: account, folderName: fi.name, syncOnAppear: syncOnAppear)
   1.189 -
   1.190 -                performSegue(withIdentifier: segueShowEmails, sender: self)
   1.191 -            }
   1.192 -        }
   1.193 +        performSegue(withIdentifier: segueShowEmails, sender: self)
   1.194      }
   1.195  
   1.196      // MARK: - Navigation
     2.1 --- a/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Mon Oct 24 16:07:07 2016 +0200
     2.2 +++ b/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Mon Oct 24 17:15:14 2016 +0200
     2.3 @@ -161,6 +161,16 @@
     2.4          return cell
     2.5      }
     2.6  
     2.7 +    /**
     2.8 +     The message at the given position.
     2.9 +     */
    2.10 +    func messageAt(indexPath: IndexPath) -> Message? {
    2.11 +        if let fol = config.folder {
    2.12 +            return fol.messageByIndex(indexPath.row)
    2.13 +        }
    2.14 +        return nil
    2.15 +    }
    2.16 +
    2.17      // MARK: - UITableViewDelegate
    2.18  
    2.19      override func tableView(_ tableView: UITableView,
    2.20 @@ -194,16 +204,6 @@
    2.21  
    2.22      // MARK: - Misc
    2.23  
    2.24 -    /**
    2.25 -     The message at the given position.
    2.26 -     */
    2.27 -    func messageAt(indexPath: IndexPath) -> Message? {
    2.28 -        if let fol = config.folder {
    2.29 -            return fol.messageByIndex(indexPath.row)
    2.30 -        }
    2.31 -        return nil
    2.32 -    }
    2.33 -
    2.34      func configureCell(_ theCell: UITableViewCell, indexPath: IndexPath) {
    2.35          guard let cell = theCell as? EmailListViewCell else {
    2.36              return