1 |
efrain |
1 |
//
|
|
|
2 |
// DownloadFile.swift
|
|
|
3 |
// twogetskills
|
|
|
4 |
//
|
|
|
5 |
// Created by Efrain Yanez Recanatini on 8/10/22.
|
|
|
6 |
//
|
|
|
7 |
|
|
|
8 |
import SwiftUI
|
|
|
9 |
|
|
|
10 |
import Network
|
|
|
11 |
import Alamofire
|
|
|
12 |
import SwiftyJSON
|
|
|
13 |
import TTGSnackbar
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
struct DownloadFileView: View {
|
|
|
17 |
@EnvironmentObject private var networkMonitor : NetworkMonitor
|
|
|
18 |
@EnvironmentObject private var appNavigation : AppNavigation
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
@State private var isDownloadFailed = false
|
|
|
22 |
@State private var bytesDownloaded : Int64 = 0
|
|
|
23 |
|
|
|
24 |
@State private var showGlobalAlert : Bool = false
|
|
|
25 |
@State private var titleGlobalAlert : String = ""
|
|
|
26 |
@State private var messageGlobalAlert : String = ""
|
|
|
27 |
|
21 |
efrain |
28 |
private var appData = Environment(\.appData).wrappedValue
|
1 |
efrain |
29 |
private let slideModel : SlideModel
|
|
|
30 |
|
|
|
31 |
init()
|
|
|
32 |
{
|
17 |
efrain |
33 |
let slideDao = SlideDao()
|
1 |
efrain |
34 |
slideModel = slideDao.selectByUuid(uuid: appData.slideUuidActive)
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
var body: some View {
|
|
|
38 |
HStack {
|
|
|
39 |
Spacer()
|
|
|
40 |
VStack(spacing: 0) {
|
|
|
41 |
|
|
|
42 |
LottieView(name: isDownloadFailed ? "download-failed": "download-inprogress")
|
|
|
43 |
.frame(width: UIScreen.main.bounds.width / 2,
|
|
|
44 |
height: UIScreen.main.bounds.height / 4)
|
|
|
45 |
.padding(.top, 40)
|
|
|
46 |
|
|
|
47 |
Text(isDownloadFailed ? Config.LANG_DOWNLOAD_FAILED_TITLE : Config.LANG_DOWNLOAD_TITLE )
|
|
|
48 |
.font(Font.custom(Config.FONT_NAME_BOLD, size:Config.FONT_SIZE_FINISH_TITLE))
|
|
|
49 |
.foregroundColor(Color("color_textview_foreground"))
|
|
|
50 |
.padding(.top, 40)
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
Text(isDownloadFailed ? Config.LANG_DOWNLOAD_FAILED_LABEL: Config.LANG_DOWNLOAD_LABEL )
|
|
|
54 |
|
|
|
55 |
.font(Font.custom(Config.FONT_NAME_BOLD, size:Config.FONT_SIZE_FINISH_MESSAGE))
|
|
|
56 |
.foregroundColor(Color("color_textview_foreground"))
|
|
|
57 |
|
|
|
58 |
.padding(.top, 20)
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
if bytesDownloaded > 0 {
|
|
|
62 |
Text(String(bytesDownloaded) + " " + Config.LANG_DOWNLOAD_BYTES)
|
|
|
63 |
.font(Font.custom(Config.FONT_NAME_BOLD, size: Config.FONT_SIZE_FINISH_MESSAGE))
|
|
|
64 |
.foregroundColor(Color("color_textview_foreground"))
|
|
|
65 |
.padding(.top, 20)
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
Spacer()
|
|
|
69 |
|
|
|
70 |
if isDownloadFailed {
|
|
|
71 |
Button(action: {
|
|
|
72 |
withAnimation {
|
|
|
73 |
appNavigation.pageActive = .home
|
|
|
74 |
}
|
|
|
75 |
}, label: {
|
|
|
76 |
Text(Config.LANG_COMMON_RETURN)
|
|
|
77 |
.font(Font.custom(Config.FONT_NAME_REGULAR, size: 16))
|
|
|
78 |
.frame(width: UIScreen.main.bounds.width - 32, height: 35)
|
|
|
79 |
|
|
|
80 |
.foregroundColor(Color("color_button_dark_foreground"))
|
|
|
81 |
.background(Color("color_button_dark_background"))
|
|
|
82 |
.border(Color( "color_button_dark_border"), width: Config.BUTTON_BORDER_SIZE)
|
|
|
83 |
.cornerRadius(Config.BUTTON_BORDER_RADIUS)
|
|
|
84 |
}).padding(.bottom, 30)
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
}
|
|
|
88 |
Spacer()
|
|
|
89 |
}
|
|
|
90 |
.onAppear {
|
|
|
91 |
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
|
|
|
92 |
downloadFile()
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
.alert(isPresented: $showGlobalAlert) {
|
|
|
96 |
Alert(
|
|
|
97 |
title: Text(self.titleGlobalAlert),
|
|
|
98 |
message: Text(self.messageGlobalAlert),
|
|
|
99 |
dismissButton: .default(Text(Config.LANG_COMMON_OK))
|
|
|
100 |
)
|
|
|
101 |
}
|
|
|
102 |
.background(Color("color_window_background"))
|
|
|
103 |
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
private func downloadFile()
|
|
|
107 |
{
|
|
|
108 |
guard let url = URL(string: slideModel.file) else {
|
|
|
109 |
return
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
let arrayFullFilename = slideModel.file.split(separator: "/")
|
|
|
113 |
let filename = String(arrayFullFilename[arrayFullFilename.count - 1])
|
|
|
114 |
|
|
|
115 |
let headerSecurity = HeaderSecurity()
|
|
|
116 |
|
|
|
117 |
let headers: HTTPHeaders = [
|
|
|
118 |
.init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),
|
|
|
119 |
.init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),
|
|
|
120 |
.init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),
|
|
|
121 |
.init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),
|
|
|
122 |
.accept(Constants.HTTP_HEADER_ACCEPT)
|
|
|
123 |
]
|
|
|
124 |
|
|
|
125 |
AF.request(url, method: .get, headers: headers)
|
|
|
126 |
.downloadProgress(closure : { (progress) in
|
|
|
127 |
|
|
|
128 |
self.bytesDownloaded = progress.completedUnitCount
|
|
|
129 |
|
|
|
130 |
})
|
|
|
131 |
.responseData{ (response) in
|
|
|
132 |
|
|
|
133 |
if let data = response.data {
|
|
|
134 |
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
|
|
|
135 |
let resourceURL = documentsURL.appendingPathComponent(filename)
|
|
|
136 |
do {
|
|
|
137 |
try data.write(to: resourceURL)
|
|
|
138 |
|
|
|
139 |
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
|
|
|
140 |
|
|
|
141 |
appData.urlExternalDownloaded = resourceURL.absoluteString
|
|
|
142 |
appData.save()
|
|
|
143 |
|
|
|
144 |
switch slideModel.type
|
|
|
145 |
{
|
|
|
146 |
case Constants.SLIDE_TYPE_VIDEO :
|
|
|
147 |
AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
|
|
|
148 |
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
|
|
|
149 |
UIViewController.attemptRotationToDeviceOrientation()
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
self.appNavigation.pageActive = .videoplayer
|
|
|
153 |
break
|
|
|
154 |
|
|
|
155 |
case Constants.SLIDE_TYPE_AUDIO :
|
|
|
156 |
self.appNavigation.pageActive = .videoplayer
|
|
|
157 |
break
|
|
|
158 |
|
|
|
159 |
default :
|
|
|
160 |
self.appNavigation.pageActive = .pdfviewer
|
|
|
161 |
break
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
} catch {
|
|
|
165 |
isDownloadFailed = true
|
|
|
166 |
titleGlobalAlert = Config.LANG_ERROR_COMMUNICATION_TITLE
|
|
|
167 |
messageGlobalAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGE
|
|
|
168 |
showGlobalAlert = true
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
} else {
|
|
|
172 |
isDownloadFailed = true
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
struct DownloadFileView_Previews: PreviewProvider {
|
|
|
180 |
static var previews: some View {
|
|
|
181 |
DownloadFileView()
|
|
|
182 |
}
|
|
|
183 |
}
|