2020-07-22 13:57:10 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
2020-12-23 15:21:23 +00:00
|
|
|
#if os(macOS)
|
|
|
|
import Sparkle
|
|
|
|
#endif
|
|
|
|
|
2020-07-22 13:57:10 +00:00
|
|
|
@main
|
2020-12-01 21:17:53 +00:00
|
|
|
struct CheckForDebugModifier {
|
|
|
|
static func main() {
|
|
|
|
#if os(macOS)
|
|
|
|
if NSEvent.modifierFlags.contains(.shift) {
|
2021-01-26 14:27:43 +00:00
|
|
|
// Clear the launch-to-last-draft values to load a new draft.
|
|
|
|
UserDefaults.standard.setValue(false, forKey: "showAllPostsFlag")
|
|
|
|
UserDefaults.standard.setValue(nil, forKey: "selectedCollectionURL")
|
|
|
|
UserDefaults.standard.setValue(nil, forKey: "lastDraftURL")
|
2020-12-01 21:17:53 +00:00
|
|
|
} else {
|
2021-01-26 14:27:43 +00:00
|
|
|
// No-op
|
2020-12-01 21:17:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
WriteFreely_MultiPlatformApp.main()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:57:10 +00:00
|
|
|
struct WriteFreely_MultiPlatformApp: App {
|
2020-08-18 18:06:02 +00:00
|
|
|
@StateObject private var model = WriteFreelyModel()
|
2020-08-12 14:34:12 +00:00
|
|
|
|
|
|
|
#if os(macOS)
|
2020-12-22 20:06:25 +00:00
|
|
|
// swiftlint:disable:next weak_delegate
|
|
|
|
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
|
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-12-01 14:53:09 +00:00
|
|
|
ContentView()
|
2020-11-04 20:23:03 +00:00
|
|
|
.onAppear(perform: {
|
2021-01-25 16:30:52 +00:00
|
|
|
if model.editor.showAllPostsFlag {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.model.selectedCollection = nil
|
|
|
|
self.model.showAllPosts = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.model.selectedCollection = model.editor.fetchSelectedCollectionFromAppStorage()
|
|
|
|
self.model.showAllPosts = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
2021-01-26 14:23:47 +00:00
|
|
|
if model.editor.lastDraftURL != nil {
|
|
|
|
self.model.selectedPost = model.editor.fetchLastDraftFromAppStorage()
|
|
|
|
} else {
|
|
|
|
createNewLocalPost()
|
|
|
|
}
|
2021-01-25 16:30:52 +00:00
|
|
|
}
|
2020-11-04 20:23:03 +00:00
|
|
|
})
|
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-12-23 15:21:23 +00:00
|
|
|
#if os(macOS)
|
|
|
|
CommandGroup(after: .appInfo, addition: {
|
|
|
|
Button("Check For Updates") {
|
|
|
|
SUUpdater.shared()?.checkForUpdates(self)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
#endif
|
2020-11-25 19:41:53 +00:00
|
|
|
CommandGroup(replacing: .newItem, addition: {
|
2020-12-01 14:38:46 +00:00
|
|
|
Button("New Post") {
|
2020-11-25 19:41:53 +00:00
|
|
|
createNewLocalPost()
|
|
|
|
}
|
|
|
|
.keyboardShortcut("n", modifiers: [.command])
|
|
|
|
})
|
2020-11-25 20:10:22 +00:00
|
|
|
CommandGroup(after: .newItem) {
|
2020-12-01 16:24:55 +00:00
|
|
|
Button("Refresh Posts") {
|
2020-11-25 20:10:22 +00:00
|
|
|
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-12-01 14:55:11 +00:00
|
|
|
SidebarCommands()
|
2020-12-28 20:15:14 +00:00
|
|
|
#if os(macOS)
|
2020-12-16 19:25:56 +00:00
|
|
|
PostCommands(model: model)
|
2020-12-28 20:15:14 +00:00
|
|
|
#endif
|
2020-12-03 13:21:50 +00:00
|
|
|
CommandGroup(after: .help) {
|
|
|
|
Button("Visit Support Forum") {
|
|
|
|
#if os(macOS)
|
|
|
|
NSWorkspace().open(model.helpURL)
|
|
|
|
#else
|
|
|
|
UIApplication.shared.open(model.helpURL)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2021-01-28 22:45:10 +00:00
|
|
|
ToolbarCommands()
|
|
|
|
TextEditingCommands()
|
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-12-23 16:53:01 +00:00
|
|
|
MacUpdatesView()
|
|
|
|
.tabItem {
|
|
|
|
Image(systemName: "arrow.down.circle")
|
|
|
|
Text("Updates")
|
|
|
|
}
|
|
|
|
.tag(2)
|
2020-08-12 14:21:15 +00:00
|
|
|
}
|
2020-12-23 19:38:53 +00:00
|
|
|
.frame(minWidth: 500, maxWidth: 500, minHeight: 200)
|
2020-08-12 14:21:15 +00:00
|
|
|
.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 {
|
2021-01-14 18:29:51 +00:00
|
|
|
// Un-set the currently selected post
|
2020-11-25 19:41:53 +00:00
|
|
|
self.model.selectedPost = nil
|
2021-01-14 18:29:51 +00:00
|
|
|
|
|
|
|
// Navigate to the Drafts list
|
|
|
|
self.model.showAllPosts = false
|
|
|
|
self.model.selectedCollection = nil
|
2020-11-25 19:41:53 +00:00
|
|
|
}
|
2021-01-14 18:29:51 +00:00
|
|
|
// Create the new-post managed object
|
2021-01-25 16:53:16 +00:00
|
|
|
let managedPost = model.editor.generateNewLocalPost(withFont: model.preferences.font)
|
2020-11-25 19:41:53 +00:00
|
|
|
withAnimation {
|
2021-01-25 16:30:52 +00:00
|
|
|
// Set it as the selectedPost
|
2021-01-14 18:29:51 +00:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now()) {
|
|
|
|
self.model.selectedPost = managedPost
|
|
|
|
}
|
2020-11-25 19:41:53 +00:00
|
|
|
}
|
2020-11-04 20:23:03 +00:00
|
|
|
}
|
2020-07-22 13:57:10 +00:00
|
|
|
}
|