InvestmentTrackerApp/PortfolioJournal/Models/MonthlySummary.swift

74 lines
2.1 KiB
Swift

import Foundation
struct MonthlySummary {
let periodLabel: String
let startDate: Date
let endDate: Date
let startingValue: Decimal
let endingValue: Decimal
let contributions: Decimal
let netPerformance: Decimal
var netPerformancePercentage: Double {
let base = startingValue + contributions
guard base > 0 else { return 0 }
return NSDecimalNumber(decimal: netPerformance / base).doubleValue * 100
}
var formattedStartingValue: String {
CurrencyFormatter.format(startingValue, style: .currency, maximumFractionDigits: 0)
}
var formattedEndingValue: String {
CurrencyFormatter.format(endingValue, style: .currency, maximumFractionDigits: 0)
}
var formattedContributions: String {
CurrencyFormatter.format(contributions, style: .currency, maximumFractionDigits: 0)
}
var formattedNetPerformance: String {
let prefix = netPerformance >= 0 ? "+" : ""
let value = CurrencyFormatter.format(netPerformance, style: .currency, maximumFractionDigits: 0)
return prefix + value
}
var formattedNetPerformancePercentage: String {
let prefix = netPerformance >= 0 ? "+" : ""
return String(format: "\(prefix)%.2f%%", netPerformancePercentage)
}
static var empty: MonthlySummary {
MonthlySummary(
periodLabel: "This Month",
startDate: Date(),
endDate: Date(),
startingValue: 0,
endingValue: 0,
contributions: 0,
netPerformance: 0
)
}
}
struct PortfolioChange {
let absolute: Decimal
let percentage: Double
let label: String
var formattedAbsolute: String {
let prefix = absolute >= 0 ? "+" : ""
let value = CurrencyFormatter.format(absolute, style: .currency, maximumFractionDigits: 0)
return prefix + value
}
var formattedPercentage: String {
let prefix = absolute >= 0 ? "+" : ""
return String(format: "\(prefix)%.2f%%", percentage)
}
static var empty: PortfolioChange {
PortfolioChange(absolute: 0, percentage: 0, label: "since last update")
}
}