| 1 |
efrain |
1 |
//
|
|
|
2 |
// PdfView.swift
|
|
|
3 |
// twogetskills
|
|
|
4 |
//
|
|
|
5 |
// Created by Efrain Yanez Recanatini on 4/26/22.
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
import Foundation
|
|
|
9 |
import SwiftUI
|
|
|
10 |
import PDFKit
|
| 11 |
efrain |
11 |
import Network
|
|
|
12 |
import Alamofire
|
|
|
13 |
import SwiftyJSON
|
|
|
14 |
import TTGSnackbar
|
| 8 |
efrain |
15 |
import NavigationStack
|
| 1 |
efrain |
16 |
|
|
|
17 |
|
|
|
18 |
struct PdfViewerView: View {
|
|
|
19 |
|
| 8 |
efrain |
20 |
@State private var backToGallery : Bool = false
|
|
|
21 |
|
| 1 |
efrain |
22 |
@State private var isCompleted : Bool = false
|
| 11 |
efrain |
23 |
@State private var url : URL? = nil
|
| 1 |
efrain |
24 |
|
| 8 |
efrain |
25 |
private var slideModel : SlideModel
|
|
|
26 |
private var slideTitle : String
|
|
|
27 |
|
| 11 |
efrain |
28 |
|
| 1 |
efrain |
29 |
|
| 11 |
efrain |
30 |
|
| 1 |
efrain |
31 |
|
|
|
32 |
|
| 8 |
efrain |
33 |
init(slideUuid : String)
|
| 1 |
efrain |
34 |
{
|
| 8 |
efrain |
35 |
let slideDao = SlideDao.sharedInstance
|
|
|
36 |
self.slideModel = slideDao.selectByUuid(uuid: slideUuid)
|
| 1 |
efrain |
37 |
|
| 8 |
efrain |
38 |
if self.slideModel.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
|
|
|
39 |
slideTitle = String(Array(self.slideModel.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
|
|
|
40 |
} else {
|
|
|
41 |
slideTitle = self.slideModel.name
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
|
| 11 |
efrain |
45 |
|
| 8 |
efrain |
46 |
|
| 11 |
efrain |
47 |
//print("Viewer PDF: \(slideModel.file) ")
|
|
|
48 |
|
|
|
49 |
//self.url = URL(string: slideModel.file)
|
| 1 |
efrain |
50 |
|
| 8 |
efrain |
51 |
|
|
|
52 |
|
| 1 |
efrain |
53 |
}
|
|
|
54 |
|
|
|
55 |
var body: some View {
|
| 8 |
efrain |
56 |
VStack(spacing: 0) {
|
|
|
57 |
PopView(
|
|
|
58 |
destination: .root,
|
|
|
59 |
isActive: self.$backToGallery,
|
|
|
60 |
label: {
|
|
|
61 |
Text("")
|
|
|
62 |
|
|
|
63 |
}
|
|
|
64 |
).frame( height: 0)
|
|
|
65 |
|
|
|
66 |
HStack {
|
|
|
67 |
Button(action: {
|
|
|
68 |
let dataService = DataService()
|
| 11 |
efrain |
69 |
dataService.completeSlide(slide: slideModel)
|
| 8 |
efrain |
70 |
self.backToGallery.toggle()
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
}, label: {
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
Image(systemName: "chevron.backward")
|
|
|
78 |
.frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
|
|
|
79 |
.aspectRatio(contentMode: .fit)
|
|
|
80 |
.foregroundColor(Color("color_app_bar_foreground"))
|
|
|
81 |
})
|
|
|
82 |
.padding(.leading, 16)
|
|
|
83 |
|
|
|
84 |
Text(self.slideTitle)
|
|
|
85 |
.font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
|
|
|
86 |
.foregroundColor(Color("color_app_bar_foreground"))
|
|
|
87 |
.padding(.leading, 4)
|
|
|
88 |
|
|
|
89 |
Spacer()
|
|
|
90 |
}
|
|
|
91 |
.background(Color("color_app_bar_background"))
|
|
|
92 |
.edgesIgnoringSafeArea(.top)
|
|
|
93 |
.frame(height: 50)
|
|
|
94 |
Divider()
|
|
|
95 |
|
| 11 |
efrain |
96 |
if self.url != nil {
|
|
|
97 |
GeometryReader { geometry in
|
| 1 |
efrain |
98 |
|
| 11 |
efrain |
99 |
PDFKitRepresentedView(url!)
|
|
|
100 |
.frame(
|
|
|
101 |
width: geometry.size.width,
|
|
|
102 |
height: geometry.size.height,
|
|
|
103 |
alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
|
|
|
104 |
.border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/, width: /*@START_MENU_TOKEN@*/1/*@END_MENU_TOKEN@*/)
|
|
|
105 |
|
|
|
106 |
}
|
| 1 |
efrain |
107 |
}
|
| 11 |
efrain |
108 |
|
|
|
109 |
|
|
|
110 |
Spacer()
|
| 8 |
efrain |
111 |
|
| 1 |
efrain |
112 |
|
| 11 |
efrain |
113 |
}.onAppear {
|
|
|
114 |
let appDao = AppDao.sharedInstance
|
|
|
115 |
let appData = appDao.selectOne()
|
|
|
116 |
let headerSecurity = HeaderSecurity()
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
let headerAccept = HTTPHeader(name : Constants.HTTP_HEADER_ACCEPT, value: Constants.HTTP_HEADER_ACCEPT_VALUE)
|
|
|
120 |
|
|
|
121 |
let headerRand = HTTPHeader(name : Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand))
|
|
|
122 |
|
|
|
123 |
let headerToken = HTTPHeader(name : Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid)
|
|
|
124 |
|
|
|
125 |
let headerCreated = HTTPHeader(name : Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created))
|
|
|
126 |
|
|
|
127 |
let headerSecret = HTTPHeader(name : Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret)
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
var headers = HTTPHeaders()
|
|
|
131 |
headers.add(headerAccept)
|
|
|
132 |
headers.add(headerRand)
|
|
|
133 |
headers.add(headerToken)
|
|
|
134 |
headers.add(headerCreated)
|
|
|
135 |
headers.add(headerSecret)
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
/*
|
|
|
139 |
Constants.HTTP_HEADER_ACCEPT: Constants.HTTP_HEADER_ACCEPT_VALUE,
|
|
|
140 |
Constants.HTTP_HEADER_SECURITY_RAND: String(headerSecurity.rand),
|
|
|
141 |
Constants.HTTP_HEADER_SECURITY_TOKEN: appData.deviceUuid,
|
|
|
142 |
Constants.HTTP_HEADER_SECURITY_CREATED: String(headerSecurity.created) ,
|
|
|
143 |
Constants.HTTP_HEADER_SECURITY_SECRET: headerSecurity.secret,
|
|
|
144 |
]*/
|
|
|
145 |
/*
|
|
|
146 |
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
|
|
|
147 |
*/
|
|
|
148 |
/*
|
|
|
149 |
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory, in: .userDomainMask)
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
let destination: DownloadRequest.DownloadFileDestination
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
print("Download")
|
|
|
156 |
AF.download(
|
|
|
157 |
url!,
|
|
|
158 |
method: .get,
|
|
|
159 |
encoding: JSONEncoding.default,
|
|
|
160 |
headers: headers, to: destination).downloadProgress(closure: { (progress) in
|
|
|
161 |
//progress closure
|
|
|
162 |
}).response(completionHandler: { (DefaultDownloadResponse) in
|
|
|
163 |
//here you able to access the DefaultDownloadResponse
|
|
|
164 |
//result closure
|
|
|
165 |
})*/
|
|
|
166 |
|
|
|
167 |
/*
|
|
|
168 |
Constants.HTTP_HEADER_ACCEPT: Constants.HTTP_HEADER_ACCEPT_VALUE,
|
|
|
169 |
Constants.HTTP_HEADER_SECURITY_RAND: String(headerSecurity.rand),
|
|
|
170 |
Constants.HTTP_HEADER_SECURITY_TOKEN: appData.deviceUuid,
|
|
|
171 |
Constants.HTTP_HEADER_SECURITY_CREATED: String(headerSecurity.created) ,
|
|
|
172 |
Constants.HTTP_HEADER_SECURITY_SECRET: headerSecurity.secret,
|
|
|
173 |
]*/
|
|
|
174 |
|
|
|
175 |
//AF.download()
|
|
|
176 |
/*
|
|
|
177 |
.downloadProgress { progress in
|
|
|
178 |
print("Download Progress: \(progress.fractionCompleted)")
|
|
|
179 |
}
|
|
|
180 |
.responseData { response in
|
|
|
181 |
do {
|
|
|
182 |
try response.result.value?.write(to: File.getPath(filename: "_cr"))
|
|
|
183 |
} catch {
|
|
|
184 |
print("oops !")
|
|
|
185 |
}
|
|
|
186 |
}*/
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
|
|
|
190 |
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
|
|
|
191 |
|
|
|
192 |
print("download")
|
|
|
193 |
AF.download(
|
|
|
194 |
URL(string: slideModel.file)!,
|
|
|
195 |
method: .get,
|
|
|
196 |
encoding: JSONEncoding.default,
|
|
|
197 |
headers: headers).downloadProgress(closure: { (progress) in
|
|
|
198 |
print(progress.completedUnitCount)
|
|
|
199 |
}).responseData { (response) in
|
|
|
200 |
|
|
|
201 |
self.url = response.fileURL
|
|
|
202 |
|
|
|
203 |
//print("fileURL = \(fileURL)")
|
|
|
204 |
|
|
|
205 |
/*
|
|
|
206 |
let destiUrl = responce.destiUrl? ""
|
|
|
207 |
print(destiUrl!)
|
|
|
208 |
*/
|
|
|
209 |
/*
|
|
|
210 |
let newUrl = destiUrl?.deletingPathExtension().appendingPathExtension("zip")
|
|
|
211 |
do {
|
|
|
212 |
try FileManager.default.copyItem(at: destiUrl!, to: newUrl!)
|
|
|
213 |
|
|
|
214 |
let unzipDirectory = try Zip.quickUnzipFile(newUrl!)
|
|
|
215 |
print(unzipDirectory.absoluteString)
|
|
|
216 |
}
|
|
|
217 |
catch let error as NSError{
|
|
|
218 |
print(error)
|
|
|
219 |
}*/
|
|
|
220 |
}
|
| 1 |
efrain |
221 |
}
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
|
|
|
226 |
struct PdfViewerView_Previews: PreviewProvider {
|
|
|
227 |
static var previews: some View {
|
| 8 |
efrain |
228 |
PdfViewerView(slideUuid: "S123")
|
| 1 |
efrain |
229 |
}
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
|
|
|
233 |
|