-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPhotoDetailViewController.swift
198 lines (174 loc) · 9.13 KB
/
PhotoDetailViewController.swift
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
//
// PhotoDetailViewController.swift
// dslrbrowser
//
// Created by Andras Bekesi on 17/12/15.
// Copyright © 2015 Andras Bekesi. All rights reserved.
//
import UIKit
class PhotoDetailViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
var imageData : MediaServer1ItemObject?
var cameraKey : String?
var index : Int = 0
var url : URL?
// Preview action items.
lazy var previewActions: [UIPreviewActionItem] = {
func previewActionForTitle(_ title: String, style: UIPreviewActionStyle = .default) -> UIPreviewAction {
return UIPreviewAction(title: title, style: style) { previewAction, viewController in
guard let detailViewController = viewController as? PhotoDetailViewController,
let item : MediaServer1ItemObject = detailViewController.imageData else { return }
print("\(previewAction.title) triggered from `PhotoDetailViewController` for item: \(item.title)")
if (previewAction.title == "Download") {
let url:String = MediaServer1BasicObjectCollection.getImageURL(fromObject: item, quality: ImageQuality.IMAGE_QUALITY_ORIGINAL_ROTATED)
let downloadItem : DownloadItem = DownloadItem.init(withURL: url, item: item)
downloadItem.download()
}
}
}
let downloadAction = previewActionForTitle("Download")
//let quickShareAction = previewActionForTitle("Quick Share")
/*
let action2 = previewActionForTitle("Destructive Action", style: .Destructive)
let subAction1 = previewActionForTitle("Sub Action 1")
let subAction2 = previewActionForTitle("Sub Action 2")
let groupedActions = UIPreviewActionGroup(title: "Sub Actions…", style: .Default, actions: [subAction1, subAction2] )
*/
return [downloadAction]
}()
// MARK: Preview actions
override var previewActionItems : [UIPreviewActionItem] {
return previewActions
}
@IBAction func downloadPhotoAction(_ sender: UIButton) {
sender.isEnabled = false
let url:String = MediaServer1BasicObjectCollection.getImageURL(fromObject: self.imageData!, quality: ImageQuality.IMAGE_QUALITY_ORIGINAL_ROTATED)
let downloadItem : DownloadItem = DownloadItem.init(withURL: url, item: self.imageData!)
downloadItem.download()
}
override func viewDidLoad() {
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "DownloadProgress"), object: nil, queue: OperationQueue.main, using: { (notification: Notification) -> Void in
let progress: DownloadProgressNotification = notification.object as! DownloadProgressNotification
print("Progress of item \(progress.item.title) is \(progress.calculateProgressPercent())%")
if let progressView = self.view.viewWithTag(3000) as? UIProgressView {
progressView.setProgress(progress.calculateProgress(), animated: true)
}
})
if let downloadButton:UIButton = self.view.viewWithTag(2000) as? UIButton,
let progressView = self.view.viewWithTag(3000) as? UIProgressView
{
downloadButton.isHidden = false
progressView.isHidden = false
if (CameraCollectionManager.isDownloadFinishedFor(title: (imageData?.title)!, cameraKey: self.cameraKey!)) {
downloadButton.isEnabled = false
progressView.setProgress(1.0, animated: false)
}
}
if ( url == nil ) {
self.url = URL(string:CameraCollectionManager.getItemCollectionFor(cameraKey: self.cameraKey!).getImageURLAt(withPosition: self.index, quality: ImageQuality.IMAGE_QUALITY_LOW))
}
loadPreviewInBackground()
}
@IBAction func swipeLeft(_ sender: Any) {
}
@IBAction func swipeRight(_ sender: Any) {
}
@IBAction func swipeDown(_ sender: Any) {
self.navigationController?.popToRootViewController(animated: true)
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
let gesture:UISwipeGestureRecognizer = sender as! UISwipeGestureRecognizer
switch gesture.direction {
case UISwipeGestureRecognizerDirection.left:
if ( index < CameraCollectionManager.getTotalImageCount()-1 ) {
return true
}
else {
print("already at last item")
return false
}
case UISwipeGestureRecognizerDirection.right:
if (index > 0) {
return true
}
else {
print("already at first item")
return false
}
default:
return true
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let gesture:UISwipeGestureRecognizer = sender as! UISwipeGestureRecognizer
let imageCollection:MediaServer1BasicObjectCollection = CameraCollectionManager.getItemCollectionFor(cameraKey: cameraKey!)
switch gesture.direction {
case UISwipeGestureRecognizerDirection.left:
(segue.destination as! PhotoDetailViewController).cameraKey = cameraKey
(segue.destination as! PhotoDetailViewController).index = index+1
let imageDataNext :MediaServer1ItemObject = imageCollection.getItemAt(index+1)
(segue.destination as! PhotoDetailViewController).imageData = imageDataNext
segue.destination.navigationItem.title = imageDataNext.title
let url : URL = URL(string: imageCollection.getImageURLAt(withPosition: index+1, quality: ImageQuality.IMAGE_QUALITY_LOW))!
(segue.destination as! PhotoDetailViewController).url = url
print("segue swiping left")
break
case UISwipeGestureRecognizerDirection.right:
let imageDataNext :MediaServer1ItemObject = imageCollection.getItemAt(index-1)
(segue.destination as! PhotoDetailViewController).imageData = imageDataNext
(segue.destination as! PhotoDetailViewController).cameraKey = cameraKey
(segue.destination as! PhotoDetailViewController).index = index-1
segue.destination.navigationItem.title = imageDataNext.title
let url : URL = URL(string: imageCollection.getImageURLAt(withPosition: index-1, quality: ImageQuality.IMAGE_QUALITY_LOW))!
(segue.destination as! PhotoDetailViewController).url = url
print("segue swiping right")
break
default:
print("default swipe")
}
}
private func loadPreviewInBackground() {
if ( self.url != nil ) {
let backgroundQueue = DispatchQueue(label: "hu.bikeonet.dslrbrowser.photocollectionviewcontroller.peek", qos: .userInteractive)
let filename:String = "dslrbrowser_preview_" + (url!.absoluteString.data(using: .utf8)?.base64EncodedString())! + ".jpg"
let cacheDirectory:URL = FileManager.default.urls(for: FileManager.SearchPathDirectory.cachesDirectory, in: FileManager.SearchPathDomainMask.userDomainMask).first!
let cacheFileName:URL = URL.init(fileURLWithPath: cacheDirectory.path + "/" + filename )
backgroundQueue.async {
if (ThumbnailCacheManager.defaultManager.isPreviewAvailableFor(cameraKey: self.cameraKey!, title: (self.imageData?.title)!)) {
self.imageView.image = ThumbnailCacheManager.defaultManager.getPreviewImageFor(cameraKey: self.cameraKey!, title: (self.imageData?.title)!)
}
else {
var data:Data
if (FileManager.default.fileExists(atPath: cacheFileName.path)) {
//load file
data = FileManager.default.contents(atPath: cacheFileName.path)!
}
else {
//get file and save to cache
do {
data = try Data(contentsOf: self.url!)
if ( data.count > 0 ) {
FileManager.default.createFile(atPath: cacheFileName.path, contents: data, attributes: nil)
}
}
catch {
DispatchQueue.main.async {
self.imageView.image = #imageLiteral(resourceName: "camera_wifi")
}
data = Data(count: 0)
}
}
if ( data.count > 0 && self.imageView != nil) {
DispatchQueue.main.async {
let image : UIImage = UIImage(data: data)!
self.imageView.image = image
}
}
}
}
}
else {
self.imageView.image = #imageLiteral(resourceName: "camera_wifi")
}
}
}