Add base implementation of auto loading example.

This commit is contained in:
Yoshinori Sano 2015-09-29 13:31:48 +09:00
parent e4bb5bf61d
commit 30dc3b84af
4 changed files with 192 additions and 5 deletions

View File

@ -294,6 +294,7 @@
C8DF92EA1B0B38C0009BCF9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C8DF92E91B0B38C0009BCF9A /* Images.xcassets */; };
C8DF92EB1B0B38C0009BCF9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C8DF92E91B0B38C0009BCF9A /* Images.xcassets */; };
C8DF92F61B0B43A4009BCF9A /* IntroductionExampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8DF92F51B0B43A4009BCF9A /* IntroductionExampleViewController.swift */; };
EC91FB951BBA144400973245 /* GitHubSearchRepositoriesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC91FB941BBA144400973245 /* GitHubSearchRepositoriesViewController.swift */; settings = {ASSET_TAGS = (); }; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
@ -564,6 +565,7 @@
C8DF92F01B0B3E67009BCF9A /* Info-OSX.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Info-OSX.plist"; sourceTree = "<group>"; };
C8DF92F21B0B3E71009BCF9A /* Info-iOS.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Info-iOS.plist"; sourceTree = "<group>"; };
C8DF92F51B0B43A4009BCF9A /* IntroductionExampleViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = IntroductionExampleViewController.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
EC91FB941BBA144400973245 /* GitHubSearchRepositoriesViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GitHubSearchRepositoriesViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -1074,6 +1076,7 @@
07A5C3D91B70B6B8001EFE5C /* 06 Calculator */,
C859B9A21B45C5D900D012D7 /* 07 PartialUpdates */,
C8A57F711B40AF4E00D5570A /* 08 CoreData */,
EC91FB931BBA12E800973245 /* 09 AutoLoading */,
);
path = Examples;
sourceTree = "<group>";
@ -1197,6 +1200,15 @@
path = Introduction;
sourceTree = "<group>";
};
EC91FB931BBA12E800973245 /* 09 AutoLoading */ = {
isa = PBXGroup;
children = (
EC91FB941BBA144400973245 /* GitHubSearchRepositoriesViewController.swift */,
);
name = "09 AutoLoading";
path = AutoLoading;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@ -1569,6 +1581,7 @@
C83367251AD029AE00C668A7 /* ImageService.swift in Sources */,
C86E2F471AE5A0CA00C31024 /* WikipediaSearchResult.swift in Sources */,
C890A65A1AEBD28A00AFF7E6 /* GitHubAPI.swift in Sources */,
EC91FB951BBA144400973245 /* GitHubSearchRepositoriesViewController.swift in Sources */,
C8A2A2C81B4049E300F11F09 /* PseudoRandomGenerator.swift in Sources */,
C84B91381B8A282000C9CCCF /* RxTableViewSectionedAnimatedDataSource.swift in Sources */,
C88C78721B3EB0A00061C5AB /* SectionedViewType.swift in Sources */,

View File

@ -0,0 +1,119 @@
//
// GitHubSearchRepositoriesViewController.swift
// RxExample
//
// Created by yoshinori_sano on 9/29/15.
// Copyright (c) 2015 Krunoslav Zaher. All rights reserved.
//
import UIKit
#if !RX_NO_MODULE
import RxSwift
import RxCocoa
#endif
struct Repository: CustomStringConvertible {
var name: String
init(name: String) {
self.name = name
}
var description: String {
return name
}
}
class GitHubSearchRepositoriesAPI {
static let sharedAPI = GitHubSearchRepositoriesAPI()
private init() {}
func search() -> Observable<[Repository]> {
let url = NSURL(string: "https://api.github.com/search/repositories?q=othello")!
return NSURLSession.sharedSession().rx_JSON(url)
.observeOn(Dependencies.sharedDependencies.backgroundWorkScheduler)
.map { json in
guard let json = json as? [String: AnyObject] else {
throw exampleError("Casting to dictionary failed")
}
return try self.parseJSON(json)
}
.observeOn(Dependencies.sharedDependencies.mainScheduler)
}
private func parseJSON(json: [String: AnyObject]) throws -> [Repository] {
guard let items = json["items"] as? [[String: AnyObject]] else {
throw exampleError("Can't find results")
}
return try items.map { item in
guard let name = item["name"] as? String else {
throw exampleError("Can't parse repository")
}
return Repository(name: name)
}
}
}
class GitHubSearchRepositoriesViewController: ViewController, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var disposeBag = DisposeBag()
let repositories = Variable([Repository]())
let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String, Repository>>()
override func viewDidLoad() {
super.viewDidLoad()
let allRepositories = repositories
.map { repositories in
return [SectionModel(model: "Repositories", items: repositories)]
}
dataSource.cellFactory = { (tv, ip, user: Repository) in
let cell = tv.dequeueReusableCellWithIdentifier("Cell")!
cell.textLabel?.text = user.name
return cell
}
dataSource.titleForHeaderInSection = { [unowned dataSource] sectionIndex in
return dataSource.sectionAtIndex(sectionIndex).model
}
// reactive data source
allRepositories
.bindTo(tableView.rx_itemsWithDataSource(dataSource))
.addDisposableTo(disposeBag)
GitHubSearchRepositoriesAPI.sharedAPI.search()
.subscribeNext { [unowned self] array in
self.repositories.value = array
}
.addDisposableTo(disposeBag)
}
override func setEditing(editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
tableView.editing = editing
}
// MARK: Table view delegate
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let title = dataSource.sectionAtIndex(section)
let label = UILabel(frame: CGRect.zero)
label.text = " \(title)"
label.textColor = UIColor.whiteColor()
label.backgroundColor = UIColor.darkGrayColor()
label.alpha = 0.9
return label
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6751" systemVersion="14C1514" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="8191" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6736"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
</dependencies>
<objects>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8187.4" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="E5v-jn-n2n">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="E5v-jn-n2n">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8151.3"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
<capability name="Aspect ratio constraints" minToolsVersion="5.1"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
@ -23,6 +23,34 @@
</objects>
<point key="canvasLocation" x="-814.39999999999998" y="75.733333333333334"/>
</scene>
<!--Git Hub Search Repositories View Controller-->
<scene sceneID="T9S-qN-XdP">
<objects>
<tableViewController id="cUc-Zm-HOf" customClass="GitHubSearchRepositoriesViewController" customModule="RxExample_iOS" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="nwe-iR-nbz">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Cell" id="rON-a5-sdH">
<rect key="frame" x="0.0" y="92" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="rON-a5-sdH" id="Vgz-Eb-3T3">
<rect key="frame" x="0.0" y="0.0" width="320" height="43"/>
<autoresizingMask key="autoresizingMask"/>
</tableViewCellContentView>
</tableViewCell>
</prototypes>
</tableView>
<navigationItem key="navigationItem" id="Dx0-YN-Pwz"/>
<connections>
<outlet property="tableView" destination="nwe-iR-nbz" id="o8o-DW-fJo"/>
</connections>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="32k-KY-2be" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-656" y="805"/>
</scene>
<!--Table View Controller-->
<scene sceneID="Mhm-lU-Uhj">
<objects>
@ -487,12 +515,39 @@
<segue destination="J6V-0T-aRq" kind="push" id="jyD-mL-MWs"/>
</connections>
</tableViewCell>
<tableViewCell contentMode="scaleToFill" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" textLabel="8FC-s3-ejV" detailTextLabel="ECT-7x-66c" style="IBUITableViewCellStyleSubtitle" id="0Xj-JL-bdb">
<rect key="frame" x="0.0" y="334" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="0Xj-JL-bdb" id="buE-3J-RLs">
<rect key="frame" x="0.0" y="0.0" width="287" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="GitHub Search Repositories" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="8FC-s3-ejV">
<rect key="frame" x="15" y="5" width="200" height="20"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Auto Loading Example" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="ECT-7x-66c">
<rect key="frame" x="15" y="25" width="117" height="14"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="11"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
<connections>
<segue destination="cUc-Zm-HOf" kind="push" id="ADd-I9-9RO"/>
</connections>
</tableViewCell>
</cells>
</tableViewSection>
<tableViewSection headerTitle="iPad Examples" footerTitle="Showcase examples for Rx. You can easily test for proper resource cleanup during popping of the navigation stack" id="dLK-dJ-eIx">
<cells>
<tableViewCell contentMode="scaleToFill" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" textLabel="vX5-dK-JyH" detailTextLabel="Ilb-8Z-x8X" style="IBUITableViewCellStyleSubtitle" id="i2V-3v-lig">
<rect key="frame" x="0.0" y="433" width="320" height="44"/>
<rect key="frame" x="0.0" y="477" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="i2V-3v-lig" id="ysT-9y-Klh">
<rect key="frame" x="0.0" y="0.0" width="287" height="43"/>