mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2026-03-26 14:10:07 +01:00
♻️ Use TimelineView for periodic updates
This commit is contained in:
@@ -11,7 +11,6 @@ import SwiftUI
|
||||
|
||||
struct CommandHistoryRow: View {
|
||||
let command: LoggedCommand
|
||||
let now: Date
|
||||
let isEvenRow: Bool
|
||||
@Binding var visibleCommandIds: Set<UUID>
|
||||
|
||||
@@ -33,9 +32,17 @@ struct CommandHistoryRow: View {
|
||||
.lineLimit(2)
|
||||
}
|
||||
|
||||
Text(command.durationText(at: now))
|
||||
.font(.system(size: 11))
|
||||
.foregroundColor(.secondary)
|
||||
if command.isCompleted {
|
||||
Text(command.durationText(at: Date()))
|
||||
.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()
|
||||
}
|
||||
|
||||
@@ -12,12 +12,6 @@ struct CommandHistoryView: View {
|
||||
// Provides access to the tracked command history
|
||||
@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
|
||||
@State private var visibleCommandIds: Set<UUID> = []
|
||||
|
||||
@@ -34,7 +28,6 @@ struct CommandHistoryView: View {
|
||||
let isEvenRow = index.isMultiple(of: 2)
|
||||
CommandHistoryRow(
|
||||
command: command,
|
||||
now: now,
|
||||
isEvenRow: isEvenRow,
|
||||
visibleCommandIds: $visibleCommandIds
|
||||
)
|
||||
@@ -50,27 +43,6 @@ struct CommandHistoryView: View {
|
||||
}
|
||||
}
|
||||
.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