refactor: add color assets and update UI components
This commit is contained in:
@@ -1,46 +1,98 @@
|
||||
import SwiftUI
|
||||
|
||||
struct ChangePasswordView: View {
|
||||
@StateObject private var viewModel = SettingsViewModel()
|
||||
@Environment(\.dismiss) var dismiss
|
||||
var viewModel: SettingsViewModel
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
@State private var currentPassword = ""
|
||||
@State private var newPassword = ""
|
||||
@State private var confirmPassword = ""
|
||||
|
||||
private var isFormInvalid: Bool {
|
||||
!isFormValid || viewModel.isLoading
|
||||
}
|
||||
|
||||
private var isFormValid: Bool {
|
||||
!currentPassword.isEmpty && newPassword.count >= 8 && newPassword == confirmPassword
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
Form {
|
||||
Section {
|
||||
SecureField("current_password", text: $currentPassword)
|
||||
VStack(spacing: 12) {
|
||||
AppSecureField(
|
||||
title: "current_password",
|
||||
icon: "lock.fill",
|
||||
text: $currentPassword
|
||||
)
|
||||
.textContentType(.password)
|
||||
SecureField("new_password", text: $newPassword)
|
||||
|
||||
AppSecureField(
|
||||
title: "new_password",
|
||||
icon: "key.fill",
|
||||
text: $newPassword
|
||||
)
|
||||
.textContentType(.newPassword)
|
||||
SecureField("confirm_new_password", text: $confirmPassword)
|
||||
|
||||
AppSecureField(
|
||||
title: "confirm_new_password",
|
||||
icon: "lock.rotation",
|
||||
text: $confirmPassword
|
||||
)
|
||||
.textContentType(.newPassword)
|
||||
}
|
||||
.padding(.vertical, 4)
|
||||
}
|
||||
|
||||
if newPassword.count > 0 && newPassword.count < 8 {
|
||||
Section {
|
||||
Text("password_min_length")
|
||||
.foregroundStyle(.brand)
|
||||
.font(.footnote)
|
||||
}
|
||||
}
|
||||
|
||||
if confirmPassword.count > 0 && newPassword != confirmPassword {
|
||||
Section {
|
||||
Text("passwords_mismatch")
|
||||
.foregroundStyle(.brand)
|
||||
.font(.footnote)
|
||||
}
|
||||
}
|
||||
|
||||
if let error = viewModel.error {
|
||||
Section {
|
||||
Text(error).foregroundStyle(.red)
|
||||
Text(error)
|
||||
.foregroundStyle(.brand)
|
||||
.font(.footnote)
|
||||
}
|
||||
}
|
||||
|
||||
if let success = viewModel.successMessage {
|
||||
Section {
|
||||
Text(success).foregroundStyle(.green)
|
||||
Text(success)
|
||||
.foregroundStyle(.success)
|
||||
.font(.footnote)
|
||||
}
|
||||
}
|
||||
|
||||
Section {
|
||||
Button("save_button") {
|
||||
Button {
|
||||
Task {
|
||||
let success = await viewModel.changePassword(current: currentPassword, new: newPassword)
|
||||
if success { dismiss() }
|
||||
}
|
||||
} label: {
|
||||
if viewModel.isLoading {
|
||||
ProgressView()
|
||||
.frame(maxWidth: .infinity)
|
||||
} else {
|
||||
Text("save_button")
|
||||
.frame(maxWidth: .infinity)
|
||||
}
|
||||
}
|
||||
.disabled(!isFormValid || viewModel.isLoading)
|
||||
.frame(maxWidth: .infinity)
|
||||
.disabled(isFormInvalid)
|
||||
}
|
||||
}
|
||||
.navigationTitle("change_password_title")
|
||||
@@ -52,8 +104,8 @@ struct ChangePasswordView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var isFormValid: Bool {
|
||||
!currentPassword.isEmpty && newPassword.count >= 8 && newPassword == confirmPassword
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ChangePasswordView(viewModel: SettingsViewModel())
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import SwiftUI
|
||||
|
||||
struct SessionsView: View {
|
||||
@EnvironmentObject var viewModel: SettingsViewModel
|
||||
@Environment(\.dismiss) var dismiss
|
||||
var viewModel: SettingsViewModel
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
@@ -18,8 +18,8 @@ struct SessionsView: View {
|
||||
.font(.caption)
|
||||
.padding(.horizontal, 6)
|
||||
.padding(.vertical, 2)
|
||||
.background(Color.green.opacity(0.2))
|
||||
.foregroundStyle(.green)
|
||||
.background(Color.success.opacity(0.2))
|
||||
.foregroundStyle(.success)
|
||||
.cornerRadius(4)
|
||||
}
|
||||
}
|
||||
@@ -51,3 +51,7 @@ struct SessionsView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
SessionsView(viewModel: SettingsViewModel())
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import SwiftUI
|
||||
|
||||
struct SettingsView: View {
|
||||
@EnvironmentObject var authViewModel: AuthViewModel
|
||||
@StateObject private var viewModel = SettingsViewModel()
|
||||
@Environment(\.dismiss) var dismiss
|
||||
@Environment(AuthViewModel.self) private var authViewModel
|
||||
@State private var viewModel = SettingsViewModel()
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@State private var showChangePassword = false
|
||||
@State private var showSessions = false
|
||||
@State private var showLogoutAllConfirm = false
|
||||
@State private var logoutAllError: String?
|
||||
@State private var showLogoutAllError = false
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
@@ -19,9 +20,12 @@ struct SettingsView: View {
|
||||
}
|
||||
|
||||
Section {
|
||||
Button("change_password") {
|
||||
Button {
|
||||
showChangePassword = true
|
||||
} label: {
|
||||
Text("change_password")
|
||||
}
|
||||
.tint(.primary)
|
||||
|
||||
Button {
|
||||
if let url = URL(string: UIApplication.openNotificationSettingsURLString) {
|
||||
@@ -29,8 +33,8 @@ struct SettingsView: View {
|
||||
}
|
||||
} label: {
|
||||
Label("push_notifications", systemImage: "bell.badge")
|
||||
.foregroundStyle(.primary)
|
||||
}
|
||||
.tint(.primary)
|
||||
}
|
||||
|
||||
Section {
|
||||
@@ -48,7 +52,7 @@ struct SettingsView: View {
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
.foregroundStyle(.primary)
|
||||
.tint(.primary)
|
||||
}
|
||||
|
||||
Section {
|
||||
@@ -71,6 +75,7 @@ struct SettingsView: View {
|
||||
await authViewModel.logout()
|
||||
} catch {
|
||||
logoutAllError = error.localizedDescription
|
||||
showLogoutAllError = true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,19 +91,12 @@ struct SettingsView: View {
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $showChangePassword) {
|
||||
ChangePasswordView()
|
||||
ChangePasswordView(viewModel: viewModel)
|
||||
}
|
||||
.sheet(isPresented: $showSessions) {
|
||||
SessionsView()
|
||||
.environmentObject(viewModel)
|
||||
SessionsView(viewModel: viewModel)
|
||||
}
|
||||
.alert(
|
||||
"error_title",
|
||||
isPresented: Binding(
|
||||
get: { logoutAllError != nil },
|
||||
set: { if !$0 { logoutAllError = nil } }
|
||||
)
|
||||
) {
|
||||
.alert("error_title", isPresented: $showLogoutAllError) {
|
||||
Button("OK") { logoutAllError = nil }
|
||||
} message: {
|
||||
Text(logoutAllError ?? "")
|
||||
@@ -109,3 +107,8 @@ struct SettingsView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
SettingsView()
|
||||
.environment(AuthViewModel())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user