// // SmallHeaderView.swift // Feels (iOS) // // Created by Trey Tartt on 1/28/22. // import SwiftUI struct SmallRollUpHeaderView: View { @Binding var viewType: MainSwitchableViewType @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 = .black let entries: [MoodEntry] private var moodMetrics = [MoodMetrics]() init(entries: [MoodEntry], viewType: Binding) { self.entries = entries self._viewType = viewType moodMetrics = Random.createTotalPerc(fromEntries: entries) } private func textView(forModel model: MoodMetrics) -> Text { switch viewType { case .total: return Text(String(model.total)) case .percentageCircle: return Text("\(model.percent, specifier: "%.0f")%") case .percentage: return Text("\(model.percent, specifier: "%.0f")%") } } private var textViews: some View { HStack() { ForEach(moodMetrics, id: \.id) { model in textView(forModel: model) .font(.title2) .fontWeight(.bold) .lineLimit(1) .foregroundColor(moodTint.color(forMood: model.mood)) .frame(maxWidth: .infinity, alignment: .center) } } .frame(maxWidth: .infinity, alignment: .leading) .padding([.top, .bottom]) } private var circularViews: some View { HStack { ForEach(moodMetrics, id: \.id) { model in ZStack { Circle().fill(moodTint.color(forMood: model.mood)) .frame(minWidth: 5, maxWidth: 70, minHeight: 5, maxHeight: 70, alignment: .center) .overlay( textView(forModel: model) .font(.title3) .fontWeight(.bold) .lineLimit(1) .clipShape(ContainerRelativeShape()) .foregroundColor(textColor) .minimumScaleFactor(0.7) ) } } } .padding([.top, .bottom]) } private var viewOnViewtype : some View { HStack { switch viewType { case .total, .percentageCircle: circularViews case .percentage: textViews } } } var body: some View { viewOnViewtype .frame(maxWidth: .infinity, alignment: .leading) } } struct SmallHeaderView_Previews: PreviewProvider { static var previews: some View { Group { SmallRollUpHeaderView(entries: PersistenceController.shared.randomEntries(count: 10), viewType: .constant(.total)) SmallRollUpHeaderView(entries: PersistenceController.shared.randomEntries(count: 10), viewType: .constant(.percentageCircle)) .background(.gray) SmallRollUpHeaderView(entries: PersistenceController.shared.randomEntries(count: 10), viewType: .constant(.percentage)) .background(.gray) } } }