Skip to content

Commit

Permalink
优化下载功能 (#106)
Browse files Browse the repository at this point in the history
* Allows navigate to downloading list view after requesting a downloading task

* Allows cancel a downloading task
  • Loading branch information
WindowsMEMZ authored Feb 1, 2024
1 parent 8cbeb6f commit ced7a26
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
7 changes: 7 additions & 0 deletions DarockBili Watch App/PersonalCenter/DownloadsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ struct DownloadingListView: View {
Spacer()
}
}
.swipeActions {
Button(role: .destructive, action: {
videoDownloadRequests[i].cancel()
}, label: {
Image(systemName: "xmark.circle.fill")
})
}
.onReceive(downloadingProgressDatas[i].pts) { data in
downloadProgresses[i] = data.data.progress
downloadedSizes[i] = data.data.currentSize
Expand Down
83 changes: 43 additions & 40 deletions DarockBili Watch App/Video/VideoDownloadView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Alamofire

var failedDownloadTasks = [Int]()
var downloadResumeDatas = [Int: [String: String]]()
var videoDownloadRequests = [DownloadRequest]()

struct VideoDownloadView: View {
var bvid: String
Expand All @@ -33,29 +34,29 @@ struct VideoDownloadView: View {
@AppStorage("bili_jct") var biliJct = ""
@State var isLoading = true
var body: some View {
List {
if isLoading {
Text("Download.preloading...")
.bold()
} else {
VStack {
HStack {
Spacer()
Label("Download.task-created", systemImage: "checkmark.circle.fill")
.bold()
Spacer()
NavigationStack {
List {
if isLoading {
Text("Download.preloading...")
.bold()
} else {
Section {
HStack {
Spacer()
Label("Download.task-created", systemImage: "checkmark.circle.fill")
.bold()
Spacer()
}
}
HStack {
Spacer()
Text("Download.closing-in-3sec")
.font(.footnote)
.opacity(0.65)
.onAppear {
Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false) { _ in
dismiss()
}
Section {
NavigationLink(destination: {DownloadingListView()}, label: {
HStack {
Text("视频下载列表")
Spacer()
Image(systemName: "chevron.right")
.opacity(0.65)
}
Spacer()
})
}
}
}
Expand Down Expand Up @@ -92,26 +93,28 @@ struct VideoDownloadView: View {
isLoading = false
downloadingProgressDatas.append((.init(), false))
let currentDownloadingIndex = downloadingProgressDatas.count - 1
AF.download((VideoDownloadView.downloadLink ?? (String(data: response.data!, encoding: .utf8)?.components(separatedBy: ",\"url\":\"")[1].components(separatedBy: "\",")[0])!.replacingOccurrences(of: "\\u0026", with: "&")), headers: headers, to: destination)
.downloadProgress { p in
downloadingProgressDatas[currentDownloadingIndex].pts.send(.init(data: .init(progress: p.fractionCompleted, currentSize: p.completedUnitCount, totalSize: p.totalUnitCount), videoDetails: videoDetails))
}
.response { r in
if r.error == nil, let filePath = r.fileURL?.path {
debugPrint(filePath)
debugPrint(bvid)
var detTmp = videoDetails
detTmp.updateValue(filePath, forKey: "Path")
detTmp.updateValue(String(Date.now.timeIntervalSince1970), forKey: "Time")
UserDefaults.standard.set(detTmp, forKey: bvid)
downloadingProgressDatas[currentDownloadingIndex].isFinished = true
} else {
UserDefaults.standard.set(r.resumeData, forKey: "VideoDownloadResumeData\(currentDownloadingIndex)")
downloadResumeDatas.updateValue(videoDetails, forKey: currentDownloadingIndex)
failedDownloadTasks.append(currentDownloadingIndex)
debugPrint(r.error as Any)
videoDownloadRequests.append(
AF.download((VideoDownloadView.downloadLink ?? (String(data: response.data!, encoding: .utf8)?.components(separatedBy: ",\"url\":\"")[1].components(separatedBy: "\",")[0])!.replacingOccurrences(of: "\\u0026", with: "&")), headers: headers, to: destination)
.downloadProgress { p in
downloadingProgressDatas[currentDownloadingIndex].pts.send(.init(data: .init(progress: p.fractionCompleted, currentSize: p.completedUnitCount, totalSize: p.totalUnitCount), videoDetails: videoDetails))
}
}
.response { r in
if r.error == nil, let filePath = r.fileURL?.path {
debugPrint(filePath)
debugPrint(bvid)
var detTmp = videoDetails
detTmp.updateValue(filePath, forKey: "Path")
detTmp.updateValue(String(Date.now.timeIntervalSince1970), forKey: "Time")
UserDefaults.standard.set(detTmp, forKey: bvid)
downloadingProgressDatas[currentDownloadingIndex].isFinished = true
} else {
UserDefaults.standard.set(r.resumeData, forKey: "VideoDownloadResumeData\(currentDownloadingIndex)")
downloadResumeDatas.updateValue(videoDetails, forKey: currentDownloadingIndex)
failedDownloadTasks.append(currentDownloadingIndex)
debugPrint(r.error as Any)
}
}
)
VideoDownloadView.downloadLink = nil
}
}
Expand Down
4 changes: 4 additions & 0 deletions Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,7 @@
}
},
"Download.closing-in-3sec" : {
"extractionState" : "stale",
"localizations" : {
"en" : {
"stringUnit" : {
Expand Down Expand Up @@ -4220,6 +4221,9 @@
},
"确认" : {

},
"视频下载列表" : {

},
"神秘代码" : {

Expand Down

0 comments on commit ced7a26

Please sign in to comment.