Files
Reflect/Shared/views/HeaderPercView.swift
Trey t 97e6e52c8c closed #74
If looking at totals roll up should be totals, if percentage then percentages should show
2022-02-10 16:15:06 -06:00

131 lines
4.1 KiB
Swift

//
// HeaderPercView.swift
// Feels (iOS)
//
// Created by Trey Tartt on 1/29/22.
//
import SwiftUI
enum PercViewType {
case text
case circular
}
struct HeaderPercView: View {
typealias model = (mood: Mood, total: Int, percent: Float)
var entries = [model]()
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])
}
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
})
}
private var textViews: 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()
}
}
private var circularViews: some View {
VStack {
Spacer()
HStack {
ForEach(entries.prefix(3), id: \.0) { model in
Text("\(model.percent, specifier: "%.0f")%")
.font(.title2)
.fontWeight(.bold)
.multilineTextAlignment(.center)
.padding()
.frame(maxWidth: .infinity)
.background(Circle().fill(model.mood.color))
.foregroundColor(Color(UIColor.white))
}
}
Spacer()
HStack {
ForEach(entries.suffix(2), id: \.0) { model in
Text("\(model.percent, specifier: "%.0f")%")
.font(.title2)
.fontWeight(.bold)
.multilineTextAlignment(.center)
.padding()
.frame(maxWidth: .infinity)
.background(Circle().fill(model.mood.color))
.foregroundColor(Color(UIColor.white))
}
}
Spacer()
}
}
var body: some View {
ZStack {
switch self.type {
case .text:
textViews
case .circular:
circularViews
}
}
}
}
struct HeaderPercView_Previews: PreviewProvider {
static var previews: some View {
HeaderPercView(fakeData: true, backDays: 30, type: .text)
HeaderPercView(fakeData: true, backDays: 30, type: .circular)
}
}