DayByDay/Today/RoutineScript.swift
56 lines · 2302 bytes
1//
2// RoutineScript.swift
3// DayByDay
4//
5
6import Foundation
7
8/// What the Today routine says, line by line, before the weather pick.
9nonisolated struct RoutineScript {
10 struct Line: Equatable {
11 /// Shown on the card.
12 let text: String
13 let symbol: String
14 /// Said aloud; differs from `text` only where the text holds the child's name.
15 let spoken: String
16
17 init(text: String, symbol: String, spoken: String? = nil) {
18 self.text = text
19 self.symbol = symbol
20 self.spoken = spoken ?? text
21 }
22 }
23
24 let lines: [Line]
25
26 init(date: Date, calendar: Calendar, name: String?, birthday: (month: Int, day: Int)?, picture: DayPictureKind?) {
27 func weekday(_ offset: Int) -> DayOfWeek {
28 let day = calendar.date(byAdding: .day, value: offset, to: date)!
29 return DayOfWeek(rawValue: calendar.component(.weekday, from: day) - 1)!
30 }
31 let parts = calendar.dateComponents([.month, .day], from: date)
32 let month = MonthOfYear(rawValue: parts.month! - 1)!
33 let today = weekday(0), yesterday = weekday(-1), tomorrow = weekday(1)
34
35 var lines = [Line(text: name.map { "Hi, \($0)!" } ?? "Hi!", symbol: "sun.max.fill",
36 spoken: Lines.greeting(for: name))]
37 if let birthday {
38 if birthday.month == parts.month, birthday.day == parts.day {
39 lines.append(Line(text: "Today is your birthday! Happy birthday!", symbol: "birthday.cake.fill"))
40 } else if birthday.month == parts.month {
41 lines.append(Line(text: "Your birthday is in \(month.name)!", symbol: "gift.fill"))
42 }
43 }
44 lines.append(Line(text: "Today is \(today.name).", symbol: today.symbol))
45 if let picture {
46 lines.append(Line(text: "Today is \(picture.name)!", symbol: picture.symbol))
47 }
48 lines += [
49 Line(text: "Yesterday was \(yesterday.name).", symbol: yesterday.symbol),
50 Line(text: "Tomorrow is \(tomorrow.name).", symbol: tomorrow.symbol),
51 Line(text: "It's \(month.name).", symbol: month.symbol),
52 Line(text: "It's \(month.season.name).", symbol: month.season.symbol),
53 ]
54 self.lines = lines
55 }
56}