Skip to content

Commit

Permalink
Adapts collection and table view to use Swift 4.0 capabilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
kzaher committed Oct 8, 2017
1 parent 0c4c437 commit d91e4bf
Show file tree
Hide file tree
Showing 13 changed files with 328 additions and 375 deletions.
27 changes: 13 additions & 14 deletions Example/Example1_CustomizationUsingTableViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@ class CustomizationUsingTableViewDelegate : UIViewController {

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")

let dataSource = RxTableViewSectionedAnimatedDataSource<MySection>()
let dataSource = RxTableViewSectionedAnimatedDataSource<MySection>(
configureCell: { ds, tv, ip, item in
let cell = tv.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell.textLabel?.text = "Item \(item)"

return cell
},
titleForHeaderInSection: { ds, index in
return ds.sectionModels[index].header
}
)

dataSource.configureCell = { ds, tv, ip, item in
let cell = tv.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell.textLabel?.text = "Item \(item)"

return cell
}

dataSource.titleForHeaderInSection = { ds, index in
return ds.sectionModels[index].header
}
self.dataSource = dataSource

let sections = [
MySection(header: "First section", items: [
Expand All @@ -72,8 +73,6 @@ class CustomizationUsingTableViewDelegate : UIViewController {

tableView.rx.setDelegate(self)
.addDisposableTo(disposeBag)

self.dataSource = dataSource
}
}

Expand All @@ -88,6 +87,6 @@ extension CustomizationUsingTableViewDelegate : UITableViewDelegate {
return 0.0
}

return CGFloat(40 + item)
return CGFloat(40 + item * 10)
}
}
79 changes: 45 additions & 34 deletions Example/Example2_RandomizedSectionsAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ class ViewController: UIViewController {
return a.sections
}
.shareReplay(1)
let tvAnimatedDataSource = RxTableViewSectionedAnimatedDataSource<NumberSection>()
let reloadDataSource = RxTableViewSectionedReloadDataSource<NumberSection>()

skinTableViewDataSource(tvAnimatedDataSource)
skinTableViewDataSource(reloadDataSource)
let (configureCell, titleForSection) = ViewController.tableViewDataSourceUI()
let tvAnimatedDataSource = RxTableViewSectionedAnimatedDataSource<NumberSection>(
configureCell: configureCell,
titleForHeaderInSection: titleForSection
)
let reloadDataSource = RxTableViewSectionedReloadDataSource<NumberSection>(
configureCell: configureCell,
titleForHeaderInSection: titleForSection
)

randomSections
.bind(to: animatedTableView.rx.items(dataSource: tvAnimatedDataSource))
Expand All @@ -58,8 +63,11 @@ class ViewController: UIViewController {
.bind(to: tableView.rx.items(dataSource: reloadDataSource))
.addDisposableTo(disposeBag)

let cvAnimatedDataSource = RxCollectionViewSectionedAnimatedDataSource<NumberSection>()
skinCollectionViewDataSource(cvAnimatedDataSource)
let (configureCollectionViewCell, configureSupplementaryView) = ViewController.collectionViewDataSourceUI()
let cvAnimatedDataSource = RxCollectionViewSectionedAnimatedDataSource(
configureCell: configureCollectionViewCell,
configureSupplementaryView: configureSupplementaryView
)

randomSections
.bind(to: animatedCollectionView.rx.items(dataSource: cvAnimatedDataSource))
Expand All @@ -83,36 +91,39 @@ class ViewController: UIViewController {
// MARK: Skinning
extension ViewController {

func skinTableViewDataSource(_ dataSource: TableViewSectionedDataSource<NumberSection>) {
dataSource.configureCell = { (_, tv, ip, i) in
let cell = tv.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style:.default, reuseIdentifier: "Cell")

cell.textLabel!.text = "\(i)"

return cell
}

dataSource.titleForHeaderInSection = { (ds, section) -> String? in
return ds[section].header
}
static func tableViewDataSourceUI() -> (
TableViewSectionedDataSource<NumberSection>.ConfigureCell,
TableViewSectionedDataSource<NumberSection>.TitleForHeaderInSection
) {
return (
{ (_, tv, ip, i) in
let cell = tv.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style:.default, reuseIdentifier: "Cell")
cell.textLabel!.text = "\(i)"
return cell
},
{ (ds, section) -> String? in
return ds[section].header
}
)
}

func skinCollectionViewDataSource(_ dataSource: CollectionViewSectionedDataSource<NumberSection>) {
dataSource.configureCell = { (_, cv, ip, i) in
let cell = cv.dequeueReusableCell(withReuseIdentifier: "Cell", for: ip) as! NumberCell

cell.value!.text = "\(i)"

return cell
}

dataSource.supplementaryViewFactory = { (ds ,cv, kind, ip) in
let section = cv.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Section", for: ip) as! NumberSectionView

section.value!.text = "\(ds[ip.section].header)"

return section
}
static func collectionViewDataSourceUI() -> (
CollectionViewSectionedDataSource<NumberSection>.ConfigureCell,
CollectionViewSectionedDataSource<NumberSection>.ConfigureSupplementaryView
) {
return (
{ (_, cv, ip, i) in
let cell = cv.dequeueReusableCell(withReuseIdentifier: "Cell", for: ip) as! NumberCell
cell.value!.text = "\(i)"
return cell

},
{ (ds ,cv, kind, ip) in
let section = cv.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Section", for: ip) as! NumberSectionView
section.value!.text = "\(ds[ip.section].header)"
return section
}
)
}

// MARK: Initial value
Expand Down
50 changes: 24 additions & 26 deletions Example/Example3_TableViewEditing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class EditingExampleViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

let dataSource = RxTableViewSectionedAnimatedDataSource<NumberSection>()
let dataSource = EditingExampleViewController.dataSource()

let sections: [NumberSection] = [NumberSection(header: "Section 1", numbers: [], updated: Date()),
NumberSection(header: "Section 2", numbers: [], updated: Date()),
NumberSection(header: "Section 3", numbers: [], updated: Date())]
Expand All @@ -39,7 +40,6 @@ class EditingExampleViewController: UIViewController {
let movedCommand = tableView.rx.itemMoved
.map(TableViewEditingCommand.MoveItem)

skinTableViewDataSource(dataSource: dataSource)
Observable.of(addCommand, deleteCommand, movedCommand)
.merge()
.scan(initialState) { (state: SectionedTableViewState, command: TableViewEditingCommand) -> SectionedTableViewState in
Expand All @@ -58,31 +58,29 @@ class EditingExampleViewController: UIViewController {
super.viewDidAppear(animated)
tableView.setEditing(true, animated: true)
}

func skinTableViewDataSource(dataSource: RxTableViewSectionedAnimatedDataSource<NumberSection>) {

dataSource.animationConfiguration = AnimationConfiguration(insertAnimation: .top,
}

extension EditingExampleViewController {
static func dataSource() -> RxTableViewSectionedAnimatedDataSource<NumberSection> {
return RxTableViewSectionedAnimatedDataSource(
animationConfiguration: AnimationConfiguration(insertAnimation: .top,
reloadAnimation: .fade,
deleteAnimation: .left)

dataSource.configureCell = { (dataSource, table, idxPath, item) in
let cell = table.dequeueReusableCell(withIdentifier: "Cell", for: idxPath)

cell.textLabel?.text = "\(item)"

return cell
}

dataSource.titleForHeaderInSection = { (ds, section) -> String? in
return ds[section].header
}

dataSource.canEditRowAtIndexPath = { _, _ in
return true
}
dataSource.canMoveRowAtIndexPath = { _, _ in
return true
}
deleteAnimation: .left),
configureCell: { (dataSource, table, idxPath, item) in
let cell = table.dequeueReusableCell(withIdentifier: "Cell", for: idxPath)
cell.textLabel?.text = "\(item)"
return cell
},
titleForHeaderInSection: { (ds, section) -> String? in
return ds[section].header
},
canEditRowAtIndexPath: { _, _ in
return true
},
canMoveRowAtIndexPath: { _, _ in
return true
}
)
}
}

Expand Down
62 changes: 31 additions & 31 deletions Example/Example4_DifferentSectionAndItemTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,43 @@ class MultipleSectionModelViewController: UIViewController {
items: [.StepperSectionItem(title: "1")])
]

let dataSource = RxTableViewSectionedReloadDataSource<MultipleSectionModel>()

skinTableViewDataSource(dataSource)
let dataSource = MultipleSectionModelViewController.dataSource()

Observable.just(sections)
.bind(to: tableView.rx.items(dataSource: dataSource))
.addDisposableTo(disposeBag)
}

func skinTableViewDataSource(_ dataSource: RxTableViewSectionedReloadDataSource<MultipleSectionModel>) {
dataSource.configureCell = { (dataSource, table, idxPath, _) in
switch dataSource[idxPath] {
case let .ImageSectionItem(image, title):
let cell: ImageTitleTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
cell.titleLabel.text = title
cell.cellImageView.image = image

return cell
case let .StepperSectionItem(title):
let cell: TitleSteperTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
cell.titleLabel.text = title

return cell
case let .ToggleableSectionItem(title, enabled):
let cell: TitleSwitchTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
cell.switchControl.isOn = enabled
cell.titleLabel.text = title

return cell
}
}
}

dataSource.titleForHeaderInSection = { dataSource, index in
let section = dataSource[index]

return section.title
}
extension MultipleSectionModelViewController {
static func dataSource() -> RxTableViewSectionedReloadDataSource<MultipleSectionModel> {
return RxTableViewSectionedReloadDataSource<MultipleSectionModel>(
configureCell: { (dataSource, table, idxPath, _) in
switch dataSource[idxPath] {
case let .ImageSectionItem(image, title):
let cell: ImageTitleTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
cell.titleLabel.text = title
cell.cellImageView.image = image

return cell
case let .StepperSectionItem(title):
let cell: TitleSteperTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
cell.titleLabel.text = title

return cell
case let .ToggleableSectionItem(title, enabled):
let cell: TitleSwitchTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
cell.switchControl.isOn = enabled
cell.titleLabel.text = title

return cell
}
},
titleForHeaderInSection: { dataSource, index in
let section = dataSource[index]
return section.title
}
)
}
}

Expand Down
4 changes: 4 additions & 0 deletions RxDataSources.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
C82C3C961F3B939100309AE8 /* SectionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C82C3C701F3B938C00309AE8 /* SectionModel.swift */; };
C82C3C971F3B939100309AE8 /* SectionModelType.swift in Sources */ = {isa = PBXBuildFile; fileRef = C82C3C711F3B938C00309AE8 /* SectionModelType.swift */; };
C82C3C991F3B939100309AE8 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = C82C3C731F3B938C00309AE8 /* Utilities.swift */; };
C833338D1F8A5FAC00D46EAE /* Deprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = C833338C1F8A5FAC00D46EAE /* Deprecated.swift */; };
C87C34991F363B1400DB85FE /* RxSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C87C34321F36346A00DB85FE /* RxSwift.framework */; };
C87C349A1F363B1400DB85FE /* RxSwift.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = C87C34321F36346A00DB85FE /* RxSwift.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
C87C349D1F363B1400DB85FE /* RxCocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C87C343A1F36346A00DB85FE /* RxCocoa.framework */; };
Expand Down Expand Up @@ -365,6 +366,7 @@
C82C3C701F3B938C00309AE8 /* SectionModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SectionModel.swift; sourceTree = "<group>"; };
C82C3C711F3B938C00309AE8 /* SectionModelType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SectionModelType.swift; sourceTree = "<group>"; };
C82C3C731F3B938C00309AE8 /* Utilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
C833338C1F8A5FAC00D46EAE /* Deprecated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Deprecated.swift; sourceTree = "<group>"; };
C861C0F01E153FC400BEDC46 /* RxDataSources.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = RxDataSources.podspec; sourceTree = "<group>"; };
C87C34191F36346A00DB85FE /* Rx.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Rx.xcodeproj; path = RxSwift/Rx.xcodeproj; sourceTree = "<group>"; };
C87DF3431D0219A7006308C5 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -663,6 +665,7 @@
C8BBFBD41F3B8F8D00A225F7 /* RxTableViewSectionedReloadDataSource.swift */,
C8BBFBD51F3B8F8D00A225F7 /* TableViewSectionedDataSource.swift */,
C8BBFBD61F3B8F8D00A225F7 /* UI+SectionedViewType.swift */,
C833338C1F8A5FAC00D46EAE /* Deprecated.swift */,
);
path = RxDataSources;
sourceTree = "<group>";
Expand Down Expand Up @@ -1132,6 +1135,7 @@
C8BBFBE01F3B8F8D00A225F7 /* TableViewSectionedDataSource.swift in Sources */,
C8BBFBE11F3B8F8D00A225F7 /* UI+SectionedViewType.swift in Sources */,
C8BBFBDB1F3B8F8D00A225F7 /* RxCollectionViewSectionedReloadDataSource.swift in Sources */,
C833338D1F8A5FAC00D46EAE /* Deprecated.swift in Sources */,
C81FBF631F3B9DC00094061E /* IntegerType+IdentifiableType.swift in Sources */,
C8BBFBDD1F3B8F8D00A225F7 /* RxPickerViewAdapter.swift in Sources */,
);
Expand Down
36 changes: 18 additions & 18 deletions Sources/RxDataSources/AnimationConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
//

#if os(iOS) || os(tvOS)
import Foundation
import UIKit
import Foundation
import UIKit

/**
Exposes custom animation styles for insertion, deletion and reloading behavior.
*/
public struct AnimationConfiguration {
public let insertAnimation: UITableViewRowAnimation
public let reloadAnimation: UITableViewRowAnimation
public let deleteAnimation: UITableViewRowAnimation
public init(insertAnimation: UITableViewRowAnimation = .automatic,
reloadAnimation: UITableViewRowAnimation = .automatic,
deleteAnimation: UITableViewRowAnimation = .automatic) {
self.insertAnimation = insertAnimation
self.reloadAnimation = reloadAnimation
self.deleteAnimation = deleteAnimation
}
}
/**
Exposes custom animation styles for insertion, deletion and reloading behavior.
*/
public struct AnimationConfiguration {
public let insertAnimation: UITableViewRowAnimation
public let reloadAnimation: UITableViewRowAnimation
public let deleteAnimation: UITableViewRowAnimation

public init(insertAnimation: UITableViewRowAnimation = .automatic,
reloadAnimation: UITableViewRowAnimation = .automatic,
deleteAnimation: UITableViewRowAnimation = .automatic) {
self.insertAnimation = insertAnimation
self.reloadAnimation = reloadAnimation
self.deleteAnimation = deleteAnimation
}
}
#endif
Loading

0 comments on commit d91e4bf

Please sign in to comment.