File: ReportViewController.swift

package info (click to toggle)
mediainfo 25.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,656 kB
  • sloc: cpp: 18,871; objc: 3,102; xml: 1,426; sh: 1,328; python: 263; makefile: 212
file content (214 lines) | stat: -rw-r--r-- 8,100 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
*
*  Use of this source code is governed by a BSD-style license that can
*  be found in the License.html file in the root of the source tree.
*/

import UIKit
import Toast_Swift

class ReportViewController: UIViewController, UIDocumentPickerDelegate {
    @IBOutlet weak var reportView: UIWebView!
    @IBOutlet weak var viewsButton: UIBarButtonItem!
    @IBOutlet weak var exportButton: UIButton!

    let core: Core = Core()

    var currentView: String {
        get {
            return UserDefaults.standard.string(forKey: "View") ?? "HTML"
        }
        set(newValue) {
            if (newValue != currentView) {
                UserDefaults.standard.set(newValue, forKey: "View")
                configureView()
            }
        }
    }

    var report: Event? {
        didSet {
            configureView()
        }
    }

    func configureView() {
        // Update the view
        var bgColor = "#FFFFFF"
        var fgColor = "#000000"
        if SubscriptionManager.shared.subscriptionActive && Core.shared.darkMode {
            bgColor = "#000000"
            if let bgComponents = UIColor.darkGray.cgColor.components, bgComponents.count > 0 {
                let r = Float(bgComponents[0])
                let g = Float(bgComponents.count > 2 ? bgComponents[1] : bgComponents[0])
                let b = Float(bgComponents.count > 2 ? bgComponents[2] : bgComponents[0])

                bgColor = String(format: "#%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
            }

            fgColor = "#FFFFFF"
            if let fgComponents = UIColor.white.cgColor.components, fgComponents.count > 0 {
                let r = Float(fgComponents[0])
                let g = Float(fgComponents.count > 2 ? fgComponents[1] : fgComponents[0])
                let b = Float(fgComponents.count > 2 ? fgComponents[2] : fgComponents[0])

                fgColor = String(format: "#%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
            }
        }

        if report != nil {
            if let page: UIWebView = reportView {
                let reportText: String = core.convertReport(report: (NSKeyedUnarchiver.unarchiveObject(with: report!.report!) as! Array<MediaInfo_int8u>), format: currentView)

                var html: String
                if currentView == "HTML" {
                   html = reportText
                } else {
                    html = "<html><body><pre>" + reportText
                        .replacingOccurrences(of: "\t", with: "    ")
                        .replacingOccurrences(of: "<", with: "&lt;") +
                        "</pre></body></html>"
                }

                html = html.replacingOccurrences(of: "<body>", with: "<body style=\"background-color: \(bgColor); color: \(fgColor)\">")

                page.loadHTMLString(html, baseURL: nil)
            }

            if let button: UIButton = exportButton {
                button.isEnabled = true
            }
        } else {
            navigationItem.title = NSLocalizedString("Report", tableName: "Core", comment: "")
            if let page: UIWebView = reportView {
                page.loadHTMLString("<html><body style=\"background-color: \(bgColor); color: \(fgColor)\"></body></html>", baseURL: nil)
            }

            if let button: UIButton = exportButton {
                button.isEnabled = false
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Toggle "tap to dismiss" functionality
        ToastManager.shared.isTapToDismissEnabled = true

        // Toggle queueing behavior
        ToastManager.shared.isQueueEnabled = true

        if SubscriptionManager.shared.subscriptionActive {
            subscriptionActive()
        }

        NotificationCenter.default.addObserver(self, selector: #selector(subscriptionStateChanged(_:)), name: .subscriptionStateChanged, object: nil)

        configureView()
    }

    @objc func subscriptionStateChanged(_ notification: Notification) {
        if SubscriptionManager.shared.subscriptionActive {
            subscriptionActive()
        }
    }

    open func subscriptionActive() {
        if Core.shared.darkMode {
            enableDarkMode()
        }

        NotificationCenter.default.addObserver(self, selector: #selector(darkModeEnabled(_:)), name: .darkModeEnabled, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(darkModeDisabled(_:)), name: .darkModeDisabled, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(translateReportEnabled(_:)), name: .translateReportEnabled, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(translateReportDisabled(_:)), name: .translateReportDisabled, object: nil)
    }

    // MARK: - Theme

    @objc func darkModeEnabled(_ notification: Notification) {
        enableDarkMode()
        configureView()
    }

    @objc func darkModeDisabled(_ notification: Notification) {
        disableDarkMode()
        configureView()
    }

    @objc func translateReportEnabled(_ notification: Notification) {
        configureView()
    }

    @objc func translateReportDisabled(_ notification: Notification) {
        configureView()
    }

    open func enableDarkMode() {
        self.view.backgroundColor = UIColor.darkGray
        self.navigationController?.navigationBar.barStyle = .black
    }

    open func disableDarkMode() {
        self.view.backgroundColor = UIColor.white
        self.navigationController?.navigationBar.barStyle = .default
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Bug, in iOS 11.2 navigation buttons remains faded after segue back
        if #available(iOS 11.2, *) {
            navigationController?.navigationBar.tintAdjustmentMode = .normal
            navigationController?.navigationBar.tintAdjustmentMode = .automatic
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func showViews(_ sender: Any) {
        performSegue(withIdentifier: "showViews", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showViews" {
                let controller = segue.destination as! ViewsController
                controller.currentView = currentView
        }
    }

    @IBAction func unwindFromViewsController(_ sender: UIStoryboardSegue) {
        if let senderVC: ViewsController = sender.source as? ViewsController {
            if (currentView != senderVC.currentView) {
                currentView = senderVC.currentView
            }
        }
    }

    @IBAction func saveReport(_ sender: Any) {
        if let curReport = report {
            let content: String = core.convertReport(report: (NSKeyedUnarchiver.unarchiveObject(with: curReport.report!) as! Array<MediaInfo_int8u>), format: currentView, export: true)

            let fileManager: FileManager = FileManager.default
            let fileName: String = "\(curReport.filename!).\(currentView).\(core.getExtension(view: currentView))"
            let path: String = "\(NSTemporaryDirectory())/\(fileName)"

            var urlComponents = URLComponents()
            urlComponents.scheme = "file"
            urlComponents.path = path

            if let pathURL: URL = urlComponents.url,
                fileManager.createFile(atPath: path, contents: content.data(using: .utf8)) {
                let documentPicker: UIDocumentPickerViewController =  UIDocumentPickerViewController(url: pathURL, in: UIDocumentPickerMode.moveToService)
                documentPicker.delegate = self
                present(documentPicker, animated: true, completion: nil)
            } else {
                view.makeToast("ERROR: unable to export report", duration: 5.0, position: .top)
            }
        }
    }
}