Sources/OrgApp/WhatsNew.swift

135 lines · 5670 bytes

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