From 41a5603593c20f01f452829a5003579e0d7a40c5 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Wed, 31 Aug 2022 06:56:18 -0700 Subject: [PATCH] use nsnotification to open web reader when selected from audio session --- .../Sources/App/Views/AudioPlayer/MiniPlayer.swift | 12 ++---------- .../Sources/App/Views/Home/HomeFeedViewIOS.swift | 6 ++++++ .../Sources/App/Views/Home/HomeFeedViewModel.swift | 4 ++++ .../Services/NSNotification+Operation.swift | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/AudioPlayer/MiniPlayer.swift b/apple/OmnivoreKit/Sources/App/Views/AudioPlayer/MiniPlayer.swift index 8043979bd..52ed4f824 100644 --- a/apple/OmnivoreKit/Sources/App/Views/AudioPlayer/MiniPlayer.swift +++ b/apple/OmnivoreKit/Sources/App/Views/AudioPlayer/MiniPlayer.swift @@ -16,7 +16,6 @@ public struct MiniPlayer: View { @Environment(\.colorScheme) private var colorScheme: ColorScheme private let presentingView: AnyView - @State private var webReaderItem: LinkedItem? @State var expanded = false @State var offset: CGFloat = 0 @Namespace private var animation @@ -92,7 +91,6 @@ public struct MiniPlayer: View { Button( action: { withAnimation(.interactiveSpring()) { - self.webReaderItem = nil self.expanded = false } }, @@ -145,7 +143,8 @@ public struct MiniPlayer: View { } .onTapGesture { if expanded { - webReaderItem = item + expanded = false + NSNotification.pushReaderItem(objectID: item.objectID) } } @@ -292,12 +291,6 @@ public struct MiniPlayer: View { } } } - .sheet(item: $webReaderItem) { - LinkItemDetailView( - linkedItemObjectID: $0.objectID, - isPDF: $0.isPDF - ) - } } func onDragChanged(value: DragGesture.Value) { @@ -310,7 +303,6 @@ public struct MiniPlayer: View { withAnimation(.interactiveSpring()) { if value.translation.height > minExpandedHeight { expanded = false - webReaderItem = nil } offset = 0 } diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift index 35eb891ab..a883cd790 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewIOS.swift @@ -1,3 +1,4 @@ +import CoreData import Models import Services import SwiftUI @@ -112,6 +113,11 @@ import Views viewModel.pushFeedItem(item: linkedItem) viewModel.selectedLinkItem = linkedItem.objectID } + .onReceive(NSNotification.pushReaderItemPublisher) { notification in + if let objectID = notification.userInfo?["objectID"] as? NSManagedObjectID { + viewModel.handleReaderItemNotification(objectID: objectID) + } + } .onOpenURL { url in withoutAnimation { viewModel.linkRequest = nil diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift index 755e3e979..75b34bca4 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift @@ -33,6 +33,10 @@ import Views from: Date(timeIntervalSinceReferenceDate: 0) ) + func handleReaderItemNotification(objectID: NSManagedObjectID) { + selectedLinkItem = objectID + } + var cursor: String? // These are used to make sure we handle search result diff --git a/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift b/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift index fbcf2663c..a80e0e0c3 100644 --- a/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift +++ b/apple/OmnivoreKit/Sources/Services/NSNotification+Operation.swift @@ -1,8 +1,10 @@ +import CoreData import Foundation import Models public extension NSNotification { static let PushJSONArticle = Notification.Name("PushJSONArticle") + static let PushReaderItem = Notification.Name("PushReaderItem") static let OperationSuccess = Notification.Name("OperationSuccess") static let OperationFailure = Notification.Name("OperationFailure") static let ReaderSettingsChanged = Notification.Name("ReaderSettingsChanged") @@ -11,6 +13,10 @@ public extension NSNotification { NotificationCenter.default.publisher(for: PushJSONArticle) } + static var pushReaderItemPublisher: NotificationCenter.Publisher { + NotificationCenter.default.publisher(for: PushReaderItem) + } + static var operationSuccessPublisher: NotificationCenter.Publisher { NotificationCenter.default.publisher(for: OperationSuccess) } @@ -38,6 +44,14 @@ public extension NSNotification { ) } + static func pushReaderItem(objectID: NSManagedObjectID) { + NotificationCenter.default.post( + name: NSNotification.PushReaderItem, + object: nil, + userInfo: ["objectID": objectID] + ) + } + static func operationSuccess(message: String) { NotificationCenter.default.post(name: NSNotification.OperationSuccess, object: nil, userInfo: ["message": message]) }