← All repositories

listless

A simple list app for Apple platforms

git clone https://code.inqk.net/listless.git


commit 65038672c898b6e81e81c9377618f07ff1dace23
parent 3e9ff6f4adcce67cbf66dff2af8943f1efd84839
Author: Michael Camilleri <[email protected]>
Date:   Thu, 19 Feb 2026 05:46:16 +0900

Use cache when calculating accent colours

Co-Authored-By: Codex GPT 5.3 <[email protected]>

Diffstat

MListless/Helpers/AccentColor.swift | 22++++++++++++++++++++++
MListlessMac/Views/TaskRowView.swift | 3++-
MListlessiOS/Views/TaskRowView.swift | 3++-

3 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/Listless/Helpers/AccentColor.swift b/Listless/Helpers/AccentColor.swift

@@ -1,5 +1,15 @@
 import SwiftUI
 
+private struct TaskAccentColorKey: Hashable {
+    let index: Int
+    let total: Int
+}
+
+@MainActor
+private enum TaskAccentColorCache {
+    static var colors: [TaskAccentColorKey: Color] = [:]
+}
+
 func taskColor(forIndex index: Int, total: Int) -> Color {
     guard total > 1 else { return Color(hue: 0.98, saturation: 0.85, brightness: 1.0) }
 
@@ -16,6 +26,18 @@ func taskColor(forIndex index: Int, total: Int) -> Color {
     }
 }
 
+@MainActor
+func cachedTaskColor(forIndex index: Int, total: Int) -> Color {
+    let key = TaskAccentColorKey(index: index, total: total)
+    if let cached = TaskAccentColorCache.colors[key] {
+        return cached
+    }
+
+    let computed = taskColor(forIndex: index, total: total)
+    TaskAccentColorCache.colors[key] = computed
+    return computed
+}
+
 private func interpolateHSB(
     from: (h: Double, s: Double, b: Double),
     to: (h: Double, s: Double, b: Double),

diff --git a/ListlessMac/Views/TaskRowView.swift b/ListlessMac/Views/TaskRowView.swift

@@ -26,9 +26,10 @@ struct TaskRowView: View {
         horizontalPadding + checkboxSize + checkboxTextSpacing
     }
 
+    @MainActor
     private func computeAccentColor() -> Color {
         guard !task.isCompleted else { return .clear }
-        return taskColor(forIndex: index, total: totalTasks)
+        return cachedTaskColor(forIndex: index, total: totalTasks)
     }
 
     init(

diff --git a/ListlessiOS/Views/TaskRowView.swift b/ListlessiOS/Views/TaskRowView.swift

@@ -174,9 +174,10 @@ struct TaskRowView: View {
         )
     }
 
+    @MainActor
     private func computeAccentColor() -> Color {
         guard !task.isCompleted else { return .clear }
-        return taskColor(forIndex: index, total: totalTasks)
+        return cachedTaskColor(forIndex: index, total: totalTasks)
     }
 
     @ViewBuilder