swiftui-multiplatform/Shared/WriteFreely_MultiPlatformApp.swift

140 lines
4.6 KiB
Swift
Raw Normal View History

2020-07-22 13:57:10 +00:00
import SwiftUI
#if os(macOS)
import Sparkle
#endif
2020-07-22 13:57:10 +00:00
@main
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 {
@StateObject private var model = WriteFreelyModel()
#if os(macOS)
2020-12-22 20:06:25 +00:00
// swiftlint:disable:next weak_delegate
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@State private var selectedTab = 0
#endif
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()
}
})
.environmentObject(model)
.environment(\.managedObjectContext, LocalStorageManager.persistentContainer.viewContext)
// .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info.
2020-07-22 13:57:10 +00:00
}
.commands {
#if os(macOS)
CommandGroup(after: .appInfo, addition: {
Button("Check For Updates") {
SUUpdater.shared()?.checkForUpdates(self)
}
})
#endif
CommandGroup(replacing: .newItem, addition: {
Button("New Post") {
createNewLocalPost()
}
.keyboardShortcut("n", modifiers: [.command])
})
CommandGroup(after: .newItem) {
Button("Refresh Posts") {
DispatchQueue.main.async {
model.fetchUserCollections()
model.fetchUserPosts()
}
}
.disabled(!model.account.isLoggedIn)
.keyboardShortcut("r", modifiers: [.command])
}
SidebarCommands()
PostCommands(model: model)
CommandGroup(after: .help) {
Button("Visit Support Forum") {
#if os(macOS)
NSWorkspace().open(model.helpURL)
#else
UIApplication.shared.open(model.helpURL)
#endif
}
}
}
#if os(macOS)
Settings {
TabView(selection: $selectedTab) {
MacAccountView()
.environmentObject(model)
.tabItem {
Image(systemName: "person.crop.circle")
Text("Account")
}
.tag(0)
MacPreferencesView(preferences: model.preferences)
.tabItem {
Image(systemName: "gear")
Text("Preferences")
}
.tag(1)
MacUpdatesView()
.tabItem {
Image(systemName: "arrow.down.circle")
Text("Updates")
}
.tag(2)
}
.frame(minWidth: 300, maxWidth: 300, minHeight: 200, maxHeight: 200)
.padding()
// .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info.
}
#endif
2020-07-22 13:57:10 +00:00
}
2020-11-04 20:23:03 +00:00
private func createNewLocalPost() {
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
}
withAnimation {
self.model.selectedPost = managedPost
}
2020-11-04 20:23:03 +00:00
}
2020-07-22 13:57:10 +00:00
}