Neues Icon, parallele Downloads

This commit is contained in:
Scarriffle
2026-06-08 19:32:18 +02:00
parent 9497c6e315
commit f053f1d320
7 changed files with 316 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
import ActivityKit
import Foundation
/// Shared between the main app (to drive the activity lifecycle) and the
/// DownloadWidgetExtension (to render the Lock Screen / Dynamic Island UI).
/// Both targets must compile this exact definition keep them in sync.
struct DownloadActivityAttributes: ActivityAttributes {
/// Item title set once at activity start; never changes during the download.
let itemTitle: String
struct ContentState: Codable, Hashable {
/// Overall fraction across all tracks (0.0 1.0).
var progress: Double
/// Total bytes written to disk so far, shown as "X MB downloaded".
var bytesDownloaded: Int64
}
}

View File

@@ -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)
}

View File

@@ -0,0 +1,31 @@
// MARK: - Widget Extension Entry Point
//
// HOW TO WIRE THIS UP IN XCODE (one-time setup):
//
// 1. File > New > Target > Widget Extension
// - Product Name: DownloadWidgetExtension
// - Bundle ID: com.local.ABS-Client.DownloadWidgetExtension
// - Uncheck "Include Configuration App Intent"
// - Minimum deployment: iOS 16.2 (first stable ActivityKit release)
//
// 2. In the new target's Build Phases > Compile Sources, add:
// - DownloadWidgetBundle.swift (this file)
// - DownloadLiveActivityWidget.swift
// - DownloadActivityAttributes.swift (the copy in this folder)
//
// 3. In the main app target's Build Phases > Compile Sources, confirm:
// - DownloadActivityAttributes.swift (the copy in Services/)
// is included. The two copies must stay identical.
//
// 4. In the main app's Build Phases > Embed App Extensions, add the
// DownloadWidgetExtension.appex product.
import SwiftUI
import WidgetKit
@main
struct DownloadWidgetBundle: WidgetBundle {
var body: some Widget {
DownloadLiveActivityWidget()
}
}