Files
Reflect/Shared/Views/HeaderStatsView.swift
Trey t f822927e98 Add interactive widget voting and fix warnings/bugs
Widget Features:
- Add inline voting to timeline widget when no entry exists for today
- Show random prompt from notification strings in voting mode
- Update vote widget to use simple icon style for selection
- Make stats bar full width in voted state view
- Add Localizable.strings to widget extension target

Bug Fixes:
- Fix inverted date calculation in InsightsViewModel streak logic
- Replace force unwraps with safe optional handling in widgets
- Replace fatalError calls with graceful error handling
- Fix CSV import safety in SettingsView

Warning Fixes:
- Add @retroactive to Color and Date extension conformances
- Update deprecated onChange(of:perform:) to new syntax
- Replace deprecated applicationIconBadgeNumber with setBadgeCount
- Replace deprecated UIApplication.shared.windows API
- Add @preconcurrency for Swift 6 protocol conformances
- Add missing widget family cases to switch statement
- Remove unused variables and #warning directives

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-10 16:23:12 -06:00

136 lines
4.4 KiB
Swift

//
// HeaderStatsView.swift
// Feels
//
// Created by Trey Tartt on 1/8/22.
//
import SwiftUI
import Charts
struct HeaderStatsView : UIViewRepresentable {
//Bar chart accepts data as array of BarChartDataEntry objects
var entries : [BarChartDataEntry]
var moodTints: [Color]
var textColor: Color
var tmpHolderToMakeViewDiffefrent: Color
init(fakeData: Bool, backDays: Int, moodTint: [Color], textColor: Color) {
self.moodTints = moodTint
self.textColor = textColor
assert(moodTints.count == 5, "mood tint count should be 5")
self.tmpHolderToMakeViewDiffefrent = Color.random()
entries = [BarChartDataEntry]()
var moodEntries: [MoodEntryModel]?
if fakeData {
moodEntries = DataController.shared.randomEntries(count: 10)
} else {
guard let daysAgoDate = Calendar.current.date(byAdding: .day, value: -backDays, to: Date()),
let daysAgo = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: daysAgoDate) else {
return
}
moodEntries = DataController.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
func makeUIView(context: Context) -> BarChartView {
//crate new chart
let chart = BarChartView()
chart.drawGridBackgroundEnabled = false
chart.drawValueAboveBarEnabled = false
chart.xAxis.drawAxisLineEnabled = false
chart.xAxis.labelTextColor = .clear
chart.rightAxis.drawAxisLineEnabled = false
chart.rightAxis.labelTextColor = .clear
chart.leftAxis.drawAxisLineEnabled = false
chart.leftAxis.labelTextColor = .clear
chart.xAxis.drawGridLinesEnabled = false
chart.leftAxis.drawGridLinesEnabled = false
chart.rightAxis.drawGridLinesEnabled = false
chart.leftAxis.axisLineColor = .clear
chart.rightAxis.axisLineColor = .clear
chart.legend.textColor = .clear
chart.legend.enabled = false
chart.drawBordersEnabled = false
chart.drawMarkers = false
chart.borderColor = .clear
chart.doubleTapToZoomEnabled = false
chart.leftAxis.axisMinimum = 0
chart.minOffset = 0
let data = BarChartData()
let dataSet = dataSet()
data.append(dataSet)
chart.data = data
dataSet.valueFormatter = DefaultValueFormatter(decimals: 0)
return chart
}
// this func is required to conform to UIViewRepresentable protocol
func updateUIView(_ uiView: BarChartView, context: Context) {
let data = BarChartData()
let dataSet = dataSet()
data.append(dataSet)
uiView.data = data
dataSet.valueFormatter = DefaultValueFormatter(decimals: 0)
}
func dataSet() -> BarChartDataSet {
let dataSet = BarChartDataSet(entries: entries)
// change bars color to green
dataSet.colors = moodTints.map({ NSUIColor( $0 ) })
dataSet.secondaryTextColor = UIColor(textColor)
dataSet.valueColors = [UIColor(textColor)]
dataSet.highlightAlpha = 0.0
dataSet.roundedCornerValue = 10
if let descriptor = UIFontDescriptor.preferredFontDescriptor(
withTextStyle: .title1).withSymbolicTraits([.traitBold]) {
dataSet.valueFont = UIFont(descriptor: descriptor, size: 0)
} else {
dataSet.valueFont = UIFont.preferredFont(forTextStyle: .title1)
}
return dataSet
}
typealias UIViewType = BarChartView
}
struct HeaderStatsView_Previews: PreviewProvider {
static var previews: some View {
HeaderStatsView(fakeData: true, backDays: 30, moodTint: [Color.green, Color.blue, Color.yellow, Color.red, Color.orange], textColor: .white).frame(minHeight: 85, maxHeight: 90)
}
}