113 lines
4.3 KiB
Swift
113 lines
4.3 KiB
Swift
import SwiftUI
|
|
|
|
struct LoginView: View {
|
|
@EnvironmentObject var authManager: AuthManager
|
|
@State private var email = "daniilklimov25@gmail.com"
|
|
@State private var password = ""
|
|
@State private var isLoading = false
|
|
@State private var errorMessage = ""
|
|
@State private var showPassword = false
|
|
|
|
var body: some View {
|
|
ZStack {
|
|
LinearGradient(colors: [Color(hex: "1a1a2e"), Color(hex: "16213e")],
|
|
startPoint: .top, endPoint: .bottom)
|
|
.ignoresSafeArea()
|
|
|
|
VStack(spacing: 32) {
|
|
VStack(spacing: 8) {
|
|
Text("🫀").font(.system(size: 60))
|
|
Text("Pulse Health").font(.largeTitle.bold()).foregroundColor(.white)
|
|
Text("Персональный дашборд здоровья")
|
|
.font(.subheadline).foregroundColor(.white.opacity(0.6))
|
|
}.padding(.top, 60)
|
|
|
|
VStack(spacing: 16) {
|
|
TextField("Email", text: $email)
|
|
.keyboardType(.emailAddress)
|
|
.autocapitalization(.none)
|
|
.autocorrectionDisabled()
|
|
.padding()
|
|
.background(Color.white.opacity(0.1))
|
|
.cornerRadius(12)
|
|
.foregroundColor(.white)
|
|
|
|
// Password field with show/hide toggle
|
|
HStack {
|
|
Group {
|
|
if showPassword {
|
|
TextField("Пароль", text: $password)
|
|
.autocapitalization(.none)
|
|
.autocorrectionDisabled()
|
|
} else {
|
|
SecureField("Пароль", text: $password)
|
|
}
|
|
}
|
|
.foregroundColor(.white)
|
|
|
|
Button(action: { showPassword.toggle() }) {
|
|
Image(systemName: showPassword ? "eye.slash.fill" : "eye.fill")
|
|
.foregroundColor(.white.opacity(0.6))
|
|
}
|
|
}
|
|
.padding()
|
|
.background(Color.white.opacity(0.1))
|
|
.cornerRadius(12)
|
|
|
|
if !errorMessage.isEmpty {
|
|
Text(errorMessage)
|
|
.foregroundColor(.red)
|
|
.font(.caption)
|
|
.multilineTextAlignment(.center)
|
|
.padding(.horizontal)
|
|
}
|
|
|
|
Button(action: login) {
|
|
if isLoading {
|
|
ProgressView().tint(.black)
|
|
} else {
|
|
Text("Войти").font(.headline).foregroundColor(.black)
|
|
}
|
|
}
|
|
.frame(maxWidth: .infinity)
|
|
.padding()
|
|
.background(Color(hex: "00d4aa"))
|
|
.cornerRadius(12)
|
|
.disabled(isLoading || email.isEmpty || password.isEmpty)
|
|
}
|
|
.padding(.horizontal, 24)
|
|
|
|
Spacer()
|
|
}
|
|
}
|
|
}
|
|
|
|
func login() {
|
|
isLoading = true
|
|
errorMessage = ""
|
|
Task {
|
|
do {
|
|
let response = try await APIService.shared.login(email: email.trimmingCharacters(in: .whitespaces), password: password)
|
|
let profile = try? await APIService.shared.getProfile(token: response.token)
|
|
await MainActor.run {
|
|
authManager.login(
|
|
token: response.token,
|
|
name: response.user.name,
|
|
apiKey: profile?.apiKey ?? ""
|
|
)
|
|
}
|
|
} catch let error as APIError {
|
|
await MainActor.run {
|
|
errorMessage = error.errorDescription ?? "Ошибка"
|
|
isLoading = false
|
|
}
|
|
} catch {
|
|
await MainActor.run {
|
|
errorMessage = "Ошибка: \(error.localizedDescription)"
|
|
isLoading = false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|