Add pull to refresh for Subscriptions, Popular and Trending (fixes #31)

This commit is contained in:
Arkadiusz Fal
2022-01-05 17:25:57 +01:00
parent 1db4a3197d
commit 363424fa74
13 changed files with 527 additions and 67 deletions

View 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
}
}

View 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 = 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)")
}
}

47
Vendor/RefreshControl/README vendored Normal file
View File

@@ -0,0 +1,47 @@
https://github.com/Geri-Borbas/iOS.Blog.SwiftUI_Pull_to_Refresh
# SwiftUI Pull to Refresh
⇣ SwiftUI Pull to Refresh (for iOS 13 and iOS 14) condensed into a single modifier.
Complementary repository for article [**SwiftUI Pull to Refresh**] (in progress). See [`ContentView.swift`] for usage, and [`RefreshControlModifier.swift`] for the source. Designed to work with **multiple scroll views** on the same screen.
```Swift
struct ContentView: View {
var body: some View {
VStack {
HStack {
List {
ForEach(1...100, id: \.self) { eachRowIndex in
Text("Left \(eachRowIndex)")
}
}
.refreshControl { refreshControl in
Network.refresh {
refreshControl.endRefreshing()
}
}
List {
ForEach(1...100, id: \.self) { eachRowIndex in
Text("Right \(eachRowIndex)")
}
}
.refreshControl { refreshControl in
Network.refresh {
refreshControl.endRefreshing()
}
}
}
}
}
}
```
## License
> Licensed under the [**MIT License**](https://en.wikipedia.org/wiki/MIT_License).
[`ContentView.swift`]: SwiftUI_Pull_to_Refresh/Views/ContentView.swift
[`RefreshControl.swift`]: SwiftUI_Pull_to_Refresh/Views/RefreshControl.swift

View 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
}
}

View File

@@ -0,0 +1,42 @@
//
// 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 {
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))
}
}

View 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()
}
}

View 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 = 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 = 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)
}
}