Fixed Widget stats

This commit is contained in:
2025-09-15 23:27:21 -06:00
parent 363fbd676a
commit b53dfb1aa5
13 changed files with 94 additions and 157 deletions

View File

@@ -15,7 +15,7 @@ struct ClimbingStatsProvider: TimelineProvider {
ClimbingStatsEntry(
date: Date(),
weeklyAttempts: 42,
todayAttempts: 8,
weeklySessions: 5,
currentStreak: 3,
favoriteGym: "Summit Climbing"
)
@@ -25,7 +25,7 @@ struct ClimbingStatsProvider: TimelineProvider {
let entry = ClimbingStatsEntry(
date: Date(),
weeklyAttempts: 42,
todayAttempts: 8,
weeklySessions: 5,
currentStreak: 3,
favoriteGym: "Summit Climbing"
)
@@ -41,7 +41,7 @@ struct ClimbingStatsProvider: TimelineProvider {
let entry = ClimbingStatsEntry(
date: currentDate,
weeklyAttempts: stats.weeklyAttempts,
todayAttempts: stats.todayAttempts,
weeklySessions: stats.weeklySessions,
currentStreak: stats.currentStreak,
favoriteGym: stats.favoriteGym
)
@@ -60,7 +60,7 @@ struct ClimbingStatsProvider: TimelineProvider {
let attempts = try? JSONDecoder().decode([WidgetAttempt].self, from: attemptsData)
else {
return ClimbingStats(
weeklyAttempts: 0, todayAttempts: 0, currentStreak: 0, favoriteGym: "No Data")
weeklyAttempts: 0, weeklySessions: 0, currentStreak: 0, favoriteGym: "No Data")
}
// Load sessions for streak calculation
@@ -74,16 +74,16 @@ struct ClimbingStatsProvider: TimelineProvider {
let calendar = Calendar.current
let now = Date()
let weekAgo = calendar.date(byAdding: .day, value: -7, to: now)!
let startOfToday = calendar.startOfDay(for: now)
_ = calendar.startOfDay(for: now)
// Calculate weekly attempts
let weeklyAttempts = attempts.filter { attempt in
attempt.timestamp >= weekAgo
}.count
// Calculate today's attempts
let todayAttempts = attempts.filter { attempt in
attempt.timestamp >= startOfToday
// Calculate weekly sessions
let weeklySessions = sessions.filter { session in
session.date >= weekAgo && session.status == "COMPLETED"
}.count
// Calculate current streak (consecutive days with sessions)
@@ -94,7 +94,7 @@ struct ClimbingStatsProvider: TimelineProvider {
return ClimbingStats(
weeklyAttempts: weeklyAttempts,
todayAttempts: todayAttempts,
weeklySessions: weeklySessions,
currentStreak: currentStreak,
favoriteGym: favoriteGym
)
@@ -144,14 +144,14 @@ struct ClimbingStatsProvider: TimelineProvider {
struct ClimbingStatsEntry: TimelineEntry {
let date: Date
let weeklyAttempts: Int
let todayAttempts: Int
let weeklySessions: Int
let currentStreak: Int
let favoriteGym: String
}
struct ClimbingStats {
let weeklyAttempts: Int
let todayAttempts: Int
let weeklySessions: Int
let currentStreak: Int
let favoriteGym: String
}
@@ -176,7 +176,7 @@ struct SmallWidgetView: View {
let entry: ClimbingStatsEntry
var body: some View {
VStack(spacing: 8) {
VStack(spacing: 12) {
// Header
HStack {
if let uiImage = UIImage(named: "AppIcon") {
@@ -190,51 +190,37 @@ struct SmallWidgetView: View {
.foregroundColor(.accentColor)
}
Spacer()
Text("This Week")
.font(.caption)
.foregroundColor(.secondary)
}
// Main stat - weekly attempts
VStack(spacing: 2) {
Text("\(entry.weeklyAttempts)")
.font(.largeTitle)
.fontWeight(.bold)
.foregroundColor(.primary)
Text("Attempts")
Text("Weekly")
.font(.caption)
.foregroundColor(.secondary)
}
Spacer()
// Bottom stats
HStack {
VStack(alignment: .leading, spacing: 2) {
Text("\(entry.todayAttempts)")
.font(.headline)
.fontWeight(.semibold)
Text("Today")
.font(.caption2)
.foregroundColor(.secondary)
// Main stats - weekly attempts and sessions
VStack(spacing: 16) {
HStack(spacing: 8) {
Image(systemName: "flame.fill")
.foregroundColor(.orange)
.font(.title2)
Text("\(entry.weeklyAttempts)")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.primary)
}
Spacer()
VStack(alignment: .trailing, spacing: 2) {
HStack(spacing: 2) {
Text("\(entry.currentStreak)")
.font(.headline)
.fontWeight(.semibold)
Image(systemName: "flame.fill")
.foregroundColor(.orange)
.font(.caption)
}
Text("Day Streak")
.font(.caption2)
.foregroundColor(.secondary)
HStack(spacing: 8) {
Image(systemName: "play.fill")
.foregroundColor(.blue)
.font(.title2)
Text("\(entry.weeklySessions)")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.primary)
}
}
Spacer()
}
.padding()
}
@@ -244,7 +230,7 @@ struct MediumWidgetView: View {
let entry: ClimbingStatsEntry
var body: some View {
VStack(spacing: 12) {
VStack(spacing: 16) {
// Header
HStack {
HStack(spacing: 6) {
@@ -258,69 +244,41 @@ struct MediumWidgetView: View {
.font(.title2)
.foregroundColor(.accentColor)
}
Text("Climbing Stats")
Text("Weekly")
.font(.headline)
.fontWeight(.semibold)
}
Spacer()
Text("This Week")
.font(.caption)
.foregroundColor(.secondary)
}
// Main stats row
HStack(spacing: 20) {
VStack(spacing: 4) {
Text("\(entry.weeklyAttempts)")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.primary)
Text("Total Attempts")
.font(.caption)
.foregroundColor(.secondary)
}
VStack(spacing: 4) {
Text("\(entry.todayAttempts)")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.blue)
Text("Today")
.font(.caption)
.foregroundColor(.secondary)
}
VStack(spacing: 4) {
HStack(spacing: 4) {
Text("\(entry.currentStreak)")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.orange)
// Main stats row - weekly attempts and sessions
HStack(spacing: 40) {
VStack(spacing: 8) {
HStack(spacing: 8) {
Image(systemName: "flame.fill")
.foregroundColor(.orange)
.font(.title3)
.font(.title2)
Text("\(entry.weeklyAttempts)")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.primary)
}
}
VStack(spacing: 8) {
HStack(spacing: 8) {
Image(systemName: "play.fill")
.foregroundColor(.blue)
.font(.title2)
Text("\(entry.weeklySessions)")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.primary)
}
Text("Day Streak")
.font(.caption)
.foregroundColor(.secondary)
}
}
Spacer()
// Bottom info
HStack {
VStack(alignment: .leading, spacing: 2) {
Text("Favorite Gym")
.font(.caption2)
.foregroundColor(.secondary)
Text(entry.favoriteGym)
.font(.caption)
.fontWeight(.medium)
.lineLimit(1)
}
Spacer()
}
}
.padding()
}
@@ -367,14 +325,14 @@ struct WidgetGym: Codable {
ClimbingStatsEntry(
date: .now,
weeklyAttempts: 42,
todayAttempts: 8,
weeklySessions: 5,
currentStreak: 3,
favoriteGym: "Summit Climbing"
)
ClimbingStatsEntry(
date: .now,
weeklyAttempts: 58,
todayAttempts: 12,
weeklySessions: 8,
currentStreak: 5,
favoriteGym: "Boulder Zone"
)