mirror of
https://github.com/yattee/yattee.git
synced 2025-08-09 20:24:06 +00:00
Add pull to refresh for Subscriptions, Popular and Trending (fixes #31)
This commit is contained in:
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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user