feat: add complete Mayday iOS Xcode project
- Swift 6, SwiftUI, MVVM + async/await architecture - iOS 17.0 minimum deployment target - Two targets: Mayday app + MaydayLiveActivity widget extension - Models: UserResponse, TokenPair, AppNotification, SessionResponse, AlertAttributes - Services: HTTPClient (actor), AuthService, KeychainService, NotificationsAPIService, PushNotificationService - ViewModels: AuthViewModel, NotificationsViewModel, SettingsViewModel - Views: Login/Register/VerifyEmail, NotificationsList/Detail, Settings/ChangePassword/Sessions - APNs push notifications with UIApplicationDelegate - ActivityKit Live Activities for Dynamic Island + Lock Screen - Keychain (Security framework) token storage - 30-second polling with pagination for notifications - Xcode project file (project.pbxproj) with correct build phases for both targets Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
class AuthViewModel: ObservableObject {
|
||||
@Published var isAuthenticated = false
|
||||
@Published var currentUser: UserResponse?
|
||||
@Published var isLoading = false
|
||||
@Published var error: String?
|
||||
|
||||
private let auth = AuthService.shared
|
||||
private let keychain = KeychainService.shared
|
||||
|
||||
func checkAuthStatus() async {
|
||||
guard keychain.loadAccessToken() != nil else {
|
||||
isAuthenticated = false
|
||||
return
|
||||
}
|
||||
isLoading = true
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
currentUser = try await auth.getMe()
|
||||
isAuthenticated = true
|
||||
await requestPushIfNeeded()
|
||||
} catch APIError.unauthorized {
|
||||
isAuthenticated = false
|
||||
} catch {
|
||||
isAuthenticated = false
|
||||
}
|
||||
}
|
||||
|
||||
func login(email: String, password: String) async {
|
||||
isLoading = true
|
||||
error = nil
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
currentUser = try await auth.login(email: email, password: password)
|
||||
isAuthenticated = true
|
||||
await requestPushIfNeeded()
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
func register(email: String, password: String) async -> Bool {
|
||||
isLoading = true
|
||||
error = nil
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
_ = try await auth.register(email: email, password: password)
|
||||
return true
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func verifyEmail(email: String, code: String) async {
|
||||
isLoading = true
|
||||
error = nil
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
_ = try await auth.verifyEmail(email: email, code: code)
|
||||
// Auto-login after verification is handled by calling login from view
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
func logout() async {
|
||||
isLoading = true
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
try await auth.logout()
|
||||
} catch {
|
||||
// Clear anyway
|
||||
keychain.clearTokens()
|
||||
}
|
||||
isAuthenticated = false
|
||||
currentUser = nil
|
||||
}
|
||||
|
||||
private func requestPushIfNeeded() async {
|
||||
let granted = await PushNotificationService.shared.requestPermission()
|
||||
if granted {
|
||||
PushNotificationService.shared.registerForRemoteNotifications()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
class NotificationsViewModel: ObservableObject {
|
||||
@Published var notifications: [AppNotification] = []
|
||||
@Published var isLoading = false
|
||||
@Published var isLoadingMore = false
|
||||
@Published var error: String?
|
||||
@Published var hasMore = true
|
||||
|
||||
private let service = NotificationsAPIService.shared
|
||||
private var currentPage = 1
|
||||
private let perPage = 20
|
||||
private var pollingTask: Task<Void, Never>?
|
||||
|
||||
func load() async {
|
||||
isLoading = true
|
||||
error = nil
|
||||
currentPage = 1
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
let page = try await service.getNotifications(page: 1, perPage: perPage)
|
||||
notifications = page.items
|
||||
hasMore = page.items.count == perPage
|
||||
updateBadge()
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
func loadMore() async {
|
||||
guard !isLoadingMore && hasMore else { return }
|
||||
isLoadingMore = true
|
||||
defer { isLoadingMore = false }
|
||||
do {
|
||||
let nextPage = currentPage + 1
|
||||
let page = try await service.getNotifications(page: nextPage, perPage: perPage)
|
||||
notifications.append(contentsOf: page.items)
|
||||
currentPage = nextPage
|
||||
hasMore = page.items.count == perPage
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
func markAsRead(_ notification: AppNotification) async {
|
||||
guard !notification.isRead else { return }
|
||||
do {
|
||||
try await service.markAsRead(id: notification.id)
|
||||
if let index = notifications.firstIndex(where: { $0.id == notification.id }) {
|
||||
let updated = AppNotification(
|
||||
id: notification.id,
|
||||
topic: notification.topic,
|
||||
subject: notification.subject,
|
||||
body: notification.body,
|
||||
metadata: notification.metadata,
|
||||
status: .read,
|
||||
channel: notification.channel,
|
||||
readAt: Date(),
|
||||
createdAt: notification.createdAt,
|
||||
updatedAt: Date()
|
||||
)
|
||||
notifications[index] = updated
|
||||
}
|
||||
updateBadge()
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
func startPolling() {
|
||||
pollingTask = Task {
|
||||
while !Task.isCancelled {
|
||||
try? await Task.sleep(for: .seconds(30))
|
||||
guard !Task.isCancelled else { break }
|
||||
await load()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func stopPolling() {
|
||||
pollingTask?.cancel()
|
||||
pollingTask = nil
|
||||
}
|
||||
|
||||
private func updateBadge() {
|
||||
let unreadCount = notifications.filter { !$0.isRead }.count
|
||||
Task {
|
||||
await service.updateAppBadge(unreadCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
class SettingsViewModel: ObservableObject {
|
||||
@Published var sessions: [SessionResponse] = []
|
||||
@Published var isLoading = false
|
||||
@Published var error: String?
|
||||
@Published var successMessage: String?
|
||||
|
||||
private let service = NotificationsAPIService.shared
|
||||
|
||||
func loadSessions() async {
|
||||
isLoading = true
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
sessions = try await service.getSessions()
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
func deleteSession(_ session: SessionResponse) async {
|
||||
do {
|
||||
try await service.deleteSession(id: session.id)
|
||||
sessions.removeAll { $0.id == session.id }
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
func changePassword(current: String, new: String) async -> Bool {
|
||||
isLoading = true
|
||||
error = nil
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
_ = try await service.changePassword(current: current, new: new)
|
||||
successMessage = "Пароль успешно изменён"
|
||||
return true
|
||||
} catch {
|
||||
self.error = error.localizedDescription
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user