If looking at totals roll up should be totals, if percentage then percentages should show
This commit is contained in:
Trey t
2022-02-10 16:15:06 -06:00
parent f66caf93dd
commit 97e6e52c8c
4 changed files with 114 additions and 59 deletions

View File

@@ -7,34 +7,40 @@
import SwiftUI
enum SmallRollUpHeaderViewType {
case text
case circular
}
struct SmallRollUpHeaderView: View {
var entries = [(Mood, Int)]()
let backDays: Int
let type: SmallRollUpHeaderViewType
@Binding var viewType: MainSwitchableViewType
@Binding var backDays: Int
init(fakeData: Bool, backDays: Int, type: SmallRollUpHeaderViewType) {
self.type = type
self.backDays = backDays
typealias Model = (mood: Mood, total: Int, percent: Float)
private var entries = [Model]()
init(fakeData: Bool, backDays: Binding<Int>, viewType: Binding<MainSwitchableViewType>) {
self._viewType = viewType
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())!
var daysAgo = Calendar.current.date(byAdding: .day, value: -self.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() {
entries.append((mood, moodEntries.filter({
let moodEntries = moodEntries.filter({
Int($0.moodValue) == mood.rawValue
}).count))
})
let total = moodEntries.count
let perc = (Float(total) / Float(totalEntryCount)) * 100
entries.append((mood, total, perc))
}
}
@@ -43,13 +49,24 @@ struct SmallRollUpHeaderView: View {
})
}
private func textView(forModel model: Model) -> 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(entries, id: \.0) { (mood, value) in
Text(String(value))
ForEach(entries, id: \.0) { model in
textView(forModel: model)
.font(.title)
.fontWeight(.bold)
.foregroundColor(mood.color)
.foregroundColor(model.mood.color)
.frame(maxWidth: .infinity)
}
}
@@ -57,12 +74,12 @@ struct SmallRollUpHeaderView: View {
private var circularViews: some View {
HStack {
ForEach(entries, id: \.0) { (mood, value) in
ForEach(entries, id: \.0) { model in
ZStack {
Circle().fill(mood.color)
Circle().fill(model.mood.color)
.frame(width: 50, height: 50)
Text(String(value))
textView(forModel: model)
.font(.title)
.fontWeight(.bold)
.frame(maxWidth: 50)
@@ -78,14 +95,8 @@ struct SmallRollUpHeaderView: View {
var body: some View {
VStack {
switch self.type {
case .text:
textViews
.padding([.trailing, .leading, .top])
case .circular:
circularViews
.padding([.trailing, .leading, .top])
}
circularViews
.padding([.trailing, .leading, .top])
Text(String(format: String(localized: "content_view_header_title"), self.backDays))
.font(.body)
@@ -98,8 +109,8 @@ struct SmallRollUpHeaderView: View {
struct SmallHeaderView_Previews: PreviewProvider {
static var previews: some View {
SmallRollUpHeaderView(fakeData: true, backDays: 30, type: .text)
SmallRollUpHeaderView(fakeData: true, backDays: .constant(30), viewType: .constant(.total))
SmallRollUpHeaderView(fakeData: true, backDays: 30, type: .circular)
SmallRollUpHeaderView(fakeData: true, backDays: .constant(7), viewType: .constant(.total))
}
}