mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
remove bing function from share extension view model
This commit is contained in:
parent
a5ff333ac4
commit
58cfc4f8b2
1 changed files with 32 additions and 59 deletions
|
|
@ -8,9 +8,9 @@ import Views
|
|||
|
||||
public extension PlatformViewController {
|
||||
static func makeShareExtensionController(extensionContext: NSExtensionContext?) -> PlatformViewController {
|
||||
let viewModel = ShareExtensionViewModel.make(extensionContext: extensionContext)
|
||||
let rootView = ShareExtensionView(viewModel: viewModel)
|
||||
let hostingController = PlatformHostingController(rootView: rootView)
|
||||
let hostingController = PlatformHostingController(
|
||||
rootView: ShareExtensionView(extensionContext: extensionContext)
|
||||
)
|
||||
#if os(iOS)
|
||||
hostingController.view.layer.cornerRadius = 12
|
||||
hostingController.view.layer.masksToBounds = true
|
||||
|
|
@ -20,62 +20,35 @@ public extension PlatformViewController {
|
|||
}
|
||||
}
|
||||
|
||||
public final class ShareExtensionViewModel: ObservableObject {
|
||||
public enum Action {
|
||||
case savePage(requestID: String)
|
||||
case copyLinkButtonTapped
|
||||
case readNowButtonTapped
|
||||
case archiveButtonTapped
|
||||
case dismissButtonTapped(reminderTime: ReminderTime?, hideUntilReminded: Bool)
|
||||
final class ShareExtensionViewModel: ObservableObject {
|
||||
let extensionContext: NSExtensionContext?
|
||||
|
||||
@Published var title: String?
|
||||
@Published var status = ShareExtensionStatus.successfullySaved
|
||||
@Published var debugText: String?
|
||||
|
||||
var subscriptions = Set<AnyCancellable>()
|
||||
let requestID = UUID().uuidString.lowercased()
|
||||
|
||||
init(extensionContext: NSExtensionContext?) {
|
||||
self.extensionContext = extensionContext
|
||||
}
|
||||
|
||||
@Published public var title: String?
|
||||
@Published public var status = ShareExtensionStatus.successfullySaved
|
||||
@Published public var debugText: String?
|
||||
|
||||
public var subscriptions = Set<AnyCancellable>()
|
||||
public let performActionSubject = PassthroughSubject<Action, Never>()
|
||||
public let requestID = UUID().uuidString.lowercased()
|
||||
|
||||
public init() {}
|
||||
}
|
||||
|
||||
extension ShareExtensionViewModel {
|
||||
static func make(extensionContext: NSExtensionContext?) -> ShareExtensionViewModel {
|
||||
let viewModel = ShareExtensionViewModel()
|
||||
viewModel.bind(extensionContext: extensionContext)
|
||||
return viewModel
|
||||
}
|
||||
|
||||
func bind(extensionContext: NSExtensionContext?) {
|
||||
performActionSubject.sink { [weak self] action in
|
||||
switch action {
|
||||
case let .savePage(requestID):
|
||||
self?.savePage(extensionContext: extensionContext, requestId: requestID)
|
||||
case .dismissButtonTapped:
|
||||
extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
|
||||
case .copyLinkButtonTapped:
|
||||
print("copy link button tapped")
|
||||
case .readNowButtonTapped:
|
||||
#if os(iOS)
|
||||
if let application = UIApplication.value(forKeyPath: #keyPath(UIApplication.shared)) as? UIApplication {
|
||||
let deepLinkUrl = NSURL(string: "omnivore://shareExtensionRequestID/\(self?.requestID ?? "")")
|
||||
application.perform(NSSelectorFromString("openURL:"), with: deepLinkUrl)
|
||||
}
|
||||
#endif
|
||||
extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
|
||||
case .archiveButtonTapped:
|
||||
print("archive button tapped")
|
||||
func handleReadNowAction() {
|
||||
#if os(iOS)
|
||||
if let application = UIApplication.value(forKeyPath: #keyPath(UIApplication.shared)) as? UIApplication {
|
||||
let deepLinkUrl = NSURL(string: "omnivore://shareExtensionRequestID/\(requestID)")
|
||||
application.perform(NSSelectorFromString("openURL:"), with: deepLinkUrl)
|
||||
}
|
||||
}
|
||||
.store(in: &subscriptions)
|
||||
#endif
|
||||
extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
|
||||
}
|
||||
|
||||
private func savePage(extensionContext: NSExtensionContext?, requestId: String) {
|
||||
func savePage() {
|
||||
PageScraper.scrape(extensionContext: extensionContext) { [weak self] result in
|
||||
switch result {
|
||||
case let .success(payload):
|
||||
self?.persist(pageScrapePayload: payload, requestId: requestId)
|
||||
self?.persist(pageScrapePayload: payload, requestId: self?.requestID ?? "")
|
||||
case let .failure(error):
|
||||
self?.debugText = error.message
|
||||
}
|
||||
|
|
@ -121,22 +94,22 @@ extension ShareExtensionViewModel {
|
|||
}
|
||||
}
|
||||
|
||||
public struct ShareExtensionView: View {
|
||||
struct ShareExtensionView: View {
|
||||
@ObservedObject private var viewModel: ShareExtensionViewModel
|
||||
|
||||
public init(viewModel: ShareExtensionViewModel) {
|
||||
self.viewModel = viewModel
|
||||
init(extensionContext: NSExtensionContext?) {
|
||||
self.viewModel = ShareExtensionViewModel(extensionContext: extensionContext)
|
||||
}
|
||||
|
||||
public var body: some View {
|
||||
var body: some View {
|
||||
ShareExtensionChildView(
|
||||
debugText: viewModel.debugText,
|
||||
title: viewModel.title,
|
||||
status: viewModel.status,
|
||||
onAppearAction: { viewModel.performActionSubject.send(.savePage(requestID: viewModel.requestID)) },
|
||||
readNowButtonAction: { viewModel.performActionSubject.send(.readNowButtonTapped) },
|
||||
dismissButtonTappedAction: {
|
||||
viewModel.performActionSubject.send(.dismissButtonTapped(reminderTime: $0, hideUntilReminded: $1))
|
||||
onAppearAction: viewModel.savePage,
|
||||
readNowButtonAction: viewModel.handleReadNowAction,
|
||||
dismissButtonTappedAction: { _, _ in
|
||||
viewModel.extensionContext?.completeRequest(returningItems: [], completionHandler: nil)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue