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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
//
// NCViewerMedia.swift
// Nextcloud
//
// Created by Marino Faggiana on 24/10/2020.
// Copyright © 2020 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 SVGKit
import NCCommunication
import EasyTipView
import SwiftUI
class NCViewerMedia: UIViewController {
@IBOutlet weak var detailViewTopConstraint: NSLayoutConstraint!
@IBOutlet weak var detailViewHeighConstraint: NSLayoutConstraint!
@IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
@IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var imageVideoContainer: imageVideoContainerView!
@IBOutlet weak var statusViewImage: UIImageView!
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var detailView: NCViewerMediaDetailView!
private var _autoPlay: Bool = false
private var tipView: EasyTipView?
let appDelegate = UIApplication.shared.delegate as! AppDelegate
weak var viewerMediaPage: NCViewerMediaPage?
var playerToolBar: NCPlayerToolBar?
var ncplayer: NCPlayer?
var image: UIImage?
var metadata: tableMetadata = tableMetadata()
var index: Int = 0
var doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer()
var imageViewConstraint: CGFloat = 0
var isDetailViewInitializze: Bool = false
var autoPlay: Bool {
get {
let temp = _autoPlay
_autoPlay = false
return temp
}
set(newVal) {
_autoPlay = newVal
}
}
// MARK: - View Life Cycle
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTapWith(gestureRecognizer:)))
doubleTapGestureRecognizer.numberOfTapsRequired = 2
}
deinit {
print("deinit NCViewerMedia")
self.tipView?.dismiss()
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterOpenMediaDetail), object: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
scrollView.maximumZoomScale = 4
scrollView.minimumZoomScale = 1
view.addGestureRecognizer(doubleTapGestureRecognizer)
if NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) != nil {
statusViewImage.image = NCUtility.shared.loadImage(named: "livephoto", color: .gray)
statusLabel.text = "LIVE"
} else {
statusViewImage.image = nil
statusLabel.text = ""
}
if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
playerToolBar = Bundle.main.loadNibNamed("NCPlayerToolBar", owner: self, options: nil)?.first as? NCPlayerToolBar
if let playerToolBar = playerToolBar {
view.addSubview(playerToolBar)
playerToolBar.translatesAutoresizingMaskIntoConstraints = false
playerToolBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
playerToolBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
playerToolBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
playerToolBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
playerToolBar.viewerMediaPage = viewerMediaPage
}
let urlVideo = NCKTVHTTPCache.shared.getVideoURL(metadata: metadata)
if let url = urlVideo.url {
self.ncplayer = NCPlayer.init(url: url, autoPlay: self.autoPlay, isProxy: urlVideo.isProxy, imageVideoContainer: self.imageVideoContainer, playerToolBar: self.playerToolBar, metadata: self.metadata, detailView: self.detailView, viewController: self)
}
}
// TIP
var preferences = EasyTipView.Preferences()
preferences.drawing.foregroundColor = .white
preferences.drawing.backgroundColor = NCBrandColor.shared.nextcloud
preferences.drawing.textAlignment = .left
preferences.drawing.arrowPosition = .top
preferences.drawing.cornerRadius = 10
preferences.animating.dismissTransform = CGAffineTransform(translationX: 0, y: 100)
preferences.animating.showInitialTransform = CGAffineTransform(translationX: 0, y: -100)
preferences.animating.showInitialAlpha = 0
preferences.animating.showDuration = 0.5
preferences.animating.dismissDuration = 0
tipView = EasyTipView(text: NSLocalizedString("_tip_open_mediadetail_", comment: ""), preferences: preferences, delegate: self)
detailViewTopConstraint.constant = 0
detailView.hide()
self.image = nil
self.imageVideoContainer.image = nil
reloadImage()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
viewerMediaPage?.navigationController?.navigationBar.prefersLargeTitles = false
viewerMediaPage?.navigationItem.title = metadata.fileNameView
if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue, let viewerMediaPage = self.viewerMediaPage {
viewerMediaPage.currentScreenMode = viewerMediaPage.saveScreenModeImage
if viewerMediaPage.modifiedOcId.contains(metadata.ocId) {
viewerMediaPage.modifiedOcId.removeAll(where: { $0 == metadata.ocId })
reloadImage()
}
}
if viewerMediaPage?.currentScreenMode == .full {
viewerMediaPage?.navigationController?.setNavigationBarHidden(true, animated: true)
NCUtility.shared.colorNavigationController(viewerMediaPage?.navigationController, backgroundColor: .black, titleColor: .white, tintColor: nil, withoutShadow: false)
viewerMediaPage?.view.backgroundColor = .black
viewerMediaPage?.textColor = .white
viewerMediaPage?.progressView.isHidden = true
} else {
viewerMediaPage?.navigationController?.setNavigationBarHidden(false, animated: true)
NCUtility.shared.colorNavigationController(viewerMediaPage?.navigationController, backgroundColor: NCBrandColor.shared.systemBackground, titleColor: NCBrandColor.shared.label, tintColor: nil, withoutShadow: false)
viewerMediaPage?.view.backgroundColor = NCBrandColor.shared.systemBackground
viewerMediaPage?.textColor = NCBrandColor.shared.label
viewerMediaPage?.progressView.isHidden = false
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
if let ncplayer = self.ncplayer {
ncplayer.openAVPlayer()
self.viewerMediaPage?.updateCommandCenter(ncplayer: ncplayer, metadata: self.metadata)
}
} else if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
viewerMediaPage?.clearCommandCenter()
}
// TIP
if !NCManageDatabase.shared.tipExists(NCGlobal.shared.tipNCViewerMediaDetailView), let view = self.navigationController?.navigationBar {
self.tipView?.show(forView: view)
}
NotificationCenter.default.addObserver(self, selector: #selector(openDetail(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterOpenMediaDetail), object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.tipView?.dismiss()
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
self.tipView?.dismiss()
coordinator.animate(alongsideTransition: { context in
// back to the original size
self.scrollView.zoom(to: CGRect(x: 0, y: 0, width: self.scrollView.bounds.width, height: self.scrollView.bounds.height), animated: false)
self.view.layoutIfNeeded()
UIView.animate(withDuration: context.transitionDuration) {
if self.detailView.isShow() {
self.openDetail()
}
}
}) { _ in }
}
// MARK: - Image
func reloadImage() {
if let metadata = NCManageDatabase.shared.getMetadataFromOcId(metadata.ocId) {
self.metadata = metadata
loadImage(metadata: metadata)
}
}
func loadImage(metadata: tableMetadata) {
// Download image
if !CCUtility.fileProviderStorageExists(metadata) && metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && metadata.session == "" {
if metadata.livePhoto {
let fileName = (metadata.fileNameView as NSString).deletingPathExtension + ".mov"
if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", metadata.account, metadata.serverUrl, fileName)), !CCUtility.fileProviderStorageExists(metadata) {
NCNetworking.shared.download(metadata: metadata, selector: "") { _ in }
}
}
NCNetworking.shared.download(metadata: metadata, selector: "") { _ in
let image = getImageMetadata(metadata)
if self.metadata.ocId == metadata.ocId && self.imageVideoContainer.layer.sublayers?.count == nil {
self.image = image
self.imageVideoContainer.image = image
}
}
}
// Get image
let image = getImageMetadata(metadata)
if self.metadata.ocId == metadata.ocId && self.imageVideoContainer.layer.sublayers?.count == nil {
self.image = image
self.imageVideoContainer.image = image
}
func getImageMetadata(_ metadata: tableMetadata) -> UIImage? {
if let image = getImage(metadata: metadata) {
return image
}
if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && !metadata.hasPreview {
NCUtility.shared.createImageFrom(fileNameView: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
}
if CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
if let imagePreviewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag) {
return UIImage(contentsOfFile: imagePreviewPath)
}
}
if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
return UIImage(named: "noPreviewVideo")!.image(color: .gray, size: view.frame.width)
} else if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
return UIImage(named: "noPreviewAudio")!.image(color: .gray, size: view.frame.width)
} else {
return UIImage(named: "noPreview")!.image(color: .gray, size: view.frame.width)
}
}
func getImage(metadata: tableMetadata) -> UIImage? {
let ext = CCUtility.getExtension(metadata.fileNameView)
var image: UIImage?
if CCUtility.fileProviderStorageExists(metadata) && metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
let previewPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
let imagePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
if ext == "GIF" {
if !FileManager().fileExists(atPath: previewPath) {
NCUtility.shared.createImageFrom(fileNameView: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
}
image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: imagePath))
} else if ext == "SVG" {
if let svgImage = SVGKImage(contentsOfFile: imagePath) {
svgImage.size = CGSize(width: NCGlobal.shared.sizePreview, height: NCGlobal.shared.sizePreview)
if let image = svgImage.uiImage {
if !FileManager().fileExists(atPath: previewPath) {
do {
try image.pngData()?.write(to: URL(fileURLWithPath: previewPath), options: .atomic)
} catch { }
}
return image
} else {
return nil
}
} else {
return nil
}
} else {
NCUtility.shared.createImageFrom(fileNameView: metadata.fileNameView, ocId: metadata.ocId, etag: metadata.etag, classFile: metadata.classFile)
image = UIImage(contentsOfFile: imagePath)
}
}
return image
}
}
// MARK: - Gesture
@objc func didDoubleTapWith(gestureRecognizer: UITapGestureRecognizer) {
if detailView.isShow() { return }
// NO ZOOM for Audio
if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue { return }
let pointInView = gestureRecognizer.location(in: self.imageVideoContainer)
var newZoomScale = self.scrollView.maximumZoomScale
if self.scrollView.zoomScale >= newZoomScale || abs(self.scrollView.zoomScale - newZoomScale) <= 0.01 {
newZoomScale = self.scrollView.minimumZoomScale
}
let width = self.scrollView.bounds.width / newZoomScale
let height = self.scrollView.bounds.height / newZoomScale
let originX = pointInView.x - (width / 2.0)
let originY = pointInView.y - (height / 2.0)
let rectToZoomTo = CGRect(x: originX, y: originY, width: width, height: height)
self.scrollView.zoom(to: rectToZoomTo, animated: true)
}
@objc func didPanWith(gestureRecognizer: UIPanGestureRecognizer) {
let currentLocation = gestureRecognizer.translation(in: self.view)
switch gestureRecognizer.state {
case .began:
// let velocity = gestureRecognizer.velocity(in: self.view)
// gesture moving Up
// if velocity.y < 0 {
// }
break
case .ended:
if detailView.isShow() {
self.imageViewTopConstraint.constant = -imageViewConstraint
self.imageViewBottomConstraint.constant = imageViewConstraint
} else {
self.imageViewTopConstraint.constant = 0
self.imageViewBottomConstraint.constant = 0
}
case .changed:
imageViewTopConstraint.constant = (currentLocation.y - imageViewConstraint)
imageViewBottomConstraint.constant = -(currentLocation.y - imageViewConstraint)
// DISMISS VIEW
if detailView.isHidden && (currentLocation.y > 20) {
viewerMediaPage?.navigationController?.popViewController(animated: true)
gestureRecognizer.state = .ended
}
// CLOSE DETAIL
if !detailView.isHidden && (currentLocation.y > 20) {
self.closeDetail()
gestureRecognizer.state = .ended
}
// OPEN DETAIL
if detailView.isHidden && (currentLocation.y < -20) {
self.openDetail()
gestureRecognizer.state = .ended
}
default:
break
}
}
}
// MARK: -
extension NCViewerMedia {
@objc func openDetail(_ notification: NSNotification) {
if let userInfo = notification.userInfo as NSDictionary?, let ocId = userInfo["ocId"] as? String, ocId == metadata.ocId {
openDetail()
}
}
private func openDetail() {
self.dismissTip()
CCUtility.setExif(metadata) { latitude, longitude, location, date, lensModel in
if latitude != -1 && latitude != 0 && longitude != -1 && longitude != 0 {
self.detailViewHeighConstraint.constant = self.view.bounds.height / 2
} else {
self.detailViewHeighConstraint.constant = 170
}
self.view.layoutIfNeeded()
self.detailView.show(
metadata: self.metadata,
image: self.image,
textColor: self.viewerMediaPage?.textColor,
mediaMetadata: (latitude: latitude, longitude: longitude, location: location, date: date, lensModel: lensModel),
ncplayer: self.ncplayer,
delegate: self)
if let image = self.imageVideoContainer.image {
let ratioW = self.imageVideoContainer.frame.width / image.size.width
let ratioH = self.imageVideoContainer.frame.height / image.size.height
let ratio = ratioW < ratioH ? ratioW : ratioH
let imageHeight = image.size.height * ratio
self.imageViewConstraint = self.detailView.frame.height - ((self.view.frame.height - imageHeight) / 2) + self.view.safeAreaInsets.bottom
if self.imageViewConstraint < 0 { self.imageViewConstraint = 0 }
}
UIView.animate(withDuration: 0.3) {
self.imageViewTopConstraint.constant = -self.imageViewConstraint
self.imageViewBottomConstraint.constant = self.imageViewConstraint
self.detailViewTopConstraint.constant = self.detailViewHeighConstraint.constant
self.view.layoutIfNeeded()
} completion: { _ in
}
self.scrollView.pinchGestureRecognizer?.isEnabled = false
self.playerToolBar?.hide()
}
}
private func closeDetail() {
self.detailView.hide()
imageViewConstraint = 0
UIView.animate(withDuration: 0.3) {
self.imageViewTopConstraint.constant = 0
self.imageViewBottomConstraint.constant = 0
self.detailViewTopConstraint.constant = 0
self.view.layoutIfNeeded()
} completion: { _ in
}
scrollView.pinchGestureRecognizer?.isEnabled = true
if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && !metadata.livePhoto && ncplayer?.player?.timeControlStatus == .paused {
NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterShowPlayerToolBar, userInfo: ["ocId": metadata.ocId, "enableTimerAutoHide": false])
}
}
func reloadDetail() {
if self.detailView.isShow() {
CCUtility.setExif(metadata) { (latitude, longitude, location, date, lensModel) in
self.detailView.show(
metadata: self.metadata,
image: self.image,
textColor: self.viewerMediaPage?.textColor,
mediaMetadata: (latitude: latitude, longitude: longitude, location: location, date: date, lensModel: lensModel),
ncplayer: self.ncplayer,
delegate: self)
}
}
}
}
// MARK: -
extension NCViewerMedia: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageVideoContainer
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
if scrollView.zoomScale > 1 {
if let image = imageVideoContainer.image {
let ratioW = imageVideoContainer.frame.width / image.size.width
let ratioH = imageVideoContainer.frame.height / image.size.height
let ratio = ratioW < ratioH ? ratioW : ratioH
let newWidth = image.size.width * ratio
let newHeight = image.size.height * ratio
let conditionLeft = newWidth*scrollView.zoomScale > imageVideoContainer.frame.width
let left = 0.5 * (conditionLeft ? newWidth - imageVideoContainer.frame.width : (scrollView.frame.width - scrollView.contentSize.width))
let conditioTop = newHeight*scrollView.zoomScale > imageVideoContainer.frame.height
let top = 0.5 * (conditioTop ? newHeight - imageVideoContainer.frame.height : (scrollView.frame.height - scrollView.contentSize.height))
scrollView.contentInset = UIEdgeInsets(top: top, left: left, bottom: top, right: left)
}
} else {
scrollView.contentInset = .zero
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
}
}
extension NCViewerMedia: NCViewerMediaDetailViewDelegate {
func downloadFullResolution() {
closeDetail()
NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorOpenDetail) { _ in }
}
}
extension NCViewerMedia: EasyTipViewDelegate {
// TIP
func easyTipViewDidTap(_ tipView: EasyTipView) {
NCManageDatabase.shared.addTip(NCGlobal.shared.tipNCViewerMediaDetailView)
}
func easyTipViewDidDismiss(_ tipView: EasyTipView) { }
func dismissTip() {
NCManageDatabase.shared.addTip(NCGlobal.shared.tipNCViewerMediaDetailView)
self.tipView?.dismiss()
}
}
// MARK: -
class imageVideoContainerView: UIImageView {
var playerLayer: CALayer?
var metadata: tableMetadata?
override func layoutSublayers(of layer: CALayer) {
super.layoutSublayers(of: layer)
playerLayer?.frame = self.bounds
}
}
|