|
6 | 6 | // |
7 | 7 |
|
8 | 8 | import ActivityKit |
9 | | -import WidgetKit |
10 | 9 | import SwiftUI |
| 10 | +import WidgetKit |
11 | 11 |
|
12 | 12 | struct SystemStatusAttributes: ActivityAttributes { |
13 | | - public struct ContentState: Codable, Hashable { |
14 | | - var total_mem: Int |
15 | | - var used_mem: Int |
16 | | - var total_swap: Int |
17 | | - var used_swap: Int |
18 | | - var uptime: Int |
19 | | - var load_one: Double |
20 | | - var load_five: Double |
21 | | - var load_fifteen: Double |
22 | | - var sessions: Int |
23 | | - } |
24 | | - var hostname: String |
| 13 | + public struct ContentState: Codable, Hashable { |
| 14 | + var total_mem: Int |
| 15 | + var used_mem: Int |
| 16 | + var total_swap: Int |
| 17 | + var used_swap: Int |
| 18 | + var uptime: Int |
| 19 | + var load_one: Double |
| 20 | + var load_five: Double |
| 21 | + var load_fifteen: Double |
| 22 | + var sessions: Int |
| 23 | + } |
| 24 | + var hostname: String |
25 | 25 | } |
26 | 26 |
|
27 | 27 | struct SystemStatusLiveActivity: Widget { |
28 | | - var body: some WidgetConfiguration { |
29 | | - ActivityConfiguration(for: SystemStatusAttributes.self) { context in |
30 | | - // Lock screen/banner UI goes here |
31 | | - VStack { |
32 | | - HStack { |
33 | | - ProgressView(value: Float(context.state.used_mem) / Float(context.state.total_mem), total: 1.0) { |
34 | | - Text("RAM\n\(Int(Float(context.state.used_mem) / Float(context.state.total_mem) * 100))%") |
35 | | - } |
36 | | - .progressViewStyle(CircularProgressViewStyle(tint: getColor(value: Float(context.state.used_mem) / Float(context.state.total_mem)))) |
37 | | - Spacer() |
38 | | - ProgressView(value: context.state.load_five, total: 1.0) { |
39 | | - Text("CPU\n\(Int(context.state.load_five * 100))%") |
40 | | - } |
41 | | - .progressViewStyle(CircularProgressViewStyle(tint: getColor(value: Float(context.state.load_five)))) |
42 | | - } |
43 | | - Text("\(context.state.sessions) active sessions") |
44 | | - } |
45 | | - .padding() |
46 | | - .activityBackgroundTint(Color.black) |
47 | | - .activitySystemActionForegroundColor(Color.white) |
| 28 | + var body: some WidgetConfiguration { |
| 29 | + ActivityConfiguration(for: SystemStatusAttributes.self) { context in |
| 30 | + // Lock screen/banner UI goes here |
| 31 | + VStack { |
| 32 | + HStack { |
| 33 | + ProgressView( |
| 34 | + value: Float(context.state.used_mem) / Float(context.state.total_mem), total: 1.0 |
| 35 | + ) { |
| 36 | + Text( |
| 37 | + "RAM\n\(Int(Float(context.state.used_mem) / Float(context.state.total_mem) * 100))%") |
| 38 | + } |
| 39 | + .progressViewStyle( |
| 40 | + CircularProgressViewStyle( |
| 41 | + tint: getColor(value: Float(context.state.used_mem) / Float(context.state.total_mem))) |
| 42 | + ) |
| 43 | + Spacer() |
| 44 | + ProgressView(value: context.state.load_five, total: 1.0) { |
| 45 | + Text("CPU\n\(Int(context.state.load_five * 100))%") |
| 46 | + } |
| 47 | + .progressViewStyle( |
| 48 | + CircularProgressViewStyle(tint: getColor(value: Float(context.state.load_five)))) |
| 49 | + } |
| 50 | + Text("\(context.state.sessions) active sessions") |
| 51 | + } |
| 52 | + .padding() |
| 53 | + .activityBackgroundTint(Color.black) |
| 54 | + .activitySystemActionForegroundColor(Color.white) |
48 | 55 |
|
49 | | - } dynamicIsland: { context in |
50 | | - DynamicIsland { |
51 | | - DynamicIslandExpandedRegion(.leading) { |
52 | | - ProgressView(value: Float(context.state.used_mem) / Float(context.state.total_mem), total: 1.0) { |
53 | | - Text("RAM\n\(Int(100 * Float(context.state.used_mem) / Float(context.state.total_mem)))%") |
54 | | - } |
55 | | - .progressViewStyle(CircularProgressViewStyle(tint: getColor(value: Float(context.state.used_mem) / Float(context.state.total_mem)))) |
56 | | - } |
57 | | - DynamicIslandExpandedRegion(.trailing) { |
58 | | - ProgressView(value: context.state.load_five, total: 1.0) { |
59 | | - Text("CPU\n\(Int((context.state.load_five * 100).rounded()))%") |
60 | | - } |
61 | | - .progressViewStyle(CircularProgressViewStyle(tint: getColor(value: Float(context.state.load_five)))) |
62 | | - } |
63 | | - DynamicIslandExpandedRegion(.bottom) { |
64 | | - Text("\(context.state.sessions) active sessions") |
65 | | - } |
66 | | - } compactLeading: { |
67 | | - ProgressView(value: Float(context.state.used_mem) / Float(context.state.total_mem), total: 1.0) { |
68 | | - Label("RAM", systemImage: "memorychip") |
69 | | - .labelStyle(.iconOnly) |
70 | | - } |
71 | | - .progressViewStyle(CircularProgressViewStyle(tint: getColor(value: Float(context.state.used_mem) / Float(context.state.total_mem)))) |
72 | | - } compactTrailing: { |
73 | | - ProgressView(value: context.state.load_five, total: 1.0) { |
74 | | - Label("5m Load", systemImage: "gauge.open.with.lines.needle.33percent") |
75 | | - .labelStyle(.iconOnly) |
76 | | - } |
77 | | - .progressViewStyle(CircularProgressViewStyle(tint: getColor(value: Float(context.state.load_five)))) |
78 | | - } minimal: { |
79 | | - ProgressView(value: context.state.load_five, total: 1.0) { |
80 | | - Label("5m Load", systemImage: "gauge.open.with.lines.needle.33percent") |
81 | | - .labelStyle(.iconOnly) |
82 | | - } |
83 | | - .progressViewStyle(CircularProgressViewStyle(tint: getColor(value: Float(context.state.load_five)))) |
84 | | - } |
85 | | - .widgetURL(URL(string: "https://beartracks.io")) |
86 | | - .keylineTint(Color.red) |
| 56 | + } dynamicIsland: { context in |
| 57 | + DynamicIsland { |
| 58 | + DynamicIslandExpandedRegion(.leading) { |
| 59 | + ProgressView( |
| 60 | + value: Float(context.state.used_mem) / Float(context.state.total_mem), total: 1.0 |
| 61 | + ) { |
| 62 | + Text( |
| 63 | + "RAM\n\(Int(100 * Float(context.state.used_mem) / Float(context.state.total_mem)))%") |
| 64 | + } |
| 65 | + .progressViewStyle( |
| 66 | + CircularProgressViewStyle( |
| 67 | + tint: getColor(value: Float(context.state.used_mem) / Float(context.state.total_mem))) |
| 68 | + ) |
87 | 69 | } |
88 | | - } |
89 | | - |
90 | | - private func getColor(value: Float) -> Color { |
91 | | - if value > 0.9 { |
92 | | - return Color.red |
93 | | - } else if value > 0.8 { |
94 | | - return Color.yellow |
95 | | - } else { |
96 | | - return Color.green |
| 70 | + DynamicIslandExpandedRegion(.trailing) { |
| 71 | + ProgressView(value: context.state.load_five, total: 1.0) { |
| 72 | + Text("CPU\n\(Int((context.state.load_five * 100).rounded()))%") |
| 73 | + } |
| 74 | + .progressViewStyle( |
| 75 | + CircularProgressViewStyle(tint: getColor(value: Float(context.state.load_five)))) |
| 76 | + } |
| 77 | + DynamicIslandExpandedRegion(.bottom) { |
| 78 | + Text("\(context.state.sessions) active sessions") |
| 79 | + } |
| 80 | + } compactLeading: { |
| 81 | + ProgressView( |
| 82 | + value: Float(context.state.used_mem) / Float(context.state.total_mem), total: 1.0 |
| 83 | + ) { |
| 84 | + Label("RAM", systemImage: "memorychip") |
| 85 | + .labelStyle(.iconOnly) |
97 | 86 | } |
| 87 | + .progressViewStyle( |
| 88 | + CircularProgressViewStyle( |
| 89 | + tint: getColor(value: Float(context.state.used_mem) / Float(context.state.total_mem)))) |
| 90 | + } compactTrailing: { |
| 91 | + ProgressView(value: context.state.load_five, total: 1.0) { |
| 92 | + Label("5m Load", systemImage: "gauge.open.with.lines.needle.33percent") |
| 93 | + .labelStyle(.iconOnly) |
| 94 | + } |
| 95 | + .progressViewStyle( |
| 96 | + CircularProgressViewStyle(tint: getColor(value: Float(context.state.load_five)))) |
| 97 | + } minimal: { |
| 98 | + ProgressView(value: context.state.load_five, total: 1.0) { |
| 99 | + Label("5m Load", systemImage: "gauge.open.with.lines.needle.33percent") |
| 100 | + .labelStyle(.iconOnly) |
| 101 | + } |
| 102 | + .progressViewStyle( |
| 103 | + CircularProgressViewStyle(tint: getColor(value: Float(context.state.load_five)))) |
| 104 | + } |
| 105 | + .widgetURL(URL(string: "https://beartracks.io")) |
| 106 | + .keylineTint(Color.red) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private func getColor(value: Float) -> Color { |
| 111 | + if value > 0.9 { |
| 112 | + return Color.red |
| 113 | + } else if value > 0.8 { |
| 114 | + return Color.yellow |
| 115 | + } else { |
| 116 | + return Color.green |
98 | 117 | } |
| 118 | + } |
99 | 119 | } |
100 | 120 |
|
101 | 121 | extension SystemStatusAttributes { |
102 | | - fileprivate static var preview: SystemStatusAttributes { |
103 | | - SystemStatusAttributes(hostname: "beartracks.io") |
104 | | - } |
| 122 | + fileprivate static var preview: SystemStatusAttributes { |
| 123 | + SystemStatusAttributes(hostname: "beartracks.io") |
| 124 | + } |
105 | 125 | } |
106 | 126 |
|
107 | 127 | extension SystemStatusAttributes.ContentState { |
108 | | - fileprivate static var smiley: SystemStatusAttributes.ContentState { |
109 | | - SystemStatusAttributes.ContentState.init(total_mem: 1000, used_mem: 839, total_swap: 0, used_swap: 0, uptime: 2313124, load_one: 0.02, load_five: 0.40, load_fifteen: 0.93, sessions: 2) |
110 | | - } |
111 | | - |
112 | | - fileprivate static var starEyes: SystemStatusAttributes.ContentState { |
113 | | - SystemStatusAttributes.ContentState.init(total_mem: 1000, used_mem: 839, total_swap: 100, used_swap: 0, uptime: 2313124, load_one: 0.02, load_five: 0.40, load_fifteen: 0.93, sessions: 2) |
114 | | - } |
| 128 | + fileprivate static var smiley: SystemStatusAttributes.ContentState { |
| 129 | + SystemStatusAttributes.ContentState.init( |
| 130 | + total_mem: 1000, used_mem: 839, total_swap: 0, used_swap: 0, uptime: 2_313_124, |
| 131 | + load_one: 0.02, load_five: 0.40, load_fifteen: 0.93, sessions: 2) |
| 132 | + } |
| 133 | + |
| 134 | + fileprivate static var starEyes: SystemStatusAttributes.ContentState { |
| 135 | + SystemStatusAttributes.ContentState.init( |
| 136 | + total_mem: 1000, used_mem: 839, total_swap: 100, used_swap: 0, uptime: 2_313_124, |
| 137 | + load_one: 0.02, load_five: 0.40, load_fifteen: 0.93, sessions: 2) |
| 138 | + } |
115 | 139 | } |
116 | 140 |
|
117 | 141 | #Preview("Notification", as: .content, using: SystemStatusAttributes.preview) { |
118 | | - SystemStatusLiveActivity() |
| 142 | + SystemStatusLiveActivity() |
119 | 143 | } contentStates: { |
120 | | - SystemStatusAttributes.ContentState.smiley |
121 | | - SystemStatusAttributes.ContentState.starEyes |
| 144 | + SystemStatusAttributes.ContentState.smiley |
| 145 | + SystemStatusAttributes.ContentState.starEyes |
122 | 146 | } |
0 commit comments