Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply if let shorthand syntax #762

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
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
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
2 changes: 1 addition & 1 deletion Sources/Nuke/Decoding/ImageDecoders+Default.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension ImageDecoders {
defer { lock.unlock() }

func makeImage() -> PlatformImage? {
if let thumbnail = self.thumbnail {
if let thumbnail {
return makeThumbnail(data: data, options: thumbnail)
}
return ImageDecoders.Default._decode(data, scale: scale)
Expand Down
6 changes: 3 additions & 3 deletions Sources/Nuke/Internal/Graphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ struct ImageProcessingExtensions {
ctx.clip()
ctx.draw(cgImage, in: CGRect(origin: CGPoint.zero, size: cgImage.size))

if let border = border {
if let border {
ctx.setStrokeColor(border.color.cgColor)
ctx.addPath(path)
ctx.setLineWidth(border.width)
Expand All @@ -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
4 changes: 2 additions & 2 deletions 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 All @@ -62,7 +62,7 @@ private final class ImageSubscription<S>: Subscription where S: Subscriber, S: S
with: request,
queue: nil,
progress: { response, _, _ in
if let response = response {
if let response {
// Send progressively decoded image (if enabled and if any)
_ = subscriber.receive(response)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Nuke/Internal/LinkedList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class LinkedList<Element> {

/// Adds a node to the end of the list.
func append(_ node: Node) {
if let last = last {
if let last {
last.next = node
node.previous = last
self.last = node
Expand Down
6 changes: 3 additions & 3 deletions Sources/Nuke/Pipeline/ImagePipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public final class ImagePipeline: @unchecked Sendable {
continuation.resume(throwing: CancellationError())
}
self.startImageTask(task, progress: { response, progress in
if let response = response {
if let response {
context?.previews?.yield(response)
} else {
context?.progress?.yield(progress)
Expand Down 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/Pipeline/ImagePipelineConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ extension ImagePipeline {
config.dataLoader = dataLoader

let dataCache = try? DataCache(name: name)
if let sizeLimit = sizeLimit {
if let sizeLimit {
dataCache?.sizeLimit = sizeLimit
}
config.dataCache = dataCache
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
8 changes: 4 additions & 4 deletions Sources/Nuke/Tasks/AsyncTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class AsyncTask<Value: Sendable, Error: Sendable>: AsyncTaskSubscriptionDelegate
}

inlineSubscription?.closure(event)
if let subscriptions = subscriptions {
if let subscriptions {
for subscription in subscriptions.values {
subscription.closure(event)
}
Expand Down Expand Up @@ -203,7 +203,7 @@ class AsyncTask<Value: Sendable, Error: Sendable>: AsyncTaskSubscriptionDelegate
// MARK: - Priority

private func updatePriority(suggestedPriority: TaskPriority?) {
if let suggestedPriority = suggestedPriority, suggestedPriority >= priority {
if let suggestedPriority, suggestedPriority >= priority {
// No need to recompute, won't go higher than that
priority = suggestedPriority
return
Expand All @@ -212,7 +212,7 @@ class AsyncTask<Value: Sendable, Error: Sendable>: AsyncTaskSubscriptionDelegate
var newPriority = inlineSubscription?.priority
// Same as subscriptions.map { $0?.priority }.max() but without allocating
// any memory for redundant arrays
if let subscriptions = subscriptions {
if let subscriptions {
for subscription in subscriptions.values {
if newPriority == nil {
newPriority = subscription.priority
Expand Down 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
4 changes: 2 additions & 2 deletions 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 All @@ -74,7 +74,7 @@ final class TaskFetchDecodedImage: ImagePipelineTask<ImageResponse> {
// Lazily creates decoding for task
private func getDecoder(for context: ImageDecodingContext) -> (any ImageDecoding)? {
// Return the existing processor in case it has already been created.
if let decoder = self.decoder {
if let decoder {
return decoder
}
let decoder = pipeline.delegate.imageDecoder(for: context, pipeline: pipeline)
Expand Down
14 changes: 7 additions & 7 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 All @@ -100,7 +100,7 @@ final class TaskFetchOriginalImageData: ImagePipelineTask<(Data, URLResponse?)>
// Check if this is the first response.
if urlResponse == nil {
// See if the server confirmed that the resumable data can be used
if let resumableData = resumableData, ResumableData.isResumedResponse(response) {
if let resumableData, ResumableData.isResumedResponse(response) {
data = resumableData.data
resumedDataCount = Int64(resumableData.data.count)
signpost(self, "LoadImageData", .event, "Resumed with data \(Formatter.bytes(resumedDataCount))")
Expand Down Expand Up @@ -128,7 +128,7 @@ final class TaskFetchOriginalImageData: ImagePipelineTask<(Data, URLResponse?)>
}

private func dataTaskDidFinish(error: Swift.Error?) {
if let error = error {
if let error {
tryToSaveResumableData()
send(error: .dataLoadingFailed(error: error))
return
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
2 changes: 1 addition & 1 deletion Sources/Nuke/Tasks/TaskLoadData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class TaskLoadData: ImagePipelineTask<(Data, URLResponse?)> {
pipeline.cache.cachedData(for: request)
}
pipeline.queue.async {
if let data = data {
if let data {
self.send(value: (data, nil), isCompleted: true)
} else {
self.loadData()
Expand Down
12 changes: 6 additions & 6 deletions Sources/Nuke/Tasks/TaskLoadImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class TaskLoadImage: ImagePipelineTask<ImageResponse> {
pipeline.cache.cachedData(for: request)
}
pipeline.queue.async {
if let data = data {
if let data {
self.didReceiveCachedData(data)
} else {
self.fetchImage()
Expand All @@ -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 All @@ -77,7 +77,7 @@ final class TaskLoadImage: ImagePipelineTask<ImageResponse> {
}

private func didDecodeCachedData(_ response: ImageResponse?) {
if let response = response {
if let response {
decompressImage(response, isCompleted: true, isFromDiskCache: true)
} else {
fetchImage()
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
12 changes: 6 additions & 6 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 @@ -286,7 +286,7 @@ private final class ImageViewController {
}

task = pipeline.loadImage(with: request, queue: .main, progress: { [weak self] response, completedCount, totalCount in
if let response = response, options.isProgressiveRenderingEnabled {
if let response, options.isProgressiveRenderingEnabled {
self?.handle(partialImage: response)
}
progress?(response, completedCount, totalCount)
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
Loading
Loading