Skip to content

Commit

Permalink
Address tasks not automatically cancelling themselves when views get …
Browse files Browse the repository at this point in the history
…deallocated
  • Loading branch information
stefanceriu committed Aug 15, 2024
1 parent 088c0d0 commit 42d2f5a
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions ElementX/Sources/Other/SwiftUI/Views/LoadableImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,6 @@ private struct LoadableImageContent<TransformerView: View, PlaceholderView: View
}

var body: some View {
let _ = Task {
guard contentLoader.content == nil else {
return
}

await contentLoader.load()
}

ZStack {
switch contentLoader.content {
case .image(let image):
Expand All @@ -141,6 +133,20 @@ private struct LoadableImageContent<TransformerView: View, PlaceholderView: View
}
}
.animation(mediaType == .avatar ? .noAnimation : .elementDefault, value: contentLoader.content)
.task(id: mediaSource) {
guard contentLoader.content == nil else {
return
}

await contentLoader.load()
}
.onDisappear {
guard contentLoader.content == nil else {
return
}

contentLoader.cancel()
}
}

// MARK: - ImageDataProvider
Expand Down Expand Up @@ -214,6 +220,10 @@ private class ContentLoader: ObservableObject {
}
}

func cancel() {
imageLoadingCancellable?.cancel()
}

private var isGIF: Bool {
mediaSource.mimeType == "image/gif"
}
Expand Down

0 comments on commit 42d2f5a

Please sign in to comment.