The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import ExpoModulesCore |
| 2 | import UIKit |
| 3 | |
| 4 | final class T3PresentationSources { |
| 5 | private class Entry { |
| 6 | weak var view: UIView? |
| 7 | init(_ view: UIView) { self.view = view } |
| 8 | } |
| 9 | |
| 10 | /// The same file embedded twice registers twice; removing one copy keeps the other. |
| 11 | private var entries: [String: [Entry]] = [:] |
| 12 | |
| 13 | func register(_ view: UIView, identifier: String) { |
| 14 | var list = (entries[identifier] ?? []).filter { $0.view != nil && $0.view !== view } |
| 15 | list.append(Entry(view)) |
| 16 | entries[identifier] = list |
| 17 | } |
| 18 | |
| 19 | func remove(_ view: UIView, identifier: String) { |
| 20 | let list = (entries[identifier] ?? []).filter { $0.view != nil && $0.view !== view } |
| 21 | entries[identifier] = list.isEmpty ? nil : list |
| 22 | } |
| 23 | |
| 24 | func view(for identifier: String) -> UIView? { |
| 25 | let views = (entries[identifier] ?? []).compactMap(\.view) |
| 26 | // Prefer an on-screen copy, the latest first; use the child bounds, not the wrapper's stretched ones. |
| 27 | let source = views.last(where: { view in |
| 28 | guard let window = view.window else { return false } |
| 29 | return window.bounds.intersects(view.convert(view.bounds, to: window)) |
| 30 | }) ?? views.last |
| 31 | return source?.subviews.first |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | final class T3PresentationSourceView: ExpoView { |
| 36 | weak var sources: T3PresentationSources? |
| 37 | var identifier = "" { |
| 38 | didSet { |
| 39 | sources?.remove(self, identifier: oldValue) |
| 40 | if !identifier.isEmpty { sources?.register(self, identifier: identifier) } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | deinit { |
| 45 | sources?.remove(self, identifier: identifier) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | final class T3ContextSheetSizeView: ExpoView { |
| 50 | var contentHeight: CGFloat = 0 { |
| 51 | didSet { updateSheet() } |
| 52 | } |
| 53 | private weak var configuredSheet: UISheetPresentationController? |
| 54 | private var appliedHeight: CGFloat = 0 |
| 55 | |
| 56 | override func didMoveToWindow() { |
| 57 | super.didMoveToWindow() |
| 58 | // The modal's presentation controller is attached after the content view. |
| 59 | DispatchQueue.main.async { [weak self] in self?.updateSheet() } |
| 60 | } |
| 61 | |
| 62 | override func layoutSubviews() { |
| 63 | super.layoutSubviews() |
| 64 | updateSheet() |
| 65 | } |
| 66 | |
| 67 | private func updateSheet() { |
| 68 | guard window != nil, contentHeight > 0 else { return } |
| 69 | var responder: UIResponder? = self |
| 70 | while let current = responder { |
| 71 | if let controller = current as? UIViewController, |
| 72 | controller.presentingViewController != nil, |
| 73 | let sheet = controller.sheetPresentationController { |
| 74 | guard configuredSheet !== sheet || abs(appliedHeight - contentHeight) > 1 else { return } |
| 75 | configuredSheet = sheet |
| 76 | appliedHeight = contentHeight |
| 77 | let height = contentHeight |
| 78 | let identifier = UISheetPresentationController.Detent.Identifier("t3-context-content") |
| 79 | sheet.animateChanges { |
| 80 | sheet.detents = [.custom(identifier: identifier) { context in |
| 81 | min(height, context.maximumDetentValue * 0.92) |
| 82 | }] |
| 83 | sheet.selectedDetentIdentifier = identifier |
| 84 | sheet.prefersGrabberVisible = true |
| 85 | sheet.prefersScrollingExpandsWhenScrolledToEdge = false |
| 86 | } |
| 87 | return |
| 88 | } |
| 89 | responder = current.next |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func presentFileShare( |
| 95 | url: URL, |
| 96 | title: String, |
| 97 | source: UIView?, |
| 98 | presenter: UIViewController, |
| 99 | promise: Promise |
| 100 | ) throws { |
| 101 | guard url.isFileURL, FileManager.default.isReadableFile(atPath: url.path) else { |
| 102 | throw NSError( |
| 103 | domain: "T3NativePresentation", |
| 104 | code: 1, |
| 105 | userInfo: [NSLocalizedDescriptionKey: "The file is no longer available."] |
| 106 | ) |
| 107 | } |
| 108 | |
| 109 | guard let origin = source ?? presenter.view else { |
| 110 | throw NSError( |
| 111 | domain: "T3NativePresentation", |
| 112 | code: 2, |
| 113 | userInfo: [NSLocalizedDescriptionKey: "The presenting screen is no longer open."] |
| 114 | ) |
| 115 | } |
| 116 | |
| 117 | let activity = UIActivityViewController(activityItems: [url], applicationActivities: nil) |
| 118 | activity.title = title |
| 119 | activity.overrideUserInterfaceStyle = source?.traitCollection.userInterfaceStyle |
| 120 | ?? presenter.traitCollection.userInterfaceStyle |
| 121 | activity.completionWithItemsHandler = { _, _, _, _ in promise.resolve(nil) } |
| 122 | if presenter.traitCollection.userInterfaceIdiom == .pad { |
| 123 | activity.popoverPresentationController?.sourceView = origin |
| 124 | activity.popoverPresentationController?.sourceRect = source?.bounds |
| 125 | ?? CGRect(x: origin.bounds.midX, y: origin.bounds.midY, width: 1, height: 1) |
| 126 | } else { |
| 127 | // Let UIKit adapt the remote share scene to the phone, not an anchored popover. |
| 128 | activity.modalPresentationStyle = .automatic |
| 129 | } |
| 130 | presenter.present(activity, animated: true) |
| 131 | } |