krz/daybyday

iOS learning games for young children: days, months, seasons, numbers, letters, animals and more. education ios kids swift https://krz.sh/apps/daybyday/

DayByDay/Home/TodayBanner.swift

main
daybyday/DayByDay/Home/TodayBanner.swift history · blame · raw

56 lines · 2351 bytes

1 symbol in this file
 1//
 2//  TodayBanner.swift
 3//  DayByDay
 4//
 5
 6import SwiftUI
 7
 8/// The mascot and today's day and date, at the top of home.
 9struct TodayBanner: View {
10    @Environment(ProfileSession.self) private var session
11    @Environment(\.dynamicTypeSize) private var typeSize
12    /// Today's sticker, read when home appears.
13    @State private var sticker: LearnWeather?
14
15    static let gradientColors: [Color] = [Color(red: 0.84, green: 0.40, blue: 0.05), Color(red: 0.80, green: 0.45, blue: 0.0)]
16
17    var body: some View {
18        // The mascot goes above the words at the accessibility text sizes.
19        let layout = typeSize.isAccessibilitySize
20            ? AnyLayout(VStackLayout(alignment: .leading, spacing: 16)) : AnyLayout(HStackLayout(spacing: 20))
21        return layout {
22            // On white: the orange is too close to the owl's purple.
23            MascotView(mood: .idle)
24                .padding(8)
25                .frame(width: 96, height: 96)
26                .background(Circle().fill(.white))
27            VStack(alignment: .leading, spacing: 6) {
28                Text("Today is \(DayOfWeek.current.name)!")
29                    .font(.heading(30, weight: .heavy))
30                Text(Date.now.formatted(.dateTime.month(.wide).day()))
31                    .font(.system(.title2, design: .rounded, weight: .bold))
32                if let sticker {
33                    StickerBadge(weather: sticker).frame(width: 56, height: 56)
34                } else {
35                    Text("Tap for your sticker!")
36                        .font(.heading(20))
37                }
38            }
39            .foregroundStyle(.white)
40            Spacer(minLength: 0)
41        }
42        .padding(24)
43        .background(
44            RoundedRectangle(cornerRadius: 32, style: .continuous)
45                .fill(LinearGradient(colors: Self.gradientColors,
46                                     startPoint: .topLeading, endPoint: .bottomTrailing))
47                .shadow(color: Self.gradientColors[0].opacity(0.4), radius: 8, y: 4)
48        )
49        .accessibilityElement(children: .combine)
50        .accessibilityAddTraits(.isButton)
51        .onAppear {
52            let today = AppCalendar.current.dateComponents([.year, .month, .day], from: .now)
53            sticker = session.current.flatMap { session.store.sticker(for: $0, on: today)?.weather }
54        }
55    }
56}