File tree Expand file tree Collapse file tree 1 file changed +30
-1
lines changed Expand file tree Collapse file tree 1 file changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,10 @@ import SwiftUI
2
2
3
3
struct AccountLogoutView : View {
4
4
@EnvironmentObject var model : WriteFreelyModel
5
+ @Environment ( \. managedObjectContext) var moc
6
+
7
+ @State private var isPresentingLogoutConfirmation : Bool = false
8
+ @State private var editedPostsWarningString : String = " "
5
9
6
10
var body : some View {
7
11
VStack {
@@ -15,10 +19,35 @@ struct AccountLogoutView: View {
15
19
Text ( " Log Out " )
16
20
} )
17
21
}
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
+ } )
18
34
}
19
35
20
36
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
22
51
}
23
52
}
24
53
You can’t perform that action at this time.
0 commit comments