Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: osx support #22

Merged
merged 12 commits into from
Dec 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Cache.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ Pod::Spec.new do |s|
s.author = { "Hyper Interaktiv AS" => "[email protected]" }
s.source = { :git => "https://github.com/hyperoslo/Cache.git", :tag => s.version.to_s }
s.social_media_url = 'https://twitter.com/hyperoslo'
s.platform = :ios, '8.0'

s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.9'

s.requires_arc = true
s.source_files = 'Source/**/*'
s.ios.source_files = 'Source/{iOS,Shared}/**/*'
s.osx.source_files = 'Source/{Mac,Shared}/**/*'

s.frameworks = 'UIKit', 'Foundation'
s.frameworks = 'Foundation'
end
920 changes: 644 additions & 276 deletions Cache.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

113 changes: 113 additions & 0 deletions Cache.xcodeproj/xcshareddata/xcschemes/Cache-Mac.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D5291D5F1C283B5300B702C9"
BuildableName = "Cache-Mac.framework"
BlueprintName = "Cache-Mac"
ReferencedContainer = "container:Cache.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D5291D681C283B5400B702C9"
BuildableName = "Cache-Mac-Tests.xctest"
BlueprintName = "Cache-Mac-Tests"
ReferencedContainer = "container:Cache.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D5291D681C283B5400B702C9"
BuildableName = "Cache-Mac-Tests.xctest"
BlueprintName = "Cache-Mac-Tests"
ReferencedContainer = "container:Cache.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D5291D5F1C283B5300B702C9"
BuildableName = "Cache-Mac.framework"
BlueprintName = "Cache-Mac"
ReferencedContainer = "container:Cache.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D5291D5F1C283B5300B702C9"
BuildableName = "Cache-Mac.framework"
BlueprintName = "Cache-Mac"
ReferencedContainer = "container:Cache.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D5291D5F1C283B5300B702C9"
BuildableName = "Cache-Mac.framework"
BlueprintName = "Cache-Mac"
ReferencedContainer = "container:Cache.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
6 changes: 3 additions & 3 deletions Cache.xcodeproj/xcshareddata/xcschemes/Cache-iOS.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D5DC59E91C20593E003BD79B"
BuildableName = "CacheTests.xctest"
BlueprintName = "CacheTests"
BlueprintIdentifier = "D5291D171C2837DB00B702C9"
BuildableName = "Cache-iOS-Tests.xctest"
BlueprintName = "Cache-iOS-Tests"
ReferencedContainer = "container:Cache.xcodeproj">
</BuildableReference>
</TestableReference>
Expand Down
2 changes: 1 addition & 1 deletion Example/CacheDemo/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ EXTERNAL SOURCES:
:path: ../../

SPEC CHECKSUMS:
Cache: 5f381cc23cb2c69bc70b8cd2a5e260d006b03b6d
Cache: 78f58bc008313c7a1182967a2ebcb3de9fddecd4

COCOAPODS: 0.39.0
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ launches. Disk cache is the most reliable choice here.
(if default implementations of memory and disk caches don't fit your purpose for some reason).
- `NSData` encoding and decoding required by `Cachable` protocol are implemented
for `UIImage`, `String`, `JSON` and `NSData`.
- iOS and OSX support.

## Usage

Expand Down
45 changes: 45 additions & 0 deletions Source/Mac/Extensions/NSImage+Cache.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Foundation
import Cocoa

// MARK: - Cachable

extension NSImage: Cachable {

public typealias CacheType = NSImage

public static func decode(data: NSData) -> CacheType? {
let image = NSImage(data: data)
return image
}

public func encode() -> NSData? {
guard let data = TIFFRepresentation else { return nil }

let imageFileType: NSBitmapImageFileType = hasAlpha
? .NSPNGFileType
: .NSJPEGFileType

return NSBitmapImageRep(data: data)?.representationUsingType(imageFileType, properties: [:])
}
}

// MARK: - Helpers

extension NSImage {

var hasAlpha: Bool {
var imageRect:CGRect = CGRectMake(0, 0, size.width, size.height)
let imageRef = CGImageForProposedRect(&imageRect, context: nil, hints: nil)
let result: Bool
let alpha = CGImageGetAlphaInfo(imageRef)

switch alpha {
case .None, .NoneSkipFirst, .NoneSkipLast:
result = false
default:
result = true
}

return result
}
}
35 changes: 35 additions & 0 deletions Source/Mac/HybridCache.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Cocoa

public class HybridCache: BasicHybridCache {

// MARK: - Inititalization

public override init(name: String, config: Config = Config.defaultConfig) {
super.init(name: name, config: config)

let notificationCenter = NSNotificationCenter.defaultCenter()

notificationCenter.addObserver(self, selector: "applicationWillTerminate",
name: NSApplicationWillTerminateNotification, object: nil)
notificationCenter.addObserver(self, selector: "applicationDidResignActive",
name: NSApplicationDidResignActiveNotification, object: nil)
}

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}

// MARK: - Notifications

func applicationDidReceiveMemoryWarning() {
frontStorage.clearExpired(nil)
}

func applicationWillTerminate() {
backStorage.clearExpired(nil)
}

func applicationDidResignActive() {
backStorage.clearExpired(nil)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import UIKit
import Foundation

public class HybridCache: NSObject {
public class BasicHybridCache: NSObject {

public let name: String

Expand All @@ -18,19 +18,6 @@ public class HybridCache: NSObject {
backStorage = StorageFactory.resolve(name, kind: config.backKind, maxSize: config.maxSize)

super.init()

let notificationCenter = NSNotificationCenter.defaultCenter()

notificationCenter.addObserver(self, selector: "applicationDidReceiveMemoryWarning",
name: UIApplicationDidReceiveMemoryWarningNotification, object: nil)
notificationCenter.addObserver(self, selector: "applicationWillTerminate",
name: UIApplicationWillTerminateNotification, object: nil)
notificationCenter.addObserver(self, selector: "applicationDidEnterBackground",
name: UIApplicationDidEnterBackgroundNotification, object: nil)
}

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}

// MARK: - Caching
Expand Down Expand Up @@ -93,38 +80,4 @@ public class HybridCache: NSObject {
}
}
}

// MARK: - Notifications

func applicationDidReceiveMemoryWarning() {
frontStorage.clearExpired(nil)
}

func applicationWillTerminate() {
backStorage.clearExpired(nil)
}

func applicationDidEnterBackground() {
let application = UIApplication.sharedApplication()
var backgroundTask: UIBackgroundTaskIdentifier?

backgroundTask = application.beginBackgroundTaskWithExpirationHandler { [weak self] in
guard let weakSelf = self, var backgroundTask = backgroundTask else { return }

weakSelf.endBackgroundTask(&backgroundTask)
}

backStorage.clearExpired { [weak self] in
guard let weakSelf = self, var backgroundTask = backgroundTask else { return }

dispatch_async(dispatch_get_main_queue()) {
weakSelf.endBackgroundTask(&backgroundTask)
}
}
}

func endBackgroundTask(inout task: UIBackgroundTaskIdentifier) {
UIApplication.sharedApplication().endBackgroundTask(task)
task = UIBackgroundTaskInvalid
}
}
2 changes: 1 addition & 1 deletion Source/Cache/Cache.swift → Source/Shared/Cache.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public class Cache<T: Cachable>: HybridCache {
public class Cache<T: Cachable>: BasicHybridCache {

public override init(name: String, config: Config = Config.defaultConfig) {
super.init(name: name, config: config)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import UIKit
import Foundation

public enum EncodingError: ErrorType {
case InvalidSize
Expand Down
57 changes: 57 additions & 0 deletions Source/iOS/HybridCache.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import UIKit

public class HybridCache: BasicHybridCache {

// MARK: - Inititalization

public override init(name: String, config: Config = Config.defaultConfig) {
super.init(name: name, config: config)

let notificationCenter = NSNotificationCenter.defaultCenter()

notificationCenter.addObserver(self, selector: "applicationDidReceiveMemoryWarning",
name: UIApplicationDidReceiveMemoryWarningNotification, object: nil)
notificationCenter.addObserver(self, selector: "applicationWillTerminate",
name: UIApplicationWillTerminateNotification, object: nil)
notificationCenter.addObserver(self, selector: "applicationDidEnterBackground",
name: UIApplicationDidEnterBackgroundNotification, object: nil)
}

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}

// MARK: - Notifications

func applicationDidReceiveMemoryWarning() {
frontStorage.clearExpired(nil)
}

func applicationWillTerminate() {
backStorage.clearExpired(nil)
}

func applicationDidEnterBackground() {
let application = UIApplication.sharedApplication()
var backgroundTask: UIBackgroundTaskIdentifier?

backgroundTask = application.beginBackgroundTaskWithExpirationHandler { [weak self] in
guard let weakSelf = self, var backgroundTask = backgroundTask else { return }

weakSelf.endBackgroundTask(&backgroundTask)
}

backStorage.clearExpired { [weak self] in
guard let weakSelf = self, var backgroundTask = backgroundTask else { return }

dispatch_async(dispatch_get_main_queue()) {
weakSelf.endBackgroundTask(&backgroundTask)
}
}
}

func endBackgroundTask(inout task: UIBackgroundTaskIdentifier) {
UIApplication.sharedApplication().endBackgroundTask(task)
task = UIBackgroundTaskInvalid
}
}
Loading