Packages/MoonriseKit/Sources/Library/Stories.swift
26 lines · 963 bytes
1import Foundation
2
3/// A mission told in a few dated chapters, shown on the orrery.
4public struct MissionStory: Sendable, Decodable, Identifiable {
5 public struct Chapter: Sendable, Decodable, Hashable {
6 /// UTC date, yyyy-MM-dd.
7 public var date: String
8 public var title: String
9 public var text: String
10
11 public var day: Date { try! Date(date + "T12:00:00Z", strategy: .iso8601) }
12 }
13
14 /// Name as in the orrery's spacecraft list.
15 public var spacecraft: String
16 public var page: String
17 public var chapters: [Chapter]
18 public var id: String { page }
19
20 public static let all: [MissionStory] = {
21 let url = Bundle.module.url(forResource: "stories", withExtension: "json", subdirectory: "stories")!
22 return try! JSONDecoder().decode([MissionStory].self, from: Data(contentsOf: url))
23 }()
24
25 public static func story(page: String) -> MissionStory? { all.first { $0.page == page } }
26}