Files
mayday/Mayday/Views/Auth/VerifyEmailView.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

122 lines
4.1 KiB
Swift

import SwiftUI
struct VerifyEmailView: View {
let email: String
@EnvironmentObject var authViewModel: AuthViewModel
@State private var codeDigits: [String] = Array(repeating: "", count: 6)
@State private var resendCooldown = 0
@FocusState private var focusedIndex: Int?
@State private var cooldownTask: Task<Void, Never>?
var body: some View {
VStack(spacing: 32) {
Spacer()
VStack(spacing: 8) {
Text("Подтвердите email")
.font(.largeTitle.bold())
Text("Код отправлен на")
.foregroundStyle(.secondary)
Text(email)
.fontWeight(.semibold)
}
HStack(spacing: 12) {
ForEach(0..<6, id: \.self) { index in
TextField("", text: $codeDigits[index])
.frame(width: 44, height: 52)
.multilineTextAlignment(.center)
.font(.title2.bold())
.keyboardType(.numberPad)
.textContentType(.oneTimeCode)
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(focusedIndex == index ? Color.accentColor : Color.secondary, lineWidth: 2)
)
.focused($focusedIndex, equals: index)
.onChange(of: codeDigits[index]) { _, newValue in
handleDigitChange(index: index, value: newValue)
}
}
}
if let error = authViewModel.error {
Text(error)
.foregroundStyle(.red)
.font(.footnote)
}
Button {
Task { await resendCode() }
} label: {
if resendCooldown > 0 {
Text("Отправить повторно (\(resendCooldown) сек)")
} else {
Text("Отправить повторно")
}
}
.disabled(resendCooldown > 0)
Spacer()
}
.padding()
.navigationTitle("Подтверждение")
.navigationBarTitleDisplayMode(.inline)
.onAppear { focusedIndex = 0 }
.onDisappear { cooldownTask?.cancel() }
}
private func handleDigitChange(index: Int, value: String) {
let filtered = value.filter { $0.isNumber }
if filtered.count > 1 {
// Paste handling
let digits = Array(filtered.prefix(6))
for (i, d) in digits.enumerated() where i < 6 {
codeDigits[i] = String(d)
}
focusedIndex = min(digits.count, 5)
} else {
codeDigits[index] = filtered.isEmpty ? "" : String(filtered.last!)
if !filtered.isEmpty && index < 5 {
focusedIndex = index + 1
}
}
let code = codeDigits.joined()
if code.count == 6 {
Task { await submitCode(code) }
}
}
private func submitCode(_ code: String) async {
await authViewModel.verifyEmail(email: email, code: code)
if authViewModel.error == nil {
// Auto-login after verification - in a real flow we'd re-login here
// since verify doesn't return tokens
}
}
private func resendCode() async {
do {
try await AuthService.shared.resendCode(email: email)
startCooldown()
} catch {
authViewModel.error = error.localizedDescription
}
}
private func startCooldown() {
cooldownTask?.cancel()
cooldownTask = Task {
for remaining in stride(from: 60, through: 1, by: -1) {
guard !Task.isCancelled else { return }
resendCooldown = remaining
try? await Task.sleep(for: .seconds(1))
}
guard !Task.isCancelled else { return }
resendCooldown = 0
}
}
}