Skip to content

Commit

Permalink
refactor: proper placement of toolbar and other improvements (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba authored Nov 27, 2024
1 parent 3e4d070 commit 1b7c6fb
Show file tree
Hide file tree
Showing 56 changed files with 849 additions and 807 deletions.
12 changes: 6 additions & 6 deletions Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct ContentView: View {
@Environment(\.modelContext) private var context
@Query private var servers: [Server]
@State private var timer: Timer?
@State private var showSignIn = false
@State private var signInIsPresented = false

var body: some View {
MainView()
Expand All @@ -27,12 +27,12 @@ struct ContentView: View {
if token.isExpired {
tokenStore.token = nil
tokenStore.deleteFromKeychain()
showSignIn = true
signInIsPresented = true
} else {
tokenStore.token = token
}
} else {
showSignIn = true
signInIsPresented = true
}

startTokenTimer()
Expand All @@ -52,15 +52,15 @@ struct ContentView: View {
// itself unexpectedly without user interaction or a direct code-triggered dismissal.
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { _ in
DispatchQueue.main.async {
showSignIn = true
signInIsPresented = true
}
}
}
}
.fullScreenCover(isPresented: $showSignIn) {
.fullScreenCover(isPresented: $signInIsPresented) {
SignIn {
startTokenTimer()
showSignIn = false
signInIsPresented = false
}
}
}
Expand Down
42 changes: 0 additions & 42 deletions Sources/Library/Modifiers/ErrorAlert.swift

This file was deleted.

12 changes: 6 additions & 6 deletions Sources/Library/Modifiers/ErrorSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ extension View {
}

#Preview {
@Previewable @State var showError = false
@Previewable @State var showLongError = false
@Previewable @State var errorIsPresented = false
@Previewable @State var longErrorIsPresented = false

VStack(spacing: VOMetrics.spacing) {
Button("Show Error") {
showError = true
errorIsPresented = true
}
Button("Show Long Error") {
showLongError = true
longErrorIsPresented = true
}
}
.voErrorSheet(
isPresented: $showError,
isPresented: $errorIsPresented,
message: "Lorem ipsum dolor sit amet."
)
.voErrorSheet(
isPresented: $showLongError,
isPresented: $longErrorIsPresented,
message:
// swiftlint:disable:next line_length
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
Expand Down
12 changes: 6 additions & 6 deletions Sources/Library/Modifiers/WarningSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ extension View {
}

#Preview {
@Previewable @State var showWarning = false
@Previewable @State var showLongWarning = false
@Previewable @State var warningIsPresented = false
@Previewable @State var longWarningIsPresented = false

VStack(spacing: VOMetrics.spacing) {
Button("Show Warning") {
showWarning = true
warningIsPresented = true
}
Button("Show Long Warning") {
showLongWarning = true
longWarningIsPresented = true
}
}
.voWarningSheet(
isPresented: $showWarning,
isPresented: $warningIsPresented,
message: "Lorem ipsum dolor sit amet."
)
.voWarningSheet(
isPresented: $showLongWarning,
isPresented: $longWarningIsPresented,
message:
// swiftlint:disable:next line_length
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
Expand Down
71 changes: 37 additions & 34 deletions Sources/Screens/Account/AccountEditEmail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,68 +15,71 @@ struct AccountEditEmail: View, LoadStateProvider, FormValidatable, ErrorPresenta
@ObservedObject private var accountStore: AccountStore
@Environment(\.dismiss) private var dismiss
@State private var value = ""
@State private var isSaving = false
@State private var isProcessing = false

init(accountStore: AccountStore) {
self.accountStore = accountStore
}

var body: some View {
if isLoading {
ProgressView()
} else if let error {
VOErrorMessage(error)
} else {
if let identityUser = accountStore.identityUser {
Form {
TextField("Email", text: $value)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.disabled(isSaving)
}
.navigationBarTitleDisplayMode(.inline)
.navigationTitle("Change Email")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
if isSaving {
ProgressView()
} else {
Button("Save") {
performSave()
}
.disabled(!isValid())
}
VStack {
if isLoading {
ProgressView()
} else if let error {
VOErrorMessage(error)
} else {
if let identityUser = accountStore.identityUser {
Form {
TextField("Email", text: $value)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.disabled(isProcessing)
}
.onAppear {
value = identityUser.pendingEmail ?? identityUser.email
}
}
.voErrorSheet(isPresented: $errorIsPresented, message: errorMessage)
.onAppear {
value = identityUser.pendingEmail ?? identityUser.email
}
.onChange(of: accountStore.identityUser) { _, newUser in
if let newUser {
value = newUser.pendingEmail ?? newUser.email
}
}
.navigationBarTitleDisplayMode(.inline)
.navigationTitle("Change Email")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
if isProcessing {
ProgressView()
} else {
Button("Save") {
performSave()
}
.disabled(!isValid())
}
}
}
.onChange(of: accountStore.identityUser) { _, newUser in
if let newUser {
value = newUser.pendingEmail ?? newUser.email
}
}
.voErrorSheet(isPresented: $errorIsPresented, message: errorMessage)
}

private var normalizedValue: String {
value.trimmingCharacters(in: .whitespaces)
}

private func performSave() {
isSaving = true
withErrorHandling {
_ = try await accountStore.updateEmail(normalizedValue)
return true
} before: {
isProcessing = true
} success: {
dismiss()
} failure: { message in
errorMessage = message
errorIsPresented = true
} anyways: {
isSaving = false
isProcessing = false
}
}

Expand Down
67 changes: 35 additions & 32 deletions Sources/Screens/Account/AccountEditFullName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,66 +15,69 @@ struct AccountEditFullName: View, LoadStateProvider, FormValidatable, ErrorPrese
@ObservedObject private var accountStore: AccountStore
@Environment(\.dismiss) private var dismiss
@State private var value = ""
@State private var isSaving = false
@State private var isProcessing = false

init(accountStore: AccountStore) {
self.accountStore = accountStore
}

var body: some View {
if isLoading {
ProgressView()
} else if let error {
VOErrorMessage(error)
} else {
if let identityUser = accountStore.identityUser {
Form {
TextField("Full Name", text: $value)
.disabled(isSaving)
}
.navigationBarTitleDisplayMode(.inline)
.navigationTitle("Change Full Name")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
if isSaving {
ProgressView()
} else {
Button("Save") {
performSave()
}
.disabled(!isValid())
}
VStack {
if isLoading {
ProgressView()
} else if let error {
VOErrorMessage(error)
} else {
if let identityUser = accountStore.identityUser {
Form {
TextField("Full Name", text: $value)
.disabled(isProcessing)
}
.onAppear {
value = identityUser.fullName
}
}
.voErrorSheet(isPresented: $errorIsPresented, message: errorMessage)
.onAppear {
value = identityUser.fullName
}
.onChange(of: accountStore.identityUser) { _, newUser in
if let newUser {
value = newUser.fullName
}
}
.navigationBarTitleDisplayMode(.inline)
.navigationTitle("Change Full Name")
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
if isProcessing {
ProgressView()
} else {
Button("Save") {
performSave()
}
.disabled(!isValid())
}
}
}
.onChange(of: accountStore.identityUser) { _, newUser in
if let newUser {
value = newUser.fullName
}
}
.voErrorSheet(isPresented: $errorIsPresented, message: errorMessage)
}

private var normalizedValue: String {
value.trimmingCharacters(in: .whitespaces)
}

private func performSave() {
isSaving = true
withErrorHandling {
_ = try await accountStore.updateFullName(normalizedValue)
return true
} before: {
isProcessing = true
} success: {
dismiss()
} failure: { message in
errorMessage = message
errorIsPresented = true
} anyways: {
isSaving = false
isProcessing = false
}
}

Expand Down
Loading

0 comments on commit 1b7c6fb

Please sign in to comment.