Primera build enviada
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import Foundation
|
||||
import CoreData
|
||||
|
||||
class SampleDataService {
|
||||
static let shared = SampleDataService()
|
||||
@@ -20,10 +21,32 @@ class SampleDataService {
|
||||
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 categories = fetchCategories(in: context)
|
||||
guard let fallbackCategory = categories.first else { return }
|
||||
let stocksCategory = resolveCategory(
|
||||
named: "Stocks",
|
||||
fallback: fallbackCategory,
|
||||
colorHex: "#3B82F6",
|
||||
icon: "chart.line.uptrend.xyaxis",
|
||||
repository: categoryRepository,
|
||||
context: context
|
||||
)
|
||||
let cryptoCategory = resolveCategory(
|
||||
named: "Crypto",
|
||||
fallback: fallbackCategory,
|
||||
colorHex: "#10B981",
|
||||
icon: "bitcoinsign.circle.fill",
|
||||
repository: categoryRepository,
|
||||
context: context
|
||||
)
|
||||
let realEstateCategory = resolveCategory(
|
||||
named: "Real Estate",
|
||||
fallback: fallbackCategory,
|
||||
colorHex: "#F59E0B",
|
||||
icon: "house.fill",
|
||||
repository: categoryRepository,
|
||||
context: context
|
||||
)
|
||||
|
||||
let stocks = sourceRepository.createSource(
|
||||
name: "Index Fund",
|
||||
@@ -98,4 +121,28 @@ class SampleDataService {
|
||||
MonthlyCheckInStore.setNote(note, for: date)
|
||||
}
|
||||
}
|
||||
|
||||
private func fetchCategories(in context: NSManagedObjectContext) -> [Category] {
|
||||
let request: NSFetchRequest<Category> = Category.fetchRequest()
|
||||
request.sortDescriptors = [
|
||||
NSSortDescriptor(keyPath: \Category.sortOrder, ascending: true),
|
||||
NSSortDescriptor(keyPath: \Category.name, ascending: true)
|
||||
]
|
||||
return (try? context.fetch(request)) ?? []
|
||||
}
|
||||
|
||||
private func resolveCategory(
|
||||
named name: String,
|
||||
fallback: Category,
|
||||
colorHex: String,
|
||||
icon: String,
|
||||
repository: CategoryRepository,
|
||||
context: NSManagedObjectContext
|
||||
) -> Category {
|
||||
if let existing = fetchCategories(in: context)
|
||||
.first(where: { $0.name == name }) {
|
||||
return existing
|
||||
}
|
||||
return repository.createCategory(name: name, colorHex: colorHex, icon: icon)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user