-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
robot-divkit
committed
Oct 30, 2023
1 parent
7ee2177
commit ae162ff
Showing
20 changed files
with
248 additions
and
66 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
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,79 @@ | ||
import Foundation | ||
|
||
import BasePublic | ||
import NetworkingPublic | ||
|
||
/// Loads images for `DivKit` views. | ||
public protocol DivImageHolderFactory { | ||
/// Provides `ImageHolder` for an image with given `URL`. | ||
func make(_ url: URL?, _ placeholder: ImagePlaceholder?) -> ImageHolder | ||
} | ||
|
||
extension DivImageHolderFactory { | ||
public func make(_ url: URL?) -> ImageHolder { | ||
make(url, nil) | ||
} | ||
|
||
public func withCache(_ cachedImageHolders: [ImageHolder]) -> DivImageHolderFactory { | ||
if cachedImageHolders.isEmpty { | ||
return self | ||
} | ||
return CachedImageHolderFactory( | ||
imageHolderFactory: self, | ||
cachedImageHolders: cachedImageHolders | ||
) | ||
} | ||
} | ||
|
||
extension ImageHolderFactory: DivImageHolderFactory {} | ||
|
||
final class DefaultImageHolderFactory: DivImageHolderFactory { | ||
private let requester: URLResourceRequesting | ||
private let imageLoadingOptimizationEnabled: Bool | ||
|
||
private let imageProcessingQueue = OperationQueue( | ||
name: "tech.divkit.image-processing", | ||
qos: .userInitiated | ||
) | ||
|
||
init( | ||
requestPerformer: URLRequestPerforming, | ||
imageLoadingOptimizationEnabled: Bool | ||
) { | ||
self.requester = NetworkURLResourceRequester(performer: requestPerformer) | ||
self.imageLoadingOptimizationEnabled = imageLoadingOptimizationEnabled | ||
} | ||
|
||
func make(_ url: URL?, _ placeholder: ImagePlaceholder?) -> ImageHolder { | ||
guard let url = url else { | ||
return placeholder?.toImageHolder() ?? NilImageHolder() | ||
} | ||
return RemoteImageHolder( | ||
url: url, | ||
placeholder: placeholder, | ||
requester: requester, | ||
imageProcessingQueue: imageProcessingQueue, | ||
imageLoadingOptimizationEnabled: imageLoadingOptimizationEnabled | ||
) | ||
} | ||
} | ||
|
||
private final class CachedImageHolderFactory: DivImageHolderFactory { | ||
private let imageHolderFactory: DivImageHolderFactory | ||
private let cachedImageHolders: [ImageHolder] | ||
|
||
init( | ||
imageHolderFactory: DivImageHolderFactory, | ||
cachedImageHolders: [ImageHolder] | ||
) { | ||
self.imageHolderFactory = imageHolderFactory | ||
self.cachedImageHolders = cachedImageHolders | ||
} | ||
|
||
func make(_ url: URL?, _ placeholder: ImagePlaceholder?) -> ImageHolder { | ||
let cachedImageHolder = cachedImageHolders.first { | ||
$0.reused(with: placeholder, remoteImageURL: url) != nil | ||
} | ||
return cachedImageHolder ?? imageHolderFactory.make(url, placeholder) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
public enum DivKitInfo { | ||
public static let version = "28.7.0" | ||
public static let version = "28.8.0" | ||
} |
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
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
Oops, something went wrong.