2020-07-22 13:57:10 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
@main
|
|
|
|
struct WriteFreely_MultiPlatformApp: App {
|
2020-08-18 18:06:02 +00:00
|
|
|
@StateObject private var model = WriteFreelyModel()
|
2020-11-25 19:41:53 +00:00
|
|
|
@State private var sidebarIsHidden: Bool = false
|
2020-08-12 14:34:12 +00:00
|
|
|
|
|
|
|
#if os(macOS)
|
2020-08-12 14:21:15 +00:00
|
|
|
@State private var selectedTab = 0
|
2020-08-12 14:34:12 +00:00
|
|
|
#endif
|
2020-08-11 18:32:45 +00:00
|
|
|
|
2020-07-22 13:57:10 +00:00
|
|
|
var body: some Scene {
|
|
|
|
WindowGroup {
|
2020-11-25 19:11:57 +00:00
|
|
|
ContentView(sidebarIsHidden: $sidebarIsHidden)
|
2020-11-04 20:23:03 +00:00
|
|
|
.onAppear(perform: {
|
|
|
|
if let lastDraft = model.editor.fetchLastDraftFromUserDefaults() {
|
|
|
|
self.model.selectedPost = lastDraft
|
|
|
|
} else {
|
|
|
|
createNewLocalPost()
|
|
|
|
}
|
|
|
|
})
|
2020-08-18 18:06:02 +00:00
|
|
|
.environmentObject(model)
|
2020-09-09 18:19:40 +00:00
|
|
|
.environment(\.managedObjectContext, LocalStorageManager.persistentContainer.viewContext)
|
2020-08-13 18:54:56 +00:00
|
|
|
// .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info.
|
2020-07-22 13:57:10 +00:00
|
|
|
}
|
2020-11-24 20:35:26 +00:00
|
|
|
.commands {
|
2020-11-25 19:41:53 +00:00
|
|
|
CommandGroup(replacing: .newItem, addition: {
|
|
|
|
Button("New Local Draft") {
|
|
|
|
createNewLocalPost()
|
|
|
|
}
|
|
|
|
.keyboardShortcut("n", modifiers: [.command])
|
|
|
|
})
|
2020-11-25 20:10:22 +00:00
|
|
|
CommandGroup(after: .newItem) {
|
|
|
|
Button("Reload From Server") {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
model.fetchUserCollections()
|
|
|
|
model.fetchUserPosts()
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 17:05:20 +00:00
|
|
|
.disabled(!model.account.isLoggedIn)
|
2020-11-25 20:10:22 +00:00
|
|
|
.keyboardShortcut("r", modifiers: [.command])
|
|
|
|
}
|
2020-11-25 19:41:53 +00:00
|
|
|
#if os(macOS)
|
2020-11-25 19:11:57 +00:00
|
|
|
CommandGroup(after: .sidebar) {
|
|
|
|
Button("Toggle Sidebar") {
|
|
|
|
NSApp.keyWindow?.contentViewController?.tryToPerform(
|
|
|
|
#selector(NSSplitViewController.toggleSidebar(_:)), with: nil
|
|
|
|
)
|
|
|
|
withAnimation { self.sidebarIsHidden.toggle() }
|
|
|
|
}
|
|
|
|
.keyboardShortcut("s", modifiers: [.command, .option])
|
|
|
|
}
|
2020-11-25 19:41:53 +00:00
|
|
|
#endif
|
2020-11-24 20:35:26 +00:00
|
|
|
}
|
2020-08-07 20:30:09 +00:00
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
Settings {
|
2020-08-12 14:21:15 +00:00
|
|
|
TabView(selection: $selectedTab) {
|
2020-08-18 21:20:16 +00:00
|
|
|
MacAccountView()
|
|
|
|
.environmentObject(model)
|
2020-08-12 14:36:49 +00:00
|
|
|
.tabItem {
|
|
|
|
Image(systemName: "person.crop.circle")
|
|
|
|
Text("Account")
|
2020-08-12 14:21:15 +00:00
|
|
|
}
|
2020-08-12 14:36:49 +00:00
|
|
|
.tag(0)
|
2020-08-18 18:06:02 +00:00
|
|
|
MacPreferencesView(preferences: model.preferences)
|
2020-08-12 14:36:10 +00:00
|
|
|
.tabItem {
|
|
|
|
Image(systemName: "gear")
|
|
|
|
Text("Preferences")
|
|
|
|
}
|
|
|
|
.tag(1)
|
2020-08-12 14:21:15 +00:00
|
|
|
}
|
|
|
|
.frame(minWidth: 300, maxWidth: 300, minHeight: 200, maxHeight: 200)
|
|
|
|
.padding()
|
2020-08-13 18:54:56 +00:00
|
|
|
// .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info.
|
2020-08-07 20:30:09 +00:00
|
|
|
}
|
|
|
|
#endif
|
2020-07-22 13:57:10 +00:00
|
|
|
}
|
2020-11-04 20:23:03 +00:00
|
|
|
|
|
|
|
private func createNewLocalPost() {
|
2020-11-25 19:41:53 +00:00
|
|
|
withAnimation {
|
|
|
|
self.model.selectedPost = nil
|
|
|
|
}
|
2020-11-04 20:23:03 +00:00
|
|
|
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
|
|
|
|
}
|
2020-11-25 19:41:53 +00:00
|
|
|
withAnimation {
|
|
|
|
self.model.selectedPost = managedPost
|
|
|
|
}
|
2020-11-04 20:23:03 +00:00
|
|
|
}
|
2020-07-22 13:57:10 +00:00
|
|
|
}
|