mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Add WriteFreelyModel to publish PostStore and Account/Preference models
This commit is contained in:
parent
143699244a
commit
d402ccd472
@ -1,15 +1,13 @@
|
||||
import Foundation
|
||||
|
||||
class PostStore: ObservableObject {
|
||||
@Published var posts: [Post]
|
||||
struct PostStore {
|
||||
var posts: [Post]
|
||||
|
||||
init(posts: [Post] = []) {
|
||||
self.posts = posts
|
||||
}
|
||||
|
||||
func add(_ post: Post) {
|
||||
mutating func add(_ post: Post) {
|
||||
posts.append(post)
|
||||
}
|
||||
}
|
||||
|
||||
var testPostStore = PostStore(posts: testPostData)
|
||||
|
22
Shared/Models/WriteFreelyModel.swift
Normal file
22
Shared/Models/WriteFreelyModel.swift
Normal file
@ -0,0 +1,22 @@
|
||||
import Foundation
|
||||
|
||||
// MARK: - WriteFreelyModel
|
||||
|
||||
class WriteFreelyModel: ObservableObject {
|
||||
@Published var account = AccountModel()
|
||||
@Published var preferences = PreferencesModel()
|
||||
@Published var store = PostStore()
|
||||
@Published var post: Post?
|
||||
|
||||
init() {
|
||||
#if DEBUG
|
||||
for post in testPostData { store.add(post) }
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - WriteFreelyModel API
|
||||
|
||||
extension WriteFreelyModel {
|
||||
// API goes here
|
||||
}
|
@ -1,9 +1,7 @@
|
||||
import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
@ObservedObject var postStore: PostStore
|
||||
@ObservedObject var preferences: PreferencesModel
|
||||
@ObservedObject var account: AccountModel
|
||||
@EnvironmentObject var model: WriteFreelyModel
|
||||
|
||||
var body: some View {
|
||||
NavigationView {
|
||||
@ -14,14 +12,13 @@ struct ContentView: View {
|
||||
Text("Select a post, or create a new draft.")
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
.environmentObject(postStore)
|
||||
.environmentObject(preferences)
|
||||
.environmentObject(account)
|
||||
.environmentObject(model)
|
||||
}
|
||||
}
|
||||
|
||||
struct ContentView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
ContentView(postStore: testPostStore, preferences: PreferencesModel(), account: AccountModel())
|
||||
ContentView()
|
||||
.environmentObject(WriteFreelyModel())
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import SwiftUI
|
||||
|
||||
struct PostEditorView: View {
|
||||
@EnvironmentObject var postStore: PostStore
|
||||
@EnvironmentObject var model: WriteFreelyModel
|
||||
|
||||
@ObservedObject var post: Post
|
||||
|
||||
@ -47,12 +47,12 @@ struct PostEditorView: View {
|
||||
}
|
||||
|
||||
private func checkIfNewPost() {
|
||||
self.isNewPost = !postStore.posts.contains(where: { $0.id == post.id })
|
||||
self.isNewPost = !model.store.posts.contains(where: { $0.id == post.id })
|
||||
}
|
||||
|
||||
private func addNewPostToStore() {
|
||||
withAnimation {
|
||||
postStore.add(post)
|
||||
model.store.add(post)
|
||||
self.isNewPost = false
|
||||
}
|
||||
}
|
||||
@ -61,13 +61,13 @@ struct PostEditorView: View {
|
||||
struct PostEditorView_NewDraftPreviews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
PostEditorView(post: Post())
|
||||
.environmentObject(testPostStore)
|
||||
.environmentObject(WriteFreelyModel())
|
||||
}
|
||||
}
|
||||
|
||||
struct PostEditorView_ExistingPostPreviews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
PostEditorView(post: testPostData[0])
|
||||
.environmentObject(testPostStore)
|
||||
.environmentObject(WriteFreelyModel())
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
import SwiftUI
|
||||
|
||||
struct PostCellView: View {
|
||||
@EnvironmentObject var postStore: PostStore
|
||||
|
||||
@ObservedObject var post: Post
|
||||
|
||||
var body: some View {
|
||||
|
@ -1,8 +1,7 @@
|
||||
import SwiftUI
|
||||
|
||||
struct PostListView: View {
|
||||
@EnvironmentObject var postStore: PostStore
|
||||
|
||||
@EnvironmentObject var model: WriteFreelyModel
|
||||
@State var selectedCollection: PostCollection
|
||||
|
||||
#if os(iOS)
|
||||
@ -23,12 +22,13 @@ struct PostListView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.environmentObject(model)
|
||||
.navigationTitle(selectedCollection.title)
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button(action: {
|
||||
let post = Post()
|
||||
postStore.add(post)
|
||||
model.store.add(post)
|
||||
}, label: {
|
||||
Image(systemName: "square.and.pencil")
|
||||
})
|
||||
@ -74,7 +74,7 @@ struct PostListView: View {
|
||||
.toolbar {
|
||||
Button(action: {
|
||||
let post = Post()
|
||||
postStore.add(post)
|
||||
model.store.add(post)
|
||||
}, label: {
|
||||
Image(systemName: "square.and.pencil")
|
||||
})
|
||||
@ -92,9 +92,9 @@ struct PostListView: View {
|
||||
|
||||
private func showPosts(for collection: PostCollection) -> [Post] {
|
||||
if collection == allPostsCollection {
|
||||
return postStore.posts
|
||||
return model.store.posts
|
||||
} else {
|
||||
return postStore.posts.filter {
|
||||
return model.store.posts.filter {
|
||||
$0.collection.title == collection.title
|
||||
}
|
||||
}
|
||||
@ -105,7 +105,7 @@ struct PostList_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
Group {
|
||||
PostListView(selectedCollection: allPostsCollection)
|
||||
.environmentObject(testPostStore)
|
||||
.environmentObject(WriteFreelyModel())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,35 +2,29 @@ import SwiftUI
|
||||
|
||||
@main
|
||||
struct WriteFreely_MultiPlatformApp: App {
|
||||
@StateObject private var preferences = PreferencesModel()
|
||||
@StateObject private var account = AccountModel()
|
||||
@StateObject private var model = WriteFreelyModel()
|
||||
|
||||
#if os(macOS)
|
||||
@State private var selectedTab = 0
|
||||
#endif
|
||||
|
||||
#if DEBUG
|
||||
@StateObject private var store = testPostStore
|
||||
#else
|
||||
@StateObject private var store = PostStore()
|
||||
#endif
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView(postStore: store, preferences: preferences, account: account)
|
||||
ContentView()
|
||||
.environmentObject(model)
|
||||
// .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info.
|
||||
}
|
||||
|
||||
#if os(macOS)
|
||||
Settings {
|
||||
TabView(selection: $selectedTab) {
|
||||
MacAccountView(account: account)
|
||||
MacAccountView(account: model.account)
|
||||
.tabItem {
|
||||
Image(systemName: "person.crop.circle")
|
||||
Text("Account")
|
||||
}
|
||||
.tag(0)
|
||||
MacPreferencesView(preferences: preferences)
|
||||
MacPreferencesView(preferences: model.preferences)
|
||||
.tabItem {
|
||||
Image(systemName: "gear")
|
||||
Text("Preferences")
|
||||
|
@ -20,6 +20,8 @@
|
||||
171BFDF824D49FD400888236 /* PostCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 171BFDF624D49FD400888236 /* PostCollection.swift */; };
|
||||
171BFDFA24D4AF8300888236 /* CollectionListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 171BFDF924D4AF8300888236 /* CollectionListView.swift */; };
|
||||
171BFDFB24D4AF8300888236 /* CollectionListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 171BFDF924D4AF8300888236 /* CollectionListView.swift */; };
|
||||
174D313224EC2831006CA9EE /* WriteFreelyModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 174D313124EC2831006CA9EE /* WriteFreelyModel.swift */; };
|
||||
174D313324EC2831006CA9EE /* WriteFreelyModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 174D313124EC2831006CA9EE /* WriteFreelyModel.swift */; };
|
||||
1753F6AC24E431CC00309365 /* MacPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1753F6AB24E431CC00309365 /* MacPreferencesView.swift */; };
|
||||
1756AE6B24CB1E4B00FD7257 /* Post.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756AE6A24CB1E4B00FD7257 /* Post.swift */; };
|
||||
1756AE6C24CB1E4B00FD7257 /* Post.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756AE6A24CB1E4B00FD7257 /* Post.swift */; };
|
||||
@ -80,6 +82,7 @@
|
||||
17120DB124E1E19C002B9F6C /* SettingsHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsHeaderView.swift; sourceTree = "<group>"; };
|
||||
171BFDF624D49FD400888236 /* PostCollection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostCollection.swift; sourceTree = "<group>"; };
|
||||
171BFDF924D4AF8300888236 /* CollectionListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionListView.swift; sourceTree = "<group>"; };
|
||||
174D313124EC2831006CA9EE /* WriteFreelyModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WriteFreelyModel.swift; sourceTree = "<group>"; };
|
||||
1753F6AB24E431CC00309365 /* MacPreferencesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacPreferencesView.swift; sourceTree = "<group>"; };
|
||||
1756AE6A24CB1E4B00FD7257 /* Post.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Post.swift; sourceTree = "<group>"; };
|
||||
1756AE6D24CB255B00FD7257 /* PostStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostStore.swift; sourceTree = "<group>"; };
|
||||
@ -181,6 +184,7 @@
|
||||
1756AE6A24CB1E4B00FD7257 /* Post.swift */,
|
||||
171BFDF624D49FD400888236 /* PostCollection.swift */,
|
||||
1756AE6D24CB255B00FD7257 /* PostStore.swift */,
|
||||
174D313124EC2831006CA9EE /* WriteFreelyModel.swift */,
|
||||
);
|
||||
path = Models;
|
||||
sourceTree = "<group>";
|
||||
@ -555,6 +559,7 @@
|
||||
17120DA224E1985C002B9F6C /* AccountModel.swift in Sources */,
|
||||
17120DA324E19A42002B9F6C /* PreferencesView.swift in Sources */,
|
||||
1756AE6E24CB255B00FD7257 /* PostStore.swift in Sources */,
|
||||
174D313224EC2831006CA9EE /* WriteFreelyModel.swift in Sources */,
|
||||
1756AE6B24CB1E4B00FD7257 /* Post.swift in Sources */,
|
||||
17120DA124E19839002B9F6C /* AccountView.swift in Sources */,
|
||||
1756AE7424CB26FA00FD7257 /* PostCellView.swift in Sources */,
|
||||
@ -568,6 +573,7 @@
|
||||
171BFDF824D49FD400888236 /* PostCollection.swift in Sources */,
|
||||
17DF32AD24C87D3500BCE2E3 /* ContentView.swift in Sources */,
|
||||
1765F62B24E18EA200C9EBF0 /* SidebarView.swift in Sources */,
|
||||
174D313324EC2831006CA9EE /* WriteFreelyModel.swift in Sources */,
|
||||
1756AE7824CB2EDD00FD7257 /* PostEditorView.swift in Sources */,
|
||||
17D435E924E3128F0036B539 /* PreferencesModel.swift in Sources */,
|
||||
17120DAA24E1B2F5002B9F6C /* AccountLogoutView.swift in Sources */,
|
||||
|
@ -7,12 +7,12 @@
|
||||
<key>WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_</key>
|
||||
<dict>
|
||||
<key>orderHint</key>
|
||||
<integer>0</integer>
|
||||
<integer>1</integer>
|
||||
</dict>
|
||||
<key>WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_</key>
|
||||
<dict>
|
||||
<key>orderHint</key>
|
||||
<integer>1</integer>
|
||||
<integer>0</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
|
@ -1,8 +1,7 @@
|
||||
import SwiftUI
|
||||
|
||||
struct SettingsView: View {
|
||||
@EnvironmentObject var preferences: PreferencesModel
|
||||
@EnvironmentObject var account: AccountModel
|
||||
@EnvironmentObject var model: WriteFreelyModel
|
||||
|
||||
@Binding var isPresented: Bool
|
||||
|
||||
@ -11,10 +10,10 @@ struct SettingsView: View {
|
||||
SettingsHeaderView(isPresented: $isPresented)
|
||||
Form {
|
||||
Section(header: Text("Login Details")) {
|
||||
AccountView(account: account)
|
||||
AccountView(account: model.account)
|
||||
}
|
||||
Section(header: Text("Appearance")) {
|
||||
PreferencesView(preferences: preferences)
|
||||
PreferencesView(preferences: model.preferences)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -25,5 +24,6 @@ struct SettingsView: View {
|
||||
struct SettingsView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
SettingsView(isPresented: .constant(true))
|
||||
.environmentObject(WriteFreelyModel())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user