krz/domain-dig

an ios app for DNS & SSL analysis

clone: git clone https://gitbay.org/krz/domain-dig.git

v5.0.2: DomainDigWidget/SweepLiveActivity.swift · raw

 1import ActivityKit
 2import SwiftUI
 3import WidgetKit
 4
 5struct SweepLiveActivity: Widget {
 6    var body: some WidgetConfiguration {
 7        ActivityConfiguration(for: SweepActivityAttributes.self) { context in
 8            // Lock Screen / banner presentation.
 9            SweepActivityBannerView(context: context)
10                .padding()
11                .activityBackgroundTint(Color(.systemBackground).opacity(0.85))
12        } dynamicIsland: { context in
13            DynamicIsland {
14                DynamicIslandExpandedRegion(.leading) {
15                    Label(context.attributes.title, systemImage: "arrow.trianglehead.2.clockwise")
16                        .font(.caption)
17                        .foregroundStyle(Color(.appTextSecondary))
18                }
19                DynamicIslandExpandedRegion(.trailing) {
20                    Text("\(context.state.completed)/\(context.state.total)")
21                        .font(.caption)
22                        .monospacedDigit()
23                }
24                DynamicIslandExpandedRegion(.bottom) {
25                    VStack(alignment: .leading, spacing: 4) {
26                        ProgressView(value: context.state.fractionComplete)
27                            .tint(Color(.statusInfo))
28                        if let current = context.state.currentDomain {
29                            Text(current)
30                                .font(.caption2)
31                                .foregroundStyle(Color(.appTextSecondary))
32                                .lineLimit(1)
33                        }
34                    }
35                }
36            } compactLeading: {
37                Image(systemName: "arrow.trianglehead.2.clockwise")
38                    .foregroundStyle(Color(.statusInfo))
39            } compactTrailing: {
40                Text("\(context.state.completed)/\(context.state.total)")
41                    .font(.caption2)
42                    .monospacedDigit()
43            } minimal: {
44                ProgressView(value: context.state.fractionComplete)
45                    .progressViewStyle(.circular)
46                    .tint(Color(.statusInfo))
47            }
48        }
49    }
50}
51
52private struct SweepActivityBannerView: View {
53    let context: ActivityViewContext<SweepActivityAttributes>
54
55    var body: some View {
56        VStack(alignment: .leading, spacing: 8) {
57            HStack {
58                Label(context.attributes.title, systemImage: "arrow.trianglehead.2.clockwise")
59                    .font(.caption)
60                    .fontWeight(.semibold)
61                    .foregroundStyle(Color(.appTextSecondary))
62                Spacer()
63                Text("\(context.state.completed) of \(context.state.total)")
64                    .font(.caption)
65                    .monospacedDigit()
66            }
67
68            ProgressView(value: context.state.fractionComplete)
69                .tint(Color(.statusInfo))
70
71            HStack {
72                if let current = context.state.currentDomain {
73                    Text(current)
74                        .font(.caption2)
75                        .foregroundStyle(Color(.appTextSecondary))
76                        .lineLimit(1)
77                } else {
78                    Text(context.state.completed >= context.state.total ? "Finished" : "Working…")
79                        .font(.caption2)
80                        .foregroundStyle(Color(.appTextSecondary))
81                }
82                Spacer()
83                if context.state.changed > 0 {
84                    Text("\(context.state.changed) changed")
85                        .font(.caption2)
86                        .foregroundStyle(Color(.statusWarning))
87                }
88                if context.state.warnings > 0 {
89                    Text("\(context.state.warnings) warnings")
90                        .font(.caption2)
91                        .foregroundStyle(Color(.statusCritical))
92                }
93            }
94        }
95    }
96}