Statistics
| Branch: | Tag: | Revision:

chatsecureios / ChatSecure / Classes / Views / Cells / MediaDownloadView.swift @ 8d76e2e3

History | View | Annotate | Download (2.15 KB)

1
//
2
//  MediaDownloadView.swift
3
//  ChatSecureCore
4
//
5
//  Created by Chris Ballinger on 6/12/17.
6
//  Copyright © 2017 Chris Ballinger. All rights reserved.
7
//
8

    
9
import UIKit
10
import OTRAssets
11

    
12
public extension NSError {
13
    /// Returns true if the message error is caused by automatic downloads being disabled
14
    @objc public var isAutomaticDownloadError: Bool {
15
        if self.domain == FileTransferError.errorDomain &&
16
            self.code == FileTransferError.automaticDownloadsDisabled.errorCode {
17
            return true
18
        } else {
19
            return false
20
        }
21
    }
22
}
23

    
24
public class MediaDownloadView: UIView {
25
    @IBOutlet weak var downloadButton: UIButton!
26
    @IBOutlet weak var statusLabel: UILabel!
27
    
28
    @objc public var downloadAction: ((_ view: MediaDownloadView, _ sender: Any) -> ())?
29
    
30
    @objc public func setMediaItem(_ mediaItem: OTRMediaItem, message: OTRDownloadMessage) {
31
        if let error = message.messageError {
32
            let nsError = error as NSError
33
            if nsError.isAutomaticDownloadError {
34
                statusLabel.text = mediaItem.displayText()
35
            } else {
36
                statusLabel.text = "⚠️ \(ERROR_STRING())"
37
            }
38
        } else {
39
            statusLabel.text = mediaItem.displayText()
40
        }
41
        self.downloadButton.setTitle(DOWNLOAD_STRING(), for: .normal)
42
        self.downloadButton.isEnabled = true
43
        
44
        self.downloadAction = { [weak self] view, sender in
45
            self?.downloadButton.isEnabled = false
46
            var xmpp: XMPPManager? = nil
47
            OTRDatabaseManager.shared.readOnlyDatabaseConnection?.read { transaction in
48
                guard let thread = message.threadOwner(with: transaction) else { return }
49
                guard let account = thread.account(with: transaction) else { return }
50
                xmpp = OTRProtocolManager.shared.protocol(for: account) as? XMPPManager
51
                xmpp?.fileTransferManager.downloadMediaIfNeeded(message)
52
            }
53
        }
54
    }
55
    
56
    @IBAction func downloadButtonPressed(_ sender: Any) {
57
        if let downloadAction = downloadAction {
58
            downloadAction(self, sender)
59
        }
60
    }
61
    
62
}