Repository Public
1// SP Notes iOS2// File: ShareExtension/ShareViewController.swift3// Description: Share extension entry point: hosts the SwiftUI sheet and ends the request. The extension only writes one4// `SharedItem` into the App Group container; it never opens the database nor the network. The app turns the5// item into a note at its next activation.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import SwiftUI10import UIKit1112final class ShareViewController: UIViewController {13 private let model = ShareModel()1415 override func viewDidLoad() {16 super.viewDidLoad()17 view.backgroundColor = UIColor(named: "Background") ?? .systemBackground18 view.tintColor = UIColor(named: "AccentColor")19 model.onFinish = { [weak self] saved in self?.finish(saved: saved) }20 let host = UIHostingController(rootView: ShareView(model: model).tint(Color.accentColor))21 addChild(host)22 host.view.translatesAutoresizingMaskIntoConstraints = false23 view.addSubview(host.view)24 NSLayoutConstraint.activate([25 host.view.topAnchor.constraint(equalTo: view.topAnchor), host.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),26 host.view.leadingAnchor.constraint(equalTo: view.leadingAnchor), host.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),27 ])28 host.didMove(toParent: self)29 let items = (extensionContext?.inputItems ?? []).compactMap { $0 as? NSExtensionItem }30 Task { await model.load(items) }31 }3233 private func finish(saved: Bool) {34 if saved {35 extensionContext?.completeRequest(returningItems: nil)36 } else {37 extensionContext?.cancelRequest(withError: CocoaError(.userCancelled))38 }39 }40}41