mirror of https://github.com/oxen-io/session-ios
parent
b579ea5915
commit
f6720f9afa
@ -0,0 +1,53 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import UIKit
|
||||||
|
|
||||||
|
class ImageCacheRecord: NSObject {
|
||||||
|
var variations: [CGFloat: UIImage]
|
||||||
|
init(variations: [CGFloat: UIImage]) {
|
||||||
|
self.variations = variations
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A two dimensional hash, allowing you to store variations under a single key.
|
||||||
|
* This is useful because we generate multiple diameters of an image, but when we
|
||||||
|
* want to clear out the images for a key we want to clear out *all* variations.
|
||||||
|
*/
|
||||||
|
@objc
|
||||||
|
class ImageCache: NSObject {
|
||||||
|
|
||||||
|
let backingCache: NSCache<AnyObject, ImageCacheRecord>
|
||||||
|
|
||||||
|
override init() {
|
||||||
|
self.backingCache = NSCache()
|
||||||
|
}
|
||||||
|
|
||||||
|
func image(forKey key: AnyObject, diameter: CGFloat) -> UIImage? {
|
||||||
|
guard let record = backingCache.object(forKey: key) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return record.variations[diameter]
|
||||||
|
}
|
||||||
|
|
||||||
|
func setImage(_ image: UIImage, forKey key: AnyObject, diameter: CGFloat) {
|
||||||
|
if let existingRecord = backingCache.object(forKey: key) {
|
||||||
|
existingRecord.variations[diameter] = image
|
||||||
|
backingCache.setObject(existingRecord, forKey: key)
|
||||||
|
} else {
|
||||||
|
let newRecord = ImageCacheRecord(variations: [diameter: image])
|
||||||
|
backingCache.setObject(newRecord, forKey: key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeAllImages() {
|
||||||
|
backingCache.removeAllObjects()
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeAllImages(forKey key: AnyObject) {
|
||||||
|
backingCache.removeObject(forKey: key)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import XCTest
|
||||||
|
|
||||||
|
class ImageCacheTest: XCTestCase {
|
||||||
|
|
||||||
|
var imageCache: ImageCache!
|
||||||
|
|
||||||
|
let firstVariation = UIImage()
|
||||||
|
let secondVariation = UIImage()
|
||||||
|
let otherImage = UIImage()
|
||||||
|
|
||||||
|
let cacheKey1 = "cache-key-1" as NSString
|
||||||
|
let cacheKey2 = "cache-key-2" as NSString
|
||||||
|
|
||||||
|
override func setUp() {
|
||||||
|
super.setUp()
|
||||||
|
self.imageCache = ImageCache()
|
||||||
|
imageCache.setImage(firstVariation, forKey:cacheKey1, diameter:100)
|
||||||
|
imageCache.setImage(secondVariation, forKey:cacheKey1, diameter:200)
|
||||||
|
imageCache.setImage(otherImage, forKey:cacheKey2, diameter:100)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func tearDown() {
|
||||||
|
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
||||||
|
super.tearDown()
|
||||||
|
}
|
||||||
|
|
||||||
|
func testSetGet() {
|
||||||
|
XCTAssertEqual(firstVariation, imageCache.image(forKey:cacheKey1, diameter: 100))
|
||||||
|
XCTAssertEqual(secondVariation, imageCache.image(forKey:cacheKey1, diameter: 200))
|
||||||
|
XCTAssertNotEqual(secondVariation, imageCache.image(forKey:cacheKey1, diameter: 100))
|
||||||
|
XCTAssertEqual(otherImage, imageCache.image(forKey:cacheKey2, diameter: 100))
|
||||||
|
XCTAssertNil(imageCache.image(forKey:cacheKey2, diameter: 200))
|
||||||
|
}
|
||||||
|
|
||||||
|
func testRemoveAllForKey() {
|
||||||
|
// sanity check
|
||||||
|
XCTAssertEqual(firstVariation, imageCache.image(forKey:cacheKey1, diameter: 100))
|
||||||
|
XCTAssertEqual(otherImage, imageCache.image(forKey:cacheKey2, diameter: 100))
|
||||||
|
|
||||||
|
imageCache.removeAllImages(forKey:cacheKey1)
|
||||||
|
|
||||||
|
XCTAssertNil(imageCache.image(forKey:cacheKey1, diameter: 100))
|
||||||
|
XCTAssertNil(imageCache.image(forKey:cacheKey1, diameter: 200))
|
||||||
|
XCTAssertEqual(otherImage, imageCache.image(forKey:cacheKey2, diameter: 100))
|
||||||
|
}
|
||||||
|
|
||||||
|
func testRemoveAll() {
|
||||||
|
XCTAssertEqual(firstVariation, imageCache.image(forKey:cacheKey1, diameter: 100))
|
||||||
|
|
||||||
|
imageCache.removeAllImages()
|
||||||
|
|
||||||
|
XCTAssertNil(imageCache.image(forKey:cacheKey1, diameter: 100))
|
||||||
|
XCTAssertNil(imageCache.image(forKey:cacheKey1, diameter: 200))
|
||||||
|
XCTAssertNil(imageCache.image(forKey:cacheKey2, diameter: 100))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue