aboutsummaryrefslogtreecommitdiff
path: root/DomainDigWidget/SweepLiveActivity.swift
blob: 900ebc1d44417da668192bca4bb3fdcc0c73da18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import ActivityKit
import SwiftUI
import WidgetKit

struct SweepLiveActivity: Widget {
    var body: some WidgetConfiguration {
        ActivityConfiguration(for: SweepActivityAttributes.self) { context in
            // Lock Screen / banner presentation.
            SweepActivityBannerView(context: context)
                .padding()
                .activityBackgroundTint(Color(.systemBackground).opacity(0.85))
        } dynamicIsland: { context in
            DynamicIsland {
                DynamicIslandExpandedRegion(.leading) {
                    Label(context.attributes.title, systemImage: "arrow.trianglehead.2.clockwise")
                        .font(.caption)
                        .foregroundStyle(Color(.appTextSecondary))
                }
                DynamicIslandExpandedRegion(.trailing) {
                    Text("\(context.state.completed)/\(context.state.total)")
                        .font(.caption)
                        .monospacedDigit()
                }
                DynamicIslandExpandedRegion(.bottom) {
                    VStack(alignment: .leading, spacing: 4) {
                        ProgressView(value: context.state.fractionComplete)
                            .tint(Color(.statusInfo))
                        if let current = context.state.currentDomain {
                            Text(current)
                                .font(.caption2)
                                .foregroundStyle(Color(.appTextSecondary))
                                .lineLimit(1)
                        }
                    }
                }
            } compactLeading: {
                Image(systemName: "arrow.trianglehead.2.clockwise")
                    .foregroundStyle(Color(.statusInfo))
            } compactTrailing: {
                Text("\(context.state.completed)/\(context.state.total)")
                    .font(.caption2)
                    .monospacedDigit()
            } minimal: {
                ProgressView(value: context.state.fractionComplete)
                    .progressViewStyle(.circular)
                    .tint(Color(.statusInfo))
            }
        }
    }
}

private struct SweepActivityBannerView: View {
    let context: ActivityViewContext<SweepActivityAttributes>

    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            HStack {
                Label(context.attributes.title, systemImage: "arrow.trianglehead.2.clockwise")
                    .font(.caption)
                    .fontWeight(.semibold)
                    .foregroundStyle(Color(.appTextSecondary))
                Spacer()
                Text("\(context.state.completed) of \(context.state.total)")
                    .font(.caption)
                    .monospacedDigit()
            }

            ProgressView(value: context.state.fractionComplete)
                .tint(Color(.statusInfo))

            HStack {
                if let current = context.state.currentDomain {
                    Text(current)
                        .font(.caption2)
                        .foregroundStyle(Color(.appTextSecondary))
                        .lineLimit(1)
                } else {
                    Text(context.state.completed >= context.state.total ? "Finished" : "Working…")
                        .font(.caption2)
                        .foregroundStyle(Color(.appTextSecondary))
                }
                Spacer()
                if context.state.changed > 0 {
                    Text("\(context.state.changed) changed")
                        .font(.caption2)
                        .foregroundStyle(Color(.statusWarning))
                }
                if context.state.warnings > 0 {
                    Text("\(context.state.warnings) warnings")
                        .font(.caption2)
                        .foregroundStyle(Color(.statusCritical))
                }
            }
        }
    }
}