Created
December 3, 2014 10:02
-
-
Save viteinfinite/957ced9da14faacc0e38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PhotoDownloadManager.swift | |
// XebiaSki | |
// | |
// Created by Simone Civetta on 11/11/14. | |
// Copyright (c) 2014 Xebia IT Architechts. All rights reserved. | |
// | |
import UIKit | |
public typealias RetrieveCallback = (UIImage?) -> () | |
public typealias DownloadCallback = (NSURL?) -> () | |
public class PhotoDownloadManager { | |
private var photoURL: NSURL | |
public init(photoURL: NSURL) { | |
self.photoURL = photoURL | |
} | |
// MARK: Entry point | |
public func retrievePhoto(callback: RetrieveCallback) { | |
downloadPhoto(self.photoURL, result: {[weak self] (URL) -> () in | |
if self == nil { | |
callback(nil) | |
return; | |
} | |
if let tempURL = URL? { | |
self?.retrievePhotoFromLocalURL(tempURL, callback: callback) | |
return; | |
} | |
callback(nil) | |
}) | |
} | |
private func retrievePhotoFromLocalURL(URL: NSURL, callback: RetrieveCallback) { | |
var photo: UIImage? | |
if let photoData = NSData(contentsOfURL: URL)? { | |
photo = UIImage(data: photoData) | |
} | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
callback(photo) | |
}) | |
} | |
private func downloadPhoto(URL: NSURL, result: DownloadCallback) { | |
let session = NSURLSession.sharedSession() | |
session.downloadTaskWithURL(URL, completionHandler: { (localURL: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in | |
if let HTTPResponse = response as? NSHTTPURLResponse { | |
if HTTPResponse.statusCode == 200 { | |
result(localURL) | |
return; | |
} | |
} | |
result(nil) | |
}).resume() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SkiResort.swift | |
// XebiaSki | |
// | |
// Created by JC on 30/11/14. | |
// Copyright (c) 2014 Xebia IT Architechts. All rights reserved. | |
// | |
public class SkiResort: NSObject, NSCoding { | |
public var name: String | |
public var photoURL: NSURL | |
public var temperature: Int | |
public init(name: String, photoURL: NSURL, temperature: Int) { | |
self.name = name | |
self.photoURL = photoURL | |
self.temperature = temperature | |
} | |
public required init(coder aDecoder: NSCoder) { | |
self.name = aDecoder.decodeObjectForKey("name") as String | |
self.photoURL = NSURL(string: aDecoder.decodeObjectForKey("photoURL") as String)! | |
self.temperature = aDecoder.decodeObjectForKey("temperature") as Int | |
} | |
public func encodeWithCoder(aCoder: NSCoder) { | |
aCoder.encodeObject(self.name, forKey: "name") | |
aCoder.encodeObject(self.photoURL.absoluteString, forKey: "photoURL") | |
aCoder.encodeObject(self.temperature, forKey: "temperature") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SkiResortDataSource.swift | |
// XebiaSki | |
// | |
// Created by Simone Civetta on 02/12/14. | |
// Copyright (c) 2014 Xebia IT Architechts. All rights reserved. | |
// | |
public class SkiResortDataSource { | |
var allObjects: Array<SkiResort> | |
public init() { | |
self.allObjects = [ | |
SkiResort(name: "Arcabulle", photoURL: NSURL(string: "http://www.trinum.com/ibox/ftpcam/les-arcs_arcabulle.jpg")!, temperature: 5), | |
SkiResort(name: "Arcs 1600 Pistes", photoURL: NSURL(string: "http://static1.lesarcsnet.com/image_uploader/webcam/large/lesarcs-1600-cam.jpg")!, temperature: -2), | |
SkiResort(name: "Vanoise Express", photoURL: NSURL(string: "http://trinum.com/ibox/ftpcam/Peisey-Vallandry_vanoise-expresse.jpg")!, temperature: 4), | |
SkiResort(name: "Arc 1950 Village", photoURL: NSURL(string: "http://www.trinum.com/ibox/ftpcam/arc-1950-haut-village.jpg")!, temperature: 0) | |
] | |
} | |
public var count: Int { | |
get { | |
return self.allObjects.count | |
} | |
} | |
public subscript(index: Int) -> SkiResort { | |
return self.allObjects[index] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment