// // HeaderPercView.swift // Reflect (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.shape.rawValue, store: GroupUserDefaults.groupDefaults) private var shape: BGShape = .circle @AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system private var textColor: Color { theme.currentTheme.labelColor } @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 { if let daysAgoRaw = Calendar.current.date(byAdding: .day, value: -backDays, to: Date()), let daysAgo = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: daysAgoRaw) { 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) .accessibilityLabel("\(model.mood.strValue) \(Int(model.percent)) percent") } } 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) .accessibilityLabel("\(model.mood.strValue) \(Int(model.percent)) percent") } } 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) .accessibilityLabel("\(model.mood.strValue) \(Int(model.percent)) percent") } } 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) .accessibilityLabel("\(model.mood.strValue) \(Int(model.percent)) percent") } } 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) } }