Sources/OrgApp/WhatsNew.swift
122 lines · 4631 bytes
1import SwiftUI
2
3/// What changed in one version.
4public struct ReleaseNotes: Identifiable, Sendable {
5 public struct Item: Identifiable, Sendable {
6 public let systemImage: String
7 public let title: LocalizedStringResource
8 public let text: LocalizedStringResource
9
10 public var id: String { title.key }
11
12 public init(_ systemImage: String, _ title: LocalizedStringResource, _ text: LocalizedStringResource) {
13 self.systemImage = systemImage
14 self.title = title
15 self.text = text
16 }
17 }
18
19 /// The `CFBundleShortVersionString` the notes are for.
20 public let version: String
21 public let items: [Item]
22
23 public var id: String { version }
24
25 public init(version: String, items: [Item]) {
26 self.version = version
27 self.items = items
28 }
29}
30
31/// The notes shown once after an update to a version that has some.
32public enum WhatsNew {
33 static let seenKey = "whatsNew.seenVersion"
34
35 public static var appVersion: String {
36 Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
37 }
38
39 /// Newest first. A version without an entry shows nothing.
40 public static let releases: [ReleaseNotes] = [
41 ReleaseNotes(version: "1.2.0", items: ios + [
42 .init("square.and.arrow.up", "LaTeX, OpenDocument and plain text export",
43 "Export and publish to LaTeX, ODT and ASCII without Emacs."),
44 .init("play.rectangle", "Code blocks that run in orgstar",
45 "Emacs Lisp, JavaScript, calc and sqlite blocks run without any other program installed."),
46 .init("tablecells", "More table formulas",
47 "More Calc and Lisp formulas, date arithmetic and org-sbe are worked out without Emacs."),
48 .init("chart.xyaxis.line", "Plots",
49 "Radar plots, more of gnuplot's set options, and SVG files without gnuplot."),
50 .init("function", "Math in PDFs",
51 "PDF export typesets math without a network connection."),
52 ]),
53 ]
54
55 #if os(iOS)
56 private static let ios = [
57 ReleaseNotes.Item("iphone", "More from the Mac app",
58 "Archive subtrees, publish projects, show calendars in the agenda, copy rich text, and resolve idle clock time."),
59 ]
60 #else
61 private static let ios: [ReleaseNotes.Item] = []
62 #endif
63
64 /// The notes to show at launch: those for `version`, the first time it runs after another
65 /// version did. Records `version` as seen. `firstLaunch` is a new install, which shows none.
66 public static func pending(
67 version: String = appVersion, firstLaunch: Bool, defaults: UserDefaults = .standard, releases: [ReleaseNotes] = releases
68 ) -> ReleaseNotes? {
69 let seen = defaults.string(forKey: seenKey)
70 defaults.set(version, forKey: seenKey)
71 guard seen != version, !firstLaunch else { return nil }
72 return releases.first { $0.version == version }
73 }
74}
75
76/// A version's notes, as a sheet.
77public struct WhatsNewView: View {
78 let notes: ReleaseNotes
79 @Environment(\.dismiss) private var dismiss
80
81 public init(notes: ReleaseNotes) {
82 self.notes = notes
83 }
84
85 public var body: some View {
86 VStack(spacing: 24) {
87 Text("What's New in orgstar \(notes.version)").font(.title2.bold()).multilineTextAlignment(.center)
88 #if os(macOS)
89 items
90 #else
91 ScrollView { items }
92 #endif
93 Button { dismiss() } label: {
94 Text("Continue").frame(maxWidth: .infinity)
95 }
96 .buttonStyle(.borderedProminent)
97 .controlSize(.large)
98 .keyboardShortcut(.defaultAction)
99 }
100 .padding(24)
101 #if os(macOS)
102 .frame(width: 460)
103 #endif
104 }
105
106 private var items: some View {
107 VStack(alignment: .leading, spacing: 16) {
108 ForEach(notes.items) { item in
109 HStack(alignment: .top, spacing: 14) {
110 Image(systemName: item.systemImage).font(.title2).foregroundStyle(Color.accentColor).frame(width: 32)
111 .accessibilityHidden(true)
112 VStack(alignment: .leading, spacing: 2) {
113 Text(item.title).font(.headline)
114 Text(item.text).font(.subheadline).foregroundStyle(.secondary)
115 }
116 .fixedSize(horizontal: false, vertical: true)
117 }
118 }
119 }
120 .frame(maxWidth: .infinity, alignment: .leading)
121 }
122}