InvestmentTrackerApp/PortfolioJournal/Services/SampleDataService.swift

102 lines
3.6 KiB
Swift

import Foundation
class SampleDataService {
static let shared = SampleDataService()
private init() {}
func seedSampleData() {
let context = CoreDataStack.shared.viewContext
let sourceRepository = InvestmentSourceRepository(context: context)
guard sourceRepository.sourceCount == 0 else { return }
let categoryRepository = CategoryRepository(context: context)
categoryRepository.createDefaultCategoriesIfNeeded()
let accountRepository = AccountRepository(context: context)
let account = accountRepository.createDefaultAccountIfNeeded()
let snapshotRepository = SnapshotRepository(context: context)
let goalRepository = GoalRepository(context: context)
let transactionRepository = TransactionRepository(context: context)
let categories = categoryRepository.categories
let stocksCategory = categories.first { $0.name == "Stocks" } ?? categories.first!
let cryptoCategory = categories.first { $0.name == "Crypto" } ?? categories.first!
let realEstateCategory = categories.first { $0.name == "Real Estate" } ?? categories.first!
let stocks = sourceRepository.createSource(
name: "Index Fund",
category: stocksCategory,
account: account
)
let crypto = sourceRepository.createSource(
name: "BTC",
category: cryptoCategory,
account: account
)
let realEstate = sourceRepository.createSource(
name: "Rental Property",
category: realEstateCategory,
account: account
)
seedSnapshots(for: stocks, baseValue: 12000, monthlyIncrease: 450, repository: snapshotRepository)
seedSnapshots(for: crypto, baseValue: 3000, monthlyIncrease: 250, repository: snapshotRepository)
seedSnapshots(for: realEstate, baseValue: 80000, monthlyIncrease: 600, repository: snapshotRepository)
seedMonthlyNotes()
transactionRepository.createTransaction(
source: stocks,
type: .buy,
date: Calendar.current.date(byAdding: .month, value: -3, to: Date()) ?? Date(),
shares: 10,
price: 400,
amount: nil,
notes: "Sample buy"
)
_ = goalRepository.createGoal(
name: "1M Goal",
targetAmount: 1_000_000,
targetDate: nil,
account: account
)
}
private func seedSnapshots(
for source: InvestmentSource,
baseValue: Decimal,
monthlyIncrease: Decimal,
repository: SnapshotRepository
) {
let calendar = Calendar.current
for monthOffset in (0..<6).reversed() {
let date = calendar.date(byAdding: .month, value: -monthOffset, to: Date()) ?? Date()
let value = baseValue + Decimal(monthOffset) * monthlyIncrease
repository.createSnapshot(
for: source,
date: date,
value: value,
contribution: monthOffset == 0 ? monthlyIncrease : nil,
notes: nil
)
}
}
private func seedMonthlyNotes() {
let calendar = Calendar.current
let notes = [
"Rebalanced slightly toward equities. Stayed calm despite noise.",
"Focused on contributions. No major changes.",
"Reviewed allocation drift and decided to hold positions."
]
for (index, note) in notes.enumerated() {
let date = calendar.date(byAdding: .month, value: -index, to: Date()) ?? Date()
MonthlyCheckInStore.setNote(note, for: date)
}
}
}