Re-add image analysis interaction

See #450
This commit is contained in:
Shadowfacts 2024-03-20 11:49:00 -04:00
parent cf71fc3f98
commit a80cbe79c2
5 changed files with 57 additions and 0 deletions

View File

@ -15,6 +15,8 @@ public protocol GalleryContentViewController: UIViewController {
var caption: String? { get }
var bottomControlsAccessoryViewController: UIViewController? { get }
var canAnimateFromSourceView: Bool { get }
func setControlsVisible(_ visible: Bool, animated: Bool)
}
public extension GalleryContentViewController {
@ -25,4 +27,7 @@ public extension GalleryContentViewController {
var canAnimateFromSourceView: Bool {
true
}
func setControlsVisible(_ visible: Bool, animated: Bool) {
}
}

View File

@ -9,6 +9,8 @@ import Foundation
@MainActor
public protocol GalleryContentViewControllerContainer {
var galleryControlsVisible: Bool { get }
func setGalleryContentLoading(_ loading: Bool)
func galleryContentChanged()
func disableGalleryScrollAndZoom()

View File

@ -192,6 +192,8 @@ class GalleryItemViewController: UIViewController {
}
func addContent() {
content.setControlsVisible(controlsVisible, animated: false)
content.view.translatesAutoresizingMaskIntoConstraints = false
if content.parent != self {
addChild(content)
@ -229,6 +231,7 @@ class GalleryItemViewController: UIViewController {
func updateControlsViews() {
topControlsView.transform = CGAffineTransform(translationX: 0, y: visible ? 0 : -topControlsView.bounds.height)
bottomControlsView.transform = CGAffineTransform(translationX: 0, y: visible ? 0 : bottomControlsView.bounds.height)
content.setControlsVisible(visible, animated: animated)
}
if animated {
let animator = UIViewPropertyAnimator(duration: 0.2, timingParameters: UISpringTimingParameters())
@ -396,6 +399,10 @@ class GalleryItemViewController: UIViewController {
}
extension GalleryItemViewController: GalleryContentViewControllerContainer {
var galleryControlsVisible: Bool {
controlsVisible
}
func setGalleryContentLoading(_ loading: Bool) {
if loading {
if activityIndicator == nil {

View File

@ -10,6 +10,7 @@ import UIKit
import GalleryVC
import Pachyderm
import TuskerComponents
@preconcurrency import VisionKit
class ImageGalleryContentViewController: UIViewController, GalleryContentViewController {
let url: URL
@ -18,6 +19,12 @@ class ImageGalleryContentViewController: UIViewController, GalleryContentViewCon
let image: UIImage
let gifController: GIFController?
@available(iOS 16.0, macCatalyst 17.0, *)
private static let analyzer = ImageAnalyzer()
private var _analysisInteraction: AnyObject?
@available(iOS 16.0, macCatalyst 17.0, *)
private var analysisInteraction: ImageAnalysisInteraction? { _analysisInteraction as? ImageAnalysisInteraction }
init(url: URL, caption: String?, originalData: Data?, image: UIImage, gifController: GIFController?) {
self.url = url
self.caption = caption
@ -40,6 +47,7 @@ class ImageGalleryContentViewController: UIViewController, GalleryContentViewCon
let imageView = GIFImageView(image: image)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
imageView.isUserInteractionEnabled = true
view.addSubview(imageView)
NSLayoutConstraint.activate([
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
@ -51,6 +59,22 @@ class ImageGalleryContentViewController: UIViewController, GalleryContentViewCon
if let gifController {
gifController.attach(to: imageView)
}
if gifController == nil,
#available(iOS 16.0, macCatalyst 17.0, *) {
let interaction = ImageAnalysisInteraction(self)
self._analysisInteraction = interaction
interaction.preferredInteractionTypes = .automatic
imageView.addInteraction(interaction)
Task {
do {
let result = try await ImageGalleryContentViewController.analyzer.analyze(image, configuration: ImageAnalyzer.Configuration([.text, .machineReadableCode]))
interaction.analysis = result
} catch {
// if analysis fails, we just don't show anything
}
}
}
}
override func viewWillAppear(_ animated: Bool) {
@ -76,4 +100,18 @@ class ImageGalleryContentViewController: UIViewController, GalleryContentViewCon
return []
}
}
func setControlsVisible(_ visible: Bool, animated: Bool) {
if #available(iOS 16.0, macCatalyst 17.0, *),
let analysisInteraction {
analysisInteraction.setSupplementaryInterfaceHidden(!visible, animated: animated)
}
}
}
@available(iOS 16.0, macCatalyst 17.0, *)
extension ImageGalleryContentViewController: ImageAnalysisInteractionDelegate {
func interaction(_ interaction: ImageAnalysisInteraction, shouldBeginAt point: CGPoint, for interactionType: ImageAnalysisInteraction.InteractionTypes) -> Bool {
return container?.galleryControlsVisible ?? true
}
}

View File

@ -50,6 +50,7 @@ class LoadingGalleryContentViewController: UIViewController, GalleryContentViewC
if let wrapped = await provider() {
self.wrapped = wrapped
wrapped.container = container
wrapped.setControlsVisible(container?.galleryControlsVisible ?? false, animated: false)
addChild(wrapped)
wrapped.view.translatesAutoresizingMaskIntoConstraints = false
@ -99,4 +100,8 @@ class LoadingGalleryContentViewController: UIViewController, GalleryContentViewC
])
}
func setControlsVisible(_ visible: Bool, animated: Bool) {
wrapped?.setControlsVisible(visible, animated: animated)
}
}