Header chart view on main page
change layout of main page rows
This commit is contained in:
@@ -51,7 +51,7 @@ enum Mood: Int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static var allValues: [Mood] {
|
static var allValues: [Mood] {
|
||||||
return [Mood.horrible, Mood.bad, Mood.average, Mood.good, Mood.great]
|
return [Mood.horrible, Mood.bad, Mood.average, Mood.good, Mood.great].reversed()
|
||||||
}
|
}
|
||||||
|
|
||||||
var icon: Image {
|
var icon: Image {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ struct AddMoodHeaderView: View {
|
|||||||
.foregroundColor(Color(UIColor.label))
|
.foregroundColor(Color(UIColor.label))
|
||||||
.padding()
|
.padding()
|
||||||
HStack{
|
HStack{
|
||||||
ForEach(Mood.allValues.reversed()) { mood in
|
ForEach(Mood.allValues) { mood in
|
||||||
VStack {
|
VStack {
|
||||||
Button(action: {
|
Button(action: {
|
||||||
addItem(withMood: mood)
|
addItem(withMood: mood)
|
||||||
|
|||||||
@@ -57,25 +57,40 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func weekdayName(fromDate date: Date) -> String {
|
||||||
|
let weekday = Calendar.current.component(.weekday, from: date)
|
||||||
|
let calendar = Calendar.current
|
||||||
|
let dayIndex = ((weekday - 1) + (calendar.firstWeekday - 1)) % 7
|
||||||
|
return calendar.shortWeekdaySymbols[dayIndex]
|
||||||
|
}
|
||||||
|
|
||||||
private var listView: some View {
|
private var listView: some View {
|
||||||
List {
|
List {
|
||||||
ForEach(items) { item in
|
ForEach(items) { item in
|
||||||
HStack {
|
HStack {
|
||||||
item.mood.icon
|
item.mood.icon
|
||||||
.resizable()
|
.resizable()
|
||||||
.frame(width: 50, height: 50, alignment: .center)
|
.frame(width: 40, height: 40, alignment: .center)
|
||||||
.foregroundColor(item.mood.color)
|
.foregroundColor(item.mood.color)
|
||||||
VStack {
|
VStack {
|
||||||
|
HStack {
|
||||||
|
Text(weekdayName(fromDate:item.forDate!))
|
||||||
|
.font(.title3)
|
||||||
|
.foregroundColor(Color(UIColor.label))
|
||||||
|
.frame(maxWidth: 40, alignment: .leading)
|
||||||
|
Text(" - ")
|
||||||
|
.padding([.leading, .trailing], -10)
|
||||||
|
Text(item.forDate ?? Date(), style: .date)
|
||||||
|
.font(.title3)
|
||||||
|
.foregroundColor(Color(UIColor.label))
|
||||||
|
.frame(maxWidth: .infinity, alignment: .leading)
|
||||||
|
}
|
||||||
Text("\(item.moodString)")
|
Text("\(item.moodString)")
|
||||||
.font(.title)
|
.font(.body)
|
||||||
.foregroundColor(Color(UIColor.systemGray))
|
.foregroundColor(Color(UIColor.systemGray))
|
||||||
.frame(maxWidth: .infinity, alignment: .leading)
|
.frame(maxWidth: .infinity, alignment: .leading)
|
||||||
Text(item.forDate ?? Date(), style: .date)
|
|
||||||
.font(.body)
|
|
||||||
.foregroundColor(Color(UIColor.label))
|
|
||||||
.frame(maxWidth: .infinity, alignment: .leading)
|
|
||||||
}
|
}
|
||||||
.padding(.leading)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onDelete(perform: deleteItems)
|
.onDelete(perform: deleteItems)
|
||||||
@@ -90,15 +105,13 @@ struct ContentView: View {
|
|||||||
.frame(minHeight: 85, maxHeight: 180)
|
.frame(minHeight: 85, maxHeight: 180)
|
||||||
.frame(minWidth: 0, maxWidth: .infinity)
|
.frame(minWidth: 0, maxWidth: .infinity)
|
||||||
} else {
|
} else {
|
||||||
HeaderStatsView(entries: [
|
HeaderStatsView(fakeData: false, backDays: 30)
|
||||||
//x - position of a bar, y - height of a bar
|
|
||||||
BarChartDataEntry(x: 1, y: 1),
|
|
||||||
BarChartDataEntry(x: 2, y: 5),
|
|
||||||
BarChartDataEntry(x: 3, y: 2),
|
|
||||||
BarChartDataEntry(x: 4, y: 4),
|
|
||||||
BarChartDataEntry(x: 5, y: 1)
|
|
||||||
])
|
|
||||||
.frame(minHeight: 85, maxHeight: 180)
|
.frame(minHeight: 85, maxHeight: 180)
|
||||||
|
// should match backDays above
|
||||||
|
Text("Past \(30) days")
|
||||||
|
.font(.body)
|
||||||
|
.foregroundColor(Color(UIColor.systemGray))
|
||||||
|
.frame(maxWidth: .infinity, alignment: .center)
|
||||||
}
|
}
|
||||||
listView
|
listView
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -214,7 +214,7 @@ struct FilterView: View {
|
|||||||
|
|
||||||
HStack {
|
HStack {
|
||||||
Spacer()
|
Spacer()
|
||||||
ForEach(Mood.allValues.reversed(), id: \.self) { mood in
|
ForEach(Mood.allValues, id: \.self) { mood in
|
||||||
StatsSubView(data: self.dataHolder.uncategorizedData, mood: mood)
|
StatsSubView(data: self.dataHolder.uncategorizedData, mood: mood)
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,30 @@ import Charts
|
|||||||
struct HeaderStatsView : UIViewRepresentable {
|
struct HeaderStatsView : UIViewRepresentable {
|
||||||
//Bar chart accepts data as array of BarChartDataEntry objects
|
//Bar chart accepts data as array of BarChartDataEntry objects
|
||||||
var entries : [BarChartDataEntry]
|
var entries : [BarChartDataEntry]
|
||||||
|
|
||||||
|
init(fakeData: Bool, backDays: Int) {
|
||||||
|
entries = [BarChartDataEntry]()
|
||||||
|
|
||||||
|
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])
|
||||||
|
}
|
||||||
|
if let moodEntries = moodEntries {
|
||||||
|
for (index, mood) in Mood.allValues.enumerated() {
|
||||||
|
entries.append(BarChartDataEntry(x: Double(index + 1),
|
||||||
|
y: Double(moodEntries.filter({
|
||||||
|
Int($0.moodValue) == mood.rawValue
|
||||||
|
}).count)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// this func is required to conform to UIViewRepresentable protocol
|
// this func is required to conform to UIViewRepresentable protocol
|
||||||
func makeUIView(context: Context) -> BarChartView {
|
func makeUIView(context: Context) -> BarChartView {
|
||||||
//crate new chart
|
//crate new chart
|
||||||
@@ -56,7 +80,7 @@ struct HeaderStatsView : UIViewRepresentable {
|
|||||||
//BarChartDataSet is an object that contains information about your data, styling and more
|
//BarChartDataSet is an object that contains information about your data, styling and more
|
||||||
let dataSet = BarChartDataSet(entries: entries)
|
let dataSet = BarChartDataSet(entries: entries)
|
||||||
// change bars color to green
|
// change bars color to green
|
||||||
dataSet.colors = [NSUIColor.green]
|
dataSet.colors = Mood.allValues.map({ NSUIColor( $0.color ) })
|
||||||
//change data label
|
//change data label
|
||||||
data.append(dataSet)
|
data.append(dataSet)
|
||||||
return data
|
return data
|
||||||
@@ -70,13 +94,6 @@ struct HeaderStatsView : UIViewRepresentable {
|
|||||||
|
|
||||||
struct HeaderStatsView_Previews: PreviewProvider {
|
struct HeaderStatsView_Previews: PreviewProvider {
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
HeaderStatsView(entries: [
|
HeaderStatsView(fakeData: true, backDays: 30).frame(minHeight: 85, maxHeight: 90)
|
||||||
//x - position of a bar, y - height of a bar
|
|
||||||
BarChartDataEntry(x: 1, y: 1),
|
|
||||||
BarChartDataEntry(x: 2, y: 4),
|
|
||||||
BarChartDataEntry(x: 3, y: 3),
|
|
||||||
BarChartDataEntry(x: 4, y: 2),
|
|
||||||
BarChartDataEntry(x: 5, y: 1)
|
|
||||||
]).frame(minHeight: 85, maxHeight: 90)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user