mirror of
https://github.com/yattee/yattee.git
synced 2025-08-04 09:44:12 +00:00
Revert "Drop iOS 14 and macOS 11 support"
This reverts commit dcef7f47ff
.
This commit is contained in:
15
Vendor/RefreshControl/Extensions/UIResponder+Extensions.swift
vendored
Normal file
15
Vendor/RefreshControl/Extensions/UIResponder+Extensions.swift
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// UIResponder+Extensions.swift
|
||||
// SwiftUI_Pull_to_Refresh
|
||||
//
|
||||
// Created by Geri Borbás on 21/09/2021.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
extension UIResponder {
|
||||
var parentViewController: UIViewController? {
|
||||
next as? UIViewController ?? next?.parentViewController
|
||||
}
|
||||
}
|
70
Vendor/RefreshControl/Extensions/UIView+Extensions.swift
vendored
Normal file
70
Vendor/RefreshControl/Extensions/UIView+Extensions.swift
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// UIView+Extensions.swift
|
||||
// SwiftUI_Pull_to_Refresh
|
||||
//
|
||||
// Created by Geri Borbás on 19/09/2021.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
extension UIView {
|
||||
/// Returs frame in screen coordinates.
|
||||
var globalFrame: CGRect {
|
||||
if let window {
|
||||
return convert(bounds, to: window.screen.coordinateSpace)
|
||||
} else {
|
||||
return .zero
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns with all the instances of the given view type in the view hierarchy.
|
||||
func viewsInHierarchy<ViewType: UIView>() -> [ViewType]? {
|
||||
var views: [ViewType] = []
|
||||
viewsInHierarchy(views: &views)
|
||||
return views.isEmpty ? nil : views
|
||||
}
|
||||
|
||||
private func viewsInHierarchy<ViewType: UIView>(views: inout [ViewType]) {
|
||||
subviews.forEach { eachSubView in
|
||||
if let matchingView = eachSubView as? ViewType {
|
||||
views.append(matchingView)
|
||||
}
|
||||
eachSubView.viewsInHierarchy(views: &views)
|
||||
}
|
||||
}
|
||||
|
||||
/// Search ancestral view hierarcy for the given view type.
|
||||
func searchViewAnchestorsFor<ViewType: UIView>(
|
||||
_ onViewFound: (ViewType) -> Void
|
||||
) {
|
||||
if let matchingView = superview as? ViewType {
|
||||
onViewFound(matchingView)
|
||||
} else {
|
||||
superview?.searchViewAnchestorsFor(onViewFound)
|
||||
}
|
||||
}
|
||||
|
||||
/// Search ancestral view hierarcy for the given view type.
|
||||
func searchViewAnchestorsFor<ViewType: UIView>() -> ViewType? {
|
||||
if let matchingView = superview as? ViewType {
|
||||
return matchingView
|
||||
} else {
|
||||
return superview?.searchViewAnchestorsFor()
|
||||
}
|
||||
}
|
||||
|
||||
func printViewHierarchyInformation(_ level: Int = 0) {
|
||||
printViewInformation(level)
|
||||
subviews.forEach { $0.printViewHierarchyInformation(level + 1) }
|
||||
}
|
||||
|
||||
func printViewInformation(_ level: Int) {
|
||||
let leadingWhitespace = String(repeating: " ", count: level)
|
||||
let className = "\(Self.self)"
|
||||
let superclassName = "\(superclass!)"
|
||||
let frame = "\(self.frame)"
|
||||
let id = (accessibilityIdentifier == nil) ? "" : " `\(accessibilityIdentifier!)`"
|
||||
print("\(leadingWhitespace)\(className): \(superclassName)\(id) \(frame)")
|
||||
}
|
||||
}
|
56
Vendor/RefreshControl/RefreshControl.swift
vendored
Normal file
56
Vendor/RefreshControl/RefreshControl.swift
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// RefreshControl.swift
|
||||
// SwiftUI_Pull_to_Refresh
|
||||
//
|
||||
// Created by Geri Borbás on 18/09/2021.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import UIKit
|
||||
|
||||
final class RefreshControl: ObservableObject {
|
||||
static var navigationBarTitleDisplayMode: NavigationBarItem.TitleDisplayMode {
|
||||
if #available(iOS 15.0, *) {
|
||||
return .automatic
|
||||
}
|
||||
|
||||
return .inline
|
||||
}
|
||||
|
||||
let onValueChanged: (_ refreshControl: UIRefreshControl) -> Void
|
||||
|
||||
internal init(onValueChanged: @escaping ((UIRefreshControl) -> Void)) {
|
||||
self.onValueChanged = onValueChanged
|
||||
}
|
||||
|
||||
/// Adds a `UIRefreshControl` to the `UIScrollView` provided.
|
||||
func add(to scrollView: UIScrollView) {
|
||||
scrollView.refreshControl = UIRefreshControl().withTarget(
|
||||
self,
|
||||
action: #selector(onValueChangedAction),
|
||||
for: .valueChanged
|
||||
)
|
||||
.testable(as: "RefreshControl")
|
||||
}
|
||||
|
||||
@objc private func onValueChangedAction(sender: UIRefreshControl) {
|
||||
onValueChanged(sender)
|
||||
}
|
||||
}
|
||||
|
||||
extension UIRefreshControl {
|
||||
/// Convinience method to assign target action inline.
|
||||
func withTarget(_ target: Any?, action: Selector, for controlEvents: UIControl.Event) -> UIRefreshControl {
|
||||
addTarget(target, action: action, for: controlEvents)
|
||||
return self
|
||||
}
|
||||
|
||||
/// Convinience method to match refresh control for UI testing.
|
||||
func testable(as id: String) -> UIRefreshControl {
|
||||
isAccessibilityElement = true
|
||||
accessibilityIdentifier = id
|
||||
return self
|
||||
}
|
||||
}
|
46
Vendor/RefreshControl/RefreshControlModifier.swift
vendored
Normal file
46
Vendor/RefreshControl/RefreshControlModifier.swift
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// RefreshControlModifier.swift
|
||||
// SwiftUI_Pull_to_Refresh
|
||||
//
|
||||
// Created by Geri Borbás on 18/09/2021.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
struct RefreshControlModifier: ViewModifier {
|
||||
@State private var geometryReaderFrame: CGRect = .zero
|
||||
let refreshControl: RefreshControl
|
||||
|
||||
internal init(onValueChanged: @escaping (UIRefreshControl) -> Void) {
|
||||
refreshControl = RefreshControl(onValueChanged: onValueChanged)
|
||||
}
|
||||
|
||||
func body(content: Content) -> some View {
|
||||
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, *) {
|
||||
return content
|
||||
} else {
|
||||
return content
|
||||
.background(
|
||||
GeometryReader { geometry in
|
||||
ScrollViewMatcher(
|
||||
onResolve: { scrollView in
|
||||
refreshControl.add(to: scrollView)
|
||||
},
|
||||
geometryReaderFrame: $geometryReaderFrame
|
||||
)
|
||||
.preference(key: FramePreferenceKey.self, value: geometry.frame(in: .global))
|
||||
.onPreferenceChange(FramePreferenceKey.self) { frame in
|
||||
self.geometryReaderFrame = frame
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension View {
|
||||
func refreshControl(onValueChanged: @escaping (_ refreshControl: UIRefreshControl) -> Void) -> some View {
|
||||
modifier(RefreshControlModifier(onValueChanged: onValueChanged))
|
||||
}
|
||||
}
|
18
Vendor/RefreshControl/ScrollViewMatcher/FramePreferenceKey.swift
vendored
Normal file
18
Vendor/RefreshControl/ScrollViewMatcher/FramePreferenceKey.swift
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// FramePreferenceKey.swift
|
||||
// SwiftUI_Pull_to_Refresh
|
||||
//
|
||||
// Created by Geri Borbás on 21/09/2021.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
struct FramePreferenceKey: PreferenceKey {
|
||||
typealias Value = CGRect
|
||||
static var defaultValue = CGRect.zero
|
||||
|
||||
static func reduce(value: inout CGRect, nextValue: () -> CGRect) {
|
||||
value = nextValue()
|
||||
}
|
||||
}
|
106
Vendor/RefreshControl/ScrollViewMatcher/ScrollViewMatcher.swift
vendored
Normal file
106
Vendor/RefreshControl/ScrollViewMatcher/ScrollViewMatcher.swift
vendored
Normal file
@@ -0,0 +1,106 @@
|
||||
//
|
||||
// ScrollViewMatcher.swift
|
||||
// SwiftUI_Pull_to_Refresh
|
||||
//
|
||||
// Created by Geri Borbás on 17/09/2021.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
final class ScrollViewMatcher: UIViewControllerRepresentable {
|
||||
let onMatch: (UIScrollView) -> Void
|
||||
@Binding var geometryReaderFrame: CGRect
|
||||
|
||||
init(onResolve: @escaping (UIScrollView) -> Void, geometryReaderFrame: Binding<CGRect>) {
|
||||
onMatch = onResolve
|
||||
_geometryReaderFrame = geometryReaderFrame
|
||||
}
|
||||
|
||||
func makeUIViewController(context _: Context) -> ScrollViewMatcherViewController {
|
||||
ScrollViewMatcherViewController(onResolve: onMatch, geometryReaderFrame: geometryReaderFrame)
|
||||
}
|
||||
|
||||
func updateUIViewController(_ viewController: ScrollViewMatcherViewController, context _: Context) {
|
||||
viewController.geometryReaderFrame = geometryReaderFrame
|
||||
}
|
||||
}
|
||||
|
||||
final class ScrollViewMatcherViewController: UIViewController {
|
||||
let onMatch: (UIScrollView) -> Void
|
||||
private var scrollView: UIScrollView? {
|
||||
didSet {
|
||||
if oldValue != scrollView,
|
||||
let scrollView
|
||||
{
|
||||
onMatch(scrollView)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var geometryReaderFrame: CGRect {
|
||||
didSet {
|
||||
match()
|
||||
}
|
||||
}
|
||||
|
||||
init(onResolve: @escaping (UIScrollView) -> Void, geometryReaderFrame: CGRect, debug _: Bool = false) {
|
||||
onMatch = onResolve
|
||||
self.geometryReaderFrame = geometryReaderFrame
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
required init?(coder _: NSCoder) {
|
||||
fatalError("Use init(onMatch:) to instantiate ScrollViewMatcherViewController.")
|
||||
}
|
||||
|
||||
func match() {
|
||||
// matchUsingHierarchy()
|
||||
matchUsingGeometry()
|
||||
}
|
||||
|
||||
func matchUsingHierarchy() {
|
||||
if parent != nil {
|
||||
// Lookup view ancestry for any `UIScrollView`.
|
||||
view.searchViewAnchestorsFor { (scrollView: UIScrollView) in
|
||||
self.scrollView = scrollView
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func matchUsingGeometry() {
|
||||
if let parent {
|
||||
if let scrollViewsInHierarchy: [UIScrollView] = parent.view.viewsInHierarchy() {
|
||||
// Return first match if only a single scroll view is found in the hierarchy.
|
||||
if scrollViewsInHierarchy.count == 1,
|
||||
let firstScrollViewInHierarchy = scrollViewsInHierarchy.first
|
||||
{
|
||||
scrollView = firstScrollViewInHierarchy
|
||||
|
||||
// Filter by frame origins if multiple matches found.
|
||||
} else {
|
||||
if let firstMatchingFrameOrigin = scrollViewsInHierarchy.filter({
|
||||
$0.globalFrame.origin.close(to: geometryReaderFrame.origin)
|
||||
}).first {
|
||||
scrollView = firstMatchingFrameOrigin
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override func didMove(toParent parent: UIViewController?) {
|
||||
super.didMove(toParent: parent)
|
||||
match()
|
||||
}
|
||||
}
|
||||
|
||||
extension CGPoint {
|
||||
/// Returns `true` if this point is close the other point (considering a ~1 pt tolerance).
|
||||
func close(to point: CGPoint) -> Bool {
|
||||
let inset = Double(1)
|
||||
let rect = CGRect(x: x - inset, y: y - inset, width: inset * 2, height: inset * 2)
|
||||
return rect.contains(point)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user