-
Notifications
You must be signed in to change notification settings - Fork 2
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 #5 from bullinnyc/add-image-cache-protocol
Add image cache protocol.
- Loading branch information
Showing
11 changed files
with
182 additions
and
51 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
33 changes: 33 additions & 0 deletions
33
Sources/CachedAsyncImage/EnvironmentValues/EnvironmentValues + ImageCache.swift
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,33 @@ | ||
// | ||
// EnvironmentValues + ImageCache.swift | ||
// CachedAsyncImage | ||
// | ||
// Created by Dmitry Kononchuk on 02.01.2024. | ||
// Copyright © 2024 Dmitry Kononchuk. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct ImageCacheKey: EnvironmentKey { | ||
// MARK: - Public Properties | ||
|
||
static let defaultValue: ImageCache = TempImageCache() | ||
} | ||
|
||
extension EnvironmentValues { | ||
// MARK: - Public Properties | ||
|
||
/// The image cache of this environment. | ||
/// | ||
/// Read this environment value from within a view to access the image cache management. | ||
/// | ||
/// struct MyView: View { | ||
/// @Environment(\.imageCache) private var imageCache | ||
/// | ||
/// // ... | ||
/// } | ||
public var imageCache: ImageCache { | ||
get { self[ImageCacheKey.self] } | ||
set { self[ImageCacheKey.self] = newValue } | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Sources/CachedAsyncImage/EnvironmentValues/EnvironmentValues + NetworkManager.swift
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,24 @@ | ||
// | ||
// EnvironmentValues + NetworkManager.swift | ||
// CachedAsyncImage | ||
// | ||
// Created by Dmitry Kononchuk on 02.01.2024. | ||
// Copyright © 2024 Dmitry Kononchuk. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct NetworkManagerKey: EnvironmentKey { | ||
// MARK: - Public Properties | ||
|
||
static let defaultValue: NetworkManagerProtocol = NetworkManager() | ||
} | ||
|
||
extension EnvironmentValues { | ||
// MARK: - Public Properties | ||
|
||
var networkManager: NetworkManagerProtocol { | ||
get { self[NetworkManagerKey.self] } | ||
set { self[NetworkManagerKey.self] = newValue } | ||
} | ||
} |
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
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,61 @@ | ||
// | ||
// TempImageCache.swift | ||
// CachedAsyncImage | ||
// | ||
// Created by Dmitry Kononchuk on 02.01.2024. | ||
// Copyright © 2024 Dmitry Kononchuk. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Image cache protocol. | ||
public protocol ImageCache { | ||
subscript(_ url: URL) -> CPImage? { get set } | ||
|
||
/// Set cache limit. | ||
/// | ||
/// - Parameters: | ||
/// - countLimit: The maximum number of objects the cache should hold. | ||
/// If `0`, there is no count limit. The default value is `0`. | ||
/// - totalCostLimit: The maximum total cost that the cache can hold before | ||
/// it starts evicting objects. | ||
/// When you add an object to the cache, you may pass in a specified cost for the object, | ||
/// such as the size in bytes of the object. | ||
/// If `0`, there is no total cost limit. The default value is `0`. | ||
func setCacheLimit(countLimit: Int, totalCostLimit: Int) | ||
|
||
/// Empties the cache. | ||
func removeCache() | ||
} | ||
|
||
/// Temporary image cache. | ||
struct TempImageCache: ImageCache { | ||
// MARK: - Private Properties | ||
|
||
private let cache: NSCache<NSURL, CPImage> = { | ||
let cache = NSCache<NSURL, CPImage>() | ||
return cache | ||
}() | ||
|
||
// MARK: - Subscripts | ||
|
||
public subscript(_ key: URL) -> CPImage? { | ||
get { cache.object(forKey: key as NSURL) } | ||
set { | ||
newValue == nil | ||
? cache.removeObject(forKey: key as NSURL) | ||
: cache.setObject(newValue ?? CPImage(), forKey: key as NSURL) | ||
} | ||
} | ||
|
||
// MARK: - Public Methods | ||
|
||
public func setCacheLimit(countLimit: Int = 0, totalCostLimit: Int = 0) { | ||
cache.countLimit = countLimit | ||
cache.totalCostLimit = totalCostLimit | ||
} | ||
|
||
public func removeCache() { | ||
cache.removeAllObjects() | ||
} | ||
} |
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
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