import SwiftUI struct DailyChallengeView: View { @EnvironmentObject private var language: LanguageStore @EnvironmentObject private var store: GameStore let showResumePrompt: (Date) -> Void @State private var month = Calendar.current.dateInterval(of: .month, for: .now)!.start @State private var selectedDate = Calendar.current.startOfDay(for: .now) private let calendar = Calendar.current private var selectedSession: GameSession? { store.dailySession(for: selectedDate) } private var monthDates: [Date?] { let offset = calendar.component(.weekday, from: month) - 1 let days = calendar.range(of: .day, in: .month, for: month) ?? 1..<29 return Array(repeating: nil, count: offset) + days.map { calendar.date(byAdding: .day, value: $0 - 1, to: month) } } private var monthTitle: String { language.date(month, template: "yMMMM") } private var actionTitle: String { guard let selectedSession else { return "Start Game" } if selectedSession.completed { return "Challenge Complete" } return selectedSession.failed ? "Try Again" : "Continue Challenge" } var body: some View { ZStack { Color.white.ignoresSafeArea() GeometryReader { geometry in ScrollView(showsIndicators: false) { Group { if geometry.size.width >= 850 && geometry.size.width > geometry.size.height { HStack(alignment: .center, spacing: 34) { header.frame(width: 360).clipShape(RoundedRectangle(cornerRadius: 24)) VStack(spacing: 30) { calendarContent; challengeAction }.frame(maxWidth: 620) }.padding(28).frame(minHeight: geometry.size.height) } else { VStack(spacing: 0) { header calendarContent.padding(.top, 25).padding(.horizontal, 19) Spacer(minLength: 25) challengeAction.padding(.horizontal, 38).padding(.bottom, 27) }.frame(maxWidth: 760).frame(minHeight: geometry.size.height) } }.frame(maxWidth: .infinity) } } } } private var challengeAction: some View { VStack(spacing: 12) { Text(language.date(selectedDate) + " · " + language.text(selectedSession?.completed == true ? "You earned today’s star" : "One puzzle, a little progress")) .font(.system(size: 12)).foregroundStyle(Palette.muted) PrimaryButton(title: actionTitle, action: startSelectedDay) .disabled(selectedSession?.completed == true) .opacity(selectedSession?.completed == true ? 0.55 : 1) .accessibilityIdentifier("daily.start") } } private var header: some View { VStack(spacing: 22) { Text(language.text("Daily Challenge")).font(.system(size: 21, weight: .medium)).padding(.top, 14) HStack(spacing: 20) { VStack(spacing: 12) { ZStack { Circle().fill(.white.opacity(0.07)).frame(width: 110, height: 110).blur(radius: 1) Image(systemName: "trophy.fill") .font(.system(size: 67, weight: .medium)) .foregroundStyle(LinearGradient(colors: [Color(red: 1, green: 0.91, blue: 0.60), Palette.gold, Color(red: 0.95, green: 0.56, blue: 0.09)], startPoint: .topLeading, endPoint: .bottomTrailing)) .shadow(color: Color.black.opacity(0.15), radius: 5, y: 6) } Text(language.text("Every day you show up\ndeserves a star")) .font(.system(size: 11)).lineSpacing(4).multilineTextAlignment(.center).foregroundStyle(.white.opacity(0.72)) }.frame(width: 110) LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 6), count: 3), spacing: 9) { ForEach(1...12, id: \.self) { number in monthPill(number) } } }.padding(.horizontal, 20) } .foregroundStyle(.white).padding(.bottom, 25).frame(maxWidth: .infinity) .background { LinearGradient(colors: [Color(red: 0.16, green: 0.48, blue: 0.90), Color(red: 0.19, green: 0.55, blue: 0.95)], startPoint: .topTrailing, endPoint: .bottomLeading).ignoresSafeArea(edges: .top) } } private func monthPill(_ number: Int) -> some View { let year = calendar.component(.year, from: month) let date = calendar.date(from: DateComponents(year: year, month: number, day: 1)) ?? month let isSelected = calendar.component(.month, from: month) == number let isFuture = date > Date.now let count = store.completedDays(in: date) return Button { ClickAudio.shared.play() month = date selectedDate = calendar.isDate(date, equalTo: .now, toGranularity: .month) ? calendar.startOfDay(for: .now) : date } label: { HStack(spacing: 4) { Text(language.month(number)).font(.system(size: 12, weight: isSelected ? .semibold : .regular)).minimumScaleFactor(0.7).lineLimit(1) ZStack { Circle().stroke(isSelected ? Palette.blue.opacity(0.15) : .white.opacity(0.2), lineWidth: 2.5) Image(systemName: count > 0 ? "star.fill" : "star").font(.system(size: 9, weight: .bold)) }.frame(width: 18, height: 18) } .foregroundStyle(isSelected ? Palette.blue : .white.opacity(isFuture ? 0.4 : 0.95)) .frame(maxWidth: .infinity).frame(height: 34) .background(isSelected ? .white : Color.black.opacity(0.08), in: Capsule()) }.buttonStyle(.plain).disabled(isFuture) .accessibilityLabel(language.text("%@, %d days completed", language.date(date, template: "yMMMM"), count)) } private var calendarContent: some View { VStack(spacing: 17) { HStack { Button { ClickAudio.shared.play(); changeMonth(-1) } label: { Image(systemName: "chevron.left").font(.system(size: 14, weight: .semibold)).frame(width: 25, height: 36) }.accessibilityLabel(language.text("Previous Month")) Text(monthTitle).font(.system(size: 19, weight: .medium)).foregroundStyle(Palette.ink).minimumScaleFactor(0.8) Button { ClickAudio.shared.play(); changeMonth(1) } label: { Image(systemName: "chevron.right").font(.system(size: 14, weight: .semibold)).frame(width: 25, height: 36) } .disabled(calendar.isDate(month, equalTo: .now, toGranularity: .month)).accessibilityLabel(language.text("Next Month")) Spacer(minLength: 0) Image(systemName: "star.circle.fill").foregroundStyle(Palette.gold).font(.system(size: 22)) Text("\(store.completedDays(in: month))/\(calendar.range(of: .day, in: .month, for: month)?.count ?? 31)") .font(.system(size: 17, weight: .medium, design: .rounded)).foregroundStyle(Palette.ink) } HStack(spacing: 0) { ForEach(language.weekdays, id: \.self) { day in Text(day).font(.system(size: 12)).foregroundStyle(Palette.muted.opacity(0.7)).frame(maxWidth: .infinity) } } LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 2), count: 7), spacing: 9) { ForEach(Array(monthDates.enumerated()), id: \.offset) { _, date in if let date { dateCell(date) } else { Color.clear.frame(height: 36) } } } } } private func dateCell(_ date: Date) -> some View { let session = store.dailySession(for: date) let selected = calendar.isDate(date, inSameDayAs: selectedDate) let future = calendar.startOfDay(for: date) > calendar.startOfDay(for: .now) let day = calendar.component(.day, from: date) return Button { ClickAudio.shared.play(); selectedDate = date } label: { ZStack(alignment: .topTrailing) { ZStack { if session?.completed == true { Circle().fill(Palette.gold).frame(width: 35, height: 35) Image(systemName: "star.fill").font(.system(size: 20)).foregroundStyle(Color.orange) } else { Circle().fill(selected ? Palette.blue : .clear).frame(width: 35, height: 35) Text("\(day)").font(.system(size: 18, weight: selected ? .semibold : .medium, design: .rounded)) .foregroundStyle(selected ? .white : Palette.muted.opacity(future ? 0.4 : 1)) } if selected { Circle().strokeBorder(Palette.blue.opacity(0.45), lineWidth: 2).frame(width: 41, height: 41) } if let session, session.inProgress { Circle().trim(from: 0, to: max(0.05, session.progress)).stroke(.white, style: StrokeStyle(lineWidth: 2.5, lineCap: .round)) .rotationEffect(.degrees(-90)).frame(width: 30, height: 30) } }.frame(maxWidth: .infinity).frame(height: 36) if session?.inProgress == true && !selected { Circle().fill(Palette.blue).frame(width: 5, height: 5).padding(.trailing, 5) } }.contentShape(Rectangle()) }.buttonStyle(.plain).disabled(future) .accessibilityLabel(language.text("Day %d, %@", day, language.text(session?.completed == true ? "Completed" : session?.inProgress == true ? "In Progress" : "Not Started"))) .accessibilityAddTraits(selected ? [.isSelected] : []) } private func changeMonth(_ value: Int) { if let date = calendar.date(byAdding: .month, value: value, to: month) { month = date selectedDate = calendar.isDate(date, equalTo: .now, toGranularity: .month) ? calendar.startOfDay(for: .now) : date } } private func startSelectedDay() { guard selectedSession?.completed != true else { return } if selectedSession?.inProgress == true { showResumePrompt(selectedDate) } else { Task { await store.startGame(difficulty: .hard, day: selectedDate) } } } }