Files
mayday/MaydayLiveActivity/MaydayLiveActivityLiveActivity.swift
T
copilot-swe-agent[bot] 597787a6c9 fix: address all PR review comments
- HTTPClient: replace isRefreshing bool with shared Task to safely
  coalesce concurrent 401 refresh attempts; surface JSON serialization
  error instead of silently dropping request body
- AuthService.logout: always clear Keychain tokens via defer, even when
  refresh token is absent, preventing stale access token
- NotificationsAPIService: remove updateAppBadge (UIKit call moved to
  @MainActor NotificationsViewModel); drop unused UIKit import
- NotificationsViewModel: guard startPolling() against duplicate tasks;
  update badge directly on @MainActor instead of hopping to actor
- VerifyEmailView: replace Timer (never invalidated) with async Task
  cancelled in .onDisappear
- NotificationsView: use Text(date, style: .relative) — auto-updates
  without custom formatter; remove duplicate Date extension
- SettingsView: handle logoutAll errors explicitly with alert instead of
  silently proceeding with local logout
- MaydayLiveActivity/Info.plist: add NSExtensionPrincipalClass so the
  widget extension is discoverable by the system
- Live Activity widget: replace frozen duration(from:) with
  Text(date, style: .timer); replace frozen relativeFormatted with
  Text(date, style: .relative); localize status badge to Russian

Co-authored-by: robonen <26167508+robonen@users.noreply.github.com>
2026-03-13 23:29:19 +00:00

119 lines
4.9 KiB
Swift

import ActivityKit
import WidgetKit
import SwiftUI
struct MaydayLiveActivityLiveActivity: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: AlertAttributes.self) { context in
// Lock Screen / Notification Center
lockScreenView(context: context)
.activityBackgroundTint(severityColor(context.attributes.severity).opacity(0.15))
.activitySystemActionForegroundColor(.primary)
} dynamicIsland: { context in
DynamicIsland {
DynamicIslandExpandedRegion(.leading) {
Label(context.attributes.topic, systemImage: "exclamationmark.triangle.fill")
.font(.caption)
.foregroundStyle(severityColor(context.attributes.severity))
}
DynamicIslandExpandedRegion(.trailing) {
if let value = context.state.value {
Text(value)
.font(.caption.bold())
.foregroundStyle(severityColor(context.attributes.severity))
}
}
DynamicIslandExpandedRegion(.bottom) {
HStack {
VStack(alignment: .leading, spacing: 2) {
Text(context.state.title)
.font(.subheadline.bold())
Text("Начало: \(context.state.startedAt.formatted(date: .omitted, time: .shortened))")
.font(.caption2)
.foregroundStyle(.secondary)
}
Spacer()
VStack(alignment: .trailing, spacing: 2) {
statusBadge(context.state.status)
// Text(date, style: .timer) updates automatically without re-render.
HStack(spacing: 2) {
Text("Длит.:")
Text(context.state.startedAt, style: .timer)
}
.font(.caption2)
.foregroundStyle(.secondary)
}
}
.padding(.horizontal)
}
} compactLeading: {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundStyle(severityColor(context.attributes.severity))
} compactTrailing: {
let shortTopic = context.attributes.topic.components(separatedBy: "/").last ?? context.attributes.topic
let valueText = context.state.value.map { " · \($0)" } ?? ""
Text("\(shortTopic)\(valueText)")
.font(.caption2)
.lineLimit(1)
} minimal: {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundStyle(severityColor(context.attributes.severity))
}
.keylineTint(severityColor(context.attributes.severity))
}
}
@ViewBuilder
func lockScreenView(context: ActivityViewContext<AlertAttributes>) -> some View {
HStack(spacing: 12) {
Image(systemName: "exclamationmark.triangle.fill")
.font(.title2)
.foregroundStyle(severityColor(context.attributes.severity))
VStack(alignment: .leading, spacing: 4) {
Text(context.attributes.topic)
.font(.caption)
.foregroundStyle(.secondary)
Text(context.state.title)
.font(.subheadline.bold())
if let value = context.state.value {
Text(value)
.font(.caption)
.foregroundStyle(severityColor(context.attributes.severity))
}
// Text(date, style: .relative) updates automatically without re-render.
Text(context.state.startedAt, style: .relative)
.font(.caption2)
.foregroundStyle(.secondary)
}
Spacer()
statusBadge(context.state.status)
}
.padding()
}
@ViewBuilder
func statusBadge(_ status: AlertStatus) -> some View {
let (text, color): (String, Color) = status == .active
? ("активен", .red)
: ("завершён", .green)
Text(text)
.font(.caption2.bold())
.padding(.horizontal, 6)
.padding(.vertical, 2)
.background(color.opacity(0.2))
.foregroundStyle(color)
.cornerRadius(4)
}
func severityColor(_ severity: Severity) -> Color {
switch severity {
case .critical: return .red
case .warning: return .yellow
case .info: return .blue
}
}
}