pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author igor <igor@pep-project.org>
Tue, 13 Dec 2016 12:04:52 +0100
changeset 1304 1cbf166d37e9
parent 1301 110da6d9278b
child 1318 77ec1bd3901e
permissions -rw-r--r--
Updated UIRefreshControl
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@808
    12
dirk@810
    13
import MessageModel
dirk@810
    14
dirk@784
    15
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
dirk@784
    16
  switch (lhs, rhs) {
dirk@784
    17
  case let (l?, r?):
dirk@784
    18
    return l < r
dirk@784
    19
  case (nil, _?):
dirk@784
    20
    return true
dirk@784
    21
  default:
dirk@784
    22
    return false
dirk@784
    23
  }
dirk@784
    24
}
dirk@784
    25
dirk@784
    26
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
dirk@784
    27
  switch (lhs, rhs) {
dirk@784
    28
  case let (l?, r?):
dirk@784
    29
    return l > r
dirk@784
    30
  default:
dirk@784
    31
    return rhs < lhs
dirk@784
    32
  }
dirk@784
    33
}
dirk@784
    34
igor@1260
    35
struct EmailListConfig {
igor@1260
    36
    let appConfig: AppConfig
igor@1260
    37
    let account: Account?
igor@1260
    38
    /** The folder to display, if it exists */
igor@1260
    39
    var folder: Folder?
igor@1260
    40
}
dirk@30
    41
dirk@854
    42
class EmailListViewController: UITableViewController {
igor@1260
    43
    
dirk@854
    44
    var comp = "EmailListViewController"
dirk@58
    45
dirk@583
    46
    struct UIState {
dirk@583
    47
        var isSynching: Bool = false
dirk@583
    48
    }
dirk@583
    49
    
dirk@273
    50
    let segueShowEmail = "segueShowEmail"
dirk@397
    51
    let segueCompose = "segueCompose"
dirk@397
    52
    let segueUserSettings = "segueUserSettings"
dirk@30
    53
dirk@583
    54
    var config: EmailListConfig!
dirk@583
    55
dirk@275
    56
    var state = UIState()
igor@1260
    57
    
dirk@502
    58
igor@1301
    59
    //var refreshController: UIRefreshControl!
dirk@594
    60
dirk@705
    61
    /**
dirk@705
    62
     The message that should be saved as a draft when compose gets aborted.
dirk@705
    63
     */
dirk@841
    64
    var draftMessageToStore: Message?
dirk@705
    65
dirk@719
    66
    /**
dirk@719
    67
     When the user taps on a draft email, this is the message that was selected
dirk@719
    68
     and should be given to the compose view.
dirk@719
    69
     */
dirk@841
    70
    var draftMessageToCompose: Message?
igor@1301
    71
    let searchController = UISearchController(searchResultsController: nil)
dirk@719
    72
dirk@745
    73
    required init?(coder aDecoder: NSCoder) {
dirk@745
    74
        super.init(coder: aDecoder)
dirk@745
    75
        self.comp = "EmailListViewController"
dirk@745
    76
    }
dirk@745
    77
dirk@275
    78
    override func viewDidLoad() {
ylandert@935
    79
        super.viewDidLoad()
igor@1301
    80
        
igor@1284
    81
        UIHelper.emailListTableHeight(self.tableView)
igor@1301
    82
        addSearchBar()
igor@1301
    83
        addRefreshControl()
dirk@275
    84
    }
dirk@31
    85
dirk@784
    86
    override func viewWillAppear(_ animated: Bool) {
igor@1301
    87
        super.viewWillAppear(animated)
igor@1301
    88
        
dirk@854
    89
        updateModel()
igor@1301
    90
    }
igor@1301
    91
    
igor@1301
    92
    func addSearchBar() {
igor@1301
    93
        searchController.searchResultsUpdater = self
igor@1301
    94
        searchController.dimsBackgroundDuringPresentation = false
igor@1301
    95
        searchController.delegate = self
igor@1301
    96
        definesPresentationContext = true
igor@1301
    97
        tableView.tableHeaderView = searchController.searchBar
igor@1301
    98
        tableView.setContentOffset(CGPoint(x: 0.0, y: 40.0), animated: false)
igor@1301
    99
    }
igor@1301
   100
    
igor@1301
   101
    func addRefreshControl() {
igor@1301
   102
        refreshControl?.addTarget(self, action: #selector(refreshTableData), for: UIControlEvents.valueChanged)
ana@207
   103
    }
ana@152
   104
dirk@784
   105
    @IBAction func mailSentSegue(_ segue: UIStoryboardSegue) {
dirk@452
   106
    }
dirk@452
   107
dirk@784
   108
    @IBAction func backFromComposeWithoutSavingDraftSegue(_ segue: UIStoryboardSegue) {
dirk@705
   109
    }
dirk@705
   110
dirk@784
   111
    @IBAction func backFromComposeSaveDraftSegue(_ segue: UIStoryboardSegue) {
dirk@867
   112
        guard let message = draftMessageToStore else {
dirk@705
   113
            return
dirk@705
   114
        }
dirk@707
   115
dirk@707
   116
        state.isSynching = true
dirk@707
   117
        updateUI()
dirk@707
   118
dirk@965
   119
        message.imapFlags?.draft = true
dirk@867
   120
dirk@854
   121
        // TODO: IOS 222: Save as draft
dirk@867
   122
        if let folder = draftMessageToStore?.parent as? Folder {
dirk@867
   123
            if folder.folderType == .drafts {
dirk@867
   124
                return
dirk@867
   125
            }
dirk@867
   126
        }
dirk@867
   127
dirk@867
   128
        guard let account = config.account else {
dirk@867
   129
            return
dirk@867
   130
        }
xavier@870
   131
        
hernani@1082
   132
        if account.folder(ofType: FolderType.drafts) != nil {
xavier@870
   133
            return
xavier@870
   134
        }
dirk@705
   135
    }
dirk@705
   136
igor@941
   137
    
igor@941
   138
    @IBAction func showUnreadButtonTapped(_ sender: UIBarButtonItem) {
igor@941
   139
        
igor@941
   140
    }
igor@941
   141
    
dirk@854
   142
    func updateModel() {
igor@1260
   143
        config.folder = MockData.createFolder(config.account!)
dirk@31
   144
    }
dirk@31
   145
dirk@58
   146
    // MARK: - UI State
dirk@58
   147
dirk@58
   148
    func updateUI() {
dirk@784
   149
        UIApplication.shared.isNetworkActivityIndicatorVisible = state.isSynching
dirk@748
   150
        if !state.isSynching {
igor@1260
   151
            refreshControl?.endRefreshing()
dirk@58
   152
        }
dirk@58
   153
    }
dirk@58
   154
dirk@31
   155
    // MARK: - UITableViewDataSource
dirk@31
   156
dirk@784
   157
    override func numberOfSections(in tableView: UITableView) -> Int {
dirk@854
   158
        if let _ = config.folder {
dirk@854
   159
            return 1
dirk@209
   160
        }
dirk@854
   161
        return 0
dirk@31
   162
    }
dirk@31
   163
dirk@784
   164
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dirk@854
   165
        if let fol = config.folder {
dirk@854
   166
            return fol.messageCount()
dirk@31
   167
        }
dirk@31
   168
        return 0
dirk@31
   169
    }
dirk@31
   170
dirk@784
   171
    override func tableView(_ tableView: UITableView,
dirk@784
   172
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
dirk@784
   173
        let cell = tableView.dequeueReusableCell(
dirk@784
   174
            withIdentifier: "EmailListViewCell", for: indexPath) as! EmailListViewCell
igor@1260
   175
        cell.configureCell(indexPath: indexPath, config: config)
dirk@31
   176
        return cell
dirk@31
   177
    }
dirk@31
   178
igor@1260
   179
    
dirk@861
   180
dirk@719
   181
    // MARK: - UITableViewDelegate
dirk@719
   182
dirk@784
   183
    override func tableView(_ tableView: UITableView,
dirk@784
   184
                            didSelectRowAt indexPath: IndexPath) {
dirk@719
   185
        draftMessageToCompose = nil
dirk@719
   186
igor@1260
   187
        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
dirk@719
   188
dirk@855
   189
        if let fol = config.folder {
dirk@855
   190
            if fol.folderType == .drafts {
igor@1260
   191
                draftMessageToCompose = cell.messageAt(indexPath: indexPath, config: config)
dirk@784
   192
                performSegue(withIdentifier: segueCompose, sender: cell)
dirk@719
   193
                return
dirk@719
   194
            }
dirk@719
   195
        }
dirk@719
   196
dirk@784
   197
        performSegue(withIdentifier: segueShowEmail, sender: cell)
dirk@719
   198
    }
dirk@719
   199
dirk@784
   200
    override func tableView(_ tableView: UITableView, editActionsForRowAt
dirk@784
   201
        indexPath: IndexPath)-> [UITableViewRowAction]? {
dirk@784
   202
        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
igor@1260
   203
        if let email = cell.messageAt(indexPath: indexPath, config: config) {
dirk@855
   204
            let isFlagAction = createIsFlagAction(message: email, cell: cell)
dirk@855
   205
            let deleteAction = createDeleteAction(cell)
igor@1242
   206
            //let isReadAction = createIsReadAction(message: email, cell: cell)
igor@1242
   207
            let moreAction = createMoreAction(message: email, cell: cell)
igor@1242
   208
            return [deleteAction,isFlagAction,moreAction]
dirk@855
   209
        }
dirk@855
   210
        return nil
dirk@744
   211
    }
dirk@744
   212
dirk@719
   213
    // MARK: - Misc
dirk@719
   214
dirk@784
   215
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
dirk@606
   216
        // Make sure the current account is set, if defined
dirk@606
   217
        config.appConfig.currentAccount = config.account
dirk@606
   218
dirk@361
   219
        if segue.identifier == segueCompose {
yves@1278
   220
            //let destination = segue.destination as! ComposeTableViewController
yves@1278
   221
            // destination.appConfig = config.appConfig
yves@1278
   222
//            if let draft = draftMessageToCompose {
yves@1278
   223
//                draft.imapFlags?.seen = true
yves@1278
   224
//
yves@1278
   225
//                destination.originalMessage = draft
yves@1278
   226
//                destination.composeMode = .draft
yves@1278
   227
//            }
dirk@273
   228
        } else if segue.identifier == segueShowEmail {
dirk@273
   229
            guard
dirk@784
   230
                let vc = segue.destination as? EmailViewController,
igor@1260
   231
                let cell = sender as? EmailListViewCell,
dirk@784
   232
                let indexPath = self.tableView.indexPath(for: cell),
igor@1260
   233
                let email = cell.messageAt(indexPath: indexPath, config: config) else {
dirk@273
   234
                    return
dirk@273
   235
            }
dirk@583
   236
            vc.appConfig = config.appConfig
dirk@273
   237
            vc.message = email
ana@246
   238
        }
ana@246
   239
    }
dirk@744
   240
dirk@855
   241
    func syncFlagsToServer(message: Message) {
dirk@855
   242
        // TODO: IOS 222: Sync flags back to server
dirk@744
   243
    }
dirk@744
   244
dirk@855
   245
    func createIsFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@744
   246
        // preparing the title action to show when user swipe
igor@1242
   247
//        var localizedIsFlagTitle = " "
igor@1242
   248
//        if (isImportant(message: message)) {
igor@1242
   249
//            localizedIsFlagTitle = NSLocalizedString(
igor@1242
   250
//                "Unflag",
igor@1242
   251
//                comment: "Unflag button title in swipe action on EmailListViewController")
igor@1242
   252
//        } else {
igor@1242
   253
//            localizedIsFlagTitle = NSLocalizedString(
igor@1242
   254
//                "Flag",
igor@1242
   255
//                comment: "Flag button title in swipe action on EmailListViewController")
igor@1242
   256
//        }
dirk@744
   257
dirk@744
   258
        // preparing action to trigger when user swipe
dirk@784
   259
        let isFlagCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
dirk@744
   260
            { (action, indexPath) in
igor@1260
   261
                if (cell.isImportant(message: message)) {
dirk@965
   262
                    message.imapFlags?.flagged = false
dirk@744
   263
dirk@744
   264
                } else {
dirk@965
   265
                    message.imapFlags?.flagged = true
dirk@744
   266
                }
dirk@855
   267
                self.syncFlagsToServer(message: message)
dirk@784
   268
                self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@744
   269
        }
dirk@744
   270
        // creating the action
igor@1242
   271
        let isFlagAction = UITableViewRowAction(style: .normal, title: "          ",
dirk@744
   272
                                                handler: isFlagCompletionHandler)
dirk@744
   273
        // changing default action color
igor@1242
   274
        let swipeFlagImage = UIImage(named: "swipe-flag")
igor@1242
   275
        let flagIconColor = UIColor(patternImage: swipeFlagImage!)
igor@1242
   276
        isFlagAction.backgroundColor = flagIconColor
dirk@744
   277
dirk@744
   278
        return isFlagAction
dirk@744
   279
    }
dirk@744
   280
dirk@784
   281
    func createDeleteAction (_ cell: EmailListViewCell) -> UITableViewRowAction {
dirk@744
   282
dirk@744
   283
        // preparing the title action to show when user swipe
dirk@744
   284
dirk@784
   285
        let deleteCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
dirk@744
   286
            { (action, indexPath) in
igor@1260
   287
                let message = cell.messageAt(indexPath: indexPath, config: self.config)
dirk@965
   288
                message?.imapFlags?.deleted = true
dirk@855
   289
                self.syncFlagsToServer(message: message!)
dirk@744
   290
        }
dirk@744
   291
dirk@744
   292
        // creating the action
igor@1242
   293
        let deleteAction = UITableViewRowAction(style: .normal, title: "          ",
dirk@744
   294
                                                handler: deleteCompletionHandler)
igor@1242
   295
        let swipeTrashImage = UIImage(named: "swipe-trash")
igor@1242
   296
        let trashIconColor = UIColor(patternImage: swipeTrashImage!)
igor@1242
   297
        deleteAction.backgroundColor = trashIconColor
dirk@744
   298
        return deleteAction
dirk@744
   299
    }
dirk@744
   300
dirk@855
   301
    func createIsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@744
   302
        // preparing the title action to show when user swipe
dirk@744
   303
        var localizedisReadTitle = " "
igor@1260
   304
        if (cell.isRead(message: message)) {
dirk@855
   305
            localizedisReadTitle = NSLocalizedString(
dirk@855
   306
                "Unread",
dirk@855
   307
                comment: "Unread button title in swipe action on EmailListViewController")
dirk@744
   308
        } else {
dirk@855
   309
            localizedisReadTitle = NSLocalizedString(
dirk@855
   310
                "Read",
dirk@855
   311
                comment: "Read button title in swipe action on EmailListViewController")
dirk@744
   312
        }
dirk@744
   313
dirk@744
   314
        // creating the action
dirk@784
   315
        let isReadCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
dirk@744
   316
            { (action, indexPath) in
igor@1260
   317
                if (cell.isRead(message: message)) {
dirk@965
   318
                    message.imapFlags?.seen = false
dirk@744
   319
                } else {
dirk@965
   320
                    message.imapFlags?.seen = true
dirk@744
   321
                }
dirk@855
   322
                self.syncFlagsToServer(message: message)
dirk@784
   323
                self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@744
   324
        }
dirk@784
   325
        let isReadAction = UITableViewRowAction(style: .default, title: localizedisReadTitle,
dirk@744
   326
                                                handler: isReadCompletionHandler)
dirk@784
   327
        isReadAction.backgroundColor = UIColor.blue
dirk@744
   328
dirk@744
   329
        return isReadAction
dirk@744
   330
    }
igor@1242
   331
    
igor@1242
   332
    func createMoreAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
igor@1242
   333
        let moreCompletitionHandler :(UITableViewRowAction, IndexPath) -> Void = {(action, indexPath) in
igor@1242
   334
            self.showMoreActionSheet(cell: cell)
igor@1242
   335
        }
igor@1242
   336
        let moreAction = UITableViewRowAction(style: .normal, title: "          ", handler: moreCompletitionHandler)
igor@1242
   337
        let swipeMoreImage = UIImage(named: "swipe-more")
igor@1242
   338
        let moreIconColor = UIColor(patternImage: swipeMoreImage!)
igor@1242
   339
        moreAction.backgroundColor = moreIconColor
igor@1242
   340
        return moreAction
igor@1242
   341
    }
igor@1242
   342
    
igor@1242
   343
    // MARK: - Action Sheet
igor@1242
   344
    
igor@1242
   345
    func showMoreActionSheet(cell: EmailListViewCell) {
igor@1242
   346
        let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
igor@1242
   347
        let cancelAction = createCancelAction()
igor@1242
   348
        let replyAction = createReplyAction(cell: cell)
igor@1242
   349
        let forwardAction = createForwardAction(cell: cell)
igor@1242
   350
        let markAction = createMarkAction()
igor@1242
   351
        alertControler.addAction(cancelAction)
igor@1242
   352
        alertControler.addAction(replyAction)
igor@1242
   353
        alertControler.addAction(forwardAction)
igor@1242
   354
        alertControler.addAction(markAction)
igor@1242
   355
        present(alertControler, animated: true, completion: nil)
igor@1242
   356
    }
igor@1242
   357
    
igor@1242
   358
    // MARK: - Action Sheet Actions
igor@1242
   359
igor@1242
   360
    func createCancelAction() -> UIAlertAction {
igor@1242
   361
      return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in}
igor@1242
   362
    }
igor@1242
   363
    
igor@1242
   364
    func createReplyAction(cell: EmailListViewCell) ->  UIAlertAction {
igor@1242
   365
        return UIAlertAction(title: "Reply", style: .default) { (action) in
igor@1242
   366
            self.performSegue(withIdentifier: self.segueCompose, sender: cell)
igor@1242
   367
        }
igor@1242
   368
    }
igor@1242
   369
    
igor@1242
   370
    func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
igor@1242
   371
        return UIAlertAction(title: "Forward", style: .default) { (action) in
igor@1242
   372
            self.performSegue(withIdentifier: self.segueCompose, sender: cell)
igor@1242
   373
        }
igor@1242
   374
    }
igor@1242
   375
    
igor@1242
   376
    func createMarkAction() -> UIAlertAction {
igor@1242
   377
        return UIAlertAction(title: "Mark", style: .default) { (action) in
igor@1242
   378
        }
igor@1242
   379
    }
igor@1301
   380
    
igor@1301
   381
    // MARK: - Content Search
igor@1301
   382
    
igor@1301
   383
    func filterContentForSearchText(searchText: String) {
igor@1301
   384
        
igor@1301
   385
    }
igor@1301
   386
    
igor@1301
   387
    // MARK: - Refresh Table Data
igor@1301
   388
    
igor@1301
   389
    func refreshTableData() {
igor@1301
   390
        refreshControl?.beginRefreshing()
igor@1301
   391
        refreshControl?.endRefreshing()
igor@1301
   392
    }
igor@1301
   393
dirk@784
   394
}
igor@1301
   395
igor@1301
   396
extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
igor@1301
   397
    public func updateSearchResults(for searchController: UISearchController) {
igor@1301
   398
        filterContentForSearchText(searchText: searchController.searchBar.text!)
igor@1301
   399
    }
igor@1301
   400
    
igor@1301
   401
    func didDismissSearchController(_ searchController: UISearchController) {
igor@1301
   402
    }
igor@1301
   403
}