Skip to content

Commit 1170caa

Browse files
Merge pull request #110 from writeas/keep-local-posts-on-logout
Keep local posts on logging out
2 parents e7dff39 + 5a2b999 commit 1170caa

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

Shared/Account/AccountLoginView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct AccountLoginView: View {
5555
.padding()
5656
} else {
5757
Button(action: {
58+
hideKeyboard()
5859
model.login(
5960
to: URL(string: server)!,
6061
as: username, password: password

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

Shared/Models/WriteFreelyModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ private extension WriteFreelyModel {
281281
DispatchQueue.main.async {
282282
self.account.logout()
283283
LocalStorageManager().purgeUserCollections()
284-
self.posts.purgeAllPosts()
284+
self.posts.purgePublishedPosts()
285285
}
286286
} catch {
287287
print("Something went wrong purging the token from the Keychain.")
@@ -296,7 +296,7 @@ private extension WriteFreelyModel {
296296
DispatchQueue.main.async {
297297
self.account.logout()
298298
LocalStorageManager().purgeUserCollections()
299-
self.posts.purgeAllPosts()
299+
self.posts.purgePublishedPosts()
300300
}
301301
} catch {
302302
print("Something went wrong purging the token from the Keychain.")

Shared/PostList/PostListModel.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ class PostListModel: ObservableObject {
2727
LocalStorageManager().saveContext()
2828
}
2929

30-
func purgeAllPosts() {
30+
func purgePublishedPosts() {
3131
userPosts = []
3232
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "WFAPost")
33+
fetchRequest.predicate = NSPredicate(format: "status != %i", 0)
3334
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
3435

3536
do {
3637
try LocalStorageManager.persistentContainer.viewContext.executeAndMergeChanges(using: deleteRequest)
38+
loadCachedPosts()
3739
} catch {
3840
print("Error: Failed to purge cached posts.")
3941
}

0 commit comments

Comments
 (0)