pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author Dirk Zimmermann <dirk@pep-project.org>
Thu, 26 Jan 2017 15:33:06 +0100
changeset 1542 655cb6fd467b
parent 1535 f03bfb0374df
child 1558 27854b5cab91
permissions -rw-r--r--
IOS-361 making sure flag updates are communicated as such to the UI
dirk@30
     1
//
dirk@30
     2
//  EmailListViewController.swift
dirk@30
     3
//  pEpForiOS
dirk@30
     4
//
dirk@30
     5
//  Created by Dirk Zimmermann on 16/04/16.
dirk@30
     6
//  Copyright © 2016 p≡p Security S.A. All rights reserved.
dirk@30
     7
//
dirk@30
     8
dirk@30
     9
import Foundation
dirk@30
    10
import UIKit
dirk@31
    11
import CoreData
dirk@810
    12
import MessageModel
dirk@810
    13
igor@1260
    14
struct EmailListConfig {
igor@1338
    15
    var appConfig: AppConfig?
dirk@1333
    16
igor@1260
    17
    /** The folder to display, if it exists */
igor@1260
    18
    var folder: Folder?
igor@1260
    19
}
dirk@30
    20
dirk@854
    21
class EmailListViewController: UITableViewController {
dirk@583
    22
    struct UIState {
dirk@583
    23
        var isSynching: Bool = false
dirk@583
    24
    }
dirk@30
    25
dirk@1344
    26
    var config: EmailListConfig?
dirk@275
    27
    var state = UIState()
igor@1301
    28
    let searchController = UISearchController(searchResultsController: nil)
dirk@1542
    29
    let cellsByMessageID = NSCache<NSString, EmailListViewCell>()
dirk@719
    30
dirk@275
    31
    override func viewDidLoad() {
ylandert@935
    32
        super.viewDidLoad()
igor@1301
    33
        
igor@1408
    34
        title = "EmailList.title".localized
igor@1284
    35
        UIHelper.emailListTableHeight(self.tableView)
igor@1301
    36
        addSearchBar()
dirk@275
    37
    }
dirk@31
    38
dirk@784
    39
    override func viewWillAppear(_ animated: Bool) {
igor@1301
    40
        super.viewWillAppear(animated)
igor@1301
    41
        
dirk@1344
    42
        if MiscUtil.isUnitTest() {
dirk@1344
    43
            return
dirk@1344
    44
        }
dirk@1344
    45
dirk@1344
    46
        initialConfig()
dirk@854
    47
        updateModel()
dirk@1348
    48
dirk@1348
    49
        MessageModelConfig.messageFolderDelegate = self
dirk@1348
    50
    }
dirk@1348
    51
dirk@1348
    52
    override func viewWillDisappear(_ animated: Bool) {
dirk@1348
    53
        super.viewWillDisappear(animated)
dirk@1348
    54
        MessageModelConfig.messageFolderDelegate = nil
igor@1301
    55
    }
igor@1301
    56
    
igor@1338
    57
    func initialConfig() {
dirk@1344
    58
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
igor@1338
    59
            return
igor@1338
    60
        }
dirk@1344
    61
dirk@1344
    62
        if config == nil {
igor@1395
    63
            config = EmailListConfig(appConfig: appDelegate.appConfig, folder: Folder.unifiedInbox())
dirk@1344
    64
        }
dirk@1348
    65
        if Account.all().isEmpty {
igor@1338
    66
            performSegue(withIdentifier:.segueAddNewAccount, sender: self)
igor@1338
    67
        }
igor@1338
    68
    }
igor@1338
    69
    
igor@1301
    70
    func addSearchBar() {
igor@1301
    71
        searchController.searchResultsUpdater = self
igor@1301
    72
        searchController.dimsBackgroundDuringPresentation = false
igor@1301
    73
        searchController.delegate = self
igor@1301
    74
        definesPresentationContext = true
igor@1301
    75
        tableView.tableHeaderView = searchController.searchBar
igor@1301
    76
        tableView.setContentOffset(CGPoint(x: 0.0, y: 40.0), animated: false)
igor@1301
    77
    }
dirk@705
    78
igor@941
    79
    
dirk@1330
    80
    @IBAction func showUnreadButtonTapped(_ sender: UIBarButtonItem) {}
igor@941
    81
    
dirk@854
    82
    func updateModel() {
igor@1318
    83
        tableView.reloadData()
dirk@31
    84
    }
dirk@31
    85
dirk@58
    86
    // MARK: - UI State
dirk@58
    87
dirk@58
    88
    func updateUI() {
dirk@784
    89
        UIApplication.shared.isNetworkActivityIndicatorVisible = state.isSynching
dirk@748
    90
        if !state.isSynching {
igor@1260
    91
            refreshControl?.endRefreshing()
dirk@58
    92
        }
dirk@58
    93
    }
dirk@58
    94
dirk@31
    95
    // MARK: - UITableViewDataSource
dirk@31
    96
dirk@784
    97
    override func numberOfSections(in tableView: UITableView) -> Int {
dirk@1344
    98
        if let _ = config?.folder {
dirk@854
    99
            return 1
dirk@209
   100
        }
dirk@854
   101
        return 0
dirk@31
   102
    }
dirk@31
   103
dirk@784
   104
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dirk@1344
   105
        if let fol = config?.folder  {
dirk@854
   106
            return fol.messageCount()
dirk@31
   107
        }
dirk@31
   108
        return 0
dirk@31
   109
    }
dirk@31
   110
dirk@784
   111
    override func tableView(_ tableView: UITableView,
dirk@784
   112
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
dirk@784
   113
        let cell = tableView.dequeueReusableCell(
dirk@784
   114
            withIdentifier: "EmailListViewCell", for: indexPath) as! EmailListViewCell
dirk@1542
   115
        if let messageID = cell.configureCell(indexPath: indexPath, config: config) {
dirk@1542
   116
            cellsByMessageID.setObject(cell, forKey: messageID as NSString)
dirk@1542
   117
        }
dirk@31
   118
        return cell
dirk@31
   119
    }
dirk@31
   120
dirk@719
   121
    // MARK: - UITableViewDelegate
dirk@719
   122
dirk@1333
   123
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
dirk@1507
   124
        let _ = tableView.cellForRow(at: indexPath) as! EmailListViewCell
dirk@719
   125
dirk@1344
   126
        if let fol = config?.folder {
dirk@855
   127
            if fol.folderType == .drafts {
igor@1395
   128
                //performSegue(withIdentifier: .segueCompose, sender: cell)
dirk@719
   129
                return
dirk@719
   130
            }
dirk@719
   131
        }
igor@1395
   132
        //performSegue(withIdentifier: .segueShowEmail, sender: cell)
dirk@719
   133
    }
dirk@719
   134
dirk@784
   135
    override func tableView(_ tableView: UITableView, editActionsForRowAt
dirk@784
   136
        indexPath: IndexPath)-> [UITableViewRowAction]? {
dirk@784
   137
        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
igor@1260
   138
        if let email = cell.messageAt(indexPath: indexPath, config: config) {
dirk@1507
   139
            let isFlagAction = createFlagAction(message: email, cell: cell)
dirk@1507
   140
            let deleteAction = createDeleteAction(message: email, cell: cell)
igor@1242
   141
            let moreAction = createMoreAction(message: email, cell: cell)
igor@1242
   142
            return [deleteAction,isFlagAction,moreAction]
dirk@855
   143
        }
dirk@855
   144
        return nil
dirk@744
   145
    }
dirk@744
   146
dirk@719
   147
    // MARK: - Misc
dirk@719
   148
dirk@1507
   149
    func createRowAction(cell: EmailListViewCell,
dirk@1507
   150
        image: UIImage?, action: @escaping (UITableViewRowAction, IndexPath) -> Void,
dirk@1510
   151
        title: String) -> UITableViewRowAction {
dirk@1510
   152
        let rowAction = UITableViewRowAction(
dirk@1510
   153
            style: .normal, title: title, handler: action)
dirk@1507
   154
dirk@1507
   155
        if let theImage = image {
dirk@1507
   156
            let iconColor = UIColor(patternImage: theImage)
dirk@1507
   157
            rowAction.backgroundColor = iconColor
dirk@744
   158
        }
dirk@744
   159
dirk@1507
   160
        return rowAction
dirk@744
   161
    }
dirk@744
   162
dirk@1507
   163
    func createFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   164
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1522
   165
            if message.imapFlags == nil {
dirk@1522
   166
                Log.warn(component: #function, content: "message.imapFlags == nil")
dirk@1522
   167
            }
dirk@1507
   168
            if (cell.isImportant(message: message)) {
dirk@1507
   169
                message.imapFlags?.flagged = false
dirk@1507
   170
            } else {
dirk@1507
   171
                message.imapFlags?.flagged = true
dirk@1507
   172
            }
dirk@1507
   173
            message.save()
dirk@1507
   174
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@1507
   175
        }
dirk@1510
   176
igor@1523
   177
        var title = "\n\nFlag".localized
dirk@1510
   178
        if (message.imapFlags?.flagged ?? true) {
igor@1530
   179
            title = "\n\nUnFlag".localized
dirk@744
   180
        }
dirk@744
   181
dirk@1510
   182
        return createRowAction(
dirk@1510
   183
            cell: cell, image: UIImage(named: "swipe-flag"), action: action, title: title)
dirk@744
   184
    }
dirk@744
   185
dirk@1507
   186
    func createDeleteAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   187
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1507
   188
            guard let message = cell.messageAt(indexPath: indexPath, config: self.config) else {
dirk@1507
   189
                return
dirk@1507
   190
            }
dirk@1507
   191
            message.imapFlags?.deleted = true
dirk@1507
   192
            message.save()
dirk@1507
   193
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@1507
   194
        }
dirk@744
   195
dirk@1510
   196
        return createRowAction(
dirk@1510
   197
            cell: cell, image: UIImage(named: "swipe-trash"), action: action,
igor@1523
   198
            title: "\n\nDelete".localized)
dirk@1507
   199
    }
dirk@1507
   200
dirk@1507
   201
    func createMarkAsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   202
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1507
   203
            if (cell.isRead(message: message)) {
dirk@1507
   204
                message.imapFlags?.seen = false
dirk@1507
   205
            } else {
dirk@1507
   206
                message.imapFlags?.seen = true
dirk@1507
   207
            }
dirk@1507
   208
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@744
   209
        }
dirk@1510
   210
dirk@1510
   211
        var title = NSLocalizedString(
dirk@1510
   212
            "Unread", comment: "Unread button title in swipe action on EmailListViewController")
dirk@1510
   213
        if (!cell.isRead(message: message)) {
dirk@1510
   214
            title = NSLocalizedString(
dirk@1510
   215
                "Read", comment: "Read button title in swipe action on EmailListViewController")
dirk@1507
   216
        }
dirk@1507
   217
dirk@1507
   218
        let isReadAction = createRowAction(cell: cell, image: nil, action: action,
dirk@1510
   219
                                           title: title)
dirk@784
   220
        isReadAction.backgroundColor = UIColor.blue
dirk@744
   221
dirk@744
   222
        return isReadAction
dirk@744
   223
    }
igor@1242
   224
    
igor@1242
   225
    func createMoreAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1510
   226
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
igor@1242
   227
            self.showMoreActionSheet(cell: cell)
igor@1242
   228
        }
dirk@1510
   229
dirk@1510
   230
        return createRowAction(
dirk@1510
   231
            cell: cell, image: UIImage(named: "swipe-more"), action: action,
igor@1523
   232
            title: "\n\nMore".localized)
igor@1242
   233
    }
igor@1242
   234
    
igor@1242
   235
    // MARK: - Action Sheet
igor@1242
   236
    
igor@1242
   237
    func showMoreActionSheet(cell: EmailListViewCell) {
igor@1242
   238
        let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
igor@1438
   239
        alertControler.view.tintColor = .pEpGreen
igor@1242
   240
        let cancelAction = createCancelAction()
igor@1242
   241
        let replyAction = createReplyAction(cell: cell)
igor@1242
   242
        let forwardAction = createForwardAction(cell: cell)
igor@1242
   243
        let markAction = createMarkAction()
igor@1242
   244
        alertControler.addAction(cancelAction)
igor@1242
   245
        alertControler.addAction(replyAction)
igor@1242
   246
        alertControler.addAction(forwardAction)
igor@1242
   247
        alertControler.addAction(markAction)
igor@1414
   248
        if let popoverPresentationController = alertControler.popoverPresentationController {
igor@1414
   249
            popoverPresentationController.sourceView = cell
igor@1414
   250
        }
igor@1242
   251
        present(alertControler, animated: true, completion: nil)
igor@1242
   252
    }
igor@1242
   253
    
igor@1242
   254
    // MARK: - Action Sheet Actions
igor@1242
   255
igor@1242
   256
    func createCancelAction() -> UIAlertAction {
igor@1242
   257
      return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in}
igor@1242
   258
    }
igor@1242
   259
    
igor@1242
   260
    func createReplyAction(cell: EmailListViewCell) ->  UIAlertAction {
igor@1242
   261
        return UIAlertAction(title: "Reply", style: .default) { (action) in
igor@1338
   262
           // self.performSegue(withIdentifier: self.segueCompose, sender: cell)
igor@1338
   263
            self.performSegue(withIdentifier: .segueCompose, sender: cell)
igor@1242
   264
        }
igor@1242
   265
    }
igor@1242
   266
    
igor@1242
   267
    func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
igor@1242
   268
        return UIAlertAction(title: "Forward", style: .default) { (action) in
igor@1338
   269
            //self.performSegue(withIdentifier: self.segueCompose, sender: cell)
igor@1338
   270
            self.performSegue(withIdentifier: .segueCompose, sender: cell)
igor@1242
   271
        }
igor@1242
   272
    }
igor@1242
   273
    
igor@1242
   274
    func createMarkAction() -> UIAlertAction {
igor@1242
   275
        return UIAlertAction(title: "Mark", style: .default) { (action) in
igor@1242
   276
        }
igor@1242
   277
    }
igor@1301
   278
    
igor@1301
   279
    // MARK: - Content Search
igor@1301
   280
    
igor@1301
   281
    func filterContentForSearchText(searchText: String) {
igor@1301
   282
        
igor@1301
   283
    }
igor@1395
   284
 
igor@1338
   285
    // MARK: - Actions
igor@1395
   286
//    @IBAction func unwindToEmailList(for unwindSegue: UIStoryboardSegue) {
igor@1395
   287
//        
igor@1395
   288
//    }
igor@1338
   289
   
dirk@784
   290
}
igor@1301
   291
igor@1301
   292
extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
igor@1301
   293
    public func updateSearchResults(for searchController: UISearchController) {
igor@1301
   294
        filterContentForSearchText(searchText: searchController.searchBar.text!)
igor@1301
   295
    }
igor@1301
   296
    
igor@1301
   297
    func didDismissSearchController(_ searchController: UISearchController) {
igor@1301
   298
    }
igor@1301
   299
}
igor@1338
   300
igor@1338
   301
// MARK: - Navigation
igor@1338
   302
igor@1338
   303
extension EmailListViewController: SegueHandlerType {
igor@1338
   304
    
igor@1338
   305
    // MARK: - SegueHandlerType
igor@1338
   306
    
igor@1338
   307
    enum SegueIdentifier: String {
igor@1338
   308
        case segueAddNewAccount
igor@1338
   309
        case segueEditAccounts
igor@1338
   310
        case segueShowEmail
igor@1338
   311
        case segueCompose
igor@1338
   312
        case noSegue
igor@1338
   313
    }
igor@1338
   314
    
igor@1338
   315
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
igor@1338
   316
        switch segueIdentifier(for: segue) {
igor@1338
   317
        case .segueCompose:
igor@1338
   318
            //let destination = segue.destination as! ComposeTableViewController
igor@1338
   319
            // destination.appConfig = config.appConfig
igor@1338
   320
            //            if let draft = draftMessageToCompose {
igor@1338
   321
            //                draft.imapFlags?.seen = true
igor@1338
   322
            //
igor@1338
   323
            //                destination.originalMessage = draft
igor@1338
   324
            //                destination.composeMode = .draft
igor@1338
   325
        //            }
igor@1338
   326
            break
igor@1338
   327
        case .segueShowEmail:
igor@1338
   328
            guard
igor@1338
   329
                let vc = segue.destination as? EmailViewController,
igor@1338
   330
                let cell = sender as? EmailListViewCell,
igor@1338
   331
                let indexPath = self.tableView.indexPath(for: cell),
igor@1338
   332
                let email = cell.messageAt(indexPath: indexPath, config: config) else {
igor@1338
   333
                    return
igor@1338
   334
            }
dirk@1344
   335
            vc.appConfig = config?.appConfig
igor@1338
   336
            vc.message = email
igor@1338
   337
            break
igor@1338
   338
        default: ()
igor@1338
   339
        }
igor@1338
   340
    }
dirk@1350
   341
dirk@1350
   342
    func didChangeInternal(messageFolder: MessageFolder) {
dirk@1350
   343
        if let folder = config?.folder,
dirk@1350
   344
            let message = messageFolder as? Message,
dirk@1350
   345
            folder.contains(message: message) {
dirk@1350
   346
            if let msg = messageFolder as? Message {
dirk@1350
   347
                if msg.isOriginal {
dirk@1535
   348
                    // new message has arrived
dirk@1350
   349
                    self.tableView.reloadData()
dirk@1535
   350
                } else {
dirk@1535
   351
                    // flags must have been changed
dirk@1542
   352
                    if let cell = cellsByMessageID.object(forKey: msg.uuid as NSString) {
dirk@1542
   353
                        cell.updateFlags(message: message)
dirk@1542
   354
                    }
dirk@1350
   355
                }
dirk@1350
   356
            }
dirk@1350
   357
        }
dirk@1350
   358
    }
igor@1338
   359
}
dirk@1348
   360
dirk@1348
   361
// MARK: - MessageFolderDelegate
dirk@1348
   362
dirk@1348
   363
extension EmailListViewController: MessageFolderDelegate {
dirk@1348
   364
    func didChange(messageFolder: MessageFolder) {
dirk@1542
   365
        if let msg = messageFolder as? Message {
dirk@1542
   366
            if msg.isOriginal {
dirk@1542
   367
                Log.info(component: #function, content: "new message")
dirk@1542
   368
            } else {
dirk@1542
   369
                Log.info(component: #function, content: "flag changes?")
dirk@1542
   370
            }
dirk@1542
   371
        }
dirk@1350
   372
        GCD.onMain {
dirk@1350
   373
            self.didChangeInternal(messageFolder: messageFolder)
dirk@1348
   374
        }
dirk@1348
   375
    }
dirk@1348
   376
}