Skip to content

Commit

Permalink
Apply guard let shorthand syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-han committed Mar 7, 2024
1 parent df6057f commit 36b5836
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ ImageDecoderRegistry.shared.register { context in

let url = URL(string: "https://upload.wikimedia.org/wikipedia/commons/9/9d/Swift_logo.svg")
ImagePipeline.shared.loadImage(with: url) { [weak self] result in
guard let self = self, let data = try? result.get().container.data else {
guard let self, let data = try? result.get().container.data else {
return
}
// You can render an image using whatever size you want, vector!
Expand Down
2 changes: 1 addition & 1 deletion Sources/Nuke/Caching/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ final class Cache<Key: Hashable, Value>: @unchecked Sendable {
let cost: Int
let expiration: Date?
var isExpired: Bool {
guard let expiration = expiration else {
guard let expiration else {
return false
}
return expiration.timeIntervalSinceNow < 0
Expand Down
2 changes: 1 addition & 1 deletion Sources/Nuke/Decoding/AssetType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extension AssetType {
return false
}
return zip(numbers.indices, numbers).allSatisfy { index, number in
guard let number = number else { return true }
guard let number else { return true }
guard (index + offset) < data.count else { return false }
return data[index + offset] == number
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Nuke/Internal/Graphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ extension PlatformImage {
///
/// - parameter drawRect: `nil` by default. If `nil` will use the canvas rect.
func draw(inCanvasWithSize canvasSize: CGSize, drawRect: CGRect? = nil) -> PlatformImage? {
guard let cgImage = cgImage else {
guard let cgImage else {
return nil
}
guard let ctx = CGContext.make(cgImage, size: canvasSize) else {
Expand All @@ -151,7 +151,7 @@ extension PlatformImage {
return preparingForDisplay()
}
#endif
guard let cgImage = cgImage else {
guard let cgImage else {
return nil
}
return draw(inCanvasWithSize: cgImage.size, drawRect: CGRect(origin: .zero, size: cgImage.size))
Expand Down
2 changes: 1 addition & 1 deletion Sources/Nuke/Internal/ImagePublisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private final class ImageSubscription<S>: Subscription where S: Subscriber, S: S

func request(_ demand: Subscribers.Demand) {
guard demand > 0 else { return }
guard let subscriber = subscriber else { return }
guard let subscriber else { return }

if let image = pipeline.cache[request] {
_ = subscriber.receive(ImageResponse(container: image, request: request, cacheType: .memory))
Expand Down
4 changes: 2 additions & 2 deletions Sources/Nuke/Pipeline/ImagePipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public final class ImagePipeline: @unchecked Sendable {

tasks[task] = makeTaskLoadImage(for: task.request)
.subscribe(priority: task.priority.taskPriority, subscriber: task) { [weak self, weak task] event in
guard let self = self, let task = task else { return }
guard let self, let task else { return }

if event.isCompleted {
self.tasks[task] = nil
Expand Down Expand Up @@ -425,7 +425,7 @@ public final class ImagePipeline: @unchecked Sendable {

tasks[task] = makeTaskLoadData(for: task.request)
.subscribe(priority: task.priority.taskPriority, subscriber: task) { [weak self, weak task] event in
guard let self = self, let task = task else { return }
guard let self, let task else { return }

if event.isCompleted {
self.tasks[task] = nil
Expand Down
2 changes: 1 addition & 1 deletion Sources/Nuke/Prefetching/ImagePrefetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public final class ImagePrefetcher: @unchecked Sendable {
}
let task = Task(request: request, key: key)
task.operation = queue.add { [weak self] finish in
guard let self = self else { return finish() }
guard let self else { return finish() }
self.loadImage(task: task, finish: finish)
}
tasks[key] = task
Expand Down
2 changes: 1 addition & 1 deletion Sources/Nuke/Tasks/AsyncTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ extension AsyncTask {
/// - notes: Returns `nil` if the task is already disposed.
func subscribe<NewValue>(_ task: AsyncTask<NewValue, Error>, onValue: @escaping (Value, Bool) -> Void) -> TaskSubscription? {
subscribe(subscriber: task) { [weak task] event in
guard let task = task else { return }
guard let task else { return }
switch event {
case let .value(value, isCompleted):
onValue(value, isCompleted)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Nuke/Tasks/OperationTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class OperationTask<T: Sendable>: AsyncTask<T, Swift.Error> {

override func start() {
operation = queue.add { [weak self] in
guard let self = self else { return }
guard let self else { return }
let result = Result(catching: { try self.process() })
self.pipeline.queue.async {
switch result {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Nuke/Tasks/TaskFetchDecodedImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class TaskFetchDecodedImage: ImagePipelineTask<ImageResponse> {
didFinishDecoding(decoder: decoder, context: context, result: decode())
} else {
operation = pipeline.configuration.imageDecodingQueue.add { [weak self] in
guard let self = self else { return }
guard let self else { return }

let result = decode()
self.pipeline.queue.async {
Expand Down
10 changes: 5 additions & 5 deletions Sources/Nuke/Tasks/TaskFetchOriginalImageData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class TaskFetchOriginalImageData: ImagePipelineTask<(Data, URLResponse?)>
// Rate limiter is synchronized on pipeline's queue. Delayed work is
// executed asynchronously also on the same queue.
rateLimiter.execute { [weak self] in
guard let self = self, !self.isDisposed else {
guard let self, !self.isDisposed else {
return false
}
self.loadData(urlRequest: urlRequest)
Expand All @@ -41,7 +41,7 @@ final class TaskFetchOriginalImageData: ImagePipelineTask<(Data, URLResponse?)>
// Wrap data request in an operation to limit the maximum number of
// concurrent data tasks.
operation = pipeline.configuration.dataLoadingQueue.add { [weak self] finish in
guard let self = self else {
guard let self else {
return finish()
}
self.pipeline.queue.async {
Expand Down Expand Up @@ -72,21 +72,21 @@ final class TaskFetchOriginalImageData: ImagePipelineTask<(Data, URLResponse?)>

let dataLoader = pipeline.delegate.dataLoader(for: request, pipeline: pipeline)
let dataTask = dataLoader.loadData(with: urlRequest, didReceiveData: { [weak self] data, response in
guard let self = self else { return }
guard let self else { return }
self.pipeline.queue.async {
self.dataTask(didReceiveData: data, response: response)
}
}, completion: { [weak self] error in
finish() // Finish the operation!
guard let self = self else { return }
guard let self else { return }
signpost(self, "LoadImageData", .end, "Finished with size \(Formatter.bytes(self.data.count))")
self.pipeline.queue.async {
self.dataTaskDidFinish(error: error)
}
})

onCancelled = { [weak self] in
guard let self = self else { return }
guard let self else { return }

signpost(self, "LoadImageData", .end, "Cancelled")
dataTask.cancel()
Expand Down
6 changes: 3 additions & 3 deletions Sources/Nuke/Tasks/TaskFetchWithPublisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class TaskFetchWithPublisher: ImagePipelineTask<(Data, URLResponse?)> {
// Wrap data request in an operation to limit the maximum number of
// concurrent data tasks.
operation = pipeline.configuration.dataLoadingQueue.add { [weak self] finish in
guard let self = self else {
guard let self else {
return finish()
}
self.pipeline.queue.async {
Expand All @@ -39,12 +39,12 @@ final class TaskFetchWithPublisher: ImagePipelineTask<(Data, URLResponse?)> {

let cancellable = publisher.sink(receiveCompletion: { [weak self] result in
finish() // Finish the operation!
guard let self = self else { return }
guard let self else { return }
self.pipeline.queue.async {
self.dataTaskDidFinish(result)
}
}, receiveValue: { [weak self] data in
guard let self = self else { return }
guard let self else { return }
self.pipeline.queue.async {
self.data.append(data)
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Nuke/Tasks/TaskLoadImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ final class TaskLoadImage: ImagePipelineTask<ImageResponse> {
didDecodeCachedData(decode())
} else {
operation = pipeline.configuration.imageDecodingQueue.add { [weak self] in
guard let self = self else { return }
guard let self else { return }
let response = decode()
self.pipeline.queue.async {
self.didDecodeCachedData(response)
Expand Down Expand Up @@ -161,7 +161,7 @@ final class TaskLoadImage: ImagePipelineTask<ImageResponse> {
return response
}
}).subscribe(priority: priority) { [weak self] event in
guard let self = self else { return }
guard let self else { return }
if event.isCompleted {
self.dependency2 = nil
}
Expand Down Expand Up @@ -196,7 +196,7 @@ final class TaskLoadImage: ImagePipelineTask<ImageResponse> {
guard !isDisposed else { return }

operation = pipeline.configuration.imageDecompressingQueue.add { [weak self] in
guard let self = self else { return }
guard let self else { return }

let response = signpost("DecompressImage", isCompleted ? "FinalImage" : "ProgressiveImage") {
self.pipeline.delegate.decompress(response: response, request: self.request, pipeline: self.pipeline)
Expand Down Expand Up @@ -240,7 +240,7 @@ final class TaskLoadImage: ImagePipelineTask<ImageResponse> {
let encoder = pipeline.delegate.imageEncoder(for: context, pipeline: pipeline)
let key = pipeline.cache.makeDataCacheKey(for: request)
pipeline.configuration.imageEncodingQueue.addOperation { [weak pipeline, request] in
guard let pipeline = pipeline else { return }
guard let pipeline else { return }
let encodedData = signpost("EncodeImage") {
encoder.encode(response.container, context: context)
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/NukeExtensions/ImageViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private final class ImageViewController {
) -> ImageTask? {
cancelOutstandingTask()

guard let imageView = imageView else {
guard let imageView else {
return nil
}

Expand All @@ -254,7 +254,7 @@ private final class ImageViewController {
}

// Handle a scenario where request is `nil` (in the same way as a failure)
guard var request = request else {
guard var request else {
if options.isPrepareForReuseEnabled {
imageView.nuke_display(image: nil, data: nil)
}
Expand Down Expand Up @@ -323,7 +323,7 @@ private final class ImageViewController {
#if os(iOS) || os(tvOS) || os(macOS) || os(visionOS)

private func display(_ image: ImageContainer, _ isFromMemory: Bool, _ response: ImageLoadingOptions.ResponseType) {
guard let imageView = imageView else {
guard let imageView else {
return
}

Expand Down Expand Up @@ -371,7 +371,7 @@ extension ImageViewController {
#if os(iOS) || os(tvOS) || os(visionOS)

private func runFadeInTransition(image: ImageContainer, params: ImageLoadingOptions.Transition.Parameters, response: ImageLoadingOptions.ResponseType) {
guard let imageView = imageView else {
guard let imageView else {
return
}

Expand All @@ -385,7 +385,7 @@ extension ImageViewController {
}

private func runSimpleFadeIn(image: ImageContainer, params: ImageLoadingOptions.Transition.Parameters) {
guard let imageView = imageView else {
guard let imageView else {
return
}

Expand Down
10 changes: 5 additions & 5 deletions Sources/NukeUI/FetchImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public final class FetchImage: ObservableObject, Identifiable {

reset()

guard var request = request else {
guard var request else {
handle(result: .failure(ImagePipeline.Error.imageRequestMissing))
return
}
Expand Down Expand Up @@ -132,7 +132,7 @@ public final class FetchImage: ObservableObject, Identifiable {
let task = pipeline.loadImage(
with: request,
progress: { [weak self] response, completed, total in
guard let self = self else { return }
guard let self else { return }
if let response {
withTransaction(self.transaction) {
self.handle(preview: response)
Expand All @@ -143,7 +143,7 @@ public final class FetchImage: ObservableObject, Identifiable {
}
},
completion: { [weak self] result in
guard let self = self else { return }
guard let self else { return }
withTransaction(self.transaction) {
self.handle(result: result.mapError { $0 })
}
Expand Down Expand Up @@ -205,7 +205,7 @@ public final class FetchImage: ObservableObject, Identifiable {
// Not using `first()` because it should support progressive decoding
isLoading = true
cancellable = publisher.sink(receiveCompletion: { [weak self] completion in
guard let self = self else { return }
guard let self else { return }
self.isLoading = false
switch completion {
case .finished:
Expand All @@ -216,7 +216,7 @@ public final class FetchImage: ObservableObject, Identifiable {
self.result = .failure(error)
}
}, receiveValue: { [weak self] response in
guard let self = self else { return }
guard let self else { return }
self.lastResponse = response
self.imageContainer = response.container
})
Expand Down
8 changes: 4 additions & 4 deletions Sources/NukeUI/LazyImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public final class LazyImageView: _PlatformBaseView {
isResetNeeded = true
}

guard var request = request else {
guard var request else {
handle(result: .failure(ImagePipeline.Error.imageRequestMissing), isSync: true)
return
}
Expand Down Expand Up @@ -290,7 +290,7 @@ public final class LazyImageView: _PlatformBaseView {
with: request,
queue: .main,
progress: { [weak self] response, completed, total in
guard let self = self else { return }
guard let self else { return }
let progress = ImageTask.Progress(completed: completed, total: total)
if let response {
self.handle(preview: response)
Expand Down Expand Up @@ -358,7 +358,7 @@ public final class LazyImageView: _PlatformBaseView {
}

private func setPlaceholderImage(_ placeholderImage: PlatformImage?) {
guard let placeholderImage = placeholderImage else {
guard let placeholderImage else {
placeholderView = nil
return
}
Expand Down Expand Up @@ -393,7 +393,7 @@ public final class LazyImageView: _PlatformBaseView {
}

private func setFailureImage(_ failureImage: PlatformImage?) {
guard let failureImage = failureImage else {
guard let failureImage else {
failureView = nil
return
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/NukeVideo/VideoPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public final class VideoPlayerView: _PlatformBaseView {
}

public func play() {
guard let asset = asset else {
guard let asset else {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class ImagePipelineProgressiveDecodingTests: XCTestCase {
// applies the remaining processors and delivers it
let previewDelivered = self.expectation(description: "previewDelivered")
pipeline.loadImage(with: request) { response, _, _ in
guard let response = response else {
guard let response else {
return XCTFail()
}
XCTAssertEqual(response.image.nk_test_processorIDs, ["2"])
Expand All @@ -339,7 +339,7 @@ class ImagePipelineProgressiveDecodingTests: XCTestCase {
// applies the remaining processors and delivers it
let previewDelivered = self.expectation(description: "previewDelivered")
pipeline.loadImage(with: request) { response, _, _ in
guard let response = response else {
guard let response else {
return
}
XCTAssertTrue(response.container.isPreview)
Expand Down

0 comments on commit 36b5836

Please sign in to comment.