Files
snail/Snail/Auth/ProfileView.swift
T
2026-10-29 13:12:45 +03:00

104 lines
3.5 KiB
Swift

//
// ProfileView.swift
// Snail
//
import SwiftUI
struct ProfileView: View {
@Environment(AuthStore.self) private var auth
@Environment(\.dismiss) private var dismiss
@State private var isConfirmingSignOut = false
@State private var isSigningOut = false
var body: some View {
NavigationStack {
List {
if let user = auth.currentUser {
header(for: user)
Section("Аккаунт") {
LabeledContent("Электронная почта", value: user.email)
LabeledContent("Почта подтверждена", value: user.emailVerified ? String(localized: "Да") : String(localized: "Нет"))
LabeledContent("Дата регистрации", value: user.createdAt.formatted(date: .long, time: .omitted))
}
}
Section {
Button(role: .destructive) {
isConfirmingSignOut = true
} label: {
HStack {
Text("Выйти")
Spacer()
if isSigningOut {
ProgressView()
}
}
}
.disabled(isSigningOut)
.confirmationDialog("Выйти из аккаунта?", isPresented: $isConfirmingSignOut, titleVisibility: .visible) {
Button("Выйти", role: .destructive, action: signOut)
} message: {
Text("Чтобы войти снова, понадобится код из письма.")
}
}
}
.navigationTitle("Профиль")
.navigationBarTitleDisplayMode(.inline)
.refreshable { await auth.refreshUser() }
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Готово") { dismiss() }
}
}
}
}
private func header(for user: User) -> some View {
Section {
VStack(spacing: 12) {
Text(user.initials)
.font(.title.weight(.semibold))
.foregroundStyle(.white)
.frame(width: 80, height: 80)
.background(Color.accentColor.gradient, in: .circle)
.accessibilityHidden(true)
VStack(spacing: 4) {
Text(user.displayName)
.font(.title2.weight(.semibold))
if user.displayName != user.email {
Text(user.email)
.foregroundStyle(.secondary)
}
}
.multilineTextAlignment(.center)
}
.frame(maxWidth: .infinity)
.listRowBackground(Color.clear)
.accessibilityElement(children: .combine)
}
}
private func signOut() {
isSigningOut = true
Task {
await auth.signOut()
isSigningOut = false
}
}
}
#Preview {
ProfileView()
.environment(AuthStore(state: .signedIn(User(
id: "1",
name: "Вадим",
email: "vadim@example.com",
emailVerified: true,
image: nil,
createdAt: .now
))))
}