Skip to content
Merged
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
56 changes: 37 additions & 19 deletions Sources/AsyncMultiplexImage/AsyncMultiplexImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ open class AsyncMultiplexImageView: UIView {

private let viewModel: _AsyncMultiplexImageViewModel = .init()

private var currentUsingImage: MultiplexImage?
private var currentUsingNetworkImage: MultiplexImage?
private var currentUsingImage: UIImage?

private var currentUsingContentSize: CGSize?
private let clearsContentBeforeDownload: Bool

Expand All @@ -54,7 +56,7 @@ open class AsyncMultiplexImageView: UIView {
offloadStrategy: any OffloadStrategy = OffloadInvisibleStrategy(),
clearsContentBeforeDownload: Bool = true
) {

self.downloader = downloader
self.offloadStrategy = offloadStrategy
self.clearsContentBeforeDownload = clearsContentBeforeDownload
Expand Down Expand Up @@ -101,18 +103,28 @@ open class AsyncMultiplexImageView: UIView {
// MARK: - Functions

private func onUpdateState(state: borrowing State) {
let offloads = offloadStrategy.offloads(using: state)

if offloads {
viewModel.cancelCurrentTask()
unloadImage()
if state.isInBackground || state.isInDisplay == false {

let offloads = offloadStrategy.offloads(using: state)

if offloads {
viewModel.cancelCurrentTask()
unloadNetworkImage()
}

} else {
if let _ = currentUsingNetworkImage {
startDownload()
}
}

}

open override func layoutSubviews() {
super.layoutSubviews()

if let _ = currentUsingImage, bounds.size != currentUsingContentSize {
if let _ = currentUsingNetworkImage, bounds.size != currentUsingContentSize {
currentUsingContentSize = bounds.size
startDownload()
}
Expand All @@ -134,25 +146,31 @@ open class AsyncMultiplexImageView: UIView {
}

public func setMultiplexImage(_ image: MultiplexImage) {
currentUsingImage = image
currentUsingNetworkImage = image
startDownload()
}

public func setImage(_ image: UIImage) {
currentUsingImage = nil

if clearsContentBeforeDownload {
imageView.image = nil
}

currentUsingNetworkImage = nil
currentUsingImage = image
viewModel.cancelCurrentTask()
imageView.image = image
}

public func clearImage() {
currentUsingImage = nil
currentUsingNetworkImage = nil
imageView.image = nil
viewModel.cancelCurrentTask()
}

private func startDownload() {

guard let image = currentUsingImage else {
guard let image = currentUsingNetworkImage else {
return
}

Expand All @@ -162,10 +180,6 @@ open class AsyncMultiplexImageView: UIView {
return
}

if clearsContentBeforeDownload {
imageView.image = nil
}

// making new candidates
let urls = image._urlsProvider(newSize)

Expand All @@ -189,7 +203,7 @@ open class AsyncMultiplexImageView: UIView {

// TODO: support custom animation

if capturedImage == self.currentUsingImage {
if capturedImage == self.currentUsingNetworkImage {

await MainActor.run {

Expand Down Expand Up @@ -223,16 +237,20 @@ open class AsyncMultiplexImageView: UIView {
viewModel.registerCurrentTask(currentTask)
}

private func unloadImage() {
private func unloadNetworkImage() {

guard let _ = currentUsingNetworkImage else {
return
}

weak var _image = imageView.image
imageView.image = nil

#if DEBUG
#if DEBUG
if _image != nil {
Log.debug(.uiKit, "\(String(describing: _image)) was not deallocated afeter unload")
}
#endif
#endif

}
}
Expand Down