Skip to content
This repository has been archived by the owner on Oct 1, 2022. It is now read-only.

Commit

Permalink
Fix #14
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Jul 26, 2021
1 parent b53a01c commit 1473264
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
14 changes: 0 additions & 14 deletions Sources/Internal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,3 @@ extension UIColor {
}
}
#endif

extension ImageRequest {
struct ID: Hashable {
let imageId: String?
let priority: ImageRequest.Priority
let options: ImageRequest.Options

init(_ request: ImageRequest) {
self.imageId = request.imageId
self.priority = request.priority
self.options = request.options
}
}
}
39 changes: 27 additions & 12 deletions Sources/LazyImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ public typealias ImageRequest = Nuke.ImageRequest
public typealias ImagePipeline = Nuke.ImagePipeline
public typealias ImageContainer = Nuke.ImageContainer

private struct HashableRequest: Hashable {
let request: ImageRequest

func hash(into hasher: inout Hasher) {
hasher.combine(request.imageId)
hasher.combine(request.options)
hasher.combine(request.priority)
}

static func == (lhs: HashableRequest, rhs: HashableRequest) -> Bool {
let lhs = lhs.request
let rhs = rhs.request
return lhs.imageId == rhs.imageId &&
lhs.priority == rhs.priority &&
lhs.options == rhs.options
}

}

/// Lazily loads and displays images.
///
/// The image view is lazy and doesn't know the size of the image before it is
Expand All @@ -21,8 +40,7 @@ public typealias ImageContainer = Nuke.ImageContainer
public struct LazyImage<Content: View>: View {
@StateObject private var model = FetchImage()

private let request: ImageRequest?
private let requestId: ImageRequest.ID?
private let request: HashableRequest?

#if !os(watchOS)
private var onCreated: ((ImageView) -> Void)?
Expand Down Expand Up @@ -51,8 +69,7 @@ public struct LazyImage<Content: View>: View {
/// - source: The image source (`String`, `URL`, `URLRequest`, or `ImageRequest`)
/// - resizingMode: `.aspectFill` by default.
public init(source: ImageRequestConvertible?, resizingMode: ImageResizingMode = .aspectFill) where Content == Image {
self.request = source?.asImageRequest()
self.requestId = request.map(ImageRequest.ID.init)
self.request = source.map { HashableRequest(request: $0.asImageRequest()) }
self.resizingMode = resizingMode
}
#else
Expand All @@ -61,8 +78,7 @@ public struct LazyImage<Content: View>: View {
/// - Parameters:
/// - source: The image source (`String`, `URL`, `URLRequest`, or `ImageRequest`)
public init(source: ImageRequestConvertible?) where Content == Image {
self.request = source?.asImageRequest()
self.requestId = request.map(ImageRequest.ID.init)
self.request = source.map { HashableRequest(request: $0.asImageRequest()) }
}
#endif

Expand All @@ -84,8 +100,7 @@ public struct LazyImage<Content: View>: View {
/// }
/// ```
public init(source: ImageRequestConvertible?, @ViewBuilder content: @escaping (LazyImageState) -> Content) {
self.request = source?.asImageRequest()
self.requestId = request.map(ImageRequest.ID.init)
self.request = source.map { HashableRequest(request: $0.asImageRequest()) }
self.makeContent = content
}

Expand Down Expand Up @@ -180,7 +195,7 @@ public struct LazyImage<Content: View>: View {
}
.onAppear(perform: onAppear)
.onDisappear(perform: onDisappear)
.onChange(of: requestId) { _ in load() }
.onChange(of: request, perform: load)
}

@ViewBuilder private var content: some View {
Expand Down Expand Up @@ -232,12 +247,12 @@ public struct LazyImage<Content: View>: View {
model.onFailure = onFailure
model.onCompletion = onCompletion

load()
load(request)
}

private func load() {
private func load(_ request: HashableRequest?) {
ImageDecoders.Video.register()
model.load(request)
model.load(request?.request)
}

private func onDisappear() {
Expand Down

0 comments on commit 1473264

Please sign in to comment.