Files
Reflect/Shared/views/HeaderPercView.swift
Trey t c77eecc6e2 create tapable header that will switch between bar chart and percentage
WIP - add secondary header for 7 days

closed #21
2022-01-30 10:56:33 -06:00

83 lines
2.5 KiB
Swift

//
// HeaderPercView.swift
// Feels (iOS)
//
// Created by Trey Tartt on 1/29/22.
//
import SwiftUI
struct HeaderPercView: View {
typealias model = (mood: Mood, total: Int, percent: Float)
var entries = [model]()
let backDays: Int
init(fakeData: Bool, backDays: Int) {
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])
}
let totalEntryCount = moodEntries?.count ?? 0
if let moodEntries = moodEntries {
for (_, mood) in Mood.allValues.enumerated() {
let moodEntries = moodEntries.filter({
Int($0.moodValue) == mood.rawValue
})
let total = moodEntries.count
let perc = (Float(total) / Float(totalEntryCount)) * 100
entries.append((mood, total, perc))
}
}
entries = entries.sorted(by: {
$0.0.rawValue > $1.0.rawValue
})
}
var body: some View {
VStack {
Spacer()
HStack {
ForEach(entries.prefix(3), id: \.0) { model in
Text("\(model.percent, specifier: "%.0f")%")
.font(.title)
.fontWeight(.bold)
.foregroundColor(model.mood.color)
.frame(maxWidth: .infinity)
}
}
Spacer()
HStack {
ForEach(entries.suffix(2), id: \.0) { model in
Text("\(model.percent, specifier: "%.0f")%")
.font(.title)
.fontWeight(.bold)
.foregroundColor(model.mood.color)
.frame(maxWidth: .infinity)
}
}
Spacer()
}
}
}
struct HeaderPercView_Previews: PreviewProvider {
static var previews: some View {
SmallRollUpHeaderView(fakeData: true, backDays: 30)
}
}