wip
This commit is contained in:
@@ -9,47 +9,18 @@ import SwiftUI
|
||||
|
||||
struct SmallRollUpHeaderView: View {
|
||||
@Binding var viewType: MainSwitchableViewType
|
||||
@Binding var backDays: Int
|
||||
|
||||
typealias Model = (mood: Mood, total: Int, percent: Float)
|
||||
private var entries = [Model]()
|
||||
let entries: [MoodEntry]
|
||||
private var moodMetrics = [MoodGroupingMetrics]()
|
||||
|
||||
init(fakeData: Bool, backDays: Binding<Int>, viewType: Binding<MainSwitchableViewType>) {
|
||||
init(entries: [MoodEntry], viewType: Binding<MainSwitchableViewType>) {
|
||||
self.entries = entries
|
||||
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: -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() {
|
||||
|
||||
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
|
||||
})
|
||||
moodMetrics = Random.createTotalPerc(fromEntries: entries)
|
||||
}
|
||||
|
||||
private func textView(forModel model: Model) -> Text {
|
||||
private func textView(forModel model: MoodGroupingMetrics) -> Text {
|
||||
switch viewType {
|
||||
case .total:
|
||||
return Text(String(model.total))
|
||||
@@ -60,12 +31,43 @@ struct SmallRollUpHeaderView: View {
|
||||
}
|
||||
}
|
||||
|
||||
private var modelTextViews: some View {
|
||||
private var textViews: some View {
|
||||
HStack {
|
||||
ForEach(entries, id: \.0) { model in
|
||||
ForEach(moodMetrics, id: \.0) { model in
|
||||
textView(forModel: model)
|
||||
.font(.title2)
|
||||
.fontWeight(.bold)
|
||||
.lineLimit(1)
|
||||
.foregroundColor(model.mood.color)
|
||||
.frame(width: 70, height: 70)
|
||||
}
|
||||
}
|
||||
.padding([.top, .bottom])
|
||||
}
|
||||
|
||||
private var circularViews: some View {
|
||||
HStack {
|
||||
ForEach(moodMetrics, id: \.0) { model in
|
||||
ZStack {
|
||||
Circle().fill(model.mood.color)
|
||||
.frame(minWidth: 5,
|
||||
maxWidth: 70,
|
||||
minHeight: 5,
|
||||
maxHeight: 70,
|
||||
alignment: .center)
|
||||
.overlay(
|
||||
textView(forModel: model)
|
||||
.font(.title3)
|
||||
.fontWeight(.bold)
|
||||
.lineLimit(1)
|
||||
.clipShape(ContainerRelativeShape()).padding()
|
||||
.foregroundColor(Color(UIColor.white))
|
||||
.minimumScaleFactor(0.1)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding([.top, .bottom])
|
||||
}
|
||||
|
||||
private var viewOnViewtype : some View {
|
||||
@@ -79,51 +81,21 @@ struct SmallRollUpHeaderView: View {
|
||||
}
|
||||
}
|
||||
|
||||
private var textViews: some View {
|
||||
HStack {
|
||||
ForEach(entries, id: \.0) { model in
|
||||
textView(forModel: model)
|
||||
.font(.title2)
|
||||
.fontWeight(.bold)
|
||||
.lineLimit(1)
|
||||
.foregroundColor(model.mood.color)
|
||||
.frame(width: 50, height: 50)
|
||||
}
|
||||
}
|
||||
.padding([.top, .bottom])
|
||||
}
|
||||
|
||||
private var circularViews: some View {
|
||||
HStack {
|
||||
ForEach(entries, id: \.0) { model in
|
||||
ZStack {
|
||||
Circle().fill(model.mood.color)
|
||||
.frame(width: 50, height: 50)
|
||||
|
||||
textView(forModel: model)
|
||||
.font(.title3)
|
||||
.fontWeight(.bold)
|
||||
.frame(width: 40, height: 40)
|
||||
.scaledToFill()
|
||||
.minimumScaleFactor(0.5)
|
||||
.lineLimit(1)
|
||||
.foregroundColor(Color(UIColor.white))
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding([.top, .bottom])
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
viewOnViewtype
|
||||
.frame(minWidth: 0, maxWidth: .infinity)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
}
|
||||
|
||||
struct SmallHeaderView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
SmallRollUpHeaderView(fakeData: true, backDays: .constant(30), viewType: .constant(.total))
|
||||
|
||||
SmallRollUpHeaderView(fakeData: true, backDays: .constant(7), viewType: .constant(.total))
|
||||
Group {
|
||||
SmallRollUpHeaderView(entries: PersistenceController.shared.randomEntries(count: 10),
|
||||
viewType: .constant(.total))
|
||||
|
||||
SmallRollUpHeaderView(entries: PersistenceController.shared.randomEntries(count: 10),
|
||||
viewType: .constant(.total))
|
||||
.frame(height: 20)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user