From 8cf94e552d094c65a72d8a6f0f884a3b79400961 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Wed, 20 Sep 2023 16:55:37 +0800 Subject: [PATCH] Fix context menus in the library, add an Add link dialog --- .../App/Views/Home/HomeFeedViewMac.swift | 16 ++- .../App/Views/Home/LibraryAddLinkView.swift | 100 ++++++++++++------ .../Sources/App/Views/LibraryTabView.swift | 2 + .../Views/LinkedItemMetadataEditView.swift | 12 +-- .../App/Views/PrimaryContentView.swift | 2 + 5 files changed, 90 insertions(+), 42 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewMac.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewMac.swift index b04cd20af..482d2f191 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewMac.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewMac.swift @@ -14,6 +14,7 @@ import Views @State private var itemToRemove: LinkedItem? @State private var confirmationShown = false @State private var presentProfileSheet = false + @State private var addLinkPresented = false @Namespace var mainNamespace @@ -62,6 +63,15 @@ import Views } } + var addLinkButton: some View { + Button( + action: { + addLinkPresented = true + }, + label: { Label("Add Link", systemImage: "plus") } + ) + } + var refreshButton: some View { Button( action: { @@ -98,7 +108,6 @@ import Views } Divider().padding(5) } - .prefersDefaultFocus(true, in: mainNamespace) if viewModel.isLoading { LoadingSection() @@ -112,6 +121,7 @@ import Views } .toolbar { Spacer() + addLinkButton refreshButton } } @@ -150,6 +160,10 @@ import Views .sheet(isPresented: $presentProfileSheet) { ProfileView() } + .sheet(isPresented: $addLinkPresented) { + LibraryAddLinkView() + .frame(width: 450, height: 160) + } .onReceive(NSNotification.displayProfilePublisher) { _ in presentProfileSheet = true } diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/LibraryAddLinkView.swift b/apple/OmnivoreKit/Sources/App/Views/Home/LibraryAddLinkView.swift index 7fb03a786..b64a99bba 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/LibraryAddLinkView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/LibraryAddLinkView.swift @@ -10,6 +10,8 @@ import Views @Published var errorMessage: String = "" @Published var showErrorMessage: Bool = false + @Environment(\.dismiss) private var dismiss + func addLink(dataService: DataService, newLinkURL: String, dismiss: DismissAction) { isLoading = true Task { @@ -48,44 +50,21 @@ struct LibraryAddLinkView: View { @FocusState private var focusedField: FocusField? var body: some View { - innerBody - .navigationTitle("Add Link") - #if os(iOS) - .navigationBarTitleDisplayMode(.inline) - #endif + Group { + #if os(iOS) + Form { + innerBody + .navigationTitle("Add Link") + .navigationBarTitleDisplayMode(.inline) + } + #else + innerBody + #endif + } + .padding() .onAppear { focusedField = .addLinkEditor } - } - - var pasteboardString: String? { - #if os(iOS) - UIPasteboard.general.url?.absoluteString - #else - NSPasteboard.general.string(forType: NSPasteboard.PasteboardType.URL) - #endif - } - - var innerBody: some View { - Form { - TextField("Add Link", text: $newLinkURL) - #if os(iOS) - .keyboardType(.URL) - #endif - .autocorrectionDisabled(true) - .textFieldStyle(StandardTextFieldStyle()) - .focused($focusedField, equals: .addLinkEditor) - - Button(action: { - if let url = pasteboardString { - newLinkURL = url - } else { - viewModel.error("No URL on pasteboard") - } - }, label: { - Text("Get from pasteboard") - }) - } .navigationTitle("Add Link") #if os(iOS) .navigationBarTitleDisplayMode(.inline) @@ -104,6 +83,53 @@ struct LibraryAddLinkView: View { } } + var cancelButton: some View { + Button( + action: { dismiss() }, + label: { Text(LocalText.cancelGeneric).foregroundColor(.appGrayTextContrast) } + ) + } + + var pasteboardString: String? { + #if os(iOS) + UIPasteboard.general.url?.absoluteString + #else + NSPasteboard.general.string(forType: NSPasteboard.PasteboardType.URL) + #endif + } + + var innerBody: some View { + Group { + TextField("Add Link", text: $newLinkURL) + #if os(iOS) + .keyboardType(.URL) + #endif + .autocorrectionDisabled(true) + .textFieldStyle(StandardTextFieldStyle()) + .focused($focusedField, equals: .addLinkEditor) + + Button(action: { + if let url = pasteboardString { + newLinkURL = url + } else { + viewModel.error("No URL on pasteboard") + } + }, label: { + Text("Get from pasteboard") + }) + + #if os(macOS) + Spacer() + HStack { + cancelButton + Spacer() + addButton + } + .frame(maxWidth: .infinity) + #endif + } + } + var addButton: some View { Button( action: { @@ -111,6 +137,10 @@ struct LibraryAddLinkView: View { }, label: { Text("Add").bold() } ) + .keyboardShortcut(.defaultAction) + .onSubmit { + viewModel.addLink(dataService: dataService, newLinkURL: newLinkURL, dismiss: dismiss) + } .disabled(viewModel.isLoading) } diff --git a/apple/OmnivoreKit/Sources/App/Views/LibraryTabView.swift b/apple/OmnivoreKit/Sources/App/Views/LibraryTabView.swift index 90e5f9c2c..9e0739417 100644 --- a/apple/OmnivoreKit/Sources/App/Views/LibraryTabView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/LibraryTabView.swift @@ -45,7 +45,9 @@ struct LibraryTabView: View { var body: some View { NavigationView { HomeView(viewModel: libraryViewModel) + #if os(iOS) .navigationBarTitleDisplayMode(.inline) + #endif } } } diff --git a/apple/OmnivoreKit/Sources/App/Views/LinkedItemMetadataEditView.swift b/apple/OmnivoreKit/Sources/App/Views/LinkedItemMetadataEditView.swift index 55291ce5f..80b58d2a5 100644 --- a/apple/OmnivoreKit/Sources/App/Views/LinkedItemMetadataEditView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/LinkedItemMetadataEditView.swift @@ -98,6 +98,12 @@ struct LinkedItemMetadataEditView: View { .navigationTitle("Edit Title and Description") .navigationBarTitleDisplayMode(.inline) .toolbar { + ToolbarItem(placement: .barLeading) { + Button( + action: { presentationMode.wrappedValue.dismiss() }, + label: { Text(LocalText.cancelGeneric).foregroundColor(.appGrayTextContrast) } + ) + } ToolbarItem(placement: .barTrailing) { Button( action: { @@ -110,12 +116,6 @@ struct LinkedItemMetadataEditView: View { label: { Text(LocalText.genericSave).foregroundColor(.appGrayTextContrast) } ) } - ToolbarItem(placement: .barLeading) { - Button( - action: { presentationMode.wrappedValue.dismiss() }, - label: { Text(LocalText.cancelGeneric).foregroundColor(.appGrayTextContrast) } - ) - } } } } diff --git a/apple/OmnivoreKit/Sources/App/Views/PrimaryContentView.swift b/apple/OmnivoreKit/Sources/App/Views/PrimaryContentView.swift index e6ba752d3..e3f25dab3 100644 --- a/apple/OmnivoreKit/Sources/App/Views/PrimaryContentView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/PrimaryContentView.swift @@ -92,7 +92,9 @@ import Views .sheet(isPresented: $addLinkPresented) { NavigationView { LibraryAddLinkView() + #if os(iOS) .navigationBarTitleDisplayMode(.inline) + #endif } } }