Skip to content

Commit 5a2b999

Browse files
committed
Present action sheet to confirm logout
1 parent 7353268 commit 5a2b999

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

Shared/Account/AccountLogoutView.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import SwiftUI
22

33
struct AccountLogoutView: View {
44
@EnvironmentObject var model: WriteFreelyModel
5+
@Environment(\.managedObjectContext) var moc
6+
7+
@State private var isPresentingLogoutConfirmation: Bool = false
8+
@State private var editedPostsWarningString: String = ""
59

610
var body: some View {
711
VStack {
@@ -15,10 +19,35 @@ struct AccountLogoutView: View {
1519
Text("Log Out")
1620
})
1721
}
22+
.actionSheet(isPresented: $isPresentingLogoutConfirmation, content: {
23+
ActionSheet(
24+
title: Text("Log Out?"),
25+
message: Text("\(editedPostsWarningString)You won't lose any local posts. Are you sure?"),
26+
buttons: [
27+
.destructive(Text("Log Out"), action: {
28+
model.logout()
29+
}),
30+
.cancel()
31+
]
32+
)
33+
})
1834
}
1935

2036
func logoutHandler() {
21-
model.logout()
37+
let request = WFAPost.createFetchRequest()
38+
request.predicate = NSPredicate(format: "status == %i", 1)
39+
do {
40+
let editedPosts = try LocalStorageManager.persistentContainer.viewContext.fetch(request)
41+
if editedPosts.count == 1 {
42+
editedPostsWarningString = "You'll lose unpublished changes to \(editedPosts.count) edited post. "
43+
}
44+
if editedPosts.count > 1 {
45+
editedPostsWarningString = "You'll lose unpublished changes to \(editedPosts.count) edited posts. "
46+
}
47+
} catch {
48+
print("Error: failed to fetch cached posts")
49+
}
50+
self.isPresentingLogoutConfirmation = true
2251
}
2352
}
2453

0 commit comments

Comments
 (0)