Skip to content

Commit

Permalink
Add convenience UIPickerView rx extensions example
Browse files Browse the repository at this point in the history
  • Loading branch information
sergdort committed Jul 12, 2017
1 parent 508454f commit f55c3f6
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 108 deletions.
4 changes: 4 additions & 0 deletions RxExample/RxExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
2864D5F21D995FCD004F8484 /* Application+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2864D5F11D995FCD004F8484 /* Application+Extensions.swift */; };
2864D5F31D995FCD004F8484 /* Application+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2864D5F11D995FCD004F8484 /* Application+Extensions.swift */; };
8479BC721C3BDAD400FB8B54 /* ImagePickerController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8479BC701C3BCB9800FB8B54 /* ImagePickerController.swift */; };
A5CD038F1F1670E50005A376 /* CustomPickerViewAdapterExampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5CD038E1F1670E50005A376 /* CustomPickerViewAdapterExampleViewController.swift */; };
AE51C1C91DE735D8005BAF5F /* APIWrappers.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AE51C1C81DE735D8005BAF5F /* APIWrappers.storyboard */; };
AE51C1CB1DE735E3005BAF5F /* Calculator.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AE51C1CA1DE735E3005BAF5F /* Calculator.storyboard */; };
AE51C1CD1DE735EF005BAF5F /* Geolocation.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AE51C1CC1DE735EF005BAF5F /* Geolocation.storyboard */; };
Expand Down Expand Up @@ -432,6 +433,7 @@
252C9F791F14115B00F5F951 /* SimpleUIPickerViewExample.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = SimpleUIPickerViewExample.storyboard; sourceTree = "<group>"; };
2864D5F11D995FCD004F8484 /* Application+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Application+Extensions.swift"; sourceTree = "<group>"; };
8479BC701C3BCB9800FB8B54 /* ImagePickerController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImagePickerController.swift; sourceTree = "<group>"; };
A5CD038E1F1670E50005A376 /* CustomPickerViewAdapterExampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomPickerViewAdapterExampleViewController.swift; sourceTree = "<group>"; };
AE51C1C81DE735D8005BAF5F /* APIWrappers.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = APIWrappers.storyboard; sourceTree = "<group>"; };
AE51C1CA1DE735E3005BAF5F /* Calculator.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Calculator.storyboard; sourceTree = "<group>"; };
AE51C1CC1DE735EF005BAF5F /* Geolocation.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Geolocation.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -645,6 +647,7 @@
children = (
252C9F771F14111800F5F951 /* SimplePickerViewExampleViewController.swift */,
252C9F791F14115B00F5F951 /* SimpleUIPickerViewExample.storyboard */,
A5CD038E1F1670E50005A376 /* CustomPickerViewAdapterExampleViewController.swift */,
);
path = UIPickerViewExample;
sourceTree = "<group>";
Expand Down Expand Up @@ -1492,6 +1495,7 @@
C864BAD71C3332F10083833C /* DetailViewController.swift in Sources */,
C822B1DF1C14CEAA0088A01A /* BindingExtensions.swift in Sources */,
C864BAD91C3332F10083833C /* RandomUserAPI.swift in Sources */,
A5CD038F1F1670E50005A376 /* CustomPickerViewAdapterExampleViewController.swift in Sources */,
C886A68B1D85AC9400653EE4 /* UIImagePickerController+Rx.swift in Sources */,
C8477FF01D29DE8C0074454A /* AnimatableSectionModelType+ItemPath.swift in Sources */,
C864BADF1C3332F10083833C /* UIImageView+Extensions.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//
// CustomPickerViewAdapterExampleViewController.swift
// RxExample
//
// Created by Sergey Shulga on 12/07/2017.
// Copyright © 2017 Krunoslav Zaher. All rights reserved.
//

import UIKit
#if !RX_NO_MODULE
import RxSwift
import RxCocoa
#endif

final class CustomPickerViewAdapterExampleViewController: ViewController {
@IBOutlet weak var pickerView1: UIPickerView!
@IBOutlet weak var pickerView2: UIPickerView!
@IBOutlet weak var pickerView3: UIPickerView!

override func viewDidLoad() {
super.viewDidLoad()

Observable.just([[1, 2, 3], [5, 8, 13], [21, 34]])
.bind(to: pickerView1.rx.items(adapter: CustomStringPickerViewAdapter()))
.disposed(by: disposeBag)

Observable.just([[1, 2, 3], [5, 8, 13], [21, 34]])
.bind(to: pickerView2.rx.items(adapter: CustomAttributedStringPickerViewAdapter()))
.disposed(by: disposeBag)

Observable.just([[1, 2, 3], [5, 8, 13], [21, 34]])
.bind(to: pickerView3.rx.items(adapter: PickerViewViewAdapter()))
.disposed(by: disposeBag)
}
}

final class CustomStringPickerViewAdapter
: NSObject
, UIPickerViewDataSource
, UIPickerViewDelegate
, RxPickerViewDataSourceType {
typealias Element = [[CustomStringConvertible]]
private var items: [[CustomStringConvertible]] = []

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return items.count
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return items[component].count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return items[component][row].description
}

func pickerView(_ pickerView: UIPickerView, observedEvent: Event<Element>) {
UIBindingObserver(UIElement: self) { (adapter, items) in
adapter.items = items
pickerView.reloadAllComponents()
}.on(observedEvent)
}
}

final class CustomAttributedStringPickerViewAdapter
: NSObject
, UIPickerViewDataSource
, UIPickerViewDelegate
, RxPickerViewDataSourceType {
typealias Element = [[CustomStringConvertible]]
private var items: [[CustomStringConvertible]] = []

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return items.count
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return items[component].count
}

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
return NSAttributedString(string: items[component][row].description,
attributes: [
NSForegroundColorAttributeName: UIColor.cyan,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue
])
}

func pickerView(_ pickerView: UIPickerView, observedEvent: Event<Element>) {
UIBindingObserver(UIElement: self) { (adapter, items) in
adapter.items = items
pickerView.reloadAllComponents()
}.on(observedEvent)
}
}

final class PickerViewViewAdapter
: NSObject
, UIPickerViewDataSource
, UIPickerViewDelegate
, RxPickerViewDataSourceType {
typealias Element = [[CustomStringConvertible]]
private var items: [[CustomStringConvertible]] = []

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return items.count
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return items[component].count
}

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label = UILabel()
label.text = items[component][row].description
label.textColor = UIColor.orange
label.font = UIFont.preferredFont(forTextStyle: .headline)
label.textAlignment = .center
return label
}

func pickerView(_ pickerView: UIPickerView, observedEvent: Event<Element>) {
UIBindingObserver(UIElement: self) { (adapter, items) in
adapter.items = items
pickerView.reloadAllComponents()
}.on(observedEvent)
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -21,111 +21,28 @@ final class SimplePickerViewExampleViewController: ViewController {
override func viewDidLoad() {
super.viewDidLoad()

Observable.just([[1, 2, 3], [5, 8, 13], [21, 34]])
.bind(to: pickerView1.rx.items(adapter: CustomStringPickerViewAdapter()))
Observable.just([1, 2, 3])
.bind(to: pickerView1.rx.itemTitles) { _, item in
return "\(item)"
}
.disposed(by: disposeBag)

Observable.just([[1, 2, 3], [5, 8, 13], [21, 34]])
.bind(to: pickerView2.rx.items(adapter: CustomAttributedStringPickerViewAdapter()))
Observable.just([1, 2, 3])
.bind(to: pickerView2.rx.itemAttributedTitles) { _, item in
return NSAttributedString(string: "\(item)",
attributes: [
NSForegroundColorAttributeName: UIColor.cyan,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue
])
}
.disposed(by: disposeBag)

Observable.just([[1, 2, 3], [5, 8, 13], [21, 34]])
.bind(to: pickerView3.rx.items(adapter: PickerViewViewAdapter()))
Observable.just([UIColor.red, UIColor.green, UIColor.blue])
.bind(to: pickerView3.rx.items) { _, item, _ in
let view = UIView()
view.backgroundColor = item
return view
}
.disposed(by: disposeBag)
}
}

final class CustomStringPickerViewAdapter
: NSObject
, UIPickerViewDataSource
, UIPickerViewDelegate
, RxPickerViewDataSourceType {
typealias Element = [[CustomStringConvertible]]
private var items: [[CustomStringConvertible]] = []

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return items.count
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return items[component].count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return items[component][row].description
}

func pickerView(_ pickerView: UIPickerView, observedEvent: Event<Element>) {
UIBindingObserver(UIElement: self) { (adapter, items) in
adapter.items = items
pickerView.reloadAllComponents()
}.on(observedEvent)
}
}

final class CustomAttributedStringPickerViewAdapter
: NSObject
, UIPickerViewDataSource
, UIPickerViewDelegate
, RxPickerViewDataSourceType {
typealias Element = [[CustomStringConvertible]]
private var items: [[CustomStringConvertible]] = []

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return items.count
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return items[component].count
}

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
return NSAttributedString(string: items[component][row].description,
attributes: [
NSForegroundColorAttributeName: UIColor.cyan,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue
])
}

func pickerView(_ pickerView: UIPickerView, observedEvent: Event<Element>) {
UIBindingObserver(UIElement: self) { (adapter, items) in
adapter.items = items
pickerView.reloadAllComponents()
}.on(observedEvent)
}
}

final class PickerViewViewAdapter
: NSObject
, UIPickerViewDataSource
, UIPickerViewDelegate
, RxPickerViewDataSourceType {
typealias Element = [[CustomStringConvertible]]
private var items: [[CustomStringConvertible]] = []

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return items.count
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return items[component].count
}

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label = UILabel()
label.text = items[component][row].description
label.textColor = UIColor.orange
label.font = UIFont.preferredFont(forTextStyle: .headline)
label.textAlignment = .center
return label
}

func pickerView(_ pickerView: UIPickerView, observedEvent: Event<Element>) {
UIBindingObserver(UIElement: self) { (adapter, items) in
adapter.items = items
pickerView.reloadAllComponents()
}.on(observedEvent)
}
}


Loading

0 comments on commit f55c3f6

Please sign in to comment.