Files
mayday/Mayday/Views/Notifications/NotificationsView.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

117 lines
3.9 KiB
Swift

import SwiftUI
struct NotificationsView: View {
@EnvironmentObject var authViewModel: AuthViewModel
@StateObject private var viewModel = NotificationsViewModel()
@State private var showSettings = false
var body: some View {
NavigationStack {
Group {
if viewModel.isLoading && viewModel.notifications.isEmpty {
ProgressView()
} else if let error = viewModel.error, viewModel.notifications.isEmpty {
ContentUnavailableView(
"Ошибка загрузки",
systemImage: "exclamationmark.triangle",
description: Text(error)
)
} else if viewModel.notifications.isEmpty {
ContentUnavailableView(
"Нет уведомлений",
systemImage: "bell.slash",
description: Text("Новые уведомления появятся здесь")
)
} else {
notificationsList
}
}
.navigationTitle("Уведомления")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button {
showSettings = true
} label: {
Image(systemName: "gear")
}
}
}
.sheet(isPresented: $showSettings) {
SettingsView()
.environmentObject(authViewModel)
}
.task {
await viewModel.load()
viewModel.startPolling()
}
.onDisappear {
viewModel.stopPolling()
}
.refreshable {
await viewModel.load()
}
}
}
var notificationsList: some View {
List {
ForEach(viewModel.notifications) { notification in
NavigationLink(destination: NotificationDetailView(notification: notification, viewModel: viewModel)) {
NotificationRowView(notification: notification)
}
.swipeActions(edge: .leading) {
if !notification.isRead {
Button {
Task { await viewModel.markAsRead(notification) }
} label: {
Label("Прочитано", systemImage: "checkmark")
}
.tint(.blue)
}
}
.onAppear {
if notification.id == viewModel.notifications.last?.id {
Task { await viewModel.loadMore() }
}
}
}
if viewModel.isLoadingMore {
HStack {
Spacer()
ProgressView()
Spacer()
}
}
}
.listStyle(.plain)
}
}
struct NotificationRowView: View {
let notification: AppNotification
var body: some View {
HStack(spacing: 12) {
Circle()
.fill(notification.isRead ? Color.clear : Color.blue)
.frame(width: 8, height: 8)
VStack(alignment: .leading, spacing: 4) {
Text(notification.topic)
.font(.footnote)
.foregroundStyle(.secondary)
Text(notification.subject)
.font(.body)
.fontWeight(notification.isRead ? .regular : .semibold)
Text(notification.createdAt, style: .relative)
.font(.caption)
.foregroundStyle(.secondary)
}
Spacer()
}
.padding(.vertical, 4)
}
}