mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2026-04-01 17:20:09 +02:00
♻️ Use TimelineView for periodic updates
This commit is contained in:
@@ -11,7 +11,6 @@ import SwiftUI
|
|||||||
|
|
||||||
struct CommandHistoryRow: View {
|
struct CommandHistoryRow: View {
|
||||||
let command: LoggedCommand
|
let command: LoggedCommand
|
||||||
let now: Date
|
|
||||||
let isEvenRow: Bool
|
let isEvenRow: Bool
|
||||||
@Binding var visibleCommandIds: Set<UUID>
|
@Binding var visibleCommandIds: Set<UUID>
|
||||||
|
|
||||||
@@ -33,9 +32,17 @@ struct CommandHistoryRow: View {
|
|||||||
.lineLimit(2)
|
.lineLimit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
Text(command.durationText(at: now))
|
if command.isCompleted {
|
||||||
.font(.system(size: 11))
|
Text(command.durationText(at: Date()))
|
||||||
.foregroundColor(.secondary)
|
.font(.system(size: 11))
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
} else {
|
||||||
|
TimelineView(.periodic(from: .now, by: 0.08)) { context in
|
||||||
|
Text(command.durationText(at: context.date))
|
||||||
|
.font(.system(size: 11))
|
||||||
|
.foregroundColor(.secondary)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,12 +12,6 @@ struct CommandHistoryView: View {
|
|||||||
// Provides access to the tracked command history
|
// Provides access to the tracked command history
|
||||||
@ObservedObject var commandTracker: CommandTracker
|
@ObservedObject var commandTracker: CommandTracker
|
||||||
|
|
||||||
// Timestamp used to compute duration labels in the list; updated when tick fires
|
|
||||||
@State private var now = Date()
|
|
||||||
|
|
||||||
// Tracks whether the window view is currently visible
|
|
||||||
@State private var isWindowVisible = false
|
|
||||||
|
|
||||||
// IDs for visible, active rows; used to avoid ticking when none are on-screen
|
// IDs for visible, active rows; used to avoid ticking when none are on-screen
|
||||||
@State private var visibleCommandIds: Set<UUID> = []
|
@State private var visibleCommandIds: Set<UUID> = []
|
||||||
|
|
||||||
@@ -34,7 +28,6 @@ struct CommandHistoryView: View {
|
|||||||
let isEvenRow = index.isMultiple(of: 2)
|
let isEvenRow = index.isMultiple(of: 2)
|
||||||
CommandHistoryRow(
|
CommandHistoryRow(
|
||||||
command: command,
|
command: command,
|
||||||
now: now,
|
|
||||||
isEvenRow: isEvenRow,
|
isEvenRow: isEvenRow,
|
||||||
visibleCommandIds: $visibleCommandIds
|
visibleCommandIds: $visibleCommandIds
|
||||||
)
|
)
|
||||||
@@ -50,27 +43,6 @@ struct CommandHistoryView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
.frame(minWidth: 400, minHeight: 200)
|
.frame(minWidth: 400, minHeight: 200)
|
||||||
.onAppear {
|
|
||||||
// Mark the window as visible so duration ticking can start
|
|
||||||
isWindowVisible = true
|
|
||||||
now = Date()
|
|
||||||
}
|
|
||||||
.onDisappear {
|
|
||||||
// Stop ticking when the window disappears
|
|
||||||
isWindowVisible = false
|
|
||||||
}
|
|
||||||
.onReceive(Timer.publish(every: 0.08, on: .main, in: .common).autoconnect()) { _ in
|
|
||||||
// Only update running commands if the window is visible + there's command IDs that are:
|
|
||||||
// - visible (in window, based on scroll position)
|
|
||||||
// - running (so we need to update the timestamp periodically)
|
|
||||||
if commandTracker.isActive, shouldTick {
|
|
||||||
now = Date()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var shouldTick: Bool {
|
|
||||||
isWindowVisible && !visibleCommandIds.isEmpty
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user