Files
Reflect/Shared/Views/HeaderPercView.swift
Trey t aaaf04f05e Migrate from Core Data to SwiftData
- Replace Core Data with SwiftData for iOS 18+
- Create MoodEntryModel as @Model class replacing MoodEntry entity
- Create SharedModelContainer for App Group container sharing
- Create DataController with CRUD extensions replacing PersistenceController
- Update all views and view models to use MoodEntryModel
- Update widget extension to use SwiftData
- Remove old Core Data files (Persistence*.swift, .xcdatamodeld)
- Add EntryType enum with all entry type cases
- Fix widget label truncation with proper spacing and text scaling

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 15:08:05 -06:00

121 lines
4.0 KiB
Swift

//
// HeaderPercView.swift
// Feels (iOS)
//
// Created by Trey Tartt on 1/29/22.
//
import SwiftUI
enum PercViewType {
case text
case shape
}
struct HeaderPercView: View {
@AppStorage(UserDefaultsStore.Keys.moodTint.rawValue, store: GroupUserDefaults.groupDefaults) private var moodTint: MoodTints = .Default
@AppStorage(UserDefaultsStore.Keys.textColor.rawValue, store: GroupUserDefaults.groupDefaults) private var textColor: Color = DefaultTextColor.textColor
@AppStorage(UserDefaultsStore.Keys.shape.rawValue, store: GroupUserDefaults.groupDefaults) private var shape: BGShape = .circle
@State private var entries = [MoodMetrics]()
let backDays: Int
let type: PercViewType
@MainActor
init(fakeData: Bool, backDays: Int, type: PercViewType) {
self.type = type
self.backDays = backDays
var moodEntries: [MoodEntryModel]?
if fakeData {
moodEntries = DataController.shared.randomEntries(count: 10)
} else {
var daysAgo = Calendar.current.date(byAdding: .day, value: -backDays, to: Date())!
daysAgo = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: daysAgo)!
moodEntries = DataController.shared.getData(startDate: daysAgo, endDate: Date(), includedDays: [1,2,3,4,5,6,7])
}
if let moodEntries = moodEntries {
let moodMetrics = Random.createTotalPerc(fromEntries: moodEntries)
_entries = State(initialValue: moodMetrics.sorted(by: {
$0.mood.rawValue > $1.mood.rawValue
}))
}
}
private var textViews: some View {
VStack {
Spacer()
HStack {
ForEach(entries.prefix(3), id: \.id) { model in
Text("\(model.percent, specifier: "%.0f")%")
.font(.title)
.fontWeight(.bold)
.foregroundColor(moodTint.color(forMood: model.mood))
.frame(maxWidth: .infinity)
}
}
Spacer()
HStack {
ForEach(entries.suffix(2), id: \.id) { model in
Text("\(model.percent, specifier: "%.0f")%")
.font(.title)
.fontWeight(.bold)
.foregroundColor(moodTint.color(forMood: model.mood))
.frame(maxWidth: .infinity)
}
}
Spacer()
}
}
private var shapeViews: some View {
VStack {
Spacer()
HStack {
ForEach(entries.prefix(3), id: \.id) { model in
shape.view(withText: Text("\(model.percent, specifier: "%.0f")%"),
bgColor: moodTint.color(forMood: model.mood),
textColor: textColor)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
Spacer()
HStack {
ForEach(entries.suffix(2), id: \.id) { model in
shape.view(withText: Text("\(model.percent, specifier: "%.0f")%"),
bgColor: moodTint.color(forMood: model.mood),
textColor: textColor)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
Spacer()
}
.foregroundColor(textColor)
}
var body: some View {
ZStack {
switch self.type {
case .text:
textViews
case .shape:
shapeViews
.padding([.leading, .trailing])
}
}
}
}
struct HeaderPercView_Previews: PreviewProvider {
static var previews: some View {
HeaderPercView(fakeData: true, backDays: 30, type: .text)
HeaderPercView(fakeData: true, backDays: 30, type: .shape)
}
}