Skip to content

Commit

Permalink
add userImage in feed
Browse files Browse the repository at this point in the history
  • Loading branch information
nishiyamaosamu committed Nov 27, 2015
1 parent 5006659 commit c028cf0
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 36 deletions.
6 changes: 5 additions & 1 deletion BondSample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
17B15C311C07D9DC003CE77B /* FeedTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17B15C301C07D9DC003CE77B /* FeedTableViewController.swift */; };
17B15C331C07DA47003CE77B /* FeedTableViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17B15C321C07DA47003CE77B /* FeedTableViewModel.swift */; };
17B15C351C07DB8E003CE77B /* Feed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17B15C341C07DB8E003CE77B /* Feed.swift */; };
17E561891C07F4D200967366 /* FeedTableCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17E561881C07F4D200967366 /* FeedTableCell.swift */; };
2E3DA46A04E907CD7D38BA98 /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9FB0F9F69C71791A4D60BE0A /* Pods.framework */; };
768288121C07549600A6CC5B /* BidirectionalBindingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 768288111C07549600A6CC5B /* BidirectionalBindingViewController.swift */; };
768288141C0754A100A6CC5B /* BidirectionalBinding.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 768288131C0754A100A6CC5B /* BidirectionalBinding.storyboard */; };
Expand Down Expand Up @@ -80,6 +81,7 @@
17B15C301C07D9DC003CE77B /* FeedTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FeedTableViewController.swift; sourceTree = "<group>"; };
17B15C321C07DA47003CE77B /* FeedTableViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FeedTableViewModel.swift; sourceTree = "<group>"; };
17B15C341C07DB8E003CE77B /* Feed.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Feed.swift; sourceTree = "<group>"; };
17E561881C07F4D200967366 /* FeedTableCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FeedTableCell.swift; sourceTree = "<group>"; };
768288111C07549600A6CC5B /* BidirectionalBindingViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BidirectionalBindingViewController.swift; sourceTree = "<group>"; };
768288131C0754A100A6CC5B /* BidirectionalBinding.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = BidirectionalBinding.storyboard; sourceTree = "<group>"; };
9FB0F9F69C71791A4D60BE0A /* Pods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -175,13 +177,13 @@
171D86E21C06A996005DB059 /* BondSample */ = {
isa = PBXGroup;
children = (
17B15C2D1C07D8FD003CE77B /* UITableView + feed with Alamofire */,
171D87111C06AAEC005DB059 /* Basic bind */,
171B5FB71C06B19700AA5EC2 /* Combine multiple inputs */,
171B5FBC1C06BFE200AA5EC2 /* Bind from ViewModel */,
171B5FC51C06CB4800AA5EC2 /* Observe a notification */,
171B5FD91C06DAD000AA5EC2 /* UITableView */,
768288101C07547B00A6CC5B /* Bidirectional Binding */,
17B15C2D1C07D8FD003CE77B /* UITableView + feed with Alamofire */,
171D86E31C06A996005DB059 /* AppDelegate.swift */,
171D86E51C06A996005DB059 /* ViewController.swift */,
171D86E71C06A996005DB059 /* Main.storyboard */,
Expand Down Expand Up @@ -226,6 +228,7 @@
17B15C301C07D9DC003CE77B /* FeedTableViewController.swift */,
17B15C321C07DA47003CE77B /* FeedTableViewModel.swift */,
17B15C341C07DB8E003CE77B /* Feed.swift */,
17E561881C07F4D200967366 /* FeedTableCell.swift */,
);
name = "UITableView + feed with Alamofire";
sourceTree = "<group>";
Expand Down Expand Up @@ -446,6 +449,7 @@
buildActionMask = 2147483647;
files = (
171B5FBE1C06C06E00AA5EC2 /* BindFromViewModelViewController.swift in Sources */,
17E561891C07F4D200967366 /* FeedTableCell.swift in Sources */,
17B15C351C07DB8E003CE77B /* Feed.swift in Sources */,
171D87131C06AB0C005DB059 /* BasicBindViewController.swift in Sources */,
17B15C331C07DA47003CE77B /* FeedTableViewModel.swift in Sources */,
Expand Down
39 changes: 34 additions & 5 deletions BondSample/Feed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,43 @@
//

import Foundation
import Bond

class Feed {

let title : String
let username : String
let title : Observable<String?>
let username : Observable<String?>
let userImage : Observable<UIImage?>
let userImageURL : NSURL
let url : NSURL

init(title : String, username: String){
self.title = title
self.username = username
init(title : String, username: String, userImageURL : NSURL, url : NSURL){
self.title = Observable(title)
self.username = Observable(username)
self.userImage = Observable<UIImage?>(nil) // initially no image
self.userImageURL = userImageURL
self.url = url
}

func fetchImageIfNeeded(){
if self.userImage.value != nil {
// already have photo
return
}
let downloadTask = NSURLSession.sharedSession().downloadTaskWithURL(userImageURL) {
[weak self] location, response, error in
if let location = location {
if let data = NSData(contentsOfURL: location) {
if let image = UIImage(data: data) {
dispatch_async(dispatch_get_main_queue()) {
// this will automatically update photo in bonded image view
self?.userImage.value = image
return
}
}
}
}
}
downloadTask.resume()
}
}
24 changes: 24 additions & 0 deletions BondSample/FeedTableCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// FeedTableCell.swift
// BondSample
//
// Created by Osamu Nishiyama on 2015/11/27.
// Copyright © 2015年 ever sense. All rights reserved.
//

import UIKit
import Bond

class FeedTableCell: UITableViewCell {

@IBOutlet weak var title: UILabel!
@IBOutlet weak var username: UILabel!
@IBOutlet weak var userImageView: UIImageView!

override func prepareForReuse() {
super.prepareForReuse()
userImageView.image = nil

bnd_bag.dispose()
}
}
34 changes: 26 additions & 8 deletions BondSample/FeedTableView.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9051"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
<scenes>
<!--Feed Table View Controller-->
Expand All @@ -17,38 +18,55 @@
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="tPw-Nq-AAz">
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="66" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="tPw-Nq-AAz">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="FeedCell" textLabel="Dtn-Kl-vkO" detailTextLabel="69B-Jc-dQj" style="IBUITableViewCellStyleSubtitle" id="963-dQ-Vq9">
<rect key="frame" x="0.0" y="92" width="600" height="44"/>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="FeedCell" textLabel="hqe-oY-ksr" detailTextLabel="iLl-Ua-LN7" rowHeight="66" style="IBUITableViewCellStyleSubtitle" id="963-dQ-Vq9" customClass="FeedTableCell" customModule="BondSample" customModuleProvider="target">
<rect key="frame" x="0.0" y="92" width="600" height="66"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="963-dQ-Vq9" id="i1J-bX-8e9">
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="600" height="65.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="Dtn-Kl-vkO">
<rect key="frame" x="15" y="6" width="31.5" height="19.5"/>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="hqe-oY-ksr">
<rect key="frame" x="15" y="17" width="31.5" height="19.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<animations/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Subtitle" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="69B-Jc-dQj">
<rect key="frame" x="15" y="25.5" width="40.5" height="13.5"/>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="username" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="iLl-Ua-LN7">
<rect key="frame" x="15" y="36.5" width="52" height="13.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<animations/>
<fontDescription key="fontDescription" type="system" pointSize="11"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="5Te-Ql-BxY">
<rect key="frame" x="534" y="0.0" width="58" height="58"/>
<animations/>
<constraints>
<constraint firstAttribute="width" constant="58" id="jCb-Kq-3K7"/>
</constraints>
</imageView>
</subviews>
<animations/>
<constraints>
<constraint firstItem="5Te-Ql-BxY" firstAttribute="top" secondItem="i1J-bX-8e9" secondAttribute="top" id="MEu-LX-s3p"/>
<constraint firstAttribute="bottomMargin" secondItem="5Te-Ql-BxY" secondAttribute="bottom" id="f28-BK-ae6"/>
<constraint firstItem="5Te-Ql-BxY" firstAttribute="trailing" secondItem="i1J-bX-8e9" secondAttribute="trailingMargin" id="pHo-VZ-RrL"/>
</constraints>
</tableViewCellContentView>
<animations/>
<connections>
<outlet property="title" destination="hqe-oY-ksr" id="l6x-AA-JfO"/>
<outlet property="userImageView" destination="5Te-Ql-BxY" id="GLE-sI-RNR"/>
<outlet property="username" destination="iLl-Ua-LN7" id="X2X-Tz-f8f"/>
</connections>
</tableViewCell>
</prototypes>
</tableView>
Expand Down
42 changes: 21 additions & 21 deletions BondSample/FeedTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,33 @@

import UIKit
import Bond
import SafariServices

class FeedTableViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
let feedViewModel = FeedTableViewModel()
var timeCounter = 5
var dataSource = ObservableArray<ObservableArray<Feed>>()
var timeCounter = 3


override func viewDidLoad() {
super.viewDidLoad()
dataSource = ObservableArray([feedViewModel.items])

// feedViewModel.request()
let dataSource = ObservableArray([feedViewModel.items])

dataSource.bindTo(tableView) { indexPath, dataSource, tableView in
let cell = tableView.dequeueReusableCellWithIdentifier("FeedCell", forIndexPath: indexPath)
let feed = dataSource[indexPath.section][indexPath.row] as Feed
cell.textLabel!.text = feed.title
cell.detailTextLabel?.text = feed.username
let cell = tableView.dequeueReusableCellWithIdentifier("FeedCell", forIndexPath: indexPath) as! FeedTableCell
let feed = dataSource[indexPath.section][indexPath.row]
feed.title.bindTo(cell.title.bnd_text).disposeIn(cell.bnd_bag)
feed.username.bindTo(cell.username.bnd_text).disposeIn(cell.bnd_bag)
feed.userImage.bindTo(cell.userImageView.bnd_image).disposeIn(cell.bnd_bag)
feed.fetchImageIfNeeded()
return cell
}

NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("onUpdate:"), userInfo: nil, repeats: true)

tableView.delegate = self
}

override func didReceiveMemoryWarning() {
Expand All @@ -49,16 +54,11 @@ class FeedTableViewController: UIViewController {
}

}
//
//extension MyTableViewController: BNDTableViewProxyDataSource {
// func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
// return "Header"
// }
//}
//
//extension MyTableViewController: UITableViewDelegate {
// func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// let name = dataSource[indexPath.section][indexPath.row]
// print(name + " selected!")
// }
//}

extension FeedTableViewController: UITableViewDelegate {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let feed = dataSource[indexPath.section][indexPath.row]
let safariVC = SFSafariViewController(URL: feed.url)
self.navigationController?.showViewController(safariVC, sender: nil)
}
}
2 changes: 1 addition & 1 deletion BondSample/FeedTableViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FeedTableViewModel : NSObject {
if let value = response.result.value {
let json = JSON(value)
for (_, subJson) in json {
let feed = Feed(title: subJson["title"].stringValue, username: subJson["user"]["id"].stringValue)
let feed = Feed(title: subJson["title"].stringValue, username: subJson["user"]["id"].stringValue, userImageURL: NSURL(string: subJson["user"]["profile_image_url"].stringValue)!, url: NSURL(string: subJson["url"].stringValue)!)
self.items.append(feed)
}
}
Expand Down
5 changes: 5 additions & 0 deletions BondSample/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand Down

0 comments on commit c028cf0

Please sign in to comment.