Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NCViewerProviderContextMenu.swift « Viewer « iOSClient - github.com/nextcloud/ios.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58727b470461685bf67c6283bd54fdd3eddfbe8a (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
//  NCViewerProviderContextMenu.swift
//  Nextcloud
//
//  Created by Marino Faggiana on 12/01/21.
//  Copyright © 2021 Marino Faggiana. All rights reserved.
//
//  Author Marino Faggiana <marino.faggiana@nextcloud.com>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import UIKit
import AVFoundation
import NCCommunication
import SVGKit

class NCViewerProviderContextMenu: UIViewController {

    private let imageView = UIImageView()
    private var videoLayer: AVPlayerLayer?
    private var audioPlayer: AVAudioPlayer?
    private var metadata: tableMetadata?
    private var metadataLivePhoto: tableMetadata?
    private var image: UIImage?

    private let sizeIcon: CGFloat = 150

    // MARK: - View Life Cycle

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    init(metadata: tableMetadata, image: UIImage?) {
        super.init(nibName: nil, bundle: nil)

        self.metadata = metadata
        self.metadataLivePhoto = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
        self.image = image

        if metadata.directory {

            var imageFolder = UIImage(named: "folder")!.image(color: NCBrandColor.shared.brandElement, size: sizeIcon*2)

            if let image = self.image {
                imageFolder =  image.image(color: NCBrandColor.shared.brandElement, size: sizeIcon*2)
            }

            imageView.image = imageFolder.colorizeFolder(metadata: metadata)
            imageView.frame = resize(CGSize(width: sizeIcon, height: sizeIcon))

        } else {

            // ICON
            if let image = UIImage(named: metadata.iconName)?.resizeImage(size: CGSize(width: sizeIcon*2, height: sizeIcon*2), isAspectRation: true) {

                imageView.image = image
                imageView.frame = resize(CGSize(width: sizeIcon, height: sizeIcon))
            }

            // PREVIEW
            if CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {

                if let image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)) {
                    imageView.image = image
                    imageView.frame = resize(image.size)
                }
            }

            // VIEW IMAGE
            if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && CCUtility.fileProviderStorageExists(metadata) {
                viewImage(metadata: metadata)
            }

            // VIEW LIVE PHOTO
            if let metadataLivePhoto = metadataLivePhoto, CCUtility.fileProviderStorageExists(metadataLivePhoto) {
                viewVideo(metadata: metadataLivePhoto)
            }

            // VIEW VIDEO
            if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && CCUtility.fileProviderStorageExists(metadata) {
                viewVideo(metadata: metadata)
            }

            // PLAY SOUND
            if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue && CCUtility.fileProviderStorageExists(metadata) {
                playSound(metadata: metadata)
            }

            // AUTO DOWNLOAD VIDEO / AUDIO
            // if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue || metadata.contentType == "application/pdf") {
            if !CCUtility.fileProviderStorageExists(metadata) && (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) {

                var maxDownload: UInt64 = 0

                if NCNetworking.shared.networkReachability == NCCommunicationCommon.typeReachability.reachableCellular {
                    maxDownload = NCGlobal.shared.maxAutoDownloadCellular
                } else {
                    maxDownload = NCGlobal.shared.maxAutoDownload
                }

                if metadata.size <= maxDownload {
                    NCOperationQueue.shared.download(metadata: metadata, selector: "")
                }
            }

            // AUTO DOWNLOAD IMAGE GIF
            if !CCUtility.fileProviderStorageExists(metadata) && metadata.contentType == "image/gif" {
                NCOperationQueue.shared.download(metadata: metadata, selector: "")
            }

            // AUTO DOWNLOAD IMAGE SVG
            if !CCUtility.fileProviderStorageExists(metadata) && metadata.contentType == "image/svg+xml" {
                NCOperationQueue.shared.download(metadata: metadata, selector: "")
            }

            // AUTO DOWNLOAD LIVE PHOTO
            if let metadataLivePhoto = self.metadataLivePhoto {
                if !CCUtility.fileProviderStorageExists(metadataLivePhoto) {
                    NCOperationQueue.shared.download(metadata: metadataLivePhoto, selector: "")
                }
            }
        }
    }

    override func loadView() {
        view = imageView
        imageView.contentMode = .scaleAspectFill
    }

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

        NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadStartFile), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadCancelFile), object: nil)
    }

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

        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadStartFile), object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadCancelFile), object: nil)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        if let videoLayer = self.videoLayer {
            if videoLayer.frame == CGRect.zero {
                videoLayer.frame = imageView.frame
            } else {
                imageView.frame = videoLayer.frame
            }
        }
        preferredContentSize = imageView.frame.size
    }

    // MARK: - NotificationCenter

    @objc func downloadStartFile(_ notification: NSNotification) {

        guard let userInfo = notification.userInfo as NSDictionary?,
              let ocId = userInfo["ocId"] as? String
        else { return }

        if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
            NCActivityIndicator.shared.start(backgroundView: self.view)
        }
    }

    @objc func downloadedFile(_ notification: NSNotification) {

        guard let userInfo = notification.userInfo as NSDictionary?,
              let ocId = userInfo["ocId"] as? String,
              let errorCode = userInfo["errorCode"] as? Int,
              let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
        else { return }

        if errorCode == 0 && metadata.ocId == self.metadata?.ocId {
            if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
                viewImage(metadata: metadata)
            } else if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
                viewVideo(metadata: metadata)
            } else if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
                playSound(metadata: metadata)
            }
        }
        if errorCode == 0 && metadata.ocId == self.metadataLivePhoto?.ocId {
            viewVideo(metadata: metadata)
        }
        if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
            NCActivityIndicator.shared.stop()
        }
    }

    @objc func downloadCancelFile(_ notification: NSNotification) {

        guard let userInfo = notification.userInfo as NSDictionary?,
              let ocId = userInfo["ocId"] as? String
        else { return }

        if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
            NCActivityIndicator.shared.stop()
        }
    }

    // MARK: - Viewer

    private func viewImage(metadata: tableMetadata) {

        var image: UIImage?

        let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!

        if metadata.contentType == "image/gif" {
            image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: filePath))
        } else if metadata.contentType == "image/svg+xml" {
            let imagePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
            if let svgImage = SVGKImage(contentsOfFile: imagePath) {
                svgImage.size = CGSize(width: NCGlobal.shared.sizePreview, height: NCGlobal.shared.sizePreview)
                image = svgImage.uiImage
            }
        } else {
            image = UIImage(contentsOfFile: filePath)
        }

        imageView.image = image
        imageView.frame = resize(image?.size)
    }

    func playSound(metadata: tableMetadata) {

        let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!

        do {
            audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: filePath), fileTypeHint: AVFileType.mp3.rawValue)

            guard let player = audioPlayer else { return }

            player.play()

        } catch let error {
            print(error.localizedDescription)
        }

        preferredContentSize = imageView.frame.size
    }

    private func viewVideo(metadata: tableMetadata) {

        let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!

        if let resolutionVideo = resolutionForLocalVideo(url: URL(fileURLWithPath: filePath)) {

            let player = AVPlayer(url: URL(fileURLWithPath: filePath))

            self.videoLayer = AVPlayerLayer(player: player)
            if let videoLayer = self.videoLayer {
                videoLayer.videoGravity = .resizeAspectFill
                imageView.image = nil
                imageView.frame = resize(resolutionVideo)
                imageView.layer.addSublayer(videoLayer)
            }

            player.isMuted = true
            player.play()
        }
    }

    private func resolutionForLocalVideo(url: URL) -> CGSize? {
        guard let track = AVURLAsset(url: url).tracks(withMediaType: AVMediaType.video).first else { return nil }
        let size = track.naturalSize.applying(track.preferredTransform)
        return CGSize(width: abs(size.width), height: abs(size.height))
    }

    private func resize(_ size: CGSize?) -> CGRect {

        var frame = CGRect.zero

        guard let size = size else {
            preferredContentSize = frame.size
            return frame
        }

        if size.width <= UIScreen.main.bounds.width {
            frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
            preferredContentSize = frame.size
            return frame
        }

        let originRatio = size.width / size.height
        let newRatio = UIScreen.main.bounds.width / UIScreen.main.bounds.height
        var newSize = CGSize.zero

        if originRatio < newRatio {
            newSize.height = UIScreen.main.bounds.height
            newSize.width = UIScreen.main.bounds.height * originRatio
        } else {
            newSize.width = UIScreen.main.bounds.width
            newSize.height = UIScreen.main.bounds.width / originRatio
        }

        frame = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
        preferredContentSize = frame.size
        return frame
    }
}