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>
This commit is contained in:
copilot-swe-agent[bot]
2026-03-13 23:29:19 +00:00
parent 9259a3693a
commit 597787a6c9
9 changed files with 91 additions and 78 deletions
+3 -1
View File
@@ -52,9 +52,11 @@ actor AuthService {
} }
func logout() async throws { func logout() async throws {
// Always clear local tokens, regardless of whether the network call succeeds,
// to avoid leaving a stale access token in Keychain.
defer { keychain.clearTokens() }
guard let refreshToken = keychain.loadRefreshToken() else { return } guard let refreshToken = keychain.loadRefreshToken() else { return }
let _: EmptyResponse = try await client.request(.logout(refreshToken: refreshToken)) let _: EmptyResponse = try await client.request(.logout(refreshToken: refreshToken))
keychain.clearTokens()
} }
func getMe() async throws -> UserResponse { func getMe() async throws -> UserResponse {
+37 -11
View File
@@ -122,7 +122,8 @@ actor HTTPClient {
private let baseURL: String private let baseURL: String
private let keychain = KeychainService.shared private let keychain = KeychainService.shared
private var isRefreshing = false // Single in-flight refresh task; concurrent 401s await this rather than racing.
private var refreshTask: Task<Void, Error>?
private init() { private init() {
#if DEBUG #if DEBUG
@@ -133,8 +134,7 @@ actor HTTPClient {
} }
func request<T: Decodable>(_ endpoint: Endpoint) async throws -> T { func request<T: Decodable>(_ endpoint: Endpoint) async throws -> T {
let response: T = try await performRequest(endpoint, retryOnUnauthorized: endpoint.requiresAuth) try await performRequest(endpoint, retryOnUnauthorized: endpoint.requiresAuth)
return response
} }
private func performRequest<T: Decodable>(_ endpoint: Endpoint, retryOnUnauthorized: Bool) async throws -> T { private func performRequest<T: Decodable>(_ endpoint: Endpoint, retryOnUnauthorized: Bool) async throws -> T {
@@ -160,7 +160,11 @@ actor HTTPClient {
urlRequest.url = components.url urlRequest.url = components.url
} }
} else { } else {
urlRequest.httpBody = try? JSONSerialization.data(withJSONObject: body) do {
urlRequest.httpBody = try JSONSerialization.data(withJSONObject: body)
} catch {
throw APIError.networkError(error)
}
} }
} }
@@ -175,10 +179,12 @@ actor HTTPClient {
throw APIError.networkError(URLError(.badServerResponse)) throw APIError.networkError(URLError(.badServerResponse))
} }
if httpResponse.statusCode == 401 && retryOnUnauthorized && !isRefreshing { if httpResponse.statusCode == 401 && retryOnUnauthorized {
isRefreshing = true do {
defer { isRefreshing = false } try await ensureTokenRefreshed()
try await refreshTokens() } catch {
throw APIError.unauthorized
}
return try await performRequest(endpoint, retryOnUnauthorized: false) return try await performRequest(endpoint, retryOnUnauthorized: false)
} }
@@ -210,15 +216,35 @@ actor HTTPClient {
} }
} }
private func refreshTokens() async throws { /// Ensures tokens are refreshed exactly once even when multiple requests receive 401
/// concurrently. All callers await the same Task; only one network request is made.
private func ensureTokenRefreshed() async throws {
if let existing = refreshTask {
try await existing.value
return
}
guard let refreshToken = keychain.loadRefreshToken() else { guard let refreshToken = keychain.loadRefreshToken() else {
keychain.clearTokens()
throw APIError.unauthorized throw APIError.unauthorized
} }
let response: TokenRefreshResponse = try await performRequest(
let task = Task<Void, Error> {
let response: TokenRefreshResponse = try await self.performRequest(
.refresh(refreshToken: refreshToken), .refresh(refreshToken: refreshToken),
retryOnUnauthorized: false retryOnUnauthorized: false
) )
try keychain.saveTokens(response.tokens) try self.keychain.saveTokens(response.tokens)
}
refreshTask = task
do {
try await task.value
refreshTask = nil
} catch {
refreshTask = nil
keychain.clearTokens()
throw error
}
} }
} }
@@ -1,5 +1,4 @@
import Foundation import Foundation
import UIKit
actor NotificationsAPIService { actor NotificationsAPIService {
static let shared = NotificationsAPIService() static let shared = NotificationsAPIService()
@@ -31,10 +30,6 @@ actor NotificationsAPIService {
func changePassword(current: String, new: String) async throws -> UserResponse { func changePassword(current: String, new: String) async throws -> UserResponse {
try await client.request(.changePassword(current: current, new: new)) try await client.request(.changePassword(current: current, new: new))
} }
func updateAppBadge(_ count: Int) async {
await UIApplication.shared.setApplicationIconBadgeNumber(count)
}
} }
struct LogoutAllResponse: Decodable { struct LogoutAllResponse: Decodable {
@@ -1,5 +1,6 @@
import Foundation import Foundation
import SwiftUI import SwiftUI
import UIKit
@MainActor @MainActor
class NotificationsViewModel: ObservableObject { class NotificationsViewModel: ObservableObject {
@@ -70,8 +71,8 @@ class NotificationsViewModel: ObservableObject {
} }
func startPolling() { func startPolling() {
// Polling always reloads page 1 to pick up new notifications. // Guard against starting a second polling loop if already running.
// Users who have scrolled to older pages will have the list reset on each interval. guard pollingTask == nil else { return }
pollingTask = Task { pollingTask = Task {
while !Task.isCancelled { while !Task.isCancelled {
try? await Task.sleep(for: .seconds(30)) try? await Task.sleep(for: .seconds(30))
@@ -88,8 +89,6 @@ class NotificationsViewModel: ObservableObject {
private func updateBadge() { private func updateBadge() {
let unreadCount = notifications.filter { !$0.isRead }.count let unreadCount = notifications.filter { !$0.isRead }.count
Task { UIApplication.shared.applicationIconBadgeNumber = unreadCount
await service.updateAppBadge(unreadCount)
}
} }
} }
+13 -11
View File
@@ -7,7 +7,7 @@ struct VerifyEmailView: View {
@State private var codeDigits: [String] = Array(repeating: "", count: 6) @State private var codeDigits: [String] = Array(repeating: "", count: 6)
@State private var resendCooldown = 0 @State private var resendCooldown = 0
@FocusState private var focusedIndex: Int? @FocusState private var focusedIndex: Int?
@State private var resendTimer: Timer? @State private var cooldownTask: Task<Void, Never>?
var body: some View { var body: some View {
VStack(spacing: 32) { VStack(spacing: 32) {
@@ -64,6 +64,7 @@ struct VerifyEmailView: View {
.navigationTitle("Подтверждение") .navigationTitle("Подтверждение")
.navigationBarTitleDisplayMode(.inline) .navigationBarTitleDisplayMode(.inline)
.onAppear { focusedIndex = 0 } .onAppear { focusedIndex = 0 }
.onDisappear { cooldownTask?.cancel() }
} }
private func handleDigitChange(index: Int, value: String) { private func handleDigitChange(index: Int, value: String) {
@@ -99,21 +100,22 @@ struct VerifyEmailView: View {
private func resendCode() async { private func resendCode() async {
do { do {
try await AuthService.shared.resendCode(email: email) try await AuthService.shared.resendCode(email: email)
resendCooldown = 60 startCooldown()
startCooldownTimer()
} catch { } catch {
authViewModel.error = error.localizedDescription authViewModel.error = error.localizedDescription
} }
} }
private func startCooldownTimer() { private func startCooldown() {
resendTimer?.invalidate() cooldownTask?.cancel()
resendTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in cooldownTask = Task {
if resendCooldown > 0 { for remaining in stride(from: 60, through: 1, by: -1) {
resendCooldown -= 1 guard !Task.isCancelled else { return }
} else { resendCooldown = remaining
resendTimer?.invalidate() try? await Task.sleep(for: .seconds(1))
} }
guard !Task.isCancelled else { return }
resendCooldown = 0
} }
} }
} }
@@ -104,7 +104,7 @@ struct NotificationRowView: View {
Text(notification.subject) Text(notification.subject)
.font(.body) .font(.body)
.fontWeight(notification.isRead ? .regular : .semibold) .fontWeight(notification.isRead ? .regular : .semibold)
Text(notification.createdAt.relativeFormatted) Text(notification.createdAt, style: .relative)
.font(.caption) .font(.caption)
.foregroundStyle(.secondary) .foregroundStyle(.secondary)
} }
@@ -114,15 +114,3 @@ struct NotificationRowView: View {
.padding(.vertical, 4) .padding(.vertical, 4)
} }
} }
extension Date {
var relativeFormatted: String {
Date.relativeDateTimeFormatter.localizedString(for: self, relativeTo: Date())
}
private static let relativeDateTimeFormatter: RelativeDateTimeFormatter = {
let formatter = RelativeDateTimeFormatter()
formatter.locale = Locale(identifier: "ru_RU")
return formatter
}()
}
+17 -1
View File
@@ -7,6 +7,7 @@ struct SettingsView: View {
@State private var showChangePassword = false @State private var showChangePassword = false
@State private var showSessions = false @State private var showSessions = false
@State private var showLogoutAllConfirm = false @State private var showLogoutAllConfirm = false
@State private var logoutAllError: String?
var body: some View { var body: some View {
NavigationStack { NavigationStack {
@@ -81,12 +82,27 @@ struct SettingsView: View {
) { ) {
Button("Выйти везде", role: .destructive) { Button("Выйти везде", role: .destructive) {
Task { Task {
_ = try? await NotificationsAPIService.shared.logoutAll() do {
_ = try await NotificationsAPIService.shared.logoutAll()
await authViewModel.logout() await authViewModel.logout()
} catch {
logoutAllError = error.localizedDescription
}
} }
} }
Button("Отмена", role: .cancel) {} Button("Отмена", role: .cancel) {}
} }
.alert(
"Ошибка",
isPresented: Binding(
get: { logoutAllError != nil },
set: { if !$0 { logoutAllError = nil } }
)
) {
Button("OK") { logoutAllError = nil }
} message: {
Text(logoutAllError ?? "")
}
.task { .task {
await viewModel.loadSessions() await viewModel.loadSessions()
} }
+2
View File
@@ -24,6 +24,8 @@
<dict> <dict>
<key>NSExtensionPointIdentifier</key> <key>NSExtensionPointIdentifier</key>
<string>com.apple.widgetkit-extension</string> <string>com.apple.widgetkit-extension</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).MaydayLiveActivityBundle</string>
</dict> </dict>
</dict> </dict>
</plist> </plist>
@@ -35,7 +35,11 @@ struct MaydayLiveActivityLiveActivity: Widget {
Spacer() Spacer()
VStack(alignment: .trailing, spacing: 2) { VStack(alignment: .trailing, spacing: 2) {
statusBadge(context.state.status) statusBadge(context.state.status)
Text("Длит.: \(duration(from: context.state.startedAt))") // Text(date, style: .timer) updates automatically without re-render.
HStack(spacing: 2) {
Text("Длит.:")
Text(context.state.startedAt, style: .timer)
}
.font(.caption2) .font(.caption2)
.foregroundStyle(.secondary) .foregroundStyle(.secondary)
} }
@@ -77,7 +81,8 @@ struct MaydayLiveActivityLiveActivity: Widget {
.font(.caption) .font(.caption)
.foregroundStyle(severityColor(context.attributes.severity)) .foregroundStyle(severityColor(context.attributes.severity))
} }
Text(context.state.startedAt.relativeFormatted) // Text(date, style: .relative) updates automatically without re-render.
Text(context.state.startedAt, style: .relative)
.font(.caption2) .font(.caption2)
.foregroundStyle(.secondary) .foregroundStyle(.secondary)
} }
@@ -92,8 +97,8 @@ struct MaydayLiveActivityLiveActivity: Widget {
@ViewBuilder @ViewBuilder
func statusBadge(_ status: AlertStatus) -> some View { func statusBadge(_ status: AlertStatus) -> some View {
let (text, color): (String, Color) = status == .active let (text, color): (String, Color) = status == .active
? ("active", .red) ? ("активен", .red)
: ("resolved", .green) : ("завершён", .green)
Text(text) Text(text)
.font(.caption2.bold()) .font(.caption2.bold())
.padding(.horizontal, 6) .padding(.horizontal, 6)
@@ -110,26 +115,4 @@ struct MaydayLiveActivityLiveActivity: Widget {
case .info: return .blue case .info: return .blue
} }
} }
func duration(from startDate: Date) -> String {
let interval = Date().timeIntervalSince(startDate)
let minutes = Int(interval / 60)
let hours = minutes / 60
if hours > 0 {
return "\(hours)ч \(minutes % 60)м"
}
return "\(minutes)м"
}
}
extension Date {
var relativeFormatted: String {
Date.relativeDateTimeFormatter.localizedString(for: self, relativeTo: Date())
}
private static let relativeDateTimeFormatter: RelativeDateTimeFormatter = {
let formatter = RelativeDateTimeFormatter()
formatter.locale = Locale(identifier: "ru_RU")
return formatter
}()
} }