Sources/OrgApp/WhatsNew.swift
131 lines · 5314 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("point.3.connected.trianglepath.dotted", "Diagrams",
51 "dot and mermaid blocks draw SVG and PNG pictures without Graphviz or mermaid installed."),
52 .init("function", "Math in PDFs",
53 "PDF export typesets math without a network connection."),
54 ] + mac),
55 ]
56
57 #if os(iOS)
58 private static let ios = [
59 ReleaseNotes.Item("iphone", "More from the Mac app",
60 "Archive subtrees, publish projects, show calendars in the agenda, copy rich text, and resolve idle clock time."),
61 ReleaseNotes.Item("doc.viewfinder", "Scan documents",
62 "More ▸ Attachments ▸ Scan Document… saves the pages as a PDF and links it at the caret."),
63 ]
64 private static let mac: [ReleaseNotes.Item] = []
65 #else
66 private static let ios: [ReleaseNotes.Item] = []
67 private static let mac = [
68 ReleaseNotes.Item("menubar.rectangle", "orgstar in the menu bar",
69 "Settings ▸ General ▸ Keep orgstar in the menu bar adds capture, the agenda and today's entries to the menu bar."),
70 ]
71 #endif
72
73 /// The notes to show at launch: those for `version`, the first time it runs after another
74 /// version did. Records `version` as seen. `firstLaunch` is a new install, which shows none.
75 public static func pending(
76 version: String = appVersion, firstLaunch: Bool, defaults: UserDefaults = .standard, releases: [ReleaseNotes] = releases
77 ) -> ReleaseNotes? {
78 let seen = defaults.string(forKey: seenKey)
79 defaults.set(version, forKey: seenKey)
80 guard seen != version, !firstLaunch else { return nil }
81 return releases.first { $0.version == version }
82 }
83}
84
85/// A version's notes, as a sheet.
86public struct WhatsNewView: View {
87 let notes: ReleaseNotes
88 @Environment(\.dismiss) private var dismiss
89
90 public init(notes: ReleaseNotes) {
91 self.notes = notes
92 }
93
94 public var body: some View {
95 VStack(spacing: 24) {
96 Text("What's New in orgstar \(notes.version)").font(.title2.bold()).multilineTextAlignment(.center)
97 #if os(macOS)
98 items
99 #else
100 ScrollView { items }
101 #endif
102 Button { dismiss() } label: {
103 Text("Continue").frame(maxWidth: .infinity)
104 }
105 .buttonStyle(.borderedProminent)
106 .controlSize(.large)
107 .keyboardShortcut(.defaultAction)
108 }
109 .padding(24)
110 #if os(macOS)
111 .frame(width: 460)
112 #endif
113 }
114
115 private var items: some View {
116 VStack(alignment: .leading, spacing: 16) {
117 ForEach(notes.items) { item in
118 HStack(alignment: .top, spacing: 14) {
119 Image(systemName: item.systemImage).font(.title2).foregroundStyle(Color.accentColor).frame(width: 32)
120 .accessibilityHidden(true)
121 VStack(alignment: .leading, spacing: 2) {
122 Text(item.title).font(.headline)
123 Text(item.text).font(.subheadline).foregroundStyle(.secondary)
124 }
125 .fixedSize(horizontal: false, vertical: true)
126 }
127 }
128 }
129 .frame(maxWidth: .infinity, alignment: .leading)
130 }
131}