swiftui-multiplatform/Shared/Account/AccountLogoutView.swift
Angelo Stavrow a51bbd3abc
Alert on error: shared code (#207)
* Initial work on presenting alert on error

* Move Account-related error handling up the hierarchy

* Handle errors on logout

* Fix for temporary debugging

* Clean up WriteFreelyModel’s published vars

* Add error handling to top-level content view

* Set current error on API call failures

* Set current error on API call handlers

* Move User Defaults errors to ErrorConstants file

* Add default values for some error strings

* Handle purging post errors

* Add FIXME to track silent failure on fetching collections

As collections are fetched and added to the `list` property in the CollectionListModel’s initializer, it’s tricky to throw an error here: we call it as a property initializer in CollectionListView, which cannot throw.

Consider refactoring this logic such that we’re using, for example, a @FetchRequest in CollectionListView instead.

* Handle errors in (most) shared code

Two outliers to come back to are:

- the LocalStoreManager, where we can’t set a current error in the WriteFreelyModel in methods that can’t throw
- the CollectionListModel, where the initializer can’t throw because we use it as a property initializer in CollectionListView

* Add error handling to Mac app

* Revert "Add error handling to Mac app"

This reverts commit b1a8b8b29c.
2022-07-27 09:56:32 -04:00

82 lines
2.8 KiB
Swift

import SwiftUI
struct AccountLogoutView: View {
@EnvironmentObject var model: WriteFreelyModel
@EnvironmentObject var errorHandling: ErrorHandling
@State private var isPresentingLogoutConfirmation: Bool = false
@State private var editedPostsWarningString: String = ""
var body: some View {
#if os(iOS)
VStack {
Spacer()
VStack {
Text("Logged in as \(model.account.username)")
Text("on \(model.account.server)")
}
Spacer()
Button(action: logoutHandler, label: {
Text("Log Out")
})
}
.actionSheet(isPresented: $isPresentingLogoutConfirmation, content: {
ActionSheet(
title: Text("Log Out?"),
message: Text("\(editedPostsWarningString)You won't lose any local posts. Are you sure?"),
buttons: [
.destructive(Text("Log Out"), action: {
model.logout()
}),
.cancel()
]
)
})
#else
VStack {
Spacer()
VStack {
Text("Logged in as \(model.account.username)")
Text("on \(model.account.server)")
}
Spacer()
Button(action: logoutHandler, label: {
Text("Log Out")
})
}
.alert(isPresented: $isPresentingLogoutConfirmation) {
Alert(
title: Text("Log Out?"),
message: Text("\(editedPostsWarningString)You won't lose any local posts. Are you sure?"),
primaryButton: .cancel(Text("Cancel"), action: { self.isPresentingLogoutConfirmation = false }),
secondaryButton: .destructive(Text("Log Out"), action: model.logout )
)
}
#endif
}
func logoutHandler() {
let request = WFAPost.createFetchRequest()
request.predicate = NSPredicate(format: "status == %i", 1)
do {
let editedPosts = try LocalStorageManager.standard.container.viewContext.fetch(request)
if editedPosts.count == 1 {
editedPostsWarningString = "You'll lose unpublished changes to \(editedPosts.count) edited post. "
}
if editedPosts.count > 1 {
editedPostsWarningString = "You'll lose unpublished changes to \(editedPosts.count) edited posts. "
}
} catch {
self.errorHandling.handle(error: LocalStoreError.couldNotFetchPosts("cached"))
}
self.isPresentingLogoutConfirmation = true
}
}
struct AccountLogoutView_Previews: PreviewProvider {
static var previews: some View {
AccountLogoutView()
.environmentObject(WriteFreelyModel())
}
}