Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
//// DownloadFile.swift// twogetskills//// Created by Efrain Yanez Recanatini on 8/10/22.//import SwiftUIimport Networkimport Alamofireimport SwiftyJSONimport TTGSnackbarstruct DownloadFileView: View {@EnvironmentObject private var networkMonitor : NetworkMonitor@EnvironmentObject private var appNavigation : AppNavigation@State private var isDownloadFailed = false@State private var bytesDownloaded : Int64 = 0@State private var showGlobalAlert : Bool = false@State private var titleGlobalAlert : String = ""@State private var messageGlobalAlert : String = ""private let appData = AppData.sharedInstanceprivate let slideModel : SlideModelinit(){let slideDao = SlideDao.sharedInstanceslideModel = slideDao.selectByUuid(uuid: appData.slideUuidActive)}var body: some View {HStack {Spacer()VStack(spacing: 0) {LottieView(name: isDownloadFailed ? "download-failed": "download-inprogress").frame(width: UIScreen.main.bounds.width / 2,height: UIScreen.main.bounds.height / 4).padding(.top, 40)Text(isDownloadFailed ? Config.LANG_DOWNLOAD_FAILED_TITLE : Config.LANG_DOWNLOAD_TITLE ).font(Font.custom(Config.FONT_NAME_BOLD, size:Config.FONT_SIZE_FINISH_TITLE)).foregroundColor(Color("color_textview_foreground")).padding(.top, 40)Text(isDownloadFailed ? Config.LANG_DOWNLOAD_FAILED_LABEL: Config.LANG_DOWNLOAD_LABEL ).font(Font.custom(Config.FONT_NAME_BOLD, size:Config.FONT_SIZE_FINISH_MESSAGE)).foregroundColor(Color("color_textview_foreground")).padding(.top, 20)if bytesDownloaded > 0 {Text(String(bytesDownloaded) + " " + Config.LANG_DOWNLOAD_BYTES).font(Font.custom(Config.FONT_NAME_BOLD, size: Config.FONT_SIZE_FINISH_MESSAGE)).foregroundColor(Color("color_textview_foreground")).padding(.top, 20)}Spacer()if isDownloadFailed {Button(action: {withAnimation {appNavigation.pageActive = .home}}, label: {Text(Config.LANG_COMMON_RETURN).font(Font.custom(Config.FONT_NAME_REGULAR, size: 16)).frame(width: UIScreen.main.bounds.width - 32, height: 35).foregroundColor(Color("color_button_dark_foreground")).background(Color("color_button_dark_background")).border(Color( "color_button_dark_border"), width: Config.BUTTON_BORDER_SIZE).cornerRadius(Config.BUTTON_BORDER_RADIUS)}).padding(.bottom, 30)}}Spacer()}.onAppear {DispatchQueue.main.asyncAfter(deadline: .now() + 3) {downloadFile()}}.alert(isPresented: $showGlobalAlert) {Alert(title: Text(self.titleGlobalAlert),message: Text(self.messageGlobalAlert),dismissButton: .default(Text(Config.LANG_COMMON_OK)))}.background(Color("color_window_background"))}private func downloadFile(){guard let url = URL(string: slideModel.file) else {return}let arrayFullFilename = slideModel.file.split(separator: "/")let filename = String(arrayFullFilename[arrayFullFilename.count - 1])let headerSecurity = HeaderSecurity()let headers: HTTPHeaders = [.init(name: Constants.HTTP_HEADER_SECURITY_TOKEN, value: appData.deviceUuid),.init(name: Constants.HTTP_HEADER_SECURITY_SECRET, value: headerSecurity.secret),.init(name: Constants.HTTP_HEADER_SECURITY_CREATED, value: String(headerSecurity.created)),.init(name: Constants.HTTP_HEADER_SECURITY_RAND, value: String(headerSecurity.rand)),.accept(Constants.HTTP_HEADER_ACCEPT)]AF.request(url, method: .get, headers: headers).downloadProgress(closure : { (progress) inself.bytesDownloaded = progress.completedUnitCount}).responseData{ (response) inif let data = response.data {let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!let resourceURL = documentsURL.appendingPathComponent(filename)do {try data.write(to: resourceURL)DispatchQueue.main.asyncAfter(deadline: .now() + 3) {appData.urlExternalDownloaded = resourceURL.absoluteStringappData.save()switch slideModel.type{case Constants.SLIDE_TYPE_VIDEO :AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeftUIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")UIViewController.attemptRotationToDeviceOrientation()self.appNavigation.pageActive = .videoplayerbreakcase Constants.SLIDE_TYPE_AUDIO :self.appNavigation.pageActive = .videoplayerbreakdefault :self.appNavigation.pageActive = .pdfviewerbreak}}} catch {isDownloadFailed = truetitleGlobalAlert = Config.LANG_ERROR_COMMUNICATION_TITLEmessageGlobalAlert = Config.LANG_ERROR_COMMUNICATION_MESSAGEshowGlobalAlert = true}} else {isDownloadFailed = true}}}}struct DownloadFileView_Previews: PreviewProvider {static var previews: some View {DownloadFileView()}}