pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author igor <igor@pep-project.org>
Wed, 14 Dec 2016 13:52:21 +0100
changeset 1318 77ec1bd3901e
parent 1304 1cbf166d37e9
child 1330 e411dbeb6048
permissions -rw-r--r--
Updated Email List and SearchBar
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!)
igor@1318
   144
        tableView.reloadData()
dirk@31
   145
    }
dirk@31
   146
dirk@58
   147
    // MARK: - UI State
dirk@58
   148
dirk@58
   149
    func updateUI() {
dirk@784
   150
        UIApplication.shared.isNetworkActivityIndicatorVisible = state.isSynching
dirk@748
   151
        if !state.isSynching {
igor@1260
   152
            refreshControl?.endRefreshing()
dirk@58
   153
        }
dirk@58
   154
    }
dirk@58
   155
dirk@31
   156
    // MARK: - UITableViewDataSource
dirk@31
   157
dirk@784
   158
    override func numberOfSections(in tableView: UITableView) -> Int {
dirk@854
   159
        if let _ = config.folder {
dirk@854
   160
            return 1
dirk@209
   161
        }
dirk@854
   162
        return 0
dirk@31
   163
    }
dirk@31
   164
dirk@784
   165
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
igor@1318
   166
        if let fol = config.folder  {
dirk@854
   167
            return fol.messageCount()
dirk@31
   168
        }
dirk@31
   169
        return 0
dirk@31
   170
    }
dirk@31
   171
dirk@784
   172
    override func tableView(_ tableView: UITableView,
dirk@784
   173
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
dirk@784
   174
        let cell = tableView.dequeueReusableCell(
dirk@784
   175
            withIdentifier: "EmailListViewCell", for: indexPath) as! EmailListViewCell
igor@1260
   176
        cell.configureCell(indexPath: indexPath, config: config)
dirk@31
   177
        return cell
dirk@31
   178
    }
dirk@31
   179
igor@1260
   180
    
dirk@861
   181
dirk@719
   182
    // MARK: - UITableViewDelegate
dirk@719
   183
dirk@784
   184
    override func tableView(_ tableView: UITableView,
dirk@784
   185
                            didSelectRowAt indexPath: IndexPath) {
dirk@719
   186
        draftMessageToCompose = nil
dirk@719
   187
igor@1260
   188
        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
dirk@719
   189
dirk@855
   190
        if let fol = config.folder {
dirk@855
   191
            if fol.folderType == .drafts {
igor@1260
   192
                draftMessageToCompose = cell.messageAt(indexPath: indexPath, config: config)
dirk@784
   193
                performSegue(withIdentifier: segueCompose, sender: cell)
dirk@719
   194
                return
dirk@719
   195
            }
dirk@719
   196
        }
dirk@719
   197
dirk@784
   198
        performSegue(withIdentifier: segueShowEmail, sender: cell)
dirk@719
   199
    }
dirk@719
   200
dirk@784
   201
    override func tableView(_ tableView: UITableView, editActionsForRowAt
dirk@784
   202
        indexPath: IndexPath)-> [UITableViewRowAction]? {
dirk@784
   203
        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
igor@1260
   204
        if let email = cell.messageAt(indexPath: indexPath, config: config) {
dirk@855
   205
            let isFlagAction = createIsFlagAction(message: email, cell: cell)
dirk@855
   206
            let deleteAction = createDeleteAction(cell)
igor@1242
   207
            //let isReadAction = createIsReadAction(message: email, cell: cell)
igor@1242
   208
            let moreAction = createMoreAction(message: email, cell: cell)
igor@1242
   209
            return [deleteAction,isFlagAction,moreAction]
dirk@855
   210
        }
dirk@855
   211
        return nil
dirk@744
   212
    }
dirk@744
   213
dirk@719
   214
    // MARK: - Misc
dirk@719
   215
dirk@784
   216
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
dirk@606
   217
        // Make sure the current account is set, if defined
dirk@606
   218
        config.appConfig.currentAccount = config.account
dirk@606
   219
dirk@361
   220
        if segue.identifier == segueCompose {
yves@1278
   221
            //let destination = segue.destination as! ComposeTableViewController
yves@1278
   222
            // destination.appConfig = config.appConfig
yves@1278
   223
//            if let draft = draftMessageToCompose {
yves@1278
   224
//                draft.imapFlags?.seen = true
yves@1278
   225
//
yves@1278
   226
//                destination.originalMessage = draft
yves@1278
   227
//                destination.composeMode = .draft
yves@1278
   228
//            }
dirk@273
   229
        } else if segue.identifier == segueShowEmail {
dirk@273
   230
            guard
dirk@784
   231
                let vc = segue.destination as? EmailViewController,
igor@1260
   232
                let cell = sender as? EmailListViewCell,
dirk@784
   233
                let indexPath = self.tableView.indexPath(for: cell),
igor@1260
   234
                let email = cell.messageAt(indexPath: indexPath, config: config) else {
dirk@273
   235
                    return
dirk@273
   236
            }
dirk@583
   237
            vc.appConfig = config.appConfig
dirk@273
   238
            vc.message = email
ana@246
   239
        }
ana@246
   240
    }
dirk@744
   241
dirk@855
   242
    func syncFlagsToServer(message: Message) {
dirk@855
   243
        // TODO: IOS 222: Sync flags back to server
dirk@744
   244
    }
dirk@744
   245
dirk@855
   246
    func createIsFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@744
   247
        // preparing the title action to show when user swipe
igor@1242
   248
//        var localizedIsFlagTitle = " "
igor@1242
   249
//        if (isImportant(message: message)) {
igor@1242
   250
//            localizedIsFlagTitle = NSLocalizedString(
igor@1242
   251
//                "Unflag",
igor@1242
   252
//                comment: "Unflag button title in swipe action on EmailListViewController")
igor@1242
   253
//        } else {
igor@1242
   254
//            localizedIsFlagTitle = NSLocalizedString(
igor@1242
   255
//                "Flag",
igor@1242
   256
//                comment: "Flag button title in swipe action on EmailListViewController")
igor@1242
   257
//        }
dirk@744
   258
dirk@744
   259
        // preparing action to trigger when user swipe
dirk@784
   260
        let isFlagCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
dirk@744
   261
            { (action, indexPath) in
igor@1260
   262
                if (cell.isImportant(message: message)) {
dirk@965
   263
                    message.imapFlags?.flagged = false
dirk@744
   264
dirk@744
   265
                } else {
dirk@965
   266
                    message.imapFlags?.flagged = true
dirk@744
   267
                }
dirk@855
   268
                self.syncFlagsToServer(message: message)
dirk@784
   269
                self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@744
   270
        }
dirk@744
   271
        // creating the action
igor@1242
   272
        let isFlagAction = UITableViewRowAction(style: .normal, title: "          ",
dirk@744
   273
                                                handler: isFlagCompletionHandler)
dirk@744
   274
        // changing default action color
igor@1242
   275
        let swipeFlagImage = UIImage(named: "swipe-flag")
igor@1242
   276
        let flagIconColor = UIColor(patternImage: swipeFlagImage!)
igor@1242
   277
        isFlagAction.backgroundColor = flagIconColor
dirk@744
   278
dirk@744
   279
        return isFlagAction
dirk@744
   280
    }
dirk@744
   281
dirk@784
   282
    func createDeleteAction (_ cell: EmailListViewCell) -> UITableViewRowAction {
dirk@744
   283
dirk@744
   284
        // preparing the title action to show when user swipe
dirk@744
   285
dirk@784
   286
        let deleteCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
dirk@744
   287
            { (action, indexPath) in
igor@1260
   288
                let message = cell.messageAt(indexPath: indexPath, config: self.config)
dirk@965
   289
                message?.imapFlags?.deleted = true
dirk@855
   290
                self.syncFlagsToServer(message: message!)
dirk@744
   291
        }
dirk@744
   292
dirk@744
   293
        // creating the action
igor@1242
   294
        let deleteAction = UITableViewRowAction(style: .normal, title: "          ",
dirk@744
   295
                                                handler: deleteCompletionHandler)
igor@1242
   296
        let swipeTrashImage = UIImage(named: "swipe-trash")
igor@1242
   297
        let trashIconColor = UIColor(patternImage: swipeTrashImage!)
igor@1242
   298
        deleteAction.backgroundColor = trashIconColor
dirk@744
   299
        return deleteAction
dirk@744
   300
    }
dirk@744
   301
dirk@855
   302
    func createIsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@744
   303
        // preparing the title action to show when user swipe
dirk@744
   304
        var localizedisReadTitle = " "
igor@1260
   305
        if (cell.isRead(message: message)) {
dirk@855
   306
            localizedisReadTitle = NSLocalizedString(
dirk@855
   307
                "Unread",
dirk@855
   308
                comment: "Unread button title in swipe action on EmailListViewController")
dirk@744
   309
        } else {
dirk@855
   310
            localizedisReadTitle = NSLocalizedString(
dirk@855
   311
                "Read",
dirk@855
   312
                comment: "Read button title in swipe action on EmailListViewController")
dirk@744
   313
        }
dirk@744
   314
dirk@744
   315
        // creating the action
dirk@784
   316
        let isReadCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
dirk@744
   317
            { (action, indexPath) in
igor@1260
   318
                if (cell.isRead(message: message)) {
dirk@965
   319
                    message.imapFlags?.seen = false
dirk@744
   320
                } else {
dirk@965
   321
                    message.imapFlags?.seen = true
dirk@744
   322
                }
dirk@855
   323
                self.syncFlagsToServer(message: message)
dirk@784
   324
                self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@744
   325
        }
dirk@784
   326
        let isReadAction = UITableViewRowAction(style: .default, title: localizedisReadTitle,
dirk@744
   327
                                                handler: isReadCompletionHandler)
dirk@784
   328
        isReadAction.backgroundColor = UIColor.blue
dirk@744
   329
dirk@744
   330
        return isReadAction
dirk@744
   331
    }
igor@1242
   332
    
igor@1242
   333
    func createMoreAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
igor@1242
   334
        let moreCompletitionHandler :(UITableViewRowAction, IndexPath) -> Void = {(action, indexPath) in
igor@1242
   335
            self.showMoreActionSheet(cell: cell)
igor@1242
   336
        }
igor@1242
   337
        let moreAction = UITableViewRowAction(style: .normal, title: "          ", handler: moreCompletitionHandler)
igor@1242
   338
        let swipeMoreImage = UIImage(named: "swipe-more")
igor@1242
   339
        let moreIconColor = UIColor(patternImage: swipeMoreImage!)
igor@1242
   340
        moreAction.backgroundColor = moreIconColor
igor@1242
   341
        return moreAction
igor@1242
   342
    }
igor@1242
   343
    
igor@1242
   344
    // MARK: - Action Sheet
igor@1242
   345
    
igor@1242
   346
    func showMoreActionSheet(cell: EmailListViewCell) {
igor@1242
   347
        let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
igor@1242
   348
        let cancelAction = createCancelAction()
igor@1242
   349
        let replyAction = createReplyAction(cell: cell)
igor@1242
   350
        let forwardAction = createForwardAction(cell: cell)
igor@1242
   351
        let markAction = createMarkAction()
igor@1242
   352
        alertControler.addAction(cancelAction)
igor@1242
   353
        alertControler.addAction(replyAction)
igor@1242
   354
        alertControler.addAction(forwardAction)
igor@1242
   355
        alertControler.addAction(markAction)
igor@1242
   356
        present(alertControler, animated: true, completion: nil)
igor@1242
   357
    }
igor@1242
   358
    
igor@1242
   359
    // MARK: - Action Sheet Actions
igor@1242
   360
igor@1242
   361
    func createCancelAction() -> UIAlertAction {
igor@1242
   362
      return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in}
igor@1242
   363
    }
igor@1242
   364
    
igor@1242
   365
    func createReplyAction(cell: EmailListViewCell) ->  UIAlertAction {
igor@1242
   366
        return UIAlertAction(title: "Reply", style: .default) { (action) in
igor@1242
   367
            self.performSegue(withIdentifier: self.segueCompose, sender: cell)
igor@1242
   368
        }
igor@1242
   369
    }
igor@1242
   370
    
igor@1242
   371
    func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
igor@1242
   372
        return UIAlertAction(title: "Forward", style: .default) { (action) in
igor@1242
   373
            self.performSegue(withIdentifier: self.segueCompose, sender: cell)
igor@1242
   374
        }
igor@1242
   375
    }
igor@1242
   376
    
igor@1242
   377
    func createMarkAction() -> UIAlertAction {
igor@1242
   378
        return UIAlertAction(title: "Mark", style: .default) { (action) in
igor@1242
   379
        }
igor@1242
   380
    }
igor@1301
   381
    
igor@1301
   382
    // MARK: - Content Search
igor@1301
   383
    
igor@1301
   384
    func filterContentForSearchText(searchText: String) {
igor@1301
   385
        
igor@1301
   386
    }
igor@1301
   387
    
igor@1301
   388
    // MARK: - Refresh Table Data
igor@1301
   389
    
igor@1301
   390
    func refreshTableData() {
igor@1301
   391
        refreshControl?.beginRefreshing()
igor@1301
   392
        refreshControl?.endRefreshing()
igor@1301
   393
    }
igor@1301
   394
dirk@784
   395
}
igor@1301
   396
igor@1301
   397
extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
igor@1301
   398
    public func updateSearchResults(for searchController: UISearchController) {
igor@1301
   399
        filterContentForSearchText(searchText: searchController.searchBar.text!)
igor@1301
   400
    }
igor@1301
   401
    
igor@1301
   402
    func didDismissSearchController(_ searchController: UISearchController) {
igor@1301
   403
    }
igor@1301
   404
}