Skip to content
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
17 changes: 16 additions & 1 deletion whitenoise-mac/Views/MessageMediaViews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ private struct AutomaticMediaDownloadModifier: ViewModifier {
let message: MessageItem
let requiresScrollVisibility: Bool
@State private var isVisibleInScrollView = false
@State private var automaticDownloadTask: Task<Void, Never>?

func body(content: Content) -> some View {
Group {
Expand All @@ -352,6 +353,8 @@ private struct AutomaticMediaDownloadModifier: ViewModifier {
isVisibleInScrollView = isVisible
if isVisible {
startAutomaticDownloadIfNeeded()
} else {
cancelAutomaticDownload()
}
}
} else {
Expand All @@ -362,6 +365,7 @@ private struct AutomaticMediaDownloadModifier: ViewModifier {
}
}
.onChange(of: attachment.id) { _, _ in
cancelAutomaticDownload()
if shouldStartForCurrentVisibility {
startAutomaticDownloadIfNeeded()
}
Expand All @@ -371,6 +375,9 @@ private struct AutomaticMediaDownloadModifier: ViewModifier {
startAutomaticDownloadIfNeeded()
}
}
.onDisappear {
cancelAutomaticDownload()
}
}

private var shouldStartForCurrentVisibility: Bool {
Expand All @@ -379,7 +386,15 @@ private struct AutomaticMediaDownloadModifier: ViewModifier {

private func startAutomaticDownloadIfNeeded() {
guard downloadState.shouldStartAutomaticDownload else { return }
Task { await workspace.loadMediaAttachment(attachment, for: message) }
automaticDownloadTask?.cancel()
automaticDownloadTask = Task {
await workspace.loadMediaAttachment(attachment, for: message)
}
}

private func cancelAutomaticDownload() {
automaticDownloadTask?.cancel()
automaticDownloadTask = nil
}
}

Expand Down
33 changes: 33 additions & 0 deletions whitenoise-macTests/whitenoise_macTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5840,6 +5840,39 @@ struct whitenoise_macTests {
#expect(tileSource.contains("Task { await workspace.loadMediaAttachment(attachment, for: message) }"))
}

@Test func automaticMediaDownloadCancelsWhenTileLeavesViewport() throws {
// AutomaticMediaDownloadModifier is SwiftUI lifecycle wiring, so exercise its source
// contract directly: the task must be retained and cancelled both on scroll-out and
// when SwiftUI removes the tile entirely.
let viewsURL =
URL(fileURLWithPath: #filePath)
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("whitenoise-mac")
.appendingPathComponent("Views")
.appendingPathComponent("MessageMediaViews.swift")
let source = try String(contentsOf: viewsURL, encoding: .utf8)
let modifierStart = try #require(
source.range(of: "private struct AutomaticMediaDownloadModifier: ViewModifier {")
)
let rest = source[modifierStart.upperBound...]
let modifierEnd = try #require(rest.range(of: "\n/// The chat-bubble shape:")?.lowerBound)
let modifierSource = String(source[modifierStart.lowerBound..<modifierEnd])

#expect(modifierSource.contains("@State private var automaticDownloadTask: Task<Void, Never>?"))
#expect(
modifierSource.contains(
"} else {\n cancelAutomaticDownload()"
)
)
#expect(
modifierSource.contains(
".onDisappear {\n cancelAutomaticDownload()\n }"
)
)
#expect(modifierSource.contains("automaticDownloadTask?.cancel()"))
}

@MainActor
@Test func loadMediaAttachmentRetriesFromFailedState() async throws {
let account = AccountSummaryFfi(
Expand Down
Loading