create tapable header that will switch between bar chart and percentage

WIP - add secondary header for 7 days

closed #21
This commit is contained in:
Trey t
2022-01-30 10:56:33 -06:00
parent 0d33aabb22
commit c77eecc6e2
6 changed files with 207 additions and 62 deletions

View File

@@ -250,14 +250,23 @@ struct ContentView: View {
.frame(height: headerHeight)
.frame(minWidth: 0, maxWidth: .infinity)
} else {
HeaderStatsView(fakeData: false, backDays: 30)
.frame(height: headerHeight)
// should match backDays above
Text(String(format: String(localized: "content_view_header_title"), 30))
.font(.body)
.foregroundColor(Color(UIColor.systemGray))
.frame(maxWidth: .infinity, alignment: .center)
TabView {
SwitchableView(daysBack: 30)
.frame(height: headerHeight)
.frame(minWidth: 0, maxWidth: .infinity)
.contentShape(Rectangle())
SwitchableView(daysBack: 7)
.frame(height: headerHeight)
.frame(minWidth: 0, maxWidth: .infinity)
.contentShape(Rectangle())
}
.tabViewStyle(.page)
.onAppear {
UIPageControl.appearance().currentPageIndicatorTintColor = UIColor.label
UIPageControl.appearance().pageIndicatorTintColor = UIColor.black.withAlphaComponent(0.2)
}
.padding(.bottom, 12)
}
}
}
@@ -305,7 +314,13 @@ struct ContentView: View {
VStack {
SmallRollUpHeaderView(fakeData: false, backDays: 30)
.padding([.leading, .trailing])
.background(
Color(UIColor.systemBackground)
)
.clipShape(RoundedRectangle(cornerRadius: 25, style: .continuous))
.padding()
.padding([.top, .bottom], 5)
Spacer()
}
.opacity(1 - headerOpacity)

View File

@@ -0,0 +1,82 @@
//
// HeaderPercView.swift
// Feels (iOS)
//
// Created by Trey Tartt on 1/29/22.
//
import SwiftUI
struct HeaderPercView: View {
typealias model = (mood: Mood, total: Int, percent: Float)
var entries = [model]()
let backDays: Int
init(fakeData: Bool, backDays: Int) {
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())!
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
})
}
var body: some View {
VStack {
Spacer()
HStack {
ForEach(entries.prefix(3), id: \.0) { model in
Text("\(model.percent, specifier: "%.0f")%")
.font(.title)
.fontWeight(.bold)
.foregroundColor(model.mood.color)
.frame(maxWidth: .infinity)
}
}
Spacer()
HStack {
ForEach(entries.suffix(2), id: \.0) { model in
Text("\(model.percent, specifier: "%.0f")%")
.font(.title)
.fontWeight(.bold)
.foregroundColor(model.mood.color)
.frame(maxWidth: .infinity)
}
}
Spacer()
}
}
}
struct HeaderPercView_Previews: PreviewProvider {
static var previews: some View {
SmallRollUpHeaderView(fakeData: true, backDays: 30)
}
}

View File

@@ -95,6 +95,8 @@ struct HeaderStatsView : UIViewRepresentable {
dataSet.colors = Mood.allValues.map({ NSUIColor( $0.color ) })
dataSet.secondaryTextColor = UIColor.systemGray
dataSet.valueColors = [.white]
dataSet.highlightAlpha = 0.0
if let descriptor = UIFontDescriptor.preferredFontDescriptor(
withTextStyle: .body).withSymbolicTraits([.traitBold]) {
dataSet.valueFont = UIFont(descriptor: descriptor, size: 0)

View File

@@ -54,7 +54,6 @@ struct SmallRollUpHeaderView: View {
.frame(maxWidth: .infinity, alignment: .center)
.padding(.top, 2)
}
.padding([.top])
}
}

View File

@@ -0,0 +1,51 @@
//
// SwitchableView.swift
// Feels (iOS)
//
// Created by Trey Tartt on 1/30/22.
//
import SwiftUI
struct SwitchableView: View {
@State var currentViewIdx = 0
let daysBack: Int
var body: some View {
VStack {
ZStack {
HeaderStatsView(fakeData: false, backDays: daysBack)
.opacity(currentViewIdx == 0 ? 1 : 0)
HeaderPercView(fakeData: false, backDays: daysBack)
.opacity(currentViewIdx == 1 ? 1 : 0)
}
.padding([.top, .bottom], -7)
Text(String(format: String(localized: "content_view_header_title"), daysBack))
.font(.body)
.foregroundColor(Color(UIColor.systemGray))
.frame(maxWidth: .infinity, alignment: .center)
.padding(.bottom, 15)
}
.background(
Color(UIColor.systemBackground)
)
.clipShape(RoundedRectangle(cornerRadius: 25, style: .continuous))
.padding()
.onTapGesture {
currentViewIdx += 1
if currentViewIdx == 2 {
currentViewIdx = 0
}
}
}
}
struct SwitchableView_Previews: PreviewProvider {
static var previews: some View {
SwitchableView(daysBack: 30)
}
}