refactor: notifications and settings view models; enhance login and registration UI

This commit is contained in:
2026-03-15 21:40:20 +07:00
parent 0947c048c1
commit 37b87ececd
45 changed files with 985 additions and 680 deletions
@@ -1,24 +1,49 @@
import SwiftUI
struct NotificationDetailView: View {
let notification: AppNotification
let viewModel: NotificationsViewModel
let notificationId: UUID
@ObservedObject var viewModel: NotificationsViewModel
private var notification: AppNotification? {
viewModel.notifications.first { $0.id == notificationId }
}
init(notification: AppNotification, viewModel: NotificationsViewModel) {
self.notificationId = notification.id
self.viewModel = viewModel
}
var body: some View {
Group {
if let notification {
scrollContent(notification)
}
}
.background(Color(.systemGroupedBackground))
.navigationTitle("details_section")
.navigationBarTitleDisplayMode(.inline)
.task {
if let notification, !notification.isRead {
await viewModel.markAsRead(notification)
}
}
}
private func scrollContent(_ notification: AppNotification) -> some View {
ScrollView {
VStack(spacing: 0) {
// Hero header
headerSection
headerSection(notification)
// Info cards
VStack(spacing: 16) {
detailsCard
detailsCard(notification)
if let metadata = notification.metadata, !metadata.isEmpty {
metadataCard(metadata)
}
statusCard
statusCard(notification)
}
.padding(.horizontal, 16)
.padding(.top, 24)
@@ -44,29 +69,24 @@ struct NotificationDetailView: View {
}
}
}
.background(Color(.systemGroupedBackground))
.navigationTitle("details_section")
.navigationBarTitleDisplayMode(.inline)
.task {
await viewModel.markAsRead(notification)
}
}
// MARK: - Hero Header
private var headerSection: some View {
VStack(spacing: 16) {
private func headerSection(_ notification: AppNotification) -> some View {
let severity = NotificationSeverity(from: notification.metadata)
return VStack(spacing: 16) {
ZStack {
Circle()
.fill(Color(.secondarySystemGroupedBackground))
.frame(width: 88, height: 88)
.shadow(color: topicColor.opacity(0.3), radius: 12, y: 4)
.shadow(color: severity.color.opacity(0.3), radius: 12, y: 4)
Circle()
.fill(topicColor.opacity(0.15))
.fill(severity.color.opacity(0.15))
.frame(width: 80, height: 80)
Image(systemName: topicIcon)
Image(systemName: severity.icon)
.font(.system(size: 32))
.foregroundStyle(topicColor)
.foregroundStyle(severity.color)
}
VStack(spacing: 6) {
@@ -79,7 +99,7 @@ struct NotificationDetailView: View {
.foregroundStyle(.secondary)
}
statusBadge
statusBadge(for: notification)
}
.padding(.vertical, 28)
.frame(maxWidth: .infinity)
@@ -87,7 +107,7 @@ struct NotificationDetailView: View {
// MARK: - Status Badge
private var statusBadge: some View {
private func statusBadge(for notification: AppNotification) -> some View {
let (text, color): (String, Color) = notification.isRead
? (String(localized: "status_read"), .green)
: (String(localized: "status_new"), .red)
@@ -102,7 +122,7 @@ struct NotificationDetailView: View {
// MARK: - Details Card
private var detailsCard: some View {
private func detailsCard(_ notification: AppNotification) -> some View {
VStack(alignment: .leading, spacing: 12) {
Label("details_section", systemImage: "doc.text.fill")
.font(.subheadline.bold())
@@ -168,14 +188,14 @@ struct NotificationDetailView: View {
// MARK: - Status Card
private var statusCard: some View {
private func statusCard(_ notification: AppNotification) -> some View {
VStack(alignment: .leading, spacing: 12) {
Label("status_section", systemImage: "clock.fill")
.font(.subheadline.bold())
.foregroundStyle(.primary)
VStack(spacing: 8) {
infoRow(icon: "paperplane.fill", label: String(localized: "channel_label"), value: channelLabel)
infoRow(icon: "paperplane.fill", label: String(localized: "channel_label"), value: channelLabel(for: notification))
Divider()
infoRow(icon: "clock", label: String(localized: "received_label"), value: notification.createdAt.formatted(date: .abbreviated, time: .shortened))
if let readAt = notification.readAt {
@@ -209,7 +229,7 @@ struct NotificationDetailView: View {
// MARK: - Helpers
private var channelLabel: String {
private func channelLabel(for notification: AppNotification) -> String {
switch notification.channel {
case .inApp: return String(localized: "channel_in_app")
case .apns: return "Push"
@@ -218,34 +238,4 @@ struct NotificationDetailView: View {
case .webhook: return "Webhook"
}
}
private var topicIcon: String {
let lowered = (notification.source ?? "").lowercased()
if lowered.contains("fire") || lowered.contains("пожар") || lowered.contains("огонь") {
return "flame.fill"
} else if lowered.contains("medical") || lowered.contains("медиц") || lowered.contains("здоров") {
return "heart.fill"
} else if lowered.contains("security") || lowered.contains("безопас") {
return "shield.fill"
} else if lowered.contains("water") || lowered.contains("вод") || lowered.contains("затоп") {
return "drop.fill"
} else {
return "exclamationmark.triangle.fill"
}
}
private var topicColor: Color {
let lowered = (notification.source ?? "").lowercased()
if lowered.contains("fire") || lowered.contains("пожар") || lowered.contains("огонь") {
return .red
} else if lowered.contains("medical") || lowered.contains("медиц") || lowered.contains("здоров") {
return .green
} else if lowered.contains("security") || lowered.contains("безопас") {
return .blue
} else if lowered.contains("water") || lowered.contains("вод") || lowered.contains("затоп") {
return .cyan
} else {
return .orange
}
}
}
@@ -36,21 +36,8 @@ struct NotificationsView: View {
}
.background(Color(.systemGroupedBackground))
.navigationTitle("notifications_title")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
#if DEBUG
if PreviewData.isPreviewMode {
ToolbarItem(placement: .topBarLeading) {
Button(action: {}) {
Text("demo_badge")
.font(.caption2.bold())
}
.buttonStyle(.borderedProminent)
.tint(.orange)
.controlSize(.mini)
.allowsHitTesting(false)
}
}
#endif
ToolbarItem(placement: .topBarTrailing) {
Button {
showSettings = true
@@ -86,6 +73,7 @@ struct NotificationsView: View {
NavigationLink(destination: NotificationDetailView(notification: notification, viewModel: viewModel)) {
ActiveNotificationCard(notification: notification)
}
.id("\(notification.id)-\(notification.isRead)")
.buttonStyle(.plain)
.padding(.horizontal, 16)
.padding(.bottom, 12)
@@ -103,6 +91,7 @@ struct NotificationsView: View {
NavigationLink(destination: NotificationDetailView(notification: notification, viewModel: viewModel)) {
ResolvedNotificationCard(notification: notification)
}
.id("\(notification.id)-\(notification.isRead)")
.buttonStyle(.plain)
.padding(.horizontal, 16)
.padding(.bottom, 12)
@@ -146,7 +135,7 @@ struct ActiveNotificationCard: View {
var body: some View {
VStack(alignment: .leading, spacing: 12) {
HStack(alignment: .top) {
NotificationIconView(source: notification.source, isActive: true)
NotificationIconView(severity: NotificationSeverity(from: notification.metadata), isActive: true)
VStack(alignment: .leading, spacing: 2) {
Text(notification.subject ?? "")
@@ -206,7 +195,7 @@ struct ResolvedNotificationCard: View {
var body: some View {
VStack(alignment: .leading, spacing: 10) {
HStack(alignment: .top) {
NotificationIconView(source: notification.source, isActive: false)
NotificationIconView(severity: NotificationSeverity(from: notification.metadata), isActive: false)
VStack(alignment: .leading, spacing: 2) {
Text(notification.subject ?? "")
@@ -255,51 +244,3 @@ struct ResolvedNotificationCard: View {
.shadow(color: .black.opacity(0.06), radius: 8, y: 2)
}
}
// MARK: - Notification Icon
struct NotificationIconView: View {
let source: String?
let isActive: Bool
private var iconName: String {
let lowered = (source ?? "").lowercased()
if lowered.contains("fire") || lowered.contains("пожар") || lowered.contains("огонь") {
return "flame.fill"
} else if lowered.contains("medical") || lowered.contains("медиц") || lowered.contains("здоров") {
return "heart.fill"
} else if lowered.contains("security") || lowered.contains("безопас") {
return "shield.fill"
} else if lowered.contains("water") || lowered.contains("вод") || lowered.contains("затоп") {
return "drop.fill"
} else {
return "exclamationmark.triangle.fill"
}
}
private var iconColor: Color {
let lowered = (source ?? "").lowercased()
if lowered.contains("fire") || lowered.contains("пожар") || lowered.contains("огонь") {
return .red
} else if lowered.contains("medical") || lowered.contains("медиц") || lowered.contains("здоров") {
return .green
} else if lowered.contains("security") || lowered.contains("безопас") {
return .blue
} else if lowered.contains("water") || lowered.contains("вод") || lowered.contains("затоп") {
return .cyan
} else {
return .orange
}
}
var body: some View {
ZStack {
Circle()
.fill(isActive ? .white.opacity(0.25) : iconColor.opacity(0.12))
.frame(width: 40, height: 40)
Image(systemName: iconName)
.font(.body)
.foregroundStyle(isActive ? .white : iconColor)
}
}
}