This commit is contained in:
DDIsFriend
2023-08-18 17:28:57 +08:00
commit f0e8a1709d
4282 changed files with 192396 additions and 0 deletions

View File

@@ -0,0 +1,108 @@
//
// RxCollectionViewReactiveArrayDataSource.swift
// RxCocoa
//
// Created by Krunoslav Zaher on 6/29/15.
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
//
#if os(iOS) || os(tvOS)
import UIKit
import RxSwift
// objc monkey business
class _RxCollectionViewReactiveArrayDataSource
: NSObject
, UICollectionViewDataSource {
@objc(numberOfSectionsInCollectionView:)
func numberOfSections(in: UICollectionView) -> Int {
1
}
func _collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
0
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
_collectionView(collectionView, numberOfItemsInSection: section)
}
fileprivate func _collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
rxAbstractMethod()
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
_collectionView(collectionView, cellForItemAt: indexPath)
}
}
class RxCollectionViewReactiveArrayDataSourceSequenceWrapper<Sequence: Swift.Sequence>
: RxCollectionViewReactiveArrayDataSource<Sequence.Element>
, RxCollectionViewDataSourceType {
typealias Element = Sequence
override init(cellFactory: @escaping CellFactory) {
super.init(cellFactory: cellFactory)
}
func collectionView(_ collectionView: UICollectionView, observedEvent: Event<Sequence>) {
Binder(self) { collectionViewDataSource, sectionModels in
let sections = Array(sectionModels)
collectionViewDataSource.collectionView(collectionView, observedElements: sections)
}.on(observedEvent)
}
}
// Please take a look at `DelegateProxyType.swift`
class RxCollectionViewReactiveArrayDataSource<Element>
: _RxCollectionViewReactiveArrayDataSource
, SectionedViewDataSourceType {
typealias CellFactory = (UICollectionView, Int, Element) -> UICollectionViewCell
var itemModels: [Element]?
func modelAtIndex(_ index: Int) -> Element? {
itemModels?[index]
}
func model(at indexPath: IndexPath) throws -> Any {
precondition(indexPath.section == 0)
guard let item = itemModels?[indexPath.item] else {
throw RxCocoaError.itemsNotYetBound(object: self)
}
return item
}
var cellFactory: CellFactory
init(cellFactory: @escaping CellFactory) {
self.cellFactory = cellFactory
}
// data source
override func _collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
itemModels?.count ?? 0
}
override func _collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cellFactory(collectionView, indexPath.item, itemModels![indexPath.item])
}
// reactive
func collectionView(_ collectionView: UICollectionView, observedElements: [Element]) {
self.itemModels = observedElements
collectionView.reloadData()
// workaround for http://stackoverflow.com/questions/39867325/ios-10-bug-uicollectionview-received-layout-attributes-for-a-cell-with-an-index
collectionView.collectionViewLayout.invalidateLayout()
}
}
#endif

View File

@@ -0,0 +1,92 @@
//
// RxPickerViewAdapter.swift
// RxCocoa
//
// Created by Sergey Shulga on 12/07/2017.
// Copyright © 2017 Krunoslav Zaher. All rights reserved.
//
#if os(iOS)
import UIKit
import RxSwift
class RxPickerViewArrayDataSource<T>: NSObject, UIPickerViewDataSource, SectionedViewDataSourceType {
fileprivate var items: [T] = []
func model(at indexPath: IndexPath) throws -> Any {
guard items.indices ~= indexPath.row else {
throw RxCocoaError.itemsNotYetBound(object: self)
}
return items[indexPath.row]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
items.count
}
}
class RxPickerViewSequenceDataSource<Sequence: Swift.Sequence>
: RxPickerViewArrayDataSource<Sequence.Element>
, RxPickerViewDataSourceType {
typealias Element = Sequence
func pickerView(_ pickerView: UIPickerView, observedEvent: Event<Sequence>) {
Binder(self) { dataSource, items in
dataSource.items = items
pickerView.reloadAllComponents()
}
.on(observedEvent.map(Array.init))
}
}
final class RxStringPickerViewAdapter<Sequence: Swift.Sequence>
: RxPickerViewSequenceDataSource<Sequence>
, UIPickerViewDelegate {
typealias TitleForRow = (Int, Sequence.Element) -> String?
private let titleForRow: TitleForRow
init(titleForRow: @escaping TitleForRow) {
self.titleForRow = titleForRow
super.init()
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
titleForRow(row, items[row])
}
}
final class RxAttributedStringPickerViewAdapter<Sequence: Swift.Sequence>: RxPickerViewSequenceDataSource<Sequence>, UIPickerViewDelegate {
typealias AttributedTitleForRow = (Int, Sequence.Element) -> NSAttributedString?
private let attributedTitleForRow: AttributedTitleForRow
init(attributedTitleForRow: @escaping AttributedTitleForRow) {
self.attributedTitleForRow = attributedTitleForRow
super.init()
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
attributedTitleForRow(row, items[row])
}
}
final class RxPickerViewAdapter<Sequence: Swift.Sequence>: RxPickerViewSequenceDataSource<Sequence>, UIPickerViewDelegate {
typealias ViewForRow = (Int, Sequence.Element, UIView?) -> UIView
private let viewForRow: ViewForRow
init(viewForRow: @escaping ViewForRow) {
self.viewForRow = viewForRow
super.init()
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
viewForRow(row, items[row], view)
}
}
#endif

View File

@@ -0,0 +1,101 @@
//
// RxTableViewReactiveArrayDataSource.swift
// RxCocoa
//
// Created by Krunoslav Zaher on 6/26/15.
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
//
#if os(iOS) || os(tvOS)
import UIKit
import RxSwift
// objc monkey business
class _RxTableViewReactiveArrayDataSource
: NSObject
, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
1
}
func _tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
_tableView(tableView, numberOfRowsInSection: section)
}
fileprivate func _tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
rxAbstractMethod()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
_tableView(tableView, cellForRowAt: indexPath)
}
}
class RxTableViewReactiveArrayDataSourceSequenceWrapper<Sequence: Swift.Sequence>
: RxTableViewReactiveArrayDataSource<Sequence.Element>
, RxTableViewDataSourceType {
typealias Element = Sequence
override init(cellFactory: @escaping CellFactory) {
super.init(cellFactory: cellFactory)
}
func tableView(_ tableView: UITableView, observedEvent: Event<Sequence>) {
Binder(self) { tableViewDataSource, sectionModels in
let sections = Array(sectionModels)
tableViewDataSource.tableView(tableView, observedElements: sections)
}.on(observedEvent)
}
}
// Please take a look at `DelegateProxyType.swift`
class RxTableViewReactiveArrayDataSource<Element>
: _RxTableViewReactiveArrayDataSource
, SectionedViewDataSourceType {
typealias CellFactory = (UITableView, Int, Element) -> UITableViewCell
var itemModels: [Element]?
func modelAtIndex(_ index: Int) -> Element? {
itemModels?[index]
}
func model(at indexPath: IndexPath) throws -> Any {
precondition(indexPath.section == 0)
guard let item = itemModels?[indexPath.item] else {
throw RxCocoaError.itemsNotYetBound(object: self)
}
return item
}
let cellFactory: CellFactory
init(cellFactory: @escaping CellFactory) {
self.cellFactory = cellFactory
}
override func _tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
itemModels?.count ?? 0
}
override func _tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cellFactory(tableView, indexPath.item, itemModels![indexPath.row])
}
// reactive
func tableView(_ tableView: UITableView, observedElements: [Element]) {
self.itemModels = observedElements
tableView.reloadData()
}
}
#endif