120 lines
4.0 KiB
Swift
120 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
|
|
|
|
init(fakeData: Bool, backDays: Int, type: PercViewType) {
|
|
self.type = type
|
|
self.backDays = backDays
|
|
var moodEntries: [MoodEntry]?
|
|
|
|
if fakeData {
|
|
moodEntries = PersistenceController.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 = PersistenceController.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)
|
|
}
|
|
}
|