File tree Expand file tree Collapse file tree 4 files changed +36
-4
lines changed Expand file tree Collapse file tree 4 files changed +36
-4
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ struct AccountLoginView: View {
55
55
. padding ( )
56
56
} else {
57
57
Button ( action: {
58
+ hideKeyboard ( )
58
59
model. login (
59
60
to: URL ( string: server) !,
60
61
as: username, password: password
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
Original file line number Diff line number Diff line change @@ -281,7 +281,7 @@ private extension WriteFreelyModel {
281
281
DispatchQueue . main. async {
282
282
self . account. logout ( )
283
283
LocalStorageManager ( ) . purgeUserCollections ( )
284
- self . posts. purgeAllPosts ( )
284
+ self . posts. purgePublishedPosts ( )
285
285
}
286
286
} catch {
287
287
print ( " Something went wrong purging the token from the Keychain. " )
@@ -296,7 +296,7 @@ private extension WriteFreelyModel {
296
296
DispatchQueue . main. async {
297
297
self . account. logout ( )
298
298
LocalStorageManager ( ) . purgeUserCollections ( )
299
- self . posts. purgeAllPosts ( )
299
+ self . posts. purgePublishedPosts ( )
300
300
}
301
301
} catch {
302
302
print ( " Something went wrong purging the token from the Keychain. " )
Original file line number Diff line number Diff line change @@ -27,13 +27,15 @@ class PostListModel: ObservableObject {
27
27
LocalStorageManager ( ) . saveContext ( )
28
28
}
29
29
30
- func purgeAllPosts ( ) {
30
+ func purgePublishedPosts ( ) {
31
31
userPosts = [ ]
32
32
let fetchRequest : NSFetchRequest < NSFetchRequestResult > = NSFetchRequest ( entityName: " WFAPost " )
33
+ fetchRequest. predicate = NSPredicate ( format: " status != %i " , 0 )
33
34
let deleteRequest = NSBatchDeleteRequest ( fetchRequest: fetchRequest)
34
35
35
36
do {
36
37
try LocalStorageManager . persistentContainer. viewContext. executeAndMergeChanges ( using: deleteRequest)
38
+ loadCachedPosts ( )
37
39
} catch {
38
40
print ( " Error: Failed to purge cached posts. " )
39
41
}
You can’t perform that action at this time.
0 commit comments