pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author igor <igor@pep-project.org>
Fri, 09 Dec 2016 11:49:31 +0100
changeset 1284 49a5197a25e5
parent 1278 c3bc82cbfbe3
child 1301 110da6d9278b
permissions -rw-r--r--
Changes on email list 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@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
dirk@594
    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?
dirk@719
    71
dirk@745
    72
    required init?(coder aDecoder: NSCoder) {
dirk@745
    73
        super.init(coder: aDecoder)
dirk@745
    74
        self.comp = "EmailListViewController"
dirk@745
    75
    }
dirk@745
    76
dirk@275
    77
    override func viewDidLoad() {
ylandert@935
    78
        super.viewDidLoad()
igor@1284
    79
        UIHelper.emailListTableHeight(self.tableView)
dirk@275
    80
    }
dirk@31
    81
dirk@784
    82
    override func viewWillAppear(_ animated: Bool) {
dirk@854
    83
        updateModel()
ana@207
    84
        super.viewWillAppear(animated)
ana@207
    85
    }
ana@152
    86
dirk@784
    87
    @IBAction func mailSentSegue(_ segue: UIStoryboardSegue) {
dirk@452
    88
    }
dirk@452
    89
dirk@784
    90
    @IBAction func backFromComposeWithoutSavingDraftSegue(_ segue: UIStoryboardSegue) {
dirk@705
    91
    }
dirk@705
    92
dirk@784
    93
    @IBAction func backFromComposeSaveDraftSegue(_ segue: UIStoryboardSegue) {
dirk@867
    94
        guard let message = draftMessageToStore else {
dirk@705
    95
            return
dirk@705
    96
        }
dirk@707
    97
dirk@707
    98
        state.isSynching = true
dirk@707
    99
        updateUI()
dirk@707
   100
dirk@965
   101
        message.imapFlags?.draft = true
dirk@867
   102
dirk@854
   103
        // TODO: IOS 222: Save as draft
dirk@867
   104
        if let folder = draftMessageToStore?.parent as? Folder {
dirk@867
   105
            if folder.folderType == .drafts {
dirk@867
   106
                return
dirk@867
   107
            }
dirk@867
   108
        }
dirk@867
   109
dirk@867
   110
        guard let account = config.account else {
dirk@867
   111
            return
dirk@867
   112
        }
xavier@870
   113
        
hernani@1082
   114
        if account.folder(ofType: FolderType.drafts) != nil {
xavier@870
   115
            return
xavier@870
   116
        }
dirk@705
   117
    }
dirk@705
   118
igor@941
   119
    
igor@941
   120
    @IBAction func showUnreadButtonTapped(_ sender: UIBarButtonItem) {
igor@941
   121
        
igor@941
   122
    }
igor@941
   123
    
dirk@854
   124
    func updateModel() {
igor@1260
   125
        config.folder = MockData.createFolder(config.account!)
dirk@31
   126
    }
dirk@31
   127
dirk@58
   128
    // MARK: - UI State
dirk@58
   129
dirk@58
   130
    func updateUI() {
dirk@784
   131
        UIApplication.shared.isNetworkActivityIndicatorVisible = state.isSynching
dirk@748
   132
        if !state.isSynching {
igor@1260
   133
            refreshControl?.endRefreshing()
dirk@58
   134
        }
dirk@58
   135
    }
dirk@58
   136
dirk@31
   137
    // MARK: - UITableViewDataSource
dirk@31
   138
dirk@784
   139
    override func numberOfSections(in tableView: UITableView) -> Int {
dirk@854
   140
        if let _ = config.folder {
dirk@854
   141
            return 1
dirk@209
   142
        }
dirk@854
   143
        return 0
dirk@31
   144
    }
dirk@31
   145
dirk@784
   146
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dirk@854
   147
        if let fol = config.folder {
dirk@854
   148
            return fol.messageCount()
dirk@31
   149
        }
dirk@31
   150
        return 0
dirk@31
   151
    }
dirk@31
   152
dirk@784
   153
    override func tableView(_ tableView: UITableView,
dirk@784
   154
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
dirk@784
   155
        let cell = tableView.dequeueReusableCell(
dirk@784
   156
            withIdentifier: "EmailListViewCell", for: indexPath) as! EmailListViewCell
igor@1260
   157
        cell.configureCell(indexPath: indexPath, config: config)
dirk@31
   158
        return cell
dirk@31
   159
    }
dirk@31
   160
igor@1260
   161
    
dirk@861
   162
dirk@719
   163
    // MARK: - UITableViewDelegate
dirk@719
   164
dirk@784
   165
    override func tableView(_ tableView: UITableView,
dirk@784
   166
                            didSelectRowAt indexPath: IndexPath) {
dirk@719
   167
        draftMessageToCompose = nil
dirk@719
   168
igor@1260
   169
        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
dirk@719
   170
dirk@855
   171
        if let fol = config.folder {
dirk@855
   172
            if fol.folderType == .drafts {
igor@1260
   173
                draftMessageToCompose = cell.messageAt(indexPath: indexPath, config: config)
dirk@784
   174
                performSegue(withIdentifier: segueCompose, sender: cell)
dirk@719
   175
                return
dirk@719
   176
            }
dirk@719
   177
        }
dirk@719
   178
dirk@784
   179
        performSegue(withIdentifier: segueShowEmail, sender: cell)
dirk@719
   180
    }
dirk@719
   181
dirk@784
   182
    override func tableView(_ tableView: UITableView, editActionsForRowAt
dirk@784
   183
        indexPath: IndexPath)-> [UITableViewRowAction]? {
dirk@784
   184
        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
igor@1260
   185
        if let email = cell.messageAt(indexPath: indexPath, config: config) {
dirk@855
   186
            let isFlagAction = createIsFlagAction(message: email, cell: cell)
dirk@855
   187
            let deleteAction = createDeleteAction(cell)
igor@1242
   188
            //let isReadAction = createIsReadAction(message: email, cell: cell)
igor@1242
   189
            let moreAction = createMoreAction(message: email, cell: cell)
igor@1242
   190
            return [deleteAction,isFlagAction,moreAction]
dirk@855
   191
        }
dirk@855
   192
        return nil
dirk@744
   193
    }
dirk@744
   194
dirk@719
   195
    // MARK: - Misc
dirk@719
   196
dirk@784
   197
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
dirk@606
   198
        // Make sure the current account is set, if defined
dirk@606
   199
        config.appConfig.currentAccount = config.account
dirk@606
   200
dirk@361
   201
        if segue.identifier == segueCompose {
yves@1278
   202
            //let destination = segue.destination as! ComposeTableViewController
yves@1278
   203
            // destination.appConfig = config.appConfig
yves@1278
   204
//            if let draft = draftMessageToCompose {
yves@1278
   205
//                draft.imapFlags?.seen = true
yves@1278
   206
//
yves@1278
   207
//                destination.originalMessage = draft
yves@1278
   208
//                destination.composeMode = .draft
yves@1278
   209
//            }
dirk@273
   210
        } else if segue.identifier == segueShowEmail {
dirk@273
   211
            guard
dirk@784
   212
                let vc = segue.destination as? EmailViewController,
igor@1260
   213
                let cell = sender as? EmailListViewCell,
dirk@784
   214
                let indexPath = self.tableView.indexPath(for: cell),
igor@1260
   215
                let email = cell.messageAt(indexPath: indexPath, config: config) else {
dirk@273
   216
                    return
dirk@273
   217
            }
dirk@583
   218
            vc.appConfig = config.appConfig
dirk@273
   219
            vc.message = email
ana@246
   220
        }
ana@246
   221
    }
dirk@744
   222
dirk@855
   223
    func syncFlagsToServer(message: Message) {
dirk@855
   224
        // TODO: IOS 222: Sync flags back to server
dirk@744
   225
    }
dirk@744
   226
dirk@855
   227
    func createIsFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@744
   228
        // preparing the title action to show when user swipe
igor@1242
   229
//        var localizedIsFlagTitle = " "
igor@1242
   230
//        if (isImportant(message: message)) {
igor@1242
   231
//            localizedIsFlagTitle = NSLocalizedString(
igor@1242
   232
//                "Unflag",
igor@1242
   233
//                comment: "Unflag button title in swipe action on EmailListViewController")
igor@1242
   234
//        } else {
igor@1242
   235
//            localizedIsFlagTitle = NSLocalizedString(
igor@1242
   236
//                "Flag",
igor@1242
   237
//                comment: "Flag button title in swipe action on EmailListViewController")
igor@1242
   238
//        }
dirk@744
   239
dirk@744
   240
        // preparing action to trigger when user swipe
dirk@784
   241
        let isFlagCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
dirk@744
   242
            { (action, indexPath) in
igor@1260
   243
                if (cell.isImportant(message: message)) {
dirk@965
   244
                    message.imapFlags?.flagged = false
dirk@744
   245
dirk@744
   246
                } else {
dirk@965
   247
                    message.imapFlags?.flagged = true
dirk@744
   248
                }
dirk@855
   249
                self.syncFlagsToServer(message: message)
dirk@784
   250
                self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@744
   251
        }
dirk@744
   252
        // creating the action
igor@1242
   253
        let isFlagAction = UITableViewRowAction(style: .normal, title: "          ",
dirk@744
   254
                                                handler: isFlagCompletionHandler)
dirk@744
   255
        // changing default action color
igor@1242
   256
        let swipeFlagImage = UIImage(named: "swipe-flag")
igor@1242
   257
        let flagIconColor = UIColor(patternImage: swipeFlagImage!)
igor@1242
   258
        isFlagAction.backgroundColor = flagIconColor
dirk@744
   259
dirk@744
   260
        return isFlagAction
dirk@744
   261
    }
dirk@744
   262
dirk@784
   263
    func createDeleteAction (_ cell: EmailListViewCell) -> UITableViewRowAction {
dirk@744
   264
dirk@744
   265
        // preparing the title action to show when user swipe
dirk@744
   266
dirk@784
   267
        let deleteCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
dirk@744
   268
            { (action, indexPath) in
igor@1260
   269
                let message = cell.messageAt(indexPath: indexPath, config: self.config)
dirk@965
   270
                message?.imapFlags?.deleted = true
dirk@855
   271
                self.syncFlagsToServer(message: message!)
dirk@744
   272
        }
dirk@744
   273
dirk@744
   274
        // creating the action
igor@1242
   275
        let deleteAction = UITableViewRowAction(style: .normal, title: "          ",
dirk@744
   276
                                                handler: deleteCompletionHandler)
igor@1242
   277
        let swipeTrashImage = UIImage(named: "swipe-trash")
igor@1242
   278
        let trashIconColor = UIColor(patternImage: swipeTrashImage!)
igor@1242
   279
        deleteAction.backgroundColor = trashIconColor
dirk@744
   280
        return deleteAction
dirk@744
   281
    }
dirk@744
   282
dirk@855
   283
    func createIsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@744
   284
        // preparing the title action to show when user swipe
dirk@744
   285
        var localizedisReadTitle = " "
igor@1260
   286
        if (cell.isRead(message: message)) {
dirk@855
   287
            localizedisReadTitle = NSLocalizedString(
dirk@855
   288
                "Unread",
dirk@855
   289
                comment: "Unread button title in swipe action on EmailListViewController")
dirk@744
   290
        } else {
dirk@855
   291
            localizedisReadTitle = NSLocalizedString(
dirk@855
   292
                "Read",
dirk@855
   293
                comment: "Read button title in swipe action on EmailListViewController")
dirk@744
   294
        }
dirk@744
   295
dirk@744
   296
        // creating the action
dirk@784
   297
        let isReadCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
dirk@744
   298
            { (action, indexPath) in
igor@1260
   299
                if (cell.isRead(message: message)) {
dirk@965
   300
                    message.imapFlags?.seen = false
dirk@744
   301
                } else {
dirk@965
   302
                    message.imapFlags?.seen = true
dirk@744
   303
                }
dirk@855
   304
                self.syncFlagsToServer(message: message)
dirk@784
   305
                self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@744
   306
        }
dirk@784
   307
        let isReadAction = UITableViewRowAction(style: .default, title: localizedisReadTitle,
dirk@744
   308
                                                handler: isReadCompletionHandler)
dirk@784
   309
        isReadAction.backgroundColor = UIColor.blue
dirk@744
   310
dirk@744
   311
        return isReadAction
dirk@744
   312
    }
igor@1242
   313
    
igor@1242
   314
    func createMoreAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
igor@1242
   315
        let moreCompletitionHandler :(UITableViewRowAction, IndexPath) -> Void = {(action, indexPath) in
igor@1242
   316
            self.showMoreActionSheet(cell: cell)
igor@1242
   317
        }
igor@1242
   318
        let moreAction = UITableViewRowAction(style: .normal, title: "          ", handler: moreCompletitionHandler)
igor@1242
   319
        let swipeMoreImage = UIImage(named: "swipe-more")
igor@1242
   320
        let moreIconColor = UIColor(patternImage: swipeMoreImage!)
igor@1242
   321
        moreAction.backgroundColor = moreIconColor
igor@1242
   322
        return moreAction
igor@1242
   323
    }
igor@1242
   324
    
igor@1242
   325
    // MARK: - Action Sheet
igor@1242
   326
    
igor@1242
   327
    func showMoreActionSheet(cell: EmailListViewCell) {
igor@1242
   328
        let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
igor@1242
   329
        let cancelAction = createCancelAction()
igor@1242
   330
        let replyAction = createReplyAction(cell: cell)
igor@1242
   331
        let forwardAction = createForwardAction(cell: cell)
igor@1242
   332
        let markAction = createMarkAction()
igor@1242
   333
        alertControler.addAction(cancelAction)
igor@1242
   334
        alertControler.addAction(replyAction)
igor@1242
   335
        alertControler.addAction(forwardAction)
igor@1242
   336
        alertControler.addAction(markAction)
igor@1242
   337
        present(alertControler, animated: true, completion: nil)
igor@1242
   338
    }
igor@1242
   339
    
igor@1242
   340
    // MARK: - Action Sheet Actions
igor@1242
   341
igor@1242
   342
    func createCancelAction() -> UIAlertAction {
igor@1242
   343
      return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in}
igor@1242
   344
    }
igor@1242
   345
    
igor@1242
   346
    func createReplyAction(cell: EmailListViewCell) ->  UIAlertAction {
igor@1242
   347
        return UIAlertAction(title: "Reply", style: .default) { (action) in
igor@1242
   348
            self.performSegue(withIdentifier: self.segueCompose, sender: cell)
igor@1242
   349
        }
igor@1242
   350
    }
igor@1242
   351
    
igor@1242
   352
    func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
igor@1242
   353
        return UIAlertAction(title: "Forward", style: .default) { (action) in
igor@1242
   354
            self.performSegue(withIdentifier: self.segueCompose, sender: cell)
igor@1242
   355
        }
igor@1242
   356
    }
igor@1242
   357
    
igor@1242
   358
    func createMarkAction() -> UIAlertAction {
igor@1242
   359
        return UIAlertAction(title: "Mark", style: .default) { (action) in
igor@1242
   360
        }
igor@1242
   361
    }
dirk@784
   362
}