This repository has been archived by the owner on Feb 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from nodes-ios/develop
Develop
- Loading branch information
Showing
4 changed files
with
207 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// Extensions.swift | ||
// Serializable | ||
// | ||
// Created by Chris Combs on 16/02/16. | ||
// Copyright © 2016 Nodes. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - Protocols - | ||
// MARK: String Initializable | ||
|
||
public protocol StringInitializable { | ||
static func fromString<T>(_ string: String) -> T? | ||
func stringRepresentation() -> String | ||
} | ||
|
||
extension URL: StringInitializable { | ||
public static func fromString<T>(_ string: String) -> T? { | ||
return self.init(string: string) as? T | ||
} | ||
|
||
public func stringRepresentation() -> String { | ||
return self.absoluteString | ||
} | ||
} | ||
|
||
extension Date: StringInitializable { | ||
static fileprivate let internalDateFormatter = DateFormatter() | ||
static fileprivate let allowedDateFormats = ["yyyy-MM-dd'T'HH:mm:ssZZZZZ", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd"] | ||
static public var customDateFormats: [String] = [] | ||
|
||
public static func fromString<T>(_ string: String) -> T? { | ||
for format in allowedDateFormats + customDateFormats { | ||
internalDateFormatter.dateFormat = format | ||
if let date = internalDateFormatter.date(from: string) as? T { | ||
return date | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
public func stringRepresentation() -> String { | ||
Date.internalDateFormatter.dateFormat = Date.allowedDateFormats.first | ||
return Date.internalDateFormatter.string(from: self) | ||
} | ||
} | ||
|
||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// Extention+HexInitializable.swift | ||
// Codemine | ||
// | ||
// Created by Andrei Hogea on 27/03/2018. | ||
// Copyright © 2018 Nodes. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: Hex Initializable | ||
#if os(OSX) | ||
import Cocoa | ||
typealias Color = NSColor | ||
#else | ||
import UIKit | ||
typealias Color = UIColor | ||
#endif | ||
|
||
public protocol HexInitializable { | ||
static func fromHexString<T>(_ hexString: String) -> T? | ||
} | ||
|
||
extension Color: HexInitializable { | ||
|
||
public static func fromHexString<T>(_ hexString: String) -> T? { | ||
let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted) | ||
var int = UInt32() | ||
let a, r, g, b: UInt32 | ||
|
||
guard Scanner(string: hex).scanHexInt32(&int) else { | ||
return nil | ||
} | ||
|
||
switch hex.count { | ||
// RGB (12-bit) | ||
case 3: | ||
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17) | ||
// RRGGBB (24-bit) | ||
case 6: | ||
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF) | ||
// ARGB (32-bit) | ||
case 8: | ||
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF) | ||
default: | ||
return nil | ||
} | ||
|
||
return self.init(red: CGFloat(r) / 255, | ||
green: CGFloat(g) / 255, | ||
blue: CGFloat(b) / 255, | ||
alpha: CGFloat(a) / 255) as? T | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// URLSession+Codable.swift | ||
// Codemine | ||
// | ||
// Created by Andrei Hogea on 22/03/2018. | ||
// Copyright © 2018 Nodes. All rights reserved. | ||
// | ||
|
||
|
||
import Foundation | ||
|
||
// Decoded Result | ||
public enum DResult<Value> { | ||
case success(Value) | ||
case successWithError(Value, Error) | ||
case failure(Error) | ||
} | ||
|
||
public extension URLSession { | ||
|
||
/** | ||
Adds a handler that attempts to parse the result of the request into a **Decodable** | ||
- parameter requestCompletion: The URLSession.dataTask completion | ||
- returns: The Decoded Result (DResult) | ||
*/ | ||
public func decode<Value: Swift.Decodable>(requestCompletion: (Data?, Error?)) -> DResult<Value> { | ||
switch requestCompletion { | ||
case (.some(let data), .some(let error)): | ||
do { | ||
let decodedData = try JSONDecoder().decode(Value.self, from: data) | ||
return .successWithError(decodedData, error) | ||
} catch let decodeError { | ||
return .failure(decodeError) | ||
} | ||
case (.some(let data), .none): | ||
do { | ||
let decodedData = try JSONDecoder().decode(Value.self, from: data) | ||
return .success(decodedData) | ||
} catch let decodeError { | ||
return .failure(decodeError) | ||
} | ||
case (.none, .some(let error)): | ||
return .failure(error) | ||
|
||
case (.none, .none): | ||
let fallBackError = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Data was not retrieved from request"]) as Error | ||
return .failure(fallBackError) | ||
} | ||
} | ||
|
||
/** | ||
Adds a handler that attempts to parse the result of the request into a **Decodable** | ||
- parameter completion: A closure that is invoked when the request is finished, containting the desired DataModel to be returned | ||
- returns: The URLSession.dataTask completion | ||
*/ | ||
public func decode<Value: Swift.Decodable>(_ completion: @escaping ((DResult<Value>) -> Void)) -> ((Data?, URLResponse?, Error?) -> Void) { | ||
return { (data, _, error) in | ||
DispatchQueue.main.async { | ||
completion(self.decode(requestCompletion: (data, error))) | ||
} | ||
} | ||
} | ||
} | ||
|