wake-ios/wake/View/Owner/AccountView.swift
2025-09-07 22:28:15 +08:00

223 lines
8.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
///
struct AccountView: View {
// MARK: -
/// - dismiss
@Environment(\.dismiss) private var dismiss
///
@State private var showDeleteConfirmation = false
// MARK: -
///
private let animation = Animation.spring(
response: 0.6, //
dampingFraction: 0.9, //
blendDuration: 0.8 //
)
// MARK: -
var body: some View {
ZStack {
// Theme background color
Color.themeTextWhiteSecondary
.edgesIgnoringSafeArea(.all)
VStack(spacing: 0) {
//
SimpleNaviHeader(title: "Account & Security") {
withAnimation(animation) {
Router.shared.pop()
}
}
.padding(.top, UIApplication.shared.windows.first?.safeAreaInsets.top)
//
ScrollView {
VStack(spacing: 0) {
//
settingRow(
title: "Log Out",
action: {
//
self.handleLogout()
}
)
.padding()
//
settingRow(
title: "Delete Account",
action: {
withAnimation {
showDeleteConfirmation = true
}
}
)
.padding()
}
.background(Color.white)
.cornerRadius(12)
.padding(.horizontal)
}
.background(Color.themeTextWhiteSecondary)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.edgesIgnoringSafeArea(.top)
}
.edgesIgnoringSafeArea(.all)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.overlay(
//
Group {
if showDeleteConfirmation {
ZStack {
//
Color.black.opacity(0.6)
.edgesIgnoringSafeArea(.all)
.onTapGesture {
withAnimation {
showDeleteConfirmation = false
}
}
//
VStack(spacing: 20) {
Text("Are you sure you want to delete this account?")
.font(.headline)
.font(.system(size: 14, weight: .medium))
.foregroundColor(.themeTextMessageMain)
Text("Once deleted, you cant get it back.")
.multilineTextAlignment(.center)
.font(.subheadline)
.font(.system(size: 12))
.foregroundColor(.themeTextMessage)
HStack(spacing: 20) {
Button(action: {
withAnimation {
showDeleteConfirmation = false
}
}) {
Text("Cancel")
.font(Typography.font(for: .subtitle, family: .quicksandBold))
.foregroundColor(.themeTextMessageMain)
.frame(maxWidth: .infinity)
.padding()
.background(Color.themePrimary)
.cornerRadius(32)
}
Button(action: {
//
NetworkService.shared.delete(path: "/iam/delete-user", parameters: nil) { (result: Result<String, NetworkError>) in
switch result {
case .success(let data):
print("删除账号成功: \(data)")
case .failure(let error):
print("删除账号失败: \(error)")
}
}
//
withAnimation {
showDeleteConfirmation = false
}
}) {
Text("Confirm")
.foregroundColor(.themeTextWhite)
.font(.system(size: 12))
.frame(maxWidth: .infinity)
.padding()
.background(Color.themeTextWhiteSecondary)
.cornerRadius(32)
}
}
}
.padding()
.frame(width: 300)
.background(Color.white)
.cornerRadius(12)
.shadow(radius: 10)
}
.transition(.opacity)
}
}
)
.navigationBarBackButtonHidden(true) //
.navigationBarHidden(true) //
}
// MARK: -
///
private func handleLogout() {
// 1. token
TokenManager.shared.clearTokens()
// 2.
UserDefaults.standard.removeObject(forKey: "lastLoginUser")
// 3.
AuthState.shared.logout()
// 4.
Router.shared.popToRoot()
Router.shared.navigate(to: .login)
}
/// TableView
private func configureTableView() {
// 线
UITableView.appearance().tableFooterView = UIView()
// 线
UITableView.appearance().tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNonzeroMagnitude))
// 线
UITableView.appearance().separatorInset = .zero
//
UITableView.appearance().contentInset = .zero
}
///
/// - Parameters:
/// - icon:
/// - title:
/// - action:
/// - Returns:
private func settingRow(title: String, action: @escaping () -> Void) -> some View {
Button(action: action) {
HStack {
//
Text(title)
.foregroundColor(.primary)
Spacer()
//
Image(systemName: "chevron.right")
.font(.system(size: 14))
.foregroundColor(.gray)
}
.padding(.vertical, 12) //
.background(Color.white)
}
.buttonStyle(PlainButtonStyle())
.listRowBackground(Color.white)
.listRowSeparator(.hidden)
}
}
// MARK: -
#Preview {
NavigationView {
AccountView()
}
}