Skip to content

Commit

Permalink
Merge pull request #17 from hugehoge/customize-swiftui-introspect
Browse files Browse the repository at this point in the history
Fix the timing of introspecting UIScrollView
  • Loading branch information
hugehoge authored Feb 20, 2022
2 parents 3047f04 + c40601f commit 860aac5
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 45 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Changelog

### Feature

### Changed & Fixed

- Fix the timing of introspecting UIScrollView [#17](https://github.com/hugehoge/Snappable/pull/17)

### Changed

### Fixed
Expand Down
15 changes: 15 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


This library includes the copied code from Introspect for SwiftUI, hosted at
https://github.com/siteline/SwiftUI-Introspect.

Here is the original license of Introspect for SwiftUI:


Copyright 2019 Timber Software

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 changes: 0 additions & 16 deletions Package.resolved

This file was deleted.

10 changes: 2 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ let package = Package(
targets: ["Snappable"]
),
],
dependencies: [
.package(
name: "Introspect",
url: "https://github.com/siteline/SwiftUI-Introspect.git",
from: "0.1.3"
),
],
dependencies: [],
targets: [
.target(
name: "Snappable",
dependencies: ["Introspect"],
dependencies: [],
path: "Sources"
),
.testTarget(
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ The goal of this library is to provide an easy way to implement Views such as ca

### Note

**Snappable depends on [Introspect for SwiftUI](https://github.com/siteline/SwiftUI-Introspect) due to detect the behavior of scrolling from UIScrollView, so this is fragile on iOS or SwiftUI upates.**
**Snappable includes the copied code from [Introspect for SwiftUI](https://github.com/siteline/SwiftUI-Introspect) ([#17](https://github.com/hugehoge/Snappable/pull/17)) due to detect the behavior of scrolling from UIScrollView.**
**So this library would be fragile on iOS or SwiftUI updates.**

## Installation

Expand Down
2 changes: 0 additions & 2 deletions Snappable.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ Pod::Spec.new do |s|
s.ios.deployment_target = '14.0'
s.swift_versions = ['5.3', '5.4', '5.5']
s.source_files = 'Sources/**/*.swift'

s.dependency 'Introspect', '~> 0.1.3'
end
16 changes: 0 additions & 16 deletions Snappable.xcworkspace/xcshareddata/swiftpm/Package.resolved

This file was deleted.

2 changes: 0 additions & 2 deletions Sources/Internal/ViewModifiers/SnappableModifier.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import SwiftUI

import Introspect

internal struct SnappableModifier: ViewModifier {
private let snapAlignment: SnapAlignment
private let snapMode: SnapMode
Expand Down
85 changes: 85 additions & 0 deletions Sources/Introspect/Introspect.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import SwiftUI

typealias PlatformView = UIView

/// Utility methods to inspect the UIKit view hierarchy.
enum Introspect {
/// Finds a previous sibling that is of the specified type.
/// This method inspects siblings recursively.
/// Returns nil if no sibling contains the specified type.
static func previousSibling<AnyViewType: PlatformView>(
ofType type: AnyViewType.Type,
from entry: PlatformView
) -> AnyViewType? {

guard let superview = entry.superview,
let entryIndex = superview.subviews.firstIndex(of: entry),
entryIndex > 0
else {
return nil
}

for subview in superview.subviews[0..<entryIndex].reversed() {
if let typed = subview as? AnyViewType {
return typed
}
}

return nil
}

/// Finds an ancestor of the specified type.
/// If it reaches the top of the view without finding the specified view type, it returns nil.
static func findAncestor<AnyViewType: PlatformView>(ofType type: AnyViewType.Type, from entry: PlatformView) -> AnyViewType? {
var superview = entry.superview
while let s = superview {
if let typed = s as? AnyViewType {
return typed
}
superview = s.superview
}
return nil
}

/// Finds the view host of a specific view.
/// SwiftUI wraps each UIView within a ViewHost, then within a HostingView.
/// Returns nil if it couldn't find a view host. This should never happen when called with an IntrospectionView.
static func findViewHost(from entry: PlatformView) -> PlatformView? {
var superview = entry.superview
while let s = superview {
if NSStringFromClass(type(of: s)).contains("ViewHost") {
return s
}
superview = s.superview
}
return nil
}
}

enum TargetViewSelector {
static func siblingOfType<TargetView: PlatformView>(from entry: PlatformView) -> TargetView? {
guard let viewHost = Introspect.findViewHost(from: entry) else {
return nil
}
return Introspect.previousSibling(ofType: TargetView.self, from: viewHost)
}

static func siblingOfTypeOrAncestor<TargetView: PlatformView>(from entry: PlatformView) -> TargetView? {
if let sibling: TargetView = siblingOfType(from: entry) {
return sibling
}
return Introspect.findAncestor(ofType: TargetView.self, from: entry)
}
}

/// Allows to safely access an array element by index
/// Usage: array[safe: 2]
private extension Array {
subscript(safe index: Int) -> Element? {
guard index >= 0, index < endIndex else {
return nil
}

return self[index]
}
}
68 changes: 68 additions & 0 deletions Sources/Introspect/UIKitIntrospectionView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import UIKit
import SwiftUI

/// Introspection UIView that is inserted alongside the target view.
class IntrospectionUIView: UIView {
var didMoveToWindowHandler: (() -> Void)?

required init() {
super.init(frame: .zero)
isHidden = true
isUserInteractionEnabled = false
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func didMoveToWindow() {
didMoveToWindowHandler?()
}
}

/// Introspection View that is injected into the UIKit hierarchy alongside the target view.
/// After `updateUIView` is called, it calls `selector` to find the target view, then `customize` when the target view is found.
struct UIKitIntrospectionView<TargetViewType: UIView>: UIViewRepresentable {

/// Method that introspects the view hierarchy to find the target view.
/// First argument is the introspection view itself, which is contained in a view host alongside the target view.
let selector: (IntrospectionUIView) -> TargetViewType?

/// User-provided customization method for the target view.
let customize: (TargetViewType) -> Void

init(
selector: @escaping (IntrospectionUIView) -> TargetViewType?,
customize: @escaping (TargetViewType) -> Void
) {
self.selector = selector
self.customize = customize
}

func makeUIView(context: UIViewRepresentableContext<UIKitIntrospectionView>) -> IntrospectionUIView {
let view = IntrospectionUIView()
view.accessibilityLabel = "IntrospectionUIView<\(TargetViewType.self)>"
return view
}

/// When `updateUiView` is called after creating the Introspection view, it is not yet in the UIKit hierarchy.
/// At this point, `introspectionView.superview.superview` is nil and we can't access the target UIKit view.
/// To workaround this, we wait until the introspection view did attach to the window and the runloop is done
/// inserting the introspection view in the hierarchy, then run the selector.
/// Finding the target view fails silently if the selector yield no result. This happens when `updateUIView`
/// gets called when the introspection view gets removed from the hierarchy.
func updateUIView(
_ uiView: IntrospectionUIView,
context: UIViewRepresentableContext<UIKitIntrospectionView>
) {
uiView.didMoveToWindowHandler = {
DispatchQueue.main.async {
guard let targetView = self.selector(uiView) else {
return
}
self.customize(targetView)
}
}
}
}
26 changes: 26 additions & 0 deletions Sources/Introspect/ViewExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import SwiftUI
import UIKit

extension View {
func inject<SomeView>(_ view: SomeView) -> some View where SomeView: View {
overlay(view.frame(width: 0, height: 0))
}
}

extension View {
/// Finds a `TargetView` from a `SwiftUI.View`
func introspect<TargetView: UIView>(
selector: @escaping (IntrospectionUIView) -> TargetView?,
customize: @escaping (TargetView) -> ()
) -> some View {
inject(UIKitIntrospectionView(
selector: selector,
customize: customize
))
}

/// Finds a `UIScrollView` from a `SwiftUI.ScrollView`, or `SwiftUI.ScrollView` child.
func introspectScrollView(customize: @escaping (UIScrollView) -> ()) -> some View {
return introspect(selector: TargetViewSelector.siblingOfTypeOrAncestor, customize: customize)
}
}

0 comments on commit 860aac5

Please sign in to comment.