NetNewsWire/Multiplatform/Shared/SceneNavigationView.swift

220 lines
5.9 KiB
Swift
Raw Normal View History

2020-06-28 21:21:43 +02:00
//
// SceneNavigationView.swift
// NetNewsWire
//
// Created by Maurice Parker on 6/28/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import SwiftUI
2020-07-16 03:09:11 +02:00
import Account
2020-08-10 02:01:16 +02:00
#if os(macOS)
import AppKit
#endif
2020-06-28 21:21:43 +02:00
2020-06-28 21:21:43 +02:00
struct SceneNavigationView: View {
@StateObject private var sceneModel = SceneModel()
@StateObject private var sceneNavigationModel = SceneNavigationModel()
2020-06-29 16:06:00 +02:00
#if os(iOS)
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
#endif
var body: some View {
2020-06-28 21:21:43 +02:00
NavigationView {
#if os(macOS)
SidebarContainerView()
2020-06-29 23:58:10 +02:00
.frame(minWidth: 100, idealWidth: 150, maxHeight: .infinity)
2020-06-28 21:21:43 +02:00
#else
2020-08-12 15:44:46 +02:00
SidebarContainerView()
2020-06-28 21:21:43 +02:00
#endif
2020-06-29 16:06:00 +02:00
#if os(iOS)
if horizontalSizeClass != .compact {
TimelineContainerView()
2020-06-29 16:06:00 +02:00
}
#else
TimelineContainerView()
2020-06-29 16:06:00 +02:00
#endif
2020-06-28 21:21:43 +02:00
ArticleContainerView()
2020-06-28 21:21:43 +02:00
}
.environmentObject(sceneModel)
.onAppear {
sceneModel.startup()
}
.onReceive(sceneModel.$accountSyncErrors) { errors in
if errors.count == 0 {
sceneNavigationModel.showAccountSyncErrorAlert = false
} else {
if errors.count > 1 {
sceneNavigationModel.showAccountSyncErrorAlert = true
} else {
sceneNavigationModel.sheetToShow = .fixCredentials
}
}
}
.sheet(isPresented: $sceneNavigationModel.showSheet,
onDismiss: {
sceneNavigationModel.sheetToShow = .none
sceneModel.accountSyncErrors = []
}) {
if sceneNavigationModel.sheetToShow == .web {
AddWebFeedView(isPresented: $sceneNavigationModel.showSheet)
}
if sceneNavigationModel.sheetToShow == .folder {
AddFolderView(isPresented: $sceneNavigationModel.showSheet)
}
#if os(iOS)
if sceneNavigationModel.sheetToShow == .settings {
SettingsView()
}
#endif
if sceneNavigationModel.sheetToShow == .fixCredentials {
FixAccountCredentialView(accountSyncError: sceneModel.accountSyncErrors[0])
}
}
.alert(isPresented: $sceneNavigationModel.showAccountSyncErrorAlert, content: {
#if os(macOS)
return Alert(title: Text("Account Sync Error"),
message: Text("The following accounts failed to sync: ") + Text(sceneModel.accountSyncErrors.map({ $0.account.nameForDisplay }).joined(separator: ", ")) + Text(". You can update credentials in Preferences"),
dismissButton: .default(Text("Dismiss")))
#else
return Alert(title: Text("Account Sync Error"),
message: Text("The following accounts failed to sync: ") + Text(sceneModel.accountSyncErrors.map({ $0.account.nameForDisplay }).joined(separator: ", ")) + Text(". You can update credentials in Settings"),
primaryButton: .default(Text("Show Settings"), action: {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
sceneNavigationModel.sheetToShow = .settings
})
}),
secondaryButton: .cancel(Text("Dismiss")))
#endif
})
.toolbar {
#if os(macOS)
2020-08-10 02:01:16 +02:00
ToolbarItem(placement: .navigation) {
Button {
NSApp.keyWindow?.firstResponder?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil)
} label: {
AppAssets.sidebarToggleImage
2020-08-10 02:01:16 +02:00
}
2020-08-10 02:31:10 +02:00
.help("Toggle Sidebar")
2020-08-10 02:01:16 +02:00
}
ToolbarItem() {
Menu {
Button("Add Web Feed", action: { sceneNavigationModel.sheetToShow = .web })
Button("Add Reddit Feed", action: { })
Button("Add Twitter Feed", action: { })
Button("Add Folder", action: { sceneNavigationModel.sheetToShow = .folder})
} label : {
AppAssets.addMenuImage
}
}
ToolbarItem {
Button {
AccountManager.shared.refreshAll(completion: nil)
} label: {
AppAssets.refreshImage
}
.help("Refresh").padding(.trailing, 40)
}
ToolbarItem {
Button {
2020-07-18 11:58:46 +02:00
sceneModel.markAllAsRead()
} label: {
AppAssets.markAllAsReadImagePNG
.offset(y: 7)
}
.disabled(sceneModel.markAllAsReadButtonState == nil)
.help("Mark All as Read")
}
2020-08-10 02:01:16 +02:00
// ToolbarItem {
// MacSearchField()
// .frame(width: 200)
// }
ToolbarItem {
Button {
2020-07-19 20:28:22 +02:00
sceneModel.goToNextUnread()
} label: {
AppAssets.nextUnreadArticleImage
}
.disabled(sceneModel.nextUnreadButtonState == nil)
.help("Go to Next Unread").padding(.trailing, 40)
}
ToolbarItem {
Button {
sceneModel.toggleReadStatusForSelectedArticles()
} label: {
if sceneModel.readButtonState ?? false {
AppAssets.readClosedImage
} else {
AppAssets.readOpenImage
}
}
.disabled(sceneModel.readButtonState == nil)
.help(sceneModel.readButtonState ?? false ? "Mark as Unread" : "Mark as Read")
}
ToolbarItem {
Button {
sceneModel.toggleStarredStatusForSelectedArticles()
} label: {
if sceneModel.starButtonState ?? false {
AppAssets.starClosedImage
} else {
AppAssets.starOpenImage
}
}
.disabled(sceneModel.starButtonState == nil)
.help(sceneModel.starButtonState ?? false ? "Mark as Unstarred" : "Mark as Starred")
}
2020-07-09 00:34:35 +02:00
ToolbarItem {
Button {
} label: {
2020-07-09 00:34:35 +02:00
AppAssets.articleExtractorOff
}
.disabled(sceneModel.extractorButtonState == nil)
.help("Show Reader View")
2020-07-09 00:34:35 +02:00
}
ToolbarItem {
Button {
sceneModel.openSelectedArticleInBrowser()
} label: {
AppAssets.openInBrowserImage
}
.disabled(sceneModel.openInBrowserButtonState == nil)
.help("Open in Browser")
}
ToolbarItem {
2020-07-15 00:10:53 +02:00
ZStack {
if sceneNavigationModel.showShareSheet {
SharingServiceView(articles: sceneModel.selectedArticles, showing: $sceneNavigationModel.showShareSheet)
2020-07-15 00:10:53 +02:00
.frame(width: 20, height: 20)
}
Button {
sceneNavigationModel.showShareSheet = true
2020-07-15 00:10:53 +02:00
} label: {
AppAssets.shareImage
}
}
.disabled(sceneModel.shareButtonState == nil)
.help("Share")
}
#endif
}
2020-06-28 21:21:43 +02:00
}
2020-07-16 03:09:11 +02:00
2020-06-28 21:21:43 +02:00
}
struct NavigationView_Previews: PreviewProvider {
static var previews: some View {
SceneNavigationView()
.environmentObject(SceneModel())
2020-06-28 21:21:43 +02:00
}
}