Skip to content

Commit

Permalink
Demo: Add a folder downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
buh committed Oct 4, 2023
1 parent 967161c commit f336546
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
10 changes: 7 additions & 3 deletions Demo/Hubble/ImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,20 @@ struct ImageView: View {
.symbolRenderingMode(.multicolor)
.padding(.horizontal)
} else {
ProgressView(value: progress)
.padding(.horizontal)
if progress < 1 {
ProgressView(value: progress)
.padding(.horizontal)
} else {
Text("Decompressing...")
}
}
}
.navigationTitle(entry?.fileName ?? "")
.task { @MainActor in
guard let entry, let url else { return }

do {
let data = try await URLSession(configuration: .ephemeral)
let data = try await URLSession(configuration: .default)
.zipEntryData(
entry,
from: url,
Expand Down
69 changes: 57 additions & 12 deletions Demo/Hubble/ImagesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ struct ImagesView: View {

let title: String
let url: URL
private let urlSession = URLSession(configuration: .default)
@State private var entries = [ZIPEntry]()
@State private var rootFolder: ZIPFolder
@State private var hoveredEntry: ZIPEntry?
@State private var isLoading = false
@State private var error: String?
@State private var progress: Double = 0
@State private var progressFolderId: UUID?
@State private var folderDownloadingTask: Task<(), Error>?

init(title: String, url: URL, rootFolder: ZIPFolder? = nil) {
self.title = title
Expand Down Expand Up @@ -39,7 +43,7 @@ struct ImagesView: View {

do {
isLoading = true
entries = try await URLSession(configuration: .ephemeral).zipEntries(from: url)
entries = try await urlSession.zipEntries(from: url)
rootFolder = entries.rootFolder()
} catch let zipError as ZIPError {
self.error = zipError.localizedDescription
Expand Down Expand Up @@ -77,21 +81,62 @@ struct ImagesView: View {
NavigationLink {
ImagesView(title: folder.name, url: url, rootFolder: folder)
} label: {
HStack(spacing: 16) {
Image(systemName: "folder")

VStack(alignment: .leading, spacing: 4) {
Text(folder.name)
folderMetaInfo(folder)
ZStack(alignment: .bottom) {
HStack(spacing: 16) {
Image(systemName: "folder")

VStack(alignment: .leading, spacing: 4) {
Text(folder.name)
folderMetaInfo(folder)
}

Spacer()

Text(ByteCountFormatter.appFormatter.string(fromByteCount: folder.compressedSize))
.foregroundColor(.secondary)
.font(.caption)
}

Spacer()

Text(ByteCountFormatter.appFormatter.string(fromByteCount: folder.compressedSize))
.foregroundColor(.secondary)
.font(.caption)
if progressFolderId == folder.id {
ProgressView(value: progress)
.offset(y: 10)
}
}
}
.swipeActions(edge: .trailing) {
Button(action: {
folderDownloadingTask?.cancel()
folderDownloadingTask = Task {
progressFolderId = folder.id

do {
_ = try await urlSession.zipFolderData(
folder, from: url,
progress: .init(callback: { value in
Task { @MainActor in
self.progress = value

if value == 1 {
self.progressFolderId = nil
}
}
})
)
} catch let zipError as ZIPError {
self.error = zipError.localizedDescription
print("💥 ImagesView:", zipError)
} catch {
self.error = error.localizedDescription
print("💥 ImagesView:", error)
}
}
}, label: {
Image(systemName: "arrow.down.circle.fill")
})
}
.onDisappear {
folderDownloadingTask?.cancel()
}
}

@ViewBuilder
Expand Down

0 comments on commit f336546

Please sign in to comment.