Add new-post logic to toolbar button and menu, fix broken iOS code

This commit is contained in:
Angelo Stavrow 2020-11-25 14:41:53 -05:00
parent 1e432c6adb
commit 735867a940
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
4 changed files with 72 additions and 7 deletions

View File

@ -19,7 +19,32 @@ struct ContentView: View {
label: { Image(systemName: "sidebar.left") }
)
Spacer()
Button(action: {}, label: { Image(systemName: "square.and.pencil") })
Button(action: {
withAnimation {
self.model.selectedPost = nil
}
let managedPost = WFAPost(context: LocalStorageManager.persistentContainer.viewContext)
managedPost.createdDate = Date()
managedPost.title = ""
managedPost.body = ""
managedPost.status = PostStatus.local.rawValue
managedPost.collectionAlias = nil
switch model.preferences.font {
case 1:
managedPost.appearance = "sans"
case 2:
managedPost.appearance = "wrap"
default:
managedPost.appearance = "serif"
}
if let languageCode = Locale.current.languageCode {
managedPost.language = languageCode
managedPost.rtl = Locale.characterDirection(forLanguage: languageCode) == .rightToLeft
}
withAnimation {
self.model.selectedPost = managedPost
}
}, label: { Image(systemName: "square.and.pencil") })
}
#else
SidebarView()

View File

@ -3,8 +3,10 @@ import CoreData
class PostListModel: ObservableObject {
func remove(_ post: WFAPost) {
LocalStorageManager.persistentContainer.viewContext.delete(post)
LocalStorageManager().saveContext()
withAnimation {
LocalStorageManager.persistentContainer.viewContext.delete(post)
LocalStorageManager().saveContext()
}
}
func purgePublishedPosts() {

View File

@ -21,7 +21,29 @@ struct PostListView: View {
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button(action: {
createNewLocalDraft()
let managedPost = WFAPost(context: self.managedObjectContext)
managedPost.createdDate = Date()
managedPost.title = ""
managedPost.body = ""
managedPost.status = PostStatus.local.rawValue
managedPost.collectionAlias = nil
switch model.preferences.font {
case 1:
managedPost.appearance = "sans"
case 2:
managedPost.appearance = "wrap"
default:
managedPost.appearance = "serif"
}
if let languageCode = Locale.current.languageCode {
managedPost.language = languageCode
managedPost.rtl = Locale.characterDirection(forLanguage: languageCode) == .rightToLeft
}
withAnimation {
self.selectedCollection = nil
self.showAllPosts = false
self.model.selectedPost = managedPost
}
}, label: {
Image(systemName: "square.and.pencil")
})
@ -41,7 +63,10 @@ struct PostListView: View {
ProgressView()
} else {
Button(action: {
reloadFromServer()
DispatchQueue.main.async {
model.fetchUserCollections()
model.fetchUserPosts()
}
}, label: {
Image(systemName: "arrow.clockwise")
})

View File

@ -3,10 +3,10 @@ import SwiftUI
@main
struct WriteFreely_MultiPlatformApp: App {
@StateObject private var model = WriteFreelyModel()
@State private var sidebarIsHidden: Bool = false
#if os(macOS)
@State private var selectedTab = 0
@State private var sidebarIsHidden: Bool = false
#endif
var body: some Scene {
@ -24,6 +24,13 @@ struct WriteFreely_MultiPlatformApp: App {
// .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info.
}
.commands {
CommandGroup(replacing: .newItem, addition: {
Button("New Local Draft") {
createNewLocalPost()
}
.keyboardShortcut("n", modifiers: [.command])
})
#if os(macOS)
CommandGroup(after: .sidebar) {
Button("Toggle Sidebar") {
NSApp.keyWindow?.contentViewController?.tryToPerform(
@ -33,6 +40,7 @@ struct WriteFreely_MultiPlatformApp: App {
}
.keyboardShortcut("s", modifiers: [.command, .option])
}
#endif
}
#if os(macOS)
@ -60,6 +68,9 @@ struct WriteFreely_MultiPlatformApp: App {
}
private func createNewLocalPost() {
withAnimation {
self.model.selectedPost = nil
}
let managedPost = WFAPost(context: LocalStorageManager.persistentContainer.viewContext)
managedPost.createdDate = Date()
managedPost.title = ""
@ -78,6 +89,8 @@ struct WriteFreely_MultiPlatformApp: App {
managedPost.language = languageCode
managedPost.rtl = Locale.characterDirection(forLanguage: languageCode) == .rightToLeft
}
self.model.selectedPost = managedPost
withAnimation {
self.model.selectedPost = managedPost
}
}
}