Neues Icon, parallele Downloads
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
import ActivityKit
|
||||
import SwiftUI
|
||||
import WidgetKit
|
||||
|
||||
// MARK: - Widget configuration
|
||||
|
||||
struct DownloadLiveActivityWidget: Widget {
|
||||
var body: some WidgetConfiguration {
|
||||
ActivityConfiguration(for: DownloadActivityAttributes.self) { context in
|
||||
// Lock Screen / Notification Center banner
|
||||
DownloadLockScreenView(context: context)
|
||||
.activityBackgroundTint(Color(.systemBackground).opacity(0.6))
|
||||
} dynamicIsland: { context in
|
||||
DynamicIsland {
|
||||
// Expanded region shown when the user long-presses the island
|
||||
DynamicIslandExpandedRegion(.leading) {
|
||||
Image(systemName: "arrow.down.circle.fill")
|
||||
.foregroundStyle(.blue)
|
||||
.font(.title2)
|
||||
}
|
||||
DynamicIslandExpandedRegion(.center) {
|
||||
Text(context.attributes.itemTitle)
|
||||
.font(.caption.bold())
|
||||
.lineLimit(1)
|
||||
.minimumScaleFactor(0.8)
|
||||
}
|
||||
DynamicIslandExpandedRegion(.bottom) {
|
||||
VStack(spacing: 4) {
|
||||
ProgressView(value: context.state.progress)
|
||||
.tint(.blue)
|
||||
HStack {
|
||||
Text(bytesFormatted(context.state.bytesDownloaded))
|
||||
.font(.caption2)
|
||||
.foregroundStyle(.secondary)
|
||||
Spacer()
|
||||
Text(percentFormatted(context.state.progress))
|
||||
.font(.caption2.bold())
|
||||
.monospacedDigit()
|
||||
}
|
||||
}
|
||||
}
|
||||
} compactLeading: {
|
||||
Image(systemName: "arrow.down.circle.fill")
|
||||
.foregroundStyle(.blue)
|
||||
.font(.caption)
|
||||
} compactTrailing: {
|
||||
Text(percentFormatted(context.state.progress))
|
||||
.font(.caption2.bold())
|
||||
.monospacedDigit()
|
||||
} minimal: {
|
||||
Image(systemName: "arrow.down.circle.fill")
|
||||
.foregroundStyle(.blue)
|
||||
.font(.caption2)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Lock Screen view
|
||||
|
||||
struct DownloadLockScreenView: View {
|
||||
let context: ActivityViewContext<DownloadActivityAttributes>
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
HStack(spacing: 8) {
|
||||
Image(systemName: "arrow.down.circle.fill")
|
||||
.foregroundStyle(.blue)
|
||||
Text(context.attributes.itemTitle)
|
||||
.font(.subheadline.bold())
|
||||
.lineLimit(1)
|
||||
Spacer()
|
||||
Text(percentFormatted(context.state.progress))
|
||||
.font(.subheadline.bold())
|
||||
.monospacedDigit()
|
||||
}
|
||||
ProgressView(value: context.state.progress)
|
||||
.tint(.blue)
|
||||
Text(bytesFormatted(context.state.bytesDownloaded) + " heruntergeladen")
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func percentFormatted(_ fraction: Double) -> String {
|
||||
String(format: "%.0f%%", fraction * 100)
|
||||
}
|
||||
|
||||
private func bytesFormatted(_ bytes: Int64) -> String {
|
||||
let mb = Double(bytes) / (1_024 * 1_024)
|
||||
if mb >= 1 { return String(format: "%.1f MB", mb) }
|
||||
let kb = Double(bytes) / 1_024
|
||||
if kb >= 1 { return String(format: "%.0f KB", kb) }
|
||||
return "\(bytes) B"
|
||||
}
|
||||
|
||||
// MARK: - Previews
|
||||
|
||||
private let previewAttributes = DownloadActivityAttributes(itemTitle: "The Name of the Wind")
|
||||
|
||||
#Preview("Lock Screen", as: .content, using: previewAttributes) {
|
||||
DownloadLiveActivityWidget()
|
||||
} contentStates: {
|
||||
DownloadActivityAttributes.ContentState(progress: 0.35, bytesDownloaded: 52_428_800)
|
||||
DownloadActivityAttributes.ContentState(progress: 0.9, bytesDownloaded: 135_266_304)
|
||||
}
|
||||
|
||||
#Preview("Dynamic Island Expanded", as: .dynamicIsland(.expanded), using: previewAttributes) {
|
||||
DownloadLiveActivityWidget()
|
||||
} contentStates: {
|
||||
DownloadActivityAttributes.ContentState(progress: 0.35, bytesDownloaded: 52_428_800)
|
||||
DownloadActivityAttributes.ContentState(progress: 0.9, bytesDownloaded: 135_266_304)
|
||||
}
|
||||
|
||||
#Preview("Dynamic Island Compact", as: .dynamicIsland(.compact), using: previewAttributes) {
|
||||
DownloadLiveActivityWidget()
|
||||
} contentStates: {
|
||||
DownloadActivityAttributes.ContentState(progress: 0.35, bytesDownloaded: 52_428_800)
|
||||
DownloadActivityAttributes.ContentState(progress: 0.9, bytesDownloaded: 135_266_304)
|
||||
}
|
||||
|
||||
#Preview("Dynamic Island Minimal", as: .dynamicIsland(.minimal), using: previewAttributes) {
|
||||
DownloadLiveActivityWidget()
|
||||
} contentStates: {
|
||||
DownloadActivityAttributes.ContentState(progress: 0.35, bytesDownloaded: 52_428_800)
|
||||
}
|
||||
Reference in New Issue
Block a user