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) {
|
|
|
|
print("Debug launch detected")
|
|
|
|
// Run debug-mode launch code here
|
|
|
|
} else {
|
|
|
|
print("Normal launch detected")
|
|
|
|
// Don't do anything
|
|
|
|
}
|
|
|
|
#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: {
|
|
|
|
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-12-23 15:21:23 +00:00
|
|
|
#if os(macOS)
|
|
|
|
CommandGroup(after: .appInfo, addition: {
|
|
|
|
Button("Check For Updates") {
|
|
|
|
print("Checking 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-16 19:25:56 +00:00
|
|
|
PostCommands(model: model)
|
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
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|