|
| 1 | +import ActivityKit |
| 2 | +import AlarmKit |
| 3 | +import WidgetKit |
| 4 | +import SwiftUI |
| 5 | +import NitroActivityKitCore |
| 6 | + |
| 7 | +@available(iOS 26.0, *) |
| 8 | +func getAlarmTimeInterval() -> Date { |
| 9 | + do { |
| 10 | + let addInterval = try AlarmManager.shared.alarms.first(where: { alarm in |
| 11 | + alarm.state == .countdown |
| 12 | + })?.countdownDuration?.preAlert ?? 10 |
| 13 | + |
| 14 | + let now = Date(timeIntervalSinceNow: addInterval) |
| 15 | + |
| 16 | + // now.addTimeInterval(addInterval) |
| 17 | + |
| 18 | + return now |
| 19 | + } catch { |
| 20 | + print("AlarmKit: failed to fetch countdown alarm — \(error)") |
| 21 | + return Date(timeIntervalSinceNow: 20) |
| 22 | + } |
| 23 | + |
| 24 | +} |
| 25 | + |
| 26 | +@available(iOS 26.0, *) |
| 27 | +struct WidgetLiveActivityAlarm: Widget { |
| 28 | + |
| 29 | + var body: some WidgetConfiguration { |
| 30 | + |
| 31 | + ActivityConfiguration( |
| 32 | + for: AlarmAttributes<GenericDictionaryAlarmStruct>.self) { context in |
| 33 | + // Changed VStack to mimic built-in timer style |
| 34 | + ZStack { |
| 35 | + /*RoundedRectangle(cornerRadius: 18, style: .continuous) |
| 36 | + .fill(.ultraThinMaterial)*/ |
| 37 | +// context.state.mode <- for checking state |
| 38 | + HStack(spacing: 8) { |
| 39 | + Text("Timer") |
| 40 | + .font(.subheadline.weight(.medium)) |
| 41 | + // .glassEffect(.tint(.orange)) |
| 42 | + .foregroundColor(.secondary) |
| 43 | + |
| 44 | + Text(context.attributes.metadata?.getDate("timerFiringAt") ?? Date(), |
| 45 | + style: .timer) |
| 46 | + .font(.system(size: 36, weight: .medium, design: .monospaced)) |
| 47 | + .foregroundColor(.orange) |
| 48 | + .minimumScaleFactor(0.5) |
| 49 | + // .glassEffect(.tint(.orange)) |
| 50 | + .lineLimit(1) |
| 51 | + } |
| 52 | + .padding() |
| 53 | + .activityBackgroundTint(Color.black.opacity(0.1)) |
| 54 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 55 | + } |
| 56 | + .clipShape(RoundedRectangle(cornerRadius: 18, style: .continuous)) |
| 57 | + .padding(16) |
| 58 | + .activityBackgroundTint(Color.black.opacity(0.1)) |
| 59 | + .containerBackground(.ultraThinMaterial, for: .widget) |
| 60 | + // .activitySystemActionForegroundColor(Color.black) |
| 61 | + |
| 62 | + } dynamicIsland: { context in |
| 63 | + DynamicIsland { |
| 64 | + DynamicIslandExpandedRegion(.leading) { |
| 65 | + Text("Leading") |
| 66 | + .glassEffect() |
| 67 | + } |
| 68 | + DynamicIslandExpandedRegion(.trailing) { |
| 69 | + Text("Trailing") |
| 70 | + .glassEffect() |
| 71 | + } |
| 72 | + DynamicIslandExpandedRegion(.center) { |
| 73 | + ZStack { |
| 74 | + RoundedRectangle(cornerRadius: 18, style: .continuous) |
| 75 | + .fill(.ultraThinMaterial) |
| 76 | + |
| 77 | + HStack(spacing: 8) { |
| 78 | + Text("Timer") |
| 79 | + .font(.subheadline.weight(.medium)) |
| 80 | + .foregroundColor(.secondary) |
| 81 | + .glassEffect() |
| 82 | + Text(context.attributes.metadata?.getDate("timerFiringAt") ?? Date(), |
| 83 | + style: .timer, |
| 84 | + ) |
| 85 | + .font(.system(size: 36, weight: .medium, design: .monospaced)) |
| 86 | + .foregroundColor(.orange) |
| 87 | + .minimumScaleFactor(0.5) |
| 88 | + .lineLimit(1) |
| 89 | + .glassEffect() |
| 90 | + } |
| 91 | + .padding() |
| 92 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 93 | + } |
| 94 | + .clipShape(RoundedRectangle(cornerRadius: 18, style: .continuous)) |
| 95 | + .padding(16) |
| 96 | + } |
| 97 | + } compactLeading: { |
| 98 | + Text("L") |
| 99 | + .glassEffect() |
| 100 | + } compactTrailing: { |
| 101 | + Text("Hello") |
| 102 | + .glassEffect() |
| 103 | + } minimal: { |
| 104 | + Text("Hello") |
| 105 | + .glassEffect() |
| 106 | + } |
| 107 | + .widgetURL(URL(string: "https://www.expo.dev")) |
| 108 | + .keylineTint(Color.red) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments