Skip to content

Commit

Permalink
Swift 3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mats-claassen committed Oct 7, 2016
1 parent 0997252 commit c7e233e
Show file tree
Hide file tree
Showing 19 changed files with 255 additions and 230 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Change Log
All notable changes to TokenRow will be documented in this file.

### [1.1.0](https://github.com/EurekaCommunity/TokenRow/releases/tag/1.0.0)
<!-- Released on 2016-10-07. -->

* Swift 3 compatibility.

### [1.0.0](https://github.com/EurekaCommunity/TokenRow/releases/tag/1.0.0)
<!-- Released on 2016-01-20. -->
<!-- Released on 2016-09-09. -->

* This is the initial version.

Expand Down
38 changes: 13 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<p align="left">
<a href="https://travis-ci.org/EurekaCommunity/TokenRow"><img src="https://travis-ci.org/EurekaCommunity/TokenRow.svg?branch=master" alt="Build status" /></a>
<img src="https://img.shields.io/badge/platform-iOS-blue.svg?style=flat" alt="Platform iOS" />
<a href="https://developer.apple.com/swift"><img src="https://img.shields.io/badge/swift2-compatible-4BC51D.svg?style=flat" alt="Swift 2 compatible" /></a>
<a href="https://developer.apple.com/swift"><img src="https://img.shields.io/badge/swift3-compatible-4BC51D.svg?style=flat" alt="Swift 2 compatible" /></a>
<!--<a href="https://github.com/Carthage/Carthage"><img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage compatible" /></a>-->
<a href="https://cocoapods.org/pods/XLActionController"><img src="https://img.shields.io/cocoapods/v/TokenRow.svg" alt="CocoaPods compatible" /></a>
<a href="https://raw.githubusercontent.com/EurekaCommunity/TokenRow/master/LICENSE"><img src="http://img.shields.io/badge/license-MIT-blue.svg?style=flat" alt="License: MIT" /></a>
Expand Down Expand Up @@ -54,13 +54,13 @@ form +++ Section()
To see what you can customize have a look at the [Customization](#customization) section.

## Dependencies
* [Eureka]
* [Eureka] 2.x
* [CLTokenInputView] which is a token view pod

## Requirements

* iOS 8.0+
* Xcode 7.3+
* Xcode 8+

## Getting involved

Expand Down Expand Up @@ -91,17 +91,6 @@ To install TokenRow, simply add the following line to your Podfile:
```ruby
pod 'TokenRow'
```
<!--
#### Carthage
[Carthage](https://github.com/Carthage/Carthage) is a simple, decentralized dependency manager for Cocoa.
To install TokenRow, simply add the following line to your Cartfile:
```ogdl
github "EurekaCommunity/TokenRow"
```
-->

## TokenSearchable

Expand Down Expand Up @@ -175,17 +164,16 @@ For example:
```swift
row.getTokensForString = { [weak self, row] string in
guard let me = self else { return nil }
Alamofire.Manager.sharedInstance.request(.GET, "https://api.github.com/search/users?q=\(string)&per_page=5")
.responseCollection({ (response: Response<[User], BackendError>) in
switch response.result {
case let .Success(value):
// update the filtered tokens and reload the tableView or collectionView
row.cell.filteredTokens = value
row.cell.reloadOptions()
case let .Failure(error):
print(error)
}
})
Alamofire.SessionManager.default.request("https://api.github.com/search/users?q=\(text)&per_page=5")
.responseCollection(completionHandler: { (response: DataResponse<[User]>) in
switch response.result {
case let .success(value):
row.cell.filteredTokens = value
row.cell.reloadOptions()
case let .failure(error):
print(error)
}
})
return []
}
```
Expand Down
4 changes: 2 additions & 2 deletions TokenRow.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "TokenRow"
s.version = "1.0.0"
s.version = "1.1.0"
s.summary = "An Eureka row that allows the user to select options into a token view."
s.homepage = "https://github.com/EurekaCommunity/TokenRow"
s.license = { type: 'MIT', file: 'LICENSE' }
Expand All @@ -11,6 +11,6 @@ Pod::Spec.new do |s|
s.requires_arc = true
s.ios.source_files = 'TokenRow/Sources/**/*.{swift}'
s.ios.frameworks = 'UIKit', 'Foundation'
s.dependency 'Eureka', '~> 1.0'
s.dependency 'Eureka', '~> 2.0.0-beta.1'
s.dependency 'CLTokenInputView', '~> 2.0'
end
73 changes: 38 additions & 35 deletions TokenRow/Example/Alamofire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,58 @@ import Alamofire
*/


public enum BackendError: ErrorType {
case Network(error: NSError)
case DataSerialization(reason: String)
case JSONSerialization(error: NSError)
case ObjectSerialization(reason: String)
case XMLSerialization(error: NSError)
public enum BackendError: Error {
case network(error: NSError)
case dataSerialization(reason: String)
case jsonSerialization(error: NSError)
case objectSerialization(reason: String)
case xmlSerialization(error: NSError)
}

public protocol ResponseObjectSerializable {
init?(response: NSHTTPURLResponse, representation: AnyObject)
init?(response: HTTPURLResponse, representation: AnyObject)
}

extension Request {
public func responseObject<T: ResponseObjectSerializable>(completionHandler: Response<T, BackendError> -> Void) -> Self {
let responseSerializer = ResponseSerializer<T, BackendError> { request, response, data, error in
guard error == nil else { return .Failure(.Network(error: error!)) }
extension DataRequest {
@discardableResult
public func responseObject<T: ResponseObjectSerializable>(queue: DispatchQueue? = nil, completionHandler: @escaping (DataResponse<T>) -> Void) -> Self {
let responseSerializer = DataResponseSerializer<T> { request, response, data, error in
guard error == nil else { return .failure(BackendError.network(error: error! as NSError)) }

let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let JSONResponseSerializer = DataRequest.jsonResponseSerializer(options: .allowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, data, error)

switch result {
case .Success(let value):
case .success(let value):
if let
response = response,
responseObject = T(response: response, representation: value)
let responseObject = T(response: response, representation: value as AnyObject)
{
return .Success(responseObject)
return .success(responseObject)
} else {
return .Failure(.ObjectSerialization(reason: "JSON could not be serialized into response object: \(value)"))
return .failure(BackendError.objectSerialization(reason: "JSON could not be serialized into response object: \(value)"))
}
case .Failure(let error):
return .Failure(.JSONSerialization(error: error))
case .failure(let error):
return .failure(BackendError.jsonSerialization(error: error as NSError))
}
}

return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
return response(queue: queue, responseSerializer: responseSerializer, completionHandler: completionHandler)
}
}

public protocol ResponseCollectionSerializable {
static func collection(response response: NSHTTPURLResponse, representation: AnyObject) -> [Self]
static func collection(response: HTTPURLResponse, representation: AnyObject) -> [Self]
}

extension ResponseCollectionSerializable where Self: ResponseObjectSerializable {
static func collection(response response: NSHTTPURLResponse, representation: AnyObject) -> [Self] {
static func collection(response: HTTPURLResponse, representation: AnyObject) -> [Self] {
var collection = [Self]()

// check in items path
if let representation = representation.valueForKeyPath("items") as? [[String: AnyObject]] {
if let representation = representation.value(forKeyPath: "items") as? [[String: AnyObject]] {
for itemRepresentation in representation {
if let item = Self(response: response, representation: itemRepresentation) {
if let item = Self(response: response, representation: itemRepresentation as AnyObject) {
collection.append(item)
}
}
Expand All @@ -76,26 +77,28 @@ extension ResponseCollectionSerializable where Self: ResponseObjectSerializable
}
}

extension Alamofire.Request {
public func responseCollection<T: ResponseCollectionSerializable>(completionHandler: Response<[T], BackendError> -> Void) -> Self {
let responseSerializer = ResponseSerializer<[T], BackendError> { request, response, data, error in
guard error == nil else { return .Failure(.Network(error: error!)) }
extension DataRequest {
@discardableResult
public func responseCollection<T: ResponseCollectionSerializable>(queue: DispatchQueue? = nil,
completionHandler: @escaping (DataResponse<[T]>) -> Void) -> Self {
let responseSerializer = DataResponseSerializer<[T]> { request, response, data, error in
guard error == nil else { return .failure(BackendError.network(error: error! as NSError)) }

let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let result = JSONSerializer.serializeResponse(request, response, data, error)
let JSONResponseSerializer = DataRequest.jsonResponseSerializer(options: .allowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, data, error)

switch result {
case .Success(let value):
case .success(let value):
if let response = response {
return .Success(T.collection(response: response, representation: value))
return .success(T.collection(response: response, representation: value as AnyObject))
} else {
return .Failure(. ObjectSerialization(reason: "Response collection could not be serialized due to nil response"))
return .failure(BackendError.objectSerialization(reason: "JSON could not be serialized into response object: \(value)"))
}
case .Failure(let error):
return .Failure(.JSONSerialization(error: error))
case .failure(let error):
return .failure(BackendError.jsonSerialization(error: error as NSError))
}
}

return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
return response(queue: queue, responseSerializer: responseSerializer, completionHandler: completionHandler)
}
}
12 changes: 6 additions & 6 deletions TokenRow/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
Loading

0 comments on commit c7e233e

Please sign in to comment.