Merge pull request #24 from writeas/fetch-posts-on-login

Fetch posts on login, purge on logout
This commit is contained in:
Angelo Stavrow 2020-08-27 11:54:15 -04:00 committed by GitHub
commit 66ea408d97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 3 deletions

View File

@ -1,4 +1,5 @@
import Foundation
import WriteFreely
enum PostStatus {
case draft
@ -12,6 +13,7 @@ class Post: Identifiable, ObservableObject {
@Published var createdDate: Date
@Published var status: PostStatus
@Published var collection: PostCollection
@Published var wfPost: WFPost?
let id = UUID()
@ -28,6 +30,16 @@ class Post: Identifiable, ObservableObject {
self.status = status
self.collection = collection
}
convenience init(wfPost: WFPost, in collection: PostCollection = draftsCollection) {
self.init(
title: wfPost.title ?? "",
body: wfPost.body,
createdDate: wfPost.createdDate ?? Date(),
status: .published,
collection: collection
)
}
}
#if DEBUG

View File

@ -10,4 +10,8 @@ struct PostStore {
mutating func add(_ post: Post) {
posts.append(post)
}
mutating func purge() {
posts = []
}
}

View File

@ -22,7 +22,7 @@ class WriteFreelyModel: ObservableObject {
}
#if DEBUG
for post in testPostData { store.add(post) }
// for post in testPostData { store.add(post) }
#endif
DispatchQueue.main.async {
@ -58,6 +58,11 @@ extension WriteFreelyModel {
guard let loggedInClient = client else { return }
loggedInClient.getUserCollections(completion: fetchUserCollectionsHandler)
}
func fetchUserPosts() {
guard let loggedInClient = client else { return }
loggedInClient.getPosts(completion: fetchUserPostsHandler)
}
}
private extension WriteFreelyModel {
@ -68,6 +73,7 @@ private extension WriteFreelyModel {
do {
let user = try result.get()
fetchUserCollections()
fetchUserPosts()
saveTokenToKeychain(user.token, username: user.username, server: account.server)
DispatchQueue.main.async {
self.account.login(user)
@ -98,6 +104,7 @@ private extension WriteFreelyModel {
DispatchQueue.main.async {
self.account.logout()
self.collections.clearUserCollection()
self.store.purge()
}
} catch {
print("Something went wrong purging the token from the Keychain.")
@ -112,6 +119,7 @@ private extension WriteFreelyModel {
DispatchQueue.main.async {
self.account.logout()
self.collections.clearUserCollection()
self.store.purge()
}
} catch {
print("Something went wrong purging the token from the Keychain.")
@ -147,6 +155,28 @@ private extension WriteFreelyModel {
print(error)
}
}
func fetchUserPostsHandler(result: Result<[WFPost], Error>) {
do {
let fetchedPosts = try result.get()
for fetchedPost in fetchedPosts {
var post: Post
if let matchingAlias = fetchedPost.collectionAlias {
let postCollection = (
collections.userCollections.filter { $0.wfCollection?.alias == matchingAlias }
).first
post = Post(wfPost: fetchedPost, in: postCollection ?? draftsCollection)
} else {
post = Post(wfPost: fetchedPost)
}
DispatchQueue.main.async {
self.store.add(post)
}
}
} catch {
print(error)
}
}
}
private extension WriteFreelyModel {

View File

@ -20,6 +20,9 @@ struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let model = WriteFreelyModel()
model.collections = CollectionListModel(with: [userCollection1, userCollection2, userCollection3])
for post in testPostData {
model.store.add(post)
}
return ContentView()
.environmentObject(model)
}

View File

@ -103,9 +103,13 @@ struct PostListView: View {
struct PostList_Previews: PreviewProvider {
static var previews: some View {
Group {
let model = WriteFreelyModel()
for post in testPostData {
model.store.add(post)
}
return Group {
PostListView(selectedCollection: allPostsCollection)
.environmentObject(WriteFreelyModel())
.environmentObject(model)
}
}
}