Refactor viewmodel code from PostCollection into CollectionListModel

This commit is contained in:
Angelo Stavrow 2020-08-17 15:24:44 -04:00
parent ae8fd973c1
commit 84e854b4ab
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
5 changed files with 39 additions and 20 deletions

View File

@ -20,7 +20,7 @@ class Post: Identifiable, ObservableObject {
body: String = "Write your post here...",
createdDate: Date = Date(),
status: PostStatus = .draft,
collection: PostCollection = defaultDraftCollection
collection: PostCollection = draftsCollection
) {
self.title = title
self.body = body
@ -30,6 +30,7 @@ class Post: Identifiable, ObservableObject {
}
}
#if DEBUG
let testPost = Post(
title: "Test Post Title",
body: """
@ -52,14 +53,14 @@ let testPostData = [
body: "Look at me, creating a first post! That's cool.",
createdDate: Date(timeIntervalSince1970: 1595429452),
status: .published,
collection: userCollections[0]
collection: userCollection1
),
Post(
title: "Post 2: The Quickening",
body: "See, here's the rule about Highlander jokes: _there can be only one_.",
createdDate: Date(timeIntervalSince1970: 1595514125),
status: .edited,
collection: userCollections[0]
collection: userCollection1
),
Post(
title: "The Post Revolutions",
@ -71,7 +72,7 @@ let testPostData = [
body: "How many movies does this person watch? How many movie-title jokes will they make?",
createdDate: Date(timeIntervalSince1970: 1596219877),
status: .published,
collection: userCollections[1]
collection: userCollection2
),
Post(
title: "Fast (Post) Five",
@ -82,6 +83,7 @@ let testPostData = [
body: "And there you have it, a Resident Evil movie reference.",
createdDate: Date(timeIntervalSince1970: 1596043684),
status: .edited,
collection: userCollections[2]
collection: userCollection3
)
]
#endif

View File

@ -6,17 +6,10 @@ struct PostCollection: Identifiable, Hashable {
}
let allPostsCollection = PostCollection(title: "All Posts")
let defaultDraftCollection = PostCollection(title: "Drafts")
let userCollections = [
PostCollection(title: "Collection 1"),
PostCollection(title: "Collection 2"),
PostCollection(title: "Collection 3")
]
let draftsCollection = PostCollection(title: "Drafts")
let postCollections = [
allPostsCollection,
defaultDraftCollection,
userCollections[0],
userCollections[1],
userCollections[2]
]
#if DEBUG
let userCollection1 = PostCollection(title: "Collection 1")
let userCollection2 = PostCollection(title: "Collection 2")
let userCollection3 = PostCollection(title: "Collection 3")
#endif

View File

@ -0,0 +1,18 @@
import SwiftUI
struct CollectionListModel {
private(set) var userCollections: [PostCollection] = []
private(set) var collectionsList: [PostCollection]
init() {
collectionsList = [ allPostsCollection, draftsCollection ]
#if DEBUG
userCollections = [ userCollection1, userCollection2, userCollection3 ]
#endif
for userCollection in userCollections {
collectionsList.append(userCollection)
}
}
}

View File

@ -1,11 +1,11 @@
import SwiftUI
struct CollectionListView: View {
private let collections = postCollections
private var collections = CollectionListModel()
var body: some View {
List {
ForEach(collections) { collection in
ForEach(collections.collectionsList) { collection in
NavigationLink(
destination: PostListView(selectedCollection: collection)
) {

View File

@ -32,6 +32,8 @@
1756AE7A24CB65DF00FD7257 /* PostListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756AE7924CB65DF00FD7257 /* PostListView.swift */; };
1756AE7B24CB65DF00FD7257 /* PostListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756AE7924CB65DF00FD7257 /* PostListView.swift */; };
1756AE8124CB844500FD7257 /* View+Keyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756AE8024CB844500FD7257 /* View+Keyboard.swift */; };
1762DCB324EB086C0019C4EB /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1762DCB224EB086C0019C4EB /* CollectionListModel.swift */; };
1762DCB424EB086C0019C4EB /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1762DCB224EB086C0019C4EB /* CollectionListModel.swift */; };
1765F62A24E18EA200C9EBF0 /* SidebarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1765F62924E18EA200C9EBF0 /* SidebarView.swift */; };
1765F62B24E18EA200C9EBF0 /* SidebarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1765F62924E18EA200C9EBF0 /* SidebarView.swift */; };
17A5388824DDA31F00DEFF9A /* MacAccountView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17A5388724DDA31F00DEFF9A /* MacAccountView.swift */; };
@ -85,6 +87,7 @@
1756AE7624CB2EDD00FD7257 /* PostEditorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostEditorView.swift; sourceTree = "<group>"; };
1756AE7924CB65DF00FD7257 /* PostListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostListView.swift; sourceTree = "<group>"; };
1756AE8024CB844500FD7257 /* View+Keyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+Keyboard.swift"; sourceTree = "<group>"; };
1762DCB224EB086C0019C4EB /* CollectionListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionListModel.swift; sourceTree = "<group>"; };
1765F62924E18EA200C9EBF0 /* SidebarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarView.swift; sourceTree = "<group>"; };
17A5388724DDA31F00DEFF9A /* MacAccountView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacAccountView.swift; sourceTree = "<group>"; };
17A5388B24DDC83F00DEFF9A /* AccountModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountModel.swift; sourceTree = "<group>"; };
@ -323,6 +326,7 @@
17DF32D224C8B78D00BCE2E3 /* PostCollection */ = {
isa = PBXGroup;
children = (
1762DCB224EB086C0019C4EB /* CollectionListModel.swift */,
171BFDF924D4AF8300888236 /* CollectionListView.swift */,
);
path = PostCollection;
@ -543,6 +547,7 @@
17DF32D524C8CA3400BCE2E3 /* PostStatusBadgeView.swift in Sources */,
17D435E824E3128F0036B539 /* PreferencesModel.swift in Sources */,
1765F62A24E18EA200C9EBF0 /* SidebarView.swift in Sources */,
1762DCB324EB086C0019C4EB /* CollectionListModel.swift in Sources */,
1756AE7A24CB65DF00FD7257 /* PostListView.swift in Sources */,
171BFDF724D49FD400888236 /* PostCollection.swift in Sources */,
17DF32AA24C87D3500BCE2E3 /* WriteFreely_MultiPlatformApp.swift in Sources */,
@ -573,6 +578,7 @@
171BFDFB24D4AF8300888236 /* CollectionListView.swift in Sources */,
17DF32AB24C87D3500BCE2E3 /* WriteFreely_MultiPlatformApp.swift in Sources */,
17A5388C24DDC83F00DEFF9A /* AccountModel.swift in Sources */,
1762DCB424EB086C0019C4EB /* CollectionListModel.swift in Sources */,
17A5389324DDED0000DEFF9A /* PreferencesView.swift in Sources */,
1756AE6F24CB255B00FD7257 /* PostStore.swift in Sources */,
1756AE6C24CB1E4B00FD7257 /* Post.swift in Sources */,