perf(efficiency): 落实首批采样减负与发行包瘦身
This commit is contained in:
@@ -8,7 +8,7 @@ enum TrendValueFormat {
|
||||
case watts
|
||||
}
|
||||
|
||||
/// Bounded, timestamp-based curves. Gaps remain gaps; animation only runs on append.
|
||||
/// Bounded, timestamp-based static curves. Gaps remain gaps; no per-sample animation task.
|
||||
struct PanelTrendChart: View {
|
||||
let dates: [Date]
|
||||
let first: [Double?]
|
||||
@@ -33,18 +33,12 @@ struct PanelTrendChart: View {
|
||||
var intervals: [TimeInterval] = []
|
||||
var plotHeight: CGFloat = PanelStyle.plotHeight
|
||||
var showsLegendValues = true
|
||||
@Environment(\.accessibilityReduceMotion) private var reduceMotion
|
||||
@State private var retainedScale: Double = 1
|
||||
@State private var smallSamples = 0
|
||||
@State private var retainedSecondScale: Double = 1
|
||||
@State private var smallSecondSamples = 0
|
||||
@State private var hoveredDate: Date?
|
||||
@State private var hoverPoint: CGPoint?
|
||||
@State private var previousBegin: Date?
|
||||
@State private var previousEnd: Date?
|
||||
@State private var progress: CGFloat = 1
|
||||
@State private var lastFrame: PanelCurveFrame?
|
||||
@State private var displayedFrame: PanelCurveFrame?
|
||||
|
||||
private var inputFrame: PanelCurveFrame {
|
||||
PanelCurveFrame(dates: dates, series: [first, second, third, fourth],
|
||||
@@ -129,11 +123,11 @@ struct PanelTrendChart: View {
|
||||
.lineLimit(1).minimumScaleFactor(0.6).frame(width: fixedScale == 1 ? 28 : 47, height: plotHeight)
|
||||
GeometryReader { proxy in
|
||||
ZStack(alignment: .topLeading) {
|
||||
PanelCurveCanvas(frame: displayedFrame ?? inputFrame, colors: [color, secondColor, thirdColor, fourthColor],
|
||||
PanelCurveCanvas(frame: inputFrame, colors: [color, secondColor, thirdColor, fourthColor],
|
||||
scales: [scale, hasSecondaryScale ? secondaryScale : scale, hasSecondaryScale ? secondaryScale : scale, scale],
|
||||
maximumGap: max(5, expectedInterval * 2),
|
||||
targetBegin: (lastFrame ?? inputFrame).dates.first, targetEnd: (lastFrame ?? inputFrame).dates.last,
|
||||
previousBegin: previousBegin, previousEnd: previousEnd, progress: progress)
|
||||
targetBegin: dates.first, targetEnd: dates.last,
|
||||
previousBegin: nil, previousEnd: nil, progress: 1)
|
||||
if hoveredDate != nil, let index, let begin = dates.first, let end = dates.last, end > begin {
|
||||
let x = proxy.size.width * dates[index].timeIntervalSince(begin) / end.timeIntervalSince(begin)
|
||||
Path { path in
|
||||
@@ -187,8 +181,6 @@ struct PanelTrendChart: View {
|
||||
.onAppear {
|
||||
retainedScale = maximum
|
||||
retainedSecondScale = secondMaximum
|
||||
lastFrame = inputFrame
|
||||
displayedFrame = inputFrame
|
||||
}
|
||||
.onChange(of: dates) { _, _ in
|
||||
let target = maximum
|
||||
@@ -203,28 +195,7 @@ struct PanelTrendChart: View {
|
||||
smallSecondSamples += 1
|
||||
if smallSecondSamples >= 10 { retainedSecondScale = secondTarget; smallSecondSamples = 0 }
|
||||
} else { smallSecondSamples = 0 }
|
||||
let next = inputFrame
|
||||
let old = lastFrame
|
||||
let gap = PanelCurveGeometry.allowedGap(at: next.dates.count - 1, intervals: next.intervals, fallback: max(5, expectedInterval * 2))
|
||||
let append = old.map { PanelCurveGeometry.isAppend(from: $0.dates, to: next.dates, maximumGap: gap) } == true
|
||||
&& !(next.dates.last.map { boundaryDates.contains($0) } ?? false)
|
||||
let animated = append && !reduceMotion
|
||||
var transaction = Transaction(); transaction.disablesAnimations = true
|
||||
withTransaction(transaction) {
|
||||
lastFrame = next
|
||||
displayedFrame = next
|
||||
if animated, let old { displayedFrame = PanelCurveFrame.transition(from: old, to: next) }
|
||||
previousBegin = animated ? old?.dates.first : nil
|
||||
previousEnd = animated ? old?.dates.last : nil
|
||||
progress = animated ? 0 : 1
|
||||
}
|
||||
}
|
||||
.task(id: lastFrame?.dates.last) {
|
||||
guard progress == 0, !reduceMotion else { return }
|
||||
do { try await Task.sleep(for: .milliseconds(16)) } catch { return }
|
||||
withAnimation(.easeInOut(duration: 0.35)) { progress = 1 }
|
||||
}
|
||||
.onChange(of: reduceMotion) { _, reduced in if reduced { progress = 1; previousBegin = nil; previousEnd = nil; lastFrame = inputFrame; displayedFrame = inputFrame } }
|
||||
.accessibilityElement(children: .combine)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user