Skip to content

Commit

Permalink
Closes #21 by converting the codebase to Swift 2.0. Some APIs broken.
Browse files Browse the repository at this point in the history
  • Loading branch information
vittoriom committed Aug 23, 2015
1 parent 75d4f74 commit b9024d6
Show file tree
Hide file tree
Showing 55 changed files with 943 additions and 635 deletions.
2 changes: 1 addition & 1 deletion Carlos.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ cache.get(NSURL(string: "http://4.bp.blogspot.com/-TXzXNotuaHk/TwG1cQOC6DI/AAAAA
let image = UIImageView(image: value)

image
}
}
2 changes: 1 addition & 1 deletion Carlos.playground/Sources/SupportCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public func sharedSubfolder() -> String {
}

public func initializePlayground() {
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)
XCPSetExecutionShouldContinueIndefinitely(true)
}
2 changes: 1 addition & 1 deletion Carlos.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<playground version='5.0' target-platform='ios' requires-full-environment='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>
13 changes: 10 additions & 3 deletions Carlos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
520863751B64268000F740C7 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5208636C1B64268000F740C7 /* Images.xcassets */; };
520863761B64268000F740C7 /* MemoryWarningSampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5208636D1B64268000F740C7 /* MemoryWarningSampleViewController.swift */; };
520863771B64268000F740C7 /* DataCacheSampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5208636E1B64268000F740C7 /* DataCacheSampleViewController.swift */; };
520863791B64268700F740C7 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 520863781B64268700F740C7 /* Info.plist */; };
5208637E1B64268E00F740C7 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5208637A1B64268E00F740C7 /* LaunchScreen.xib */; };
5208637F1B64268E00F740C7 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5208637C1B64268E00F740C7 /* Main.storyboard */; };
520863811B642D4A00F740C7 /* PooledCacheSampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520863801B642D4A00F740C7 /* PooledCacheSampleViewController.swift */; };
Expand Down Expand Up @@ -553,7 +552,9 @@
AD695CD71B46CD65004E998D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0630;
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = WeltN24;
TargetAttributes = {
52635A1C1B4F0F3D00F187EE = {
Expand Down Expand Up @@ -689,7 +690,6 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
520863791B64268700F740C7 /* Info.plist in Resources */,
5208637E1B64268E00F740C7 /* LaunchScreen.xib in Resources */,
520863751B64268000F740C7 /* Images.xcassets in Resources */,
5208637F1B64268E00F740C7 /* Main.storyboard in Resources */,
Expand Down Expand Up @@ -833,6 +833,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
ONLY_ACTIVE_ARCH = NO;
PRODUCT_BUNDLE_IDENTIFIER = "de.weltn24.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -852,6 +853,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "de.weltn24.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -874,6 +876,7 @@
INFOPLIST_FILE = Tests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.4;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "de.weltn24.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
};
Expand All @@ -890,6 +893,7 @@
INFOPLIST_FILE = Tests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.4;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "de.weltn24.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand All @@ -915,6 +919,7 @@
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -987,6 +992,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
OTHER_SWIFT_FLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = "de.weltn24.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
};
Expand All @@ -1001,6 +1007,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
OTHER_SWIFT_FLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = "de.weltn24.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand Down
13 changes: 8 additions & 5 deletions Carlos.xcodeproj/xcshareddata/xcschemes/Carlos.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
LastUpgradeVersion = "0700"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -37,10 +37,10 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -62,15 +62,18 @@
ReferencedContainer = "container:Carlos.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
Expand All @@ -85,10 +88,10 @@
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
Expand Down
13 changes: 8 additions & 5 deletions Carlos.xcodeproj/xcshareddata/xcschemes/CarlosSample.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0640"
LastUpgradeVersion = "0700"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -37,10 +37,10 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
Expand All @@ -52,15 +52,18 @@
ReferencedContainer = "container:Carlos.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand All @@ -76,10 +79,10 @@
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand Down
16 changes: 8 additions & 8 deletions Carlos/BasicCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public final class BasicCache<A, B>: CacheLevel {
/**
Initializes a new instance of a BasicCache specifying closures for get, set, clear and onMemoryWarning, thus determining the behavior of the cache level as a whole
:param: getClosure The closure to execute when you call get(key) on this instance
:param: setClosure The closure to execute when you call set(value, key) on this instance
:param: clearClosure The closure to execute when you call clear() on this instance
:param: memoryClosure The closure to execute when you call onMemoryWarning() on this instance, or when a memory warning is thrown by the system and the cache level is listening for memory pressure events
- parameter getClosure: The closure to execute when you call get(key) on this instance
- parameter setClosure: The closure to execute when you call set(value, key) on this instance
- parameter clearClosure: The closure to execute when you call clear() on this instance
- parameter memoryClosure: The closure to execute when you call onMemoryWarning() on this instance, or when a memory warning is thrown by the system and the cache level is listening for memory pressure events
*/
public init(getClosure: (key: A) -> CacheRequest<B>, setClosure: (key: A, value: B) -> Void, clearClosure: () -> Void, memoryClosure: () -> Void) {
self.getClosure = getClosure
Expand All @@ -28,9 +28,9 @@ public final class BasicCache<A, B>: CacheLevel {
/**
Asks the cache to get the value for a given key
:param: key The key you want to get the value for
- parameter key: The key you want to get the value for
:returns: The result of the getClosure specified when initializing the instance
- returns: The result of the getClosure specified when initializing the instance
*/
public func get(key: KeyType) -> CacheRequest<OutputType> {
return getClosure(key: key)
Expand All @@ -39,8 +39,8 @@ public final class BasicCache<A, B>: CacheLevel {
/**
Asks the cache to set a value for the given key
:param: value The value to set on the cache
:param: key The key to use for the given value
- parameter value: The value to set on the cache
- parameter key: The key to use for the given value
:discussion: This call executes the setClosure specified when initializing the instance
*/
Expand Down
8 changes: 4 additions & 4 deletions Carlos/CacheProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ A simple class with the purpose of providing standard caches
*/
public class CacheProvider {
/**
:returns: An initialized and configured CacheLevel that takes NSURL keys and stores NSData values. Network requests are pooled for efficiency
- returns: An initialized and configured CacheLevel that takes NSURL keys and stores NSData values. Network requests are pooled for efficiency
*/
public static func dataCache() -> BasicCache<NSURL, NSData> {
return MemoryCacheLevel() >>> DiskCacheLevel() >>> pooled(NetworkFetcher())
return MemoryCacheLevel() >>> DiskCacheLevel() >>> NetworkFetcher().pooled()
}

/**
:returns: An initialized and configured CacheLevel that takes NSURL keys and stores UIImage values. Network requests are pooled for efficiency.
- returns: An initialized and configured CacheLevel that takes NSURL keys and stores UIImage values. Network requests are pooled for efficiency.
:discussion: The code is not safe at the moment. This means if you try to store in this cache something that is not a UIImage (e.g. a NSURL pointing to a JSON or an HTML document), the app will crash (this will be fixed in a future release)
*/
public static func imageCache() -> BasicCache<NSURL, UIImage> {
return MemoryCacheLevel() >>> DiskCacheLevel() >>> (pooled(NetworkFetcher()) =>> TwoWayTransformationBox<NSData, UIImage>(transform: { UIImage(data: $0) }, inverseTransform: { UIImagePNGRepresentation($0) /* This is a waste of bytes, we should probably use a lower-level framework */ }))
return MemoryCacheLevel() >>> DiskCacheLevel() >>> (NetworkFetcher().pooled() =>> TwoWayTransformationBox<NSData, UIImage>(transform: { UIImage(data: $0) }, inverseTransform: { UIImagePNGRepresentation($0) /* This is a waste of bytes, we should probably use a lower-level framework */ }))
}
}
52 changes: 28 additions & 24 deletions Carlos/CacheRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import Foundation

/// This class wraps a cache request future, where you can attach failure and success callbacks.
public class CacheRequest<T> {
private var failureListeners: [(NSError?) -> Void] = []
private var failureListeners: [(ErrorType) -> Void] = []
private var successListeners: [(T) -> Void] = []
private var didSucceed = false
private var didFail = false
private var error: NSError?
private var error: ErrorType?
private var value: T?

/// Creates a new CacheRequest
Expand All @@ -15,7 +13,7 @@ public class CacheRequest<T> {
/**
Initializes a new CacheRequest and makes it immediately succeed with the given value
:param: value The success value of the request
- parameter value: The success value of the request
*/
public init(value: T) {
succeed(value)
Expand All @@ -24,21 +22,23 @@ public class CacheRequest<T> {
/**
Initializes a new CacheRequest and makes it immediately fail with the given error
:param: error The error of the request
- parameter error: The error of the request
*/
public init(error: NSError?) {
public init(error: ErrorType) {
fail(error)
}

/**
Makes the request succeed with a value
:param: value The value found for the request
- parameter value: The value found for the request
:discussion: Calling this method makes all the listeners get the onSuccess callback
*/
public func succeed(value: T) {
didSucceed = true
guard self.error == nil else { return }
guard self.value == nil else { return }

self.value = value

for listener in successListeners {
Expand All @@ -49,12 +49,14 @@ public class CacheRequest<T> {
/**
Makes the request fail with an error
:param: error The optional error that caused the request to fail
- parameter error: The optional error that caused the request to fail
:discussion: Calling this method makes all the listeners get the onFailure callback
*/
public func fail(error: NSError?) {
didFail = true
public func fail(error: ErrorType) {
guard self.error == nil else { return }
guard self.value == nil else { return }

self.error = error

for listener in failureListeners {
Expand All @@ -65,12 +67,12 @@ public class CacheRequest<T> {
/**
Adds a listener for the success event of this request
:param: success The closure that should be called when the request succeeds, taking the value as a parameter
- parameter success: The closure that should be called when the request succeeds, taking the value as a parameter
:returns: The updated request
- returns: The updated request
*/
public func onSuccess(success: (T) -> Void) -> CacheRequest<T> {
if let value = value where didSucceed {
if let value = value {
success(value)
} else {
successListeners.append(success)
Expand All @@ -82,12 +84,12 @@ public class CacheRequest<T> {
/**
Adds a listener for the failure event of this request
:param: success The closure that should be called when the request fails, taking the error as a parameter
- parameter success: The closure that should be called when the request fails, taking the error as a parameter
:returns: The updated request
- returns: The updated request
*/
public func onFailure(failure: (NSError?) -> Void) -> CacheRequest<T> {
if didFail {
public func onFailure(failure: (ErrorType) -> Void) -> CacheRequest<T> {
if let error = error {
failure(error)
} else {
failureListeners.append(failure)
Expand All @@ -99,13 +101,15 @@ public class CacheRequest<T> {
/**
Adds a listener for both success and failure events of this request
:param: completion The closure that should be called when the request completes (succeeds or fails), taking both an optional value in case the request succeeded and an optional error in case the request failed as parameters
- parameter completion: The closure that should be called when the request completes (succeeds or fails), taking both an optional value in case the request succeeded and an optional error in case the request failed as parameters
:returns: The updated request
- returns: The updated request
*/
public func onCompletion(completion: (value: T?, error: NSError?) -> Void) -> CacheRequest<T> {
if didFail || didSucceed {
completion(value: value, error: error)
public func onCompletion(completion: (value: T?, error: (ErrorType)?) -> Void) -> CacheRequest<T> {
if let error = error {
completion(value: nil, error: error)
} else if let value = value {
completion(value: value, error: nil)
} else {
onSuccess { value in
completion(value: value, error: nil)
Expand Down
Loading

0 comments on commit b9024d6

Please sign in to comment.