Files
InvestmentTrackerApp/PortfolioJournal/Views/Charts/AllocationSimulatorView.swift
T
alexandrev-tibco 0e0aec6bb9 Charts: fix selección iPad/charts bloqueadas, filtro Rolling 12M, KPIs específicos y botón compartir
Bugs:
- Charts bloqueadas (intermitente) y tiles que no responden: el observer Combine
  registraba el estado como actualizado ANTES de llamar a updateChartData, cuyo
  guard de reentrada descartaba la llamada en silencio — reintentarlo era no-op
  permanente. Ahora el bookkeeping vive dentro de updateChartData, las llamadas
  reentrantes se colean en vez de descartarse, y selectChart computa en síncrono
  (pulsar un tile es determinista y re-pulsar actúa de retry).
- Paywall sheet: movida del Group que cambia con el size class a cada layout —
  en NavigationSplitView no llegaba a presentarse (tiles premium 'no hacían nada').
- Rolling 12M: el filtro de periodo no filtraba — el cálculo necesita histórico
  completo para el lookback, pero ahora la salida respeta selectedTimeRange.

KPIs:
- El header de iPad muestra KPIs específicos del chart activo (los mismos que su
  stats row) en vez de las 5 métricas de cartera fijas; las stats rows dentro de
  las cards se ocultan en regular width para no duplicar (YoY opta por quedarse
  al no tener equivalente en el header).

Compartir:
- Botón de compartir por chart (toolbar iPad + navbar iPhone): renderiza la
  gráfica en una card con branding (BrandMark, KPIs, tagline, QR al App Store
  con ct=chart_share) vía ImageRenderer y abre el share sheet con imagen + link.
- drawingGroup() se desactiva durante el export (ImageRenderer no rasteriza
  capas Metal — salían en blanco). Strings nuevas en 7 idiomas.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WoScpmHdVj1aUf4rAp6hbe
2026-07-08 16:32:42 +02:00

243 lines
8.3 KiB
Swift

import SwiftUI
import Charts
struct AllocationSimulatorView: View {
@Environment(\.chartImageExport) private var chartImageExport
@ObservedObject var viewModel: ChartsViewModel
var body: some View {
VStack(alignment: .leading, spacing: 16) {
Text("Allocation Simulator")
.font(.headline)
if viewModel.simulatorSources.isEmpty {
emptyStateView
} else {
allocationCard
chartSection
}
}
.padding()
.background(Color(.systemBackground))
.cornerRadius(AppConstants.UI.cornerRadius)
.shadow(color: .black.opacity(0.05), radius: 8, y: 2)
}
// MARK: - Empty State
private var emptyStateView: some View {
VStack(spacing: 12) {
Image(systemName: "slider.horizontal.3")
.font(.system(size: 36))
.foregroundColor(.secondary)
Text("No sources available")
.font(.subheadline)
.foregroundColor(.secondary)
}
.frame(maxWidth: .infinity)
.frame(height: 300)
}
// MARK: - Allocation Card
private var allocationCard: some View {
VStack(alignment: .leading, spacing: 12) {
HStack {
Text("Adjust Allocation")
.font(.subheadline.weight(.semibold))
Spacer()
totalAllocationBadge
resetButton
}
ForEach(Array(viewModel.simulatorSources.enumerated()), id: \.element.id) { index, source in
sourceSliderRow(index: index, source: source)
}
}
.padding(12)
.background(Color(.systemGray6))
.cornerRadius(10)
}
private var totalAllocationBadge: some View {
let total = viewModel.simulatorSources.reduce(0) { $0 + $1.simulatedPct }
let isNear100 = abs(total - 100) < 5
return Text(String(format: "%.0f%%", total))
.font(.caption.weight(.semibold))
.padding(.horizontal, 8)
.padding(.vertical, 3)
.background(isNear100 ? Color.green.opacity(0.15) : Color.orange.opacity(0.15))
.foregroundColor(isNear100 ? .green : .orange)
.cornerRadius(8)
}
private var resetButton: some View {
Button {
var updated = viewModel.simulatorSources
for i in updated.indices {
updated[i] = ChartsViewModel.SimulatorSource(
id: updated[i].id,
name: updated[i].name,
currentPct: updated[i].currentPct,
simulatedPct: updated[i].currentPct,
colorHex: updated[i].colorHex
)
}
viewModel.simulatorSources = updated
} label: {
Image(systemName: "arrow.counterclockwise")
.font(.caption)
.foregroundColor(.secondary)
}
.buttonStyle(.plain)
}
private func sourceSliderRow(index: Int, source: ChartsViewModel.SimulatorSource) -> some View {
VStack(spacing: 4) {
HStack {
Circle()
.fill(Color(hex: source.colorHex) ?? .gray)
.frame(width: 8, height: 8)
Text(source.name)
.font(.caption.weight(.medium))
.lineLimit(1)
Spacer()
Text(String(format: "%.0f%%", source.simulatedPct))
.font(.caption.weight(.semibold))
.foregroundColor(.primary)
.frame(width: 36, alignment: .trailing)
Text(String(format: "(%.0f%%)", source.currentPct))
.font(.caption2)
.foregroundColor(.secondary)
}
Slider(
value: Binding<Double>(
get: { source.simulatedPct },
set: { newValue in
var updated = viewModel.simulatorSources
if index < updated.count {
updated[index] = ChartsViewModel.SimulatorSource(
id: source.id,
name: source.name,
currentPct: source.currentPct,
simulatedPct: newValue,
colorHex: source.colorHex
)
viewModel.simulatorSources = updated
}
}
),
in: 0...100,
step: 1
)
.tint(Color(hex: source.colorHex) ?? .appPrimary)
}
}
// MARK: - Chart Section
private var chartSection: some View {
VStack(alignment: .leading, spacing: 8) {
if viewModel.simulatorActualData.isEmpty && viewModel.simulatorData.isEmpty {
Text("Not enough data to simulate.")
.font(.subheadline)
.foregroundColor(.secondary)
.frame(height: 220)
} else {
simulationChart
chartLegend
}
}
}
private var simulationChart: some View {
Chart {
ForEach(viewModel.simulatorActualData, id: \.date) { item in
LineMark(
x: .value("Date", item.date),
y: .value("Value", item.value),
series: .value("Series", "Actual")
)
.foregroundStyle(Color.blue)
.interpolationMethod(.catmullRom)
}
ForEach(viewModel.simulatorData, id: \.date) { item in
LineMark(
x: .value("Date", item.date),
y: .value("Value", item.value),
series: .value("Series", "Simulated")
)
.foregroundStyle(Color.orange)
.lineStyle(StrokeStyle(lineWidth: 2, dash: [6, 3]))
.interpolationMethod(.catmullRom)
}
}
.chartXAxis {
AxisMarks(values: .stride(by: .month, count: xAxisStride)) { value in
AxisGridLine(stroke: StrokeStyle(lineWidth: 0.5, dash: [3, 3]))
.foregroundStyle(Color.secondary.opacity(0.2))
AxisValueLabel(format: .dateTime.month(.abbreviated).year(.twoDigits))
.font(.caption2)
}
}
.chartYAxis {
AxisMarks(position: .leading) { value in
AxisValueLabel {
if let d = value.as(Double.self) {
Text(String(format: "%.0f", d))
.font(.caption2)
}
}
AxisGridLine(stroke: StrokeStyle(lineWidth: 0.5, dash: [3, 3]))
.foregroundStyle(Color.secondary.opacity(0.15))
}
}
.frame(height: 220)
.chartDrawingGroup(disabledForExport: chartImageExport)
}
private var chartLegend: some View {
HStack(spacing: 20) {
legendItem(color: .blue, label: "Actual", dashed: false)
legendItem(color: .orange, label: "Simulated", dashed: true)
Spacer()
}
}
private func legendItem(color: Color, label: String, dashed: Bool) -> some View {
HStack(spacing: 6) {
if dashed {
HStack(spacing: 2) {
RoundedRectangle(cornerRadius: 2)
.fill(color)
.frame(width: 8, height: 3)
RoundedRectangle(cornerRadius: 2)
.fill(color)
.frame(width: 8, height: 3)
}
} else {
RoundedRectangle(cornerRadius: 2)
.fill(color)
.frame(width: 20, height: 3)
}
Text(label)
.font(.caption)
.foregroundColor(.secondary)
}
}
// MARK: - Helpers
private var xAxisStride: Int {
let count = max(viewModel.simulatorActualData.count, viewModel.simulatorData.count)
switch count {
case ...6: return 1
case ...12: return 2
case ...24: return 3
default: return 6
}
}
}