Skip to content

Instantly share code, notes, and snippets.

View emenegro's full-sized avatar

Mario Negro emenegro

  • Valladolid, Spain
View GitHub Profile
//: Playground - noun: a place where people can play
import UIKit
protocol OfflineRequestConvertible {}
extension String: OfflineRequestConvertible {}
struct OfflineAction {
enum `Type` {
class OfflineActionBuilder {
fileprivate var action: OfflineAction!
init() {
fatalError("This type cannot be constructed directly, use static var 'builder' instead.")
}
private init(_ action: OfflineAction) {
self.action = action
protocol OfflineController {
func execute(action: OfflineAction, completion: @escaping OfflineControllerCompletionHandler)
}
let cache = OfflineActionBuilder.builder
.toCache("https://www.medium.com")
.data(Data())
.forId("id")
.keepingAliveUntil(Date())
.build()
let get = OfflineActionBuilder.builder
.toGet("https://www.medium.com")
.withEncryptionKey("encryptionKey")
extension OfflineActionBuilder: RequestStep, DataStep, CommonsStep, CacheCommonsStep, GetCommonsStep {
func toCache(_ request: OfflineRequestConvertible) -> DataStep {
action.type = .cache
action.request = request
return self as DataStep
}
func toGet(_ request: OfflineRequestConvertible) -> GetCommonsStep {
action.type = .get
protocol BuildStep {
func build() -> OfflineAction
}
protocol RequestStep {
func toCache(_ request: OfflineRequestConvertible) -> DataStep
func toGet(_ request: OfflineRequestConvertible) -> GetCommonsStep
func toDelete(_ request: OfflineRequestConvertible) -> CommonsStep
}
struct OfflineAction {
enum `Type` {
case cache, get, delete
}
fileprivate(set) var type: Type?
fileprivate(set) var request: OfflineRequestConvertible?
fileprivate(set) var data: Data?
fileprivate(set) var id: String?
OfflineActionBuilder
.toCache("https://www.medium.com")
.ifBefore(Date())
.keepingAliveUntil(Date())
.build()
OfflineActionBuilder
.toCache("https://www.medium.com")
.data(data)
.forId("id1")
.keepingAliveUntil(Date())
.build()
protocol OfflineController {
func cache(request: OfflineRequestConvertible, forId id: String?, encryptionKey: String?, data: Data, keepAliveUntil keepAlive: Date?, completion: @escaping OfflineControllerCompletionHandler)
func get(request: OfflineRequestConvertible, forId id: String?, encryptionKey: String?, ifBefore before: Date?, completion: @escaping OfflineControllerCompletionHandler)
func delete(request: OfflineRequestConvertible, forId id: String?, encryptionKey: String?, completion: @escaping OfflineControllerCompletionHandler)
}