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
37 changes: 34 additions & 3 deletions openHAB/NewImageUITableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class NewImageUITableViewCell: GenericUITableViewCell, NoIconDisplayableCell {
private var chartStyle: ChartStyle = .light
private var activeTask: Task<Void, Never>?
private var cachedImage: UIImage?
private var cachedWidgetId: String?

var openHABRootUrl: String?

Expand Down Expand Up @@ -101,11 +102,38 @@ class NewImageUITableViewCell: GenericUITableViewCell, NoIconDisplayableCell {
}
}

override func prepareForReuse() {
super.prepareForReuse()

// Cancel any active image loading task
activeTask?.cancel()
activeTask = nil

// Invalidate and clear timer
refreshTimer?.invalidate()
refreshTimer = nil

// Clear cached image and widget ID to prevent showing wrong content in reused cells
cachedImage = nil
cachedWidgetId = nil
mainImageView?.image = nil

// Reset chart style
chartStyle = OHInterfaceStyle.current == .light ? ChartStyle.light : ChartStyle.dark
}

override func displayWidget() {
if cachedImage == nil {
loadImage()
} else {
// Check if we can reuse the cached image for the same widget
let currentWidgetId = widget?.id
let canReuseCache = cachedImage != nil && cachedWidgetId == currentWidgetId

if canReuseCache {
mainImageView.image = cachedImage
} else {
// Different widget, clear cache and load new image
cachedImage = nil
cachedWidgetId = currentWidgetId
loadImage()
}
// If widget have a refresh rate configured, i.e. different from zero, schedule an image update timer
if widget.refresh != 0 {
Expand All @@ -129,6 +157,7 @@ class NewImageUITableViewCell: GenericUITableViewCell, NoIconDisplayableCell {
switch widgetPayload {
case let .embedded(image):
cachedImage = image
cachedWidgetId = widget?.id
mainImageView.image = image
didLoad?()
case let .link(url):
Expand Down Expand Up @@ -172,6 +201,7 @@ class NewImageUITableViewCell: GenericUITableViewCell, NoIconDisplayableCell {
let (data, _): (Data, URLResponse) = try await client.doRequest(baseURL: url, timeout: 10.0, type: .data, cacheingPolicy: !shouldCache ? .reloadIgnoringCacheData : .useProtocolCachePolicy)
await MainActor.run {
self.cachedImage = UIImage(data: data)
self.cachedWidgetId = self.widget?.id
self.mainImageView?.image = self.cachedImage
self.didLoad?()
}
Expand Down Expand Up @@ -202,5 +232,6 @@ extension NewImageUITableViewCell: GenericCellCacheProtocol {
func invalidateCache() {
refreshTimer?.invalidate()
cachedImage = nil
cachedWidgetId = nil
}
}
1 change: 0 additions & 1 deletion openHABWatch/Domain/UserData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ final class UserData: ObservableObject {

func startPageHandling(sitemapName: String, pageId: String = "") {
// Don't clear widgets immediately when switching - use cached data during transition
let shouldPreserveWidgets = !widgets.isEmpty
pageHandlingTask?.cancel()

pageHandlingTask = Task {
Expand Down