-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add download manager for WKWebView (#3157)
- Loading branch information
Showing
6 changed files
with
204 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
Sources/App/WebView/DownloadManager/DownloadManagerView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import Shared | ||
import SwiftUI | ||
|
||
@available(iOS 17.0, *) | ||
struct DownloadManagerView: View { | ||
@Environment(\.dismiss) private var dismiss | ||
|
||
@StateObject var viewModel: DownloadManagerViewModel | ||
|
||
init(viewModel: DownloadManagerViewModel) { | ||
self._viewModel = .init(wrappedValue: viewModel) | ||
} | ||
|
||
var body: some View { | ||
VStack(spacing: .zero) { | ||
HStack { | ||
Button(action: { | ||
viewModel.deleteFile() | ||
dismiss() | ||
}, label: { | ||
Image(systemSymbol: .xmarkCircleFill) | ||
.font(.title) | ||
.foregroundStyle( | ||
Color(uiColor: .systemBackground), | ||
.gray.opacity(0.5) | ||
) | ||
}) | ||
.frame(maxWidth: .infinity, alignment: .trailing) | ||
.padding() | ||
} | ||
if viewModel.finished { | ||
successView | ||
} else if viewModel.failed { | ||
fileCard | ||
failedCard | ||
} else { | ||
ProgressView() | ||
.progressViewStyle(.circular) | ||
.scaleEffect(2) | ||
.padding(Spaces.four) | ||
Text(L10n.DownloadManager.Downloading.title) | ||
.font(.title.bold()) | ||
fileCard | ||
} | ||
Spacer() | ||
} | ||
} | ||
|
||
private var successView: some View { | ||
VStack(spacing: Spaces.three) { | ||
Image(systemSymbol: .checkmark) | ||
.foregroundStyle(.green) | ||
.font(.system(size: 100)) | ||
.symbolEffect( | ||
.bounce, | ||
options: .nonRepeating | ||
) | ||
Text(L10n.DownloadManager.Finished.title) | ||
.font(.title.bold()) | ||
if let url = viewModel.lastURLCreated { | ||
ShareLink(viewModel.fileName, item: url) | ||
.padding() | ||
.foregroundStyle(.white) | ||
.background(Color.asset(Asset.Colors.haPrimary)) | ||
.clipShape(RoundedRectangle(cornerRadius: 12)) | ||
.padding() | ||
} | ||
} | ||
} | ||
|
||
private var fileCard: some View { | ||
HStack { | ||
Image(systemSymbol: .docZipper) | ||
Text(viewModel.fileName) | ||
} | ||
.padding() | ||
.frame(maxWidth: .infinity, alignment: .leading) | ||
.background(.gray.opacity(0.5)) | ||
.clipShape(RoundedRectangle(cornerRadius: 12)) | ||
.padding() | ||
} | ||
|
||
private var failedCard: some View { | ||
Text(viewModel.errorMessage) | ||
.frame(maxWidth: .infinity, alignment: .leading) | ||
.multilineTextAlignment(.leading) | ||
.padding() | ||
.background(.red.opacity(0.5)) | ||
.clipShape(RoundedRectangle(cornerRadius: 12)) | ||
.padding() | ||
} | ||
} | ||
|
||
#Preview { | ||
if #available(iOS 17.0, *) { | ||
DownloadManagerView(viewModel: .init()) | ||
} else { | ||
Text("Hey there") | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
Sources/App/WebView/DownloadManager/DownloadManagerViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Foundation | ||
import Shared | ||
import WebKit | ||
|
||
@MainActor | ||
final class DownloadManagerViewModel: NSObject, ObservableObject { | ||
@Published var fileName: String = "" | ||
@Published var finished: Bool = false | ||
@Published var failed: Bool = false | ||
@Published var errorMessage: String = "" | ||
@Published var lastURLCreated: URL? | ||
|
||
func deleteFile() { | ||
if let url = lastURLCreated { | ||
// Guarantee to delete file before leaving screen | ||
do { | ||
try FileManager.default.removeItem(at: url) | ||
} catch { | ||
Current.Log.error("Failed to remove file before leaving download manager at \(url), error: \(error)") | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension DownloadManagerViewModel: WKDownloadDelegate { | ||
func download( | ||
_ download: WKDownload, | ||
decideDestinationUsing response: URLResponse, | ||
suggestedFilename: String | ||
) async -> URL? { | ||
let urls = FileManager.default.urls(for: .cachesDirectory, in: .allDomainsMask) | ||
let name = suggestedFilename.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "Unknown" | ||
fileName = name | ||
if let url = URL(string: name, relativeTo: urls[0]) { | ||
lastURLCreated = url | ||
// Guarantee file does not exist, otherwise download will fail | ||
do { | ||
try FileManager.default.removeItem(at: url) | ||
} catch { | ||
Current.Log.error("Failed to remove file for download manager at \(url), error: \(error)") | ||
} | ||
|
||
return url | ||
} else { | ||
return nil | ||
} | ||
} | ||
|
||
func downloadDidFinish(_ download: WKDownload) { | ||
finished = true | ||
} | ||
|
||
func download(_ download: WKDownload, didFailWithError error: any Error, resumeData: Data?) { | ||
errorMessage = L10n.DownloadManager.Failed.title(error.localizedDescription) | ||
failed = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters