Commit b170e9c5 authored by Steven杜宇's avatar Steven杜宇

// loading

parent 8acee64f
...@@ -153,10 +153,10 @@ class YHPlanShareViewController: YHBaseViewController { ...@@ -153,10 +153,10 @@ class YHPlanShareViewController: YHBaseViewController {
// } // }
// //
if pdfDownloadUrl.isEmpty { if pdfDownloadUrl.isEmpty {
requestPDF() requestPDF(isShowLoading: true)
} else { } else {
downloadPDF(pdfDownloadUrl) downloadPDF(pdfDownloadUrl, isShowLoading: true)
} }
} }
} }
...@@ -212,32 +212,36 @@ class YHPlanShareViewController: YHBaseViewController { ...@@ -212,32 +212,36 @@ class YHPlanShareViewController: YHBaseViewController {
} }
if pdfDownloadUrl.isEmpty { if pdfDownloadUrl.isEmpty {
requestPDF() requestPDF(isShowLoading: false)
} else { } else {
downloadPDF(pdfDownloadUrl) downloadPDF(pdfDownloadUrl, isShowLoading: false)
} }
} }
func requestPDF() { func requestPDF(isShowLoading: Bool) {
YHHUD.show(.progress(message: "加载中...")) if isShowLoading {
YHHUD.show(.progress(message: "加载中..."))
}
self.viewModel.requetPDFUrl(self.pdfCreateUrl) { [weak self] url in self.viewModel.requetPDFUrl(self.pdfCreateUrl) { [weak self] url in
YHHUD.hide() YHHUD.hide()
guard let self = self else { return } guard let self = self else { return }
self.pdfDownloadUrl = url self.pdfDownloadUrl = url
self.downloadPDF(url) self.downloadPDF(url, isShowLoading: isShowLoading)
} }
} }
func downloadPDF(_ urlString: String) { func downloadPDF(_ urlString: String, isShowLoading: Bool) {
guard let url = URL(string: urlString) else { guard let url = URL(string: urlString) else {
YHHUD.flash(message: "下载PDF失败") YHHUD.flash(message: "下载PDF失败")
return return
} }
YHHUD.show(.progress(message: "下载中...")) if isShowLoading {
YHHUD.show(.progress(message: "下载中..."))
}
let configuration = URLSessionConfiguration.default let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration) let session = URLSession(configuration: configuration)
let task = session.downloadTask(with: url) { [weak self] (locationUrl, response, error) in let task = session.downloadTask(with: url) { [weak self] (locationUrl, response, error) in
...@@ -252,13 +256,13 @@ class YHPlanShareViewController: YHBaseViewController { ...@@ -252,13 +256,13 @@ class YHPlanShareViewController: YHBaseViewController {
if let res = response { if let res = response {
printLog(res.suggestedFilename) printLog(res.suggestedFilename)
} }
self.savePDF(sourceUrl: url, locationUrl: locationUrl) self.savePDF(sourceUrl: url, locationUrl: locationUrl, isShowLoading: isShowLoading)
} }
} }
task.resume() task.resume()
} }
func savePDF(sourceUrl: URL, locationUrl: URL) { func savePDF(sourceUrl: URL, locationUrl: URL, isShowLoading: Bool) {
let fileManager = FileManager.default let fileManager = FileManager.default
...@@ -273,11 +277,15 @@ class YHPlanShareViewController: YHBaseViewController { ...@@ -273,11 +277,15 @@ class YHPlanShareViewController: YHBaseViewController {
try fileManager.moveItem(at: locationUrl, to: destinationURL) try fileManager.moveItem(at: locationUrl, to: destinationURL)
print("文件已保存到: \(destinationURL.path)") print("文件已保存到: \(destinationURL.path)")
print("文件源地址: \(locationUrl.path)") print("文件源地址: \(locationUrl.path)")
YHHUD.flash(message: "下载PDF成功") if isShowLoading {
YHHUD.flash(message: "下载PDF成功")
}
} catch { } catch {
print("保存文件失败: \(error.localizedDescription)") print("保存文件失败: \(error.localizedDescription)")
YHHUD.flash(message: "下载PDF失败") if isShowLoading {
YHHUD.flash(message: "下载PDF失败")
}
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment