instant_record 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +58 -0
- data/README.md +267 -61
- data/app/controllers/instant_record/bootstraps_controller.rb +39 -12
- data/app/controllers/instant_record/events_controller.rb +58 -6
- data/app/controllers/instant_record/files_controller.rb +116 -0
- data/app/controllers/instant_record/mutations_controller.rb +84 -5
- data/app/controllers/instant_record/native_screens_controller.rb +60 -0
- data/app/controllers/instant_record/records_controller.rb +86 -8
- data/app/controllers/instant_record/shells_controller.rb +64 -0
- data/app/controllers/instant_record/sync_notices_controller.rb +81 -0
- data/app/helpers/instant_record/sync_notices_helper.rb +41 -0
- data/app/jobs/instant_record/shell_build_job.rb +16 -0
- data/app/models/instant_record/change.rb +36 -3
- data/app/models/instant_record/mutation_applier.rb +633 -50
- data/app/views/instant_record/sync_notices/_notices.html.erb +63 -0
- data/compiled/README.md +272 -0
- data/compiled/pipeline/browser_migrations.rb +18 -0
- data/compiled/pipeline/build.rb +13 -0
- data/compiled/pipeline/build_native.rb +14 -0
- data/compiled/pipeline/native/host.c +78 -0
- data/compiled/pipeline/native/instant_record_native.h +57 -0
- data/compiled/pipeline/native_shim.c +149 -0
- data/compiled/pipeline/reactor_shim.c +66 -0
- data/compiled/pipeline/sqlite/rh_shim.c +5 -0
- data/compiled/pipeline/sqlite_release.rb +43 -0
- data/compiled/toolchain.env.example +22 -0
- data/compiled/web/index.html +20 -0
- data/compiled/web/package-lock.json +884 -0
- data/compiled/web/package.json +17 -0
- data/compiled/web/src/app-host-core.js +556 -0
- data/compiled/web/src/app-host.js +16 -0
- data/compiled/web/src/branch-manager.js +140 -0
- data/compiled/web/src/branch-preview.js +397 -0
- data/compiled/web/src/changes.js +191 -0
- data/compiled/web/src/faults.js +172 -0
- data/compiled/web/src/flight-recorder.js +347 -0
- data/compiled/web/src/history-fill.js +68 -0
- data/compiled/web/src/http-headers.js +100 -0
- data/compiled/web/src/leader-election.js +705 -0
- data/compiled/web/src/main.js +608 -0
- data/compiled/web/src/manifest.js +112 -0
- data/compiled/web/src/opfs-wasi-core.js +109 -0
- data/compiled/web/src/opfs-wasi-file.js +8 -0
- data/compiled/web/src/reactor-runtime.js +1692 -0
- data/compiled/web/src/redactor.js +518 -0
- data/compiled/web/src/release-updates.js +48 -0
- data/compiled/web/src/replay.js +338 -0
- data/compiled/web/src/schema-recovery.js +204 -0
- data/compiled/web/src/server-request.js +68 -0
- data/compiled/web/src/sha256.js +98 -0
- data/compiled/web/src/shell.js +52 -0
- data/compiled/web/src/snapshot-store.js +38 -0
- data/compiled/web/src/sqlite-redactor.js +111 -0
- data/compiled/web/src/storage-backend.js +159 -0
- data/compiled/web/src/stream-leader.js +675 -0
- data/compiled/web/src/sync-context.js +91 -0
- data/compiled/web/src/sync-drain.js +104 -0
- data/compiled/web/src/sync-schedule.js +9 -0
- data/compiled/web/src/wasi-minimal.js +275 -0
- data/compiled/web/src/worker.js +39 -0
- data/compiled/web/vite.config.js +27 -0
- data/config/routes.rb +25 -0
- data/db/migrate/20261119000201_add_scope_to_instant_record_changes.rb +7 -0
- data/db/migrate/20261121000001_create_instant_record_sync_notices.rb +21 -0
- data/db/migrate/20261121000101_add_partition_to_instant_record_changes.rb +8 -0
- data/db/migrate/20261121230300_add_delay_line_to_instant_record_outbox.rb +10 -0
- data/exe/instant_record_mcp +6 -0
- data/frontend/LiveList.js +67 -0
- data/frontend/chord.js +79 -0
- data/frontend/file.js +65 -0
- data/frontend/files.js +738 -0
- data/frontend/focus.js +10 -0
- data/frontend/held.js +192 -0
- data/frontend/http.js +76 -0
- data/frontend/index.js +53 -0
- data/frontend/keyboard.js +56 -0
- data/frontend/live.js +298 -0
- data/frontend/notices.js +118 -0
- data/frontend/package.json +15 -0
- data/frontend/range.js +10 -0
- data/frontend/selection.js +47 -0
- data/frontend/shortcuts.js +25 -0
- data/frontend/sync-notices.js +130 -0
- data/frontend/undo.js +48 -0
- data/frontend/window.js +181 -0
- data/lib/generators/instant_record/install/install_generator.rb +18 -30
- data/lib/generators/instant_record/install/templates/instant_record.rb +34 -0
- data/lib/generators/instant_record/ios/ios_generator.rb +71 -0
- data/lib/generators/instant_record/ios/templates/App.swift.tt +35 -0
- data/lib/generators/instant_record/ios/templates/Info.plist +11 -0
- data/lib/generators/instant_record/ios/templates/LibraryEngine.swift +30 -0
- data/lib/instant_record/branch/sqlite_store.rb +142 -0
- data/lib/instant_record/branch.rb +388 -0
- data/lib/instant_record/client/notifier.rb +8 -12
- data/lib/instant_record/client/rebaser.rb +565 -0
- data/lib/instant_record/client/transport.rb +64 -42
- data/lib/instant_record/client.rb +1600 -169
- data/lib/instant_record/compiled_site.rb +85 -0
- data/lib/instant_record/compiler/app_stage.rb +212 -0
- data/lib/instant_record/compiler/ast_lowerer.rb +659 -0
- data/lib/instant_record/compiler/browser_migrations.rb +161 -0
- data/lib/instant_record/compiler/entry_builder.rb +832 -0
- data/lib/instant_record/compiler/fts5_smoke.rb +205 -0
- data/lib/instant_record/compiler/gem_vendor.rb +194 -0
- data/lib/instant_record/compiler/host_adapters.rb +506 -0
- data/lib/instant_record/compiler/kotlin_codegen.rb +209 -0
- data/lib/instant_record/compiler/manifest.rb +178 -0
- data/lib/instant_record/compiler/native_build.rb +586 -0
- data/lib/instant_record/compiler/native_contracts.rb +338 -0
- data/lib/instant_record/compiler/path_scrubber.rb +31 -0
- data/lib/instant_record/compiler/reachability_tracer.rb +1188 -0
- data/lib/instant_record/compiler/route_analyzer.rb +623 -0
- data/lib/instant_record/compiler/runner.rb +42 -0
- data/lib/instant_record/compiler/schema_lock.rb +606 -0
- data/lib/instant_record/compiler/source_pages.rb +77 -0
- data/lib/instant_record/compiler/static_linter.rb +770 -0
- data/lib/instant_record/compiler/swift_codegen.rb +341 -0
- data/lib/instant_record/compiler/toolchain.rb +219 -0
- data/lib/instant_record/compiler/wasm_build.rb +704 -0
- data/lib/instant_record/compiler/wasm_tree.rb +518 -0
- data/lib/instant_record/compiler.rb +277 -0
- data/lib/instant_record/configuration.rb +104 -0
- data/lib/instant_record/engine.rb +68 -18
- data/lib/instant_record/file_store.rb +162 -0
- data/lib/instant_record/files.rb +92 -0
- data/lib/instant_record/hlc.rb +387 -0
- data/lib/instant_record/host.rb +66 -0
- data/lib/instant_record/inertia/renderer.rb +52 -0
- data/lib/instant_record/inertia.rb +222 -0
- data/lib/instant_record/mcp/cli.rb +319 -0
- data/lib/instant_record/mcp/host_process.rb +229 -0
- data/lib/instant_record/mcp/http_exchange.rb +233 -0
- data/lib/instant_record/mcp/replica.rb +556 -0
- data/lib/instant_record/mcp/runtime.rb +406 -0
- data/lib/instant_record/mcp/server.rb +429 -0
- data/lib/instant_record/mcp.rb +47 -0
- data/lib/instant_record/native/contract.rb +367 -0
- data/lib/instant_record/native/node.rb +125 -0
- data/lib/instant_record/native/screen.rb +366 -0
- data/lib/instant_record/native/xcode_project.rb +344 -0
- data/lib/instant_record/native.rb +41 -0
- data/lib/instant_record/outbox_mutation.rb +81 -9
- data/lib/instant_record/outbox_upcaster.rb +161 -0
- data/lib/instant_record/read_tracker.rb +610 -0
- data/lib/instant_record/routing.rb +33 -0
- data/lib/instant_record/runtime_scoped.rb +4 -0
- data/lib/instant_record/shell.rb +486 -0
- data/lib/instant_record/sync_actions.rb +800 -0
- data/lib/instant_record/sync_metadata.rb +17 -2
- data/lib/instant_record/sync_notice.rb +234 -0
- data/lib/instant_record/sync_partition.rb +63 -0
- data/lib/instant_record/sync_scope.rb +300 -0
- data/lib/instant_record/sync_shape.rb +102 -0
- data/lib/instant_record/sync_window.rb +92 -15
- data/lib/instant_record/syncable.rb +313 -17
- data/lib/instant_record/testing/authoritative_server.rb +547 -0
- data/lib/instant_record/testing/chaos_simulator.rb +429 -0
- data/lib/instant_record/testing/cruby_host_bridge.rb +370 -0
- data/lib/instant_record/testing/fault_network.rb +140 -0
- data/lib/instant_record/testing/parity_suite.rb +435 -0
- data/lib/instant_record/testing/runtimes/base.rb +184 -0
- data/lib/instant_record/testing/runtimes/cruby_runtime.rb +255 -0
- data/lib/instant_record/testing/runtimes/native_runtime.rb +346 -0
- data/lib/instant_record/testing/runtimes/wasm_runtime.rb +371 -0
- data/lib/instant_record/testing/wasm_host_bridge.mjs +240 -0
- data/lib/instant_record/testing.rb +333 -0
- data/lib/instant_record/trace/redactor.rb +278 -0
- data/lib/instant_record/trace/replayer.rb +126 -0
- data/lib/instant_record/trace/schema_extractor.rb +37 -0
- data/lib/instant_record/trace.rb +22 -0
- data/lib/instant_record/version.rb +1 -1
- data/lib/instant_record.rb +107 -37
- data/lib/tasks/instant_record.rake +299 -47
- data/native/ios/InstantRecordNative/Package.swift +12 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/AppHost.swift +706 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Engine.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/FileStore.swift +1048 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/HTTPEngine.swift +36 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Icons.swift +19 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Node.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/ScreenView.swift +481 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Screens.swift +94 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/StreamClient.swift +74 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncNotice.swift +282 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncSchedule.swift +153 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Theme.swift +140 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Transport.swift +108 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/TypedContracts.swift +237 -0
- data/native/ios/README.md +341 -0
- data/sig/instant_record/inertia.rbs +47 -0
- data/sig/instant_record/native.rbs +38 -0
- metadata +194 -18
- data/lib/generators/instant_record/install/templates/database.js +0 -32
- data/lib/generators/instant_record/install/templates/index.html +0 -69
- data/lib/generators/instant_record/install/templates/rails.sw.js +0 -750
- data/lib/instant_record/local_schema.rb +0 -75
- data/lib/instant_record/pglite_compat.rb +0 -40
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
/// The app's screens, asked of a running Rails server: for development, so
|
|
4
|
+
/// that editing a screen's Ruby shows on the phone after a pull, with no
|
|
5
|
+
/// rebuild. The server runs the same screen classes and answers with the same
|
|
6
|
+
/// JSON as the compiled library (config.instant_record.native_dev). There is
|
|
7
|
+
/// no database on the phone in this mode and nothing to sync.
|
|
8
|
+
public final class HTTPEngine: Engine {
|
|
9
|
+
private let server: String
|
|
10
|
+
private let transport: Transport
|
|
11
|
+
private var problem = ""
|
|
12
|
+
|
|
13
|
+
/// `server` is where the gem's engine is mounted, such as
|
|
14
|
+
/// http://localhost:3000/instant_record.
|
|
15
|
+
public init(server: String, transport: Transport = Transport()) {
|
|
16
|
+
self.server = server
|
|
17
|
+
self.transport = transport
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public var syncs: Bool { false }
|
|
21
|
+
public func boot(database: String) -> Bool { true }
|
|
22
|
+
public func open(database: String) -> Bool { true }
|
|
23
|
+
public func notice() -> String { "" }
|
|
24
|
+
public func error() -> String { problem }
|
|
25
|
+
public func onExchange(_ answer: @escaping (String) -> String) {}
|
|
26
|
+
|
|
27
|
+
public func call(_ entry: String, _ input: String) -> String? {
|
|
28
|
+
// Nothing is kept on the phone in this mode: only screens and taps go to the server.
|
|
29
|
+
guard entry == "render" || entry == "action" else { return "" }
|
|
30
|
+
let reply = Reply(transport.exchange("POST \(server)/native/\(entry)\n\(input)"))
|
|
31
|
+
// An action answers with no content, a render with the screen.
|
|
32
|
+
if reply.ok { return reply.body }
|
|
33
|
+
problem = reply.status == 0 ? "The server at \(server) did not answer." : reply.body
|
|
34
|
+
return nil
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/// Icons by what they mean. A screen names the meaning, so the same Ruby can
|
|
2
|
+
/// be drawn by a renderer for another platform, and this table picks the
|
|
3
|
+
/// SF Symbol and what it is called aloud. A name that is not here is used as
|
|
4
|
+
/// an SF Symbol name as given, and is not spoken.
|
|
5
|
+
enum Icons {
|
|
6
|
+
private static let symbols = [
|
|
7
|
+
"person": ("person.crop.circle", "Account"),
|
|
8
|
+
"filter": ("slider.horizontal.3", "View"),
|
|
9
|
+
"archive": ("archivebox", "Archive"),
|
|
10
|
+
"inbox": ("tray", "Inbox"),
|
|
11
|
+
"unread": ("circle.fill", "Unread"),
|
|
12
|
+
"read": ("envelope.open", "Read"),
|
|
13
|
+
"send": ("arrow.up.circle.fill", "Send"),
|
|
14
|
+
"compose": ("square.and.pencil", "Compose"),
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
static func symbol(_ name: String) -> String { symbols[name]?.0 ?? name }
|
|
18
|
+
static func spoken(_ name: String) -> String { symbols[name]?.1 ?? "" }
|
|
19
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
/// One element of a screen, as the Ruby view layer writes it
|
|
4
|
+
/// (lib/instant_record/native/node.rb). Every element has the same fields and
|
|
5
|
+
/// the renderer switches on `kind`. `params` are what the screen a link
|
|
6
|
+
/// leads to is opened with, and `args` are what the element's action is
|
|
7
|
+
/// performed with.
|
|
8
|
+
public struct Node: Decodable, Equatable {
|
|
9
|
+
public let kind: String
|
|
10
|
+
public let id: String
|
|
11
|
+
public let text: String
|
|
12
|
+
public let action: String
|
|
13
|
+
public let params: [String: String]
|
|
14
|
+
public let args: [String: String]
|
|
15
|
+
public let props: [String: String]
|
|
16
|
+
public let children: [Node]
|
|
17
|
+
|
|
18
|
+
public init(json: String) throws {
|
|
19
|
+
self = try JSONDecoder().decode(Node.self, from: Data(json.utf8))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public func prop(_ name: String) -> String { props[name] ?? "" }
|
|
23
|
+
public func children(of kind: String) -> [Node] { children.filter { $0.kind == kind } }
|
|
24
|
+
|
|
25
|
+
/// The children with the identity a list keeps them by: a node's `id`
|
|
26
|
+
/// when it has one, which for a row is its record, or else its place.
|
|
27
|
+
public var keyed: [Keyed] { Node.keyed(children) }
|
|
28
|
+
|
|
29
|
+
public static func keyed(_ nodes: [Node]) -> [Keyed] {
|
|
30
|
+
nodes.enumerated().map { Keyed(key: $0.element.id.isEmpty ? "#\($0.offset)" : $0.element.id, node: $0.element) }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public struct Keyed: Identifiable, Equatable {
|
|
34
|
+
public let key: String
|
|
35
|
+
public let node: Node
|
|
36
|
+
public var id: String { key }
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// A screen and the named values it was opened with.
|
|
41
|
+
public struct Route: Hashable {
|
|
42
|
+
public let screen: String
|
|
43
|
+
public let params: [String: String]
|
|
44
|
+
|
|
45
|
+
public init(_ screen: String, _ params: [String: String] = [:]) {
|
|
46
|
+
self.screen = screen
|
|
47
|
+
self.params = params
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/// What a render or an action is asked with: one JSON object, so that no
|
|
51
|
+
/// value's meaning depends on what characters it holds.
|
|
52
|
+
/// An action carries the arguments of the element that was tapped and the
|
|
53
|
+
/// values of the screen's fields, each under its own name.
|
|
54
|
+
func input(action: String? = nil, args: [String: String] = [:], values: [String: String] = [:]) -> String {
|
|
55
|
+
var object: [String: Any] = ["screen": screen, "params": params]
|
|
56
|
+
if let action {
|
|
57
|
+
object["action"] = action
|
|
58
|
+
object["args"] = args
|
|
59
|
+
object["values"] = values
|
|
60
|
+
}
|
|
61
|
+
let data = (try? JSONSerialization.data(withJSONObject: object, options: [.sortedKeys])) ?? Data("{}".utf8)
|
|
62
|
+
return String(decoding: data, as: UTF8.self)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import SwiftUI
|
|
2
|
+
|
|
3
|
+
/// The whole app: the first screen in a navigation stack, with every screen a
|
|
4
|
+
/// row links to pushed over it. Over it, one line says what went wrong, or
|
|
5
|
+
/// that the app is offline, or how many changes are waiting to be sent.
|
|
6
|
+
public struct AppView: View {
|
|
7
|
+
@ObservedObject private var host: AppHost
|
|
8
|
+
@Environment(\.scenePhase) private var phase
|
|
9
|
+
@State private var showingNotices = false
|
|
10
|
+
private let waiting: String
|
|
11
|
+
private let theme: Theme
|
|
12
|
+
|
|
13
|
+
/// `waiting` is what the first screen says while a database that has
|
|
14
|
+
/// never synced is being filled, such as "Getting your mail". `theme`
|
|
15
|
+
/// is how the app looks: the system's own look unless it is given one.
|
|
16
|
+
public init(host: AppHost, waiting: String = "Loading", theme: Theme = .system) {
|
|
17
|
+
self.host = host
|
|
18
|
+
self.waiting = waiting
|
|
19
|
+
self.theme = theme
|
|
20
|
+
theme.styleBars()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public var body: some View {
|
|
24
|
+
// The status line sits over the navigation bar, not on it: as an
|
|
25
|
+
// inset of the stack it was drawn across the bar's buttons.
|
|
26
|
+
VStack(spacing: 0) {
|
|
27
|
+
banner
|
|
28
|
+
noticesBanner
|
|
29
|
+
NavigationStack(path: $host.path) {
|
|
30
|
+
// A first screen put in another's place starts afresh.
|
|
31
|
+
ScreenView(route: host.root, host: host, first: true, waiting: waiting)
|
|
32
|
+
.id(host.root)
|
|
33
|
+
.navigationDestination(for: Route.self) { ScreenView(route: $0, host: host, first: false, waiting: waiting) }
|
|
34
|
+
}
|
|
35
|
+
.sheet(isPresented: $showingNotices) {
|
|
36
|
+
SyncNoticesSheet(host: host, theme: theme)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
.background { theme.page?.ignoresSafeArea() }
|
|
40
|
+
.sheet(isPresented: Binding(get: { host.sheet != nil }, set: { if !$0 { host.sheet = nil } })) {
|
|
41
|
+
if let route = host.sheet { SheetView(route: route, host: host, theme: theme) }
|
|
42
|
+
}
|
|
43
|
+
.tint(theme.tint)
|
|
44
|
+
.environment(\.theme, theme)
|
|
45
|
+
.onChange(of: phase, initial: true) { _, now in host.front(now == .active) }
|
|
46
|
+
.onChange(of: host.status.message) { _, now in
|
|
47
|
+
if let now { AccessibilityNotification.Announcement(now).post() }
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@ViewBuilder private var banner: some View {
|
|
52
|
+
if let message = host.status.message {
|
|
53
|
+
let failed = !host.status.error.isEmpty || !host.status.syncError.isEmpty
|
|
54
|
+
HStack(alignment: .firstTextBaseline) {
|
|
55
|
+
Label(message, systemImage: failed ? "exclamationmark.triangle" : host.status.offline ? "wifi.slash" : "arrow.triangle.2.circlepath")
|
|
56
|
+
.font(.footnote)
|
|
57
|
+
.fontDesign(theme.ui)
|
|
58
|
+
.foregroundStyle(theme.ink)
|
|
59
|
+
.accessibilityElement(children: .combine)
|
|
60
|
+
.accessibilityIdentifier("sync-status")
|
|
61
|
+
Spacer()
|
|
62
|
+
if failed { Button("Dismiss") { host.dismissError() }.font(.footnote).fontDesign(theme.ui) }
|
|
63
|
+
}
|
|
64
|
+
.padding(.horizontal)
|
|
65
|
+
.padding(.vertical, 8)
|
|
66
|
+
.background((failed ? AnyShapeStyle(.yellow.opacity(0.25)) : theme.fill.map(AnyShapeStyle.init) ?? AnyShapeStyle(.bar)), ignoresSafeAreaEdges: .top)
|
|
67
|
+
.background { theme.page?.ignoresSafeArea(edges: .top) }
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@ViewBuilder private var noticesBanner: some View {
|
|
72
|
+
if !host.notices.isEmpty {
|
|
73
|
+
let count = host.notices.count
|
|
74
|
+
let summary = count == 1
|
|
75
|
+
? (host.notices[0].isConflict ? "1 sync conflict needs review" : "1 rejected change needs review")
|
|
76
|
+
: "\(count) sync notices need review"
|
|
77
|
+
HStack(alignment: .firstTextBaseline) {
|
|
78
|
+
Label(summary, systemImage: "exclamationmark.bubble")
|
|
79
|
+
.font(.footnote)
|
|
80
|
+
.fontDesign(theme.ui)
|
|
81
|
+
.foregroundStyle(theme.ink)
|
|
82
|
+
.accessibilityElement(children: .combine)
|
|
83
|
+
.accessibilityIdentifier("sync-notices-banner")
|
|
84
|
+
Spacer()
|
|
85
|
+
Button("Review") { showingNotices = true }
|
|
86
|
+
.font(.footnote.weight(.medium))
|
|
87
|
+
.fontDesign(theme.ui)
|
|
88
|
+
.accessibilityIdentifier("sync-notices-review")
|
|
89
|
+
}
|
|
90
|
+
.padding(.horizontal)
|
|
91
|
+
.padding(.vertical, 8)
|
|
92
|
+
.background(Color.orange.opacity(0.18), ignoresSafeAreaEdges: host.status.message == nil ? .top : [])
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/// A screen brought up as a sheet. One that Ruby draws gets a navigation
|
|
98
|
+
/// stack, for its title and bar buttons. One that is a single Swift view is
|
|
99
|
+
/// shown as it is: such a view brings its own bar, and a stack inside a
|
|
100
|
+
/// stack is not allowed.
|
|
101
|
+
private struct SheetView: View {
|
|
102
|
+
let route: Route
|
|
103
|
+
@ObservedObject var host: AppHost
|
|
104
|
+
let theme: Theme
|
|
105
|
+
|
|
106
|
+
var body: some View {
|
|
107
|
+
Group {
|
|
108
|
+
// Nothing until the screen is drawn: which of the two it is
|
|
109
|
+
// must not change under a view that holds what was typed.
|
|
110
|
+
if host.screens[route] == nil {
|
|
111
|
+
Color.clear
|
|
112
|
+
} else if bare {
|
|
113
|
+
ScreenView(route: route, host: host, first: false)
|
|
114
|
+
} else {
|
|
115
|
+
NavigationStack { ScreenView(route: route, host: host, first: false) }
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
.tint(theme.tint)
|
|
119
|
+
.environment(\.theme, theme)
|
|
120
|
+
.presentationBackground(theme.page.map(AnyShapeStyle.init) ?? AnyShapeStyle(.background))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private var bare: Bool {
|
|
124
|
+
guard let screen = host.screens[route] else { return false }
|
|
125
|
+
let holder = screen.children.first { $0.kind == "navigation" } ?? screen
|
|
126
|
+
return !holder.children.isEmpty && holder.children.allSatisfy { $0.kind == "swift" }
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/// One screen, drawn from the description the app last gave for it.
|
|
131
|
+
struct ScreenView: View {
|
|
132
|
+
let route: Route
|
|
133
|
+
let host: AppHost
|
|
134
|
+
let first: Bool
|
|
135
|
+
let waiting: String
|
|
136
|
+
@ObservedObject private var watched: AppHost
|
|
137
|
+
@Environment(\.theme) private var theme
|
|
138
|
+
|
|
139
|
+
init(route: Route, host: AppHost, first: Bool, waiting: String = "Loading") {
|
|
140
|
+
self.route = route
|
|
141
|
+
self.host = host
|
|
142
|
+
self.first = first
|
|
143
|
+
self.waiting = waiting
|
|
144
|
+
watched = host
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
var body: some View {
|
|
148
|
+
if host.fetchingFirst, first {
|
|
149
|
+
ProgressView(waiting)
|
|
150
|
+
.fontDesign(theme.ui)
|
|
151
|
+
.foregroundStyle(theme.muted)
|
|
152
|
+
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
153
|
+
.background { theme.page?.ignoresSafeArea() }
|
|
154
|
+
} else if let screen = host.screens[route] {
|
|
155
|
+
NodeView(node: screen, route: route, host: host, first: first)
|
|
156
|
+
} else {
|
|
157
|
+
Color.clear
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/// Draws one node with the stock control its `kind` names. A kind this
|
|
163
|
+
/// renderer does not know draws nothing. The host is handed down and not read
|
|
164
|
+
/// from the environment, which a swipe action's button does not inherit.
|
|
165
|
+
struct NodeView: View {
|
|
166
|
+
let node: Node
|
|
167
|
+
let route: Route
|
|
168
|
+
/// Watched, because what a node draws also depends on what the host
|
|
169
|
+
/// holds: what is typed into a field, and how a list's next page stands.
|
|
170
|
+
@ObservedObject var host: AppHost
|
|
171
|
+
var first = false
|
|
172
|
+
/// True for what sits beside fields that are all blank: there is
|
|
173
|
+
/// nothing to send yet.
|
|
174
|
+
var idle = false
|
|
175
|
+
@Environment(\.theme) private var theme
|
|
176
|
+
|
|
177
|
+
var body: some View {
|
|
178
|
+
switch node.kind {
|
|
179
|
+
case "screen", "navigation": page
|
|
180
|
+
case "list": list
|
|
181
|
+
case "section": section
|
|
182
|
+
case "row": row
|
|
183
|
+
case "stack": stack
|
|
184
|
+
case "spacer": Spacer(minLength: 8)
|
|
185
|
+
case "text": Text(node.text).font(font).fontDesign(face).foregroundStyle(tone).lineLimit(Int(node.prop("lines")))
|
|
186
|
+
case "date": Text(Moment.written(node.text)).font(font).fontDesign(theme.ui).foregroundStyle(tone)
|
|
187
|
+
case "image": image
|
|
188
|
+
case "button": button
|
|
189
|
+
case "item": item
|
|
190
|
+
case "field": field
|
|
191
|
+
case "more": more
|
|
192
|
+
case "swift": swift
|
|
193
|
+
default: EmptyView()
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private func each(_ items: [Node.Keyed], idle: Bool = false) -> some View {
|
|
198
|
+
ForEach(items) { NodeView(node: $0.node, route: route, host: host, first: first, idle: idle) }
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private func keyed(except kinds: String...) -> [Node.Keyed] {
|
|
202
|
+
node.keyed.filter { !kinds.contains($0.node.kind) }
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/// What a field shows: what was typed, or else what the screen gave it.
|
|
206
|
+
private func shown(_ field: Node) -> String {
|
|
207
|
+
host.typed[route]?[field.prop("name")] ?? field.text
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// The screen itself, or its navigation: content, with a title and bar
|
|
211
|
+
// buttons when it names them, and a bar pinned under it when it has one.
|
|
212
|
+
private var page: some View {
|
|
213
|
+
let content = keyed(except: "toolbar", "bottom")
|
|
214
|
+
let controls = node.children(of: "toolbar").flatMap(\.children)
|
|
215
|
+
let leading = Node.keyed(controls.filter { $0.prop("position") == "leading" })
|
|
216
|
+
let trailing = Node.keyed(controls.filter { $0.prop("position") != "leading" })
|
|
217
|
+
let under = node.children(of: "bottom").flatMap(\.children)
|
|
218
|
+
let fields = under.filter { $0.kind == "field" }
|
|
219
|
+
let blank = !fields.isEmpty && fields.allSatisfy { shown($0).trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }
|
|
220
|
+
return Group {
|
|
221
|
+
if content.allSatisfy({ ["list", "navigation", "swift"].contains($0.node.kind) }) {
|
|
222
|
+
each(content)
|
|
223
|
+
} else {
|
|
224
|
+
VStack(alignment: .leading, spacing: 8) { each(content) }
|
|
225
|
+
.padding()
|
|
226
|
+
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
|
|
227
|
+
.background { theme.page?.ignoresSafeArea() }
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
.modifier(Titled(title: node.kind == "navigation" ? node.prop("title") : nil, large: first))
|
|
231
|
+
.toolbar {
|
|
232
|
+
ToolbarItemGroup(placement: NodeView.leadingEdge) { each(leading) }
|
|
233
|
+
ToolbarItemGroup(placement: .primaryAction) { each(trailing) }
|
|
234
|
+
}
|
|
235
|
+
.safeAreaInset(edge: .bottom, spacing: 0) {
|
|
236
|
+
if !under.isEmpty {
|
|
237
|
+
HStack(alignment: .bottom, spacing: 10) { each(Node.keyed(under), idle: blank) }
|
|
238
|
+
.padding(.horizontal)
|
|
239
|
+
.padding(.vertical, 8)
|
|
240
|
+
.background(theme.page.map(AnyShapeStyle.init) ?? AnyShapeStyle(.bar))
|
|
241
|
+
.overlay(alignment: .top) { theme.hairline.frame(height: 1) }
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Pulling a list that asks for it syncs and draws the screens again.
|
|
247
|
+
@ViewBuilder private var list: some View {
|
|
248
|
+
let rows = List { each(node.keyed).listRowBackground(theme.surface).listRowSeparatorTint(theme.hairline) }
|
|
249
|
+
.scrollContentBackground(theme.page == nil ? .automatic : .hidden)
|
|
250
|
+
.background { theme.page?.ignoresSafeArea() }
|
|
251
|
+
if node.prop("refresh") == "true" {
|
|
252
|
+
rows.refreshable { await host.refresh() }
|
|
253
|
+
} else {
|
|
254
|
+
rows
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
@ViewBuilder private var section: some View {
|
|
259
|
+
if node.text.isEmpty {
|
|
260
|
+
Section { each(node.keyed) }
|
|
261
|
+
} else {
|
|
262
|
+
Section(node.text) { each(node.keyed) }
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// A row's children one under another, read aloud as one thing. With a
|
|
267
|
+
// `link` the row is a navigation link to that screen, opened with the
|
|
268
|
+
// params the row carries.
|
|
269
|
+
@ViewBuilder private var row: some View {
|
|
270
|
+
let content = VStack(alignment: .leading, spacing: 3) { each(keyed(except: "swipe")) }
|
|
271
|
+
.accessibilityElement(children: .combine)
|
|
272
|
+
Group {
|
|
273
|
+
if node.prop("link").isEmpty {
|
|
274
|
+
content
|
|
275
|
+
} else {
|
|
276
|
+
NavigationLink(value: Route(node.prop("link"), node.params)) { content }
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
.accessibilityIdentifier(node.id)
|
|
280
|
+
.swipeActions(edge: .trailing) { swipes(leading: false) }
|
|
281
|
+
.swipeActions(edge: .leading) { swipes(leading: true) }
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
private func swipes(leading: Bool) -> some View {
|
|
285
|
+
let items = node.keyed.filter { $0.node.kind == "swipe" && ($0.node.prop("edge") == "leading") == leading }
|
|
286
|
+
return ForEach(items) { NodeView(node: $0.node, route: route, host: host).button }
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
@ViewBuilder private var stack: some View {
|
|
290
|
+
if node.prop("axis") == "horizontal" {
|
|
291
|
+
HStack(alignment: .firstTextBaseline, spacing: 6) { each(node.keyed) }
|
|
292
|
+
} else {
|
|
293
|
+
VStack(alignment: .leading, spacing: 3) { each(node.keyed) }
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// An icon by what it means, called that aloud. One the table does not
|
|
298
|
+
// know is decoration.
|
|
299
|
+
@ViewBuilder private var image: some View {
|
|
300
|
+
let picture = Image(systemName: Icons.symbol(node.text)).imageScale(.small).foregroundStyle(tone)
|
|
301
|
+
if Icons.spoken(node.text).isEmpty {
|
|
302
|
+
picture.accessibilityHidden(true)
|
|
303
|
+
} else {
|
|
304
|
+
picture.accessibilityLabel(Icons.spoken(node.text))
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
#if os(iOS)
|
|
309
|
+
private static let leadingEdge = ToolbarItemPlacement.topBarLeading
|
|
310
|
+
#else
|
|
311
|
+
private static let leadingEdge = ToolbarItemPlacement.navigation
|
|
312
|
+
#endif
|
|
313
|
+
|
|
314
|
+
// A button or a swipe: both are a label and an action for this screen.
|
|
315
|
+
// A button that holds items opens them as a menu.
|
|
316
|
+
@ViewBuilder var button: some View {
|
|
317
|
+
let items = node.keyed.filter { $0.node.kind == "item" }
|
|
318
|
+
if node.kind == "swipe" {
|
|
319
|
+
// Not the destructive role: with it the list takes the row out
|
|
320
|
+
// itself, at once, and the app's own answer arrives a moment
|
|
321
|
+
// later from another thread. The two updates can collide. A red
|
|
322
|
+
// tint says the same, and the row leaves when Ruby says it has.
|
|
323
|
+
Button { host.perform(node.action, args: node.args, on: route) } label: { label(image: node.prop("image")) }
|
|
324
|
+
.tint(role == .destructive ? .red : nil)
|
|
325
|
+
} else if items.isEmpty {
|
|
326
|
+
Button(role: role) { host.perform(node.action, args: node.args, on: route) } label: { label(image: node.prop("image")) }
|
|
327
|
+
.disabled(idle)
|
|
328
|
+
} else {
|
|
329
|
+
Menu { each(items) } label: { label(image: node.prop("image")) }
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// One line of a menu, ticked when it is the selected one. With `replace`
|
|
334
|
+
// it puts that screen in the first one's place, with `sheet` it brings
|
|
335
|
+
// it up as a sheet, with `link` it opens it over this one, and
|
|
336
|
+
// otherwise it is an action.
|
|
337
|
+
private var item: some View {
|
|
338
|
+
Button(role: role) {
|
|
339
|
+
if !node.prop("replace").isEmpty {
|
|
340
|
+
host.replace(with: Route(node.prop("replace"), node.params))
|
|
341
|
+
} else if !node.prop("sheet").isEmpty {
|
|
342
|
+
host.sheet = Route(node.prop("sheet"), node.params)
|
|
343
|
+
} else if !node.prop("link").isEmpty {
|
|
344
|
+
host.path.append(Route(node.prop("link"), node.params))
|
|
345
|
+
} else {
|
|
346
|
+
host.perform(node.action, args: node.args, on: route)
|
|
347
|
+
}
|
|
348
|
+
} label: {
|
|
349
|
+
label(image: node.prop("selected") == "true" ? "checkmark" : node.prop("image"))
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
private var role: ButtonRole? { node.prop("role") == "destructive" ? .destructive : nil }
|
|
354
|
+
|
|
355
|
+
private func label(image: String) -> some View {
|
|
356
|
+
Group {
|
|
357
|
+
if image.isEmpty {
|
|
358
|
+
Text(node.text)
|
|
359
|
+
} else {
|
|
360
|
+
Label(node.text, systemImage: Icons.symbol(image))
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
.fontDesign(theme.ui)
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// A box to type in. What is typed is the host's to keep, by screen, so
|
|
367
|
+
// it is still there after a redraw or a trip back and forth.
|
|
368
|
+
private var field: some View {
|
|
369
|
+
let name = node.prop("name")
|
|
370
|
+
let text = Binding(get: { shown(node) }, set: { host.typed[route, default: [:]][name] = $0 })
|
|
371
|
+
let box = Group {
|
|
372
|
+
if node.prop("multiline") == "true" {
|
|
373
|
+
TextField(node.prop("label"), text: text, axis: .vertical).lineLimit(1...5)
|
|
374
|
+
} else {
|
|
375
|
+
TextField(node.prop("label"), text: text)
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
.accessibilityIdentifier(name)
|
|
379
|
+
return Group {
|
|
380
|
+
if let fill = theme.fill {
|
|
381
|
+
// Flat, with a hairline: the theme's own box.
|
|
382
|
+
box.textFieldStyle(.plain)
|
|
383
|
+
.fontDesign(theme.reading)
|
|
384
|
+
.foregroundStyle(theme.ink)
|
|
385
|
+
.padding(.horizontal, 12)
|
|
386
|
+
.padding(.vertical, 8)
|
|
387
|
+
.background(fill, in: RoundedRectangle(cornerRadius: 10))
|
|
388
|
+
.overlay(RoundedRectangle(cornerRadius: 10).strokeBorder(theme.hairline ?? .clear))
|
|
389
|
+
} else {
|
|
390
|
+
box.textFieldStyle(.roundedBorder)
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// The list goes on at the server. Coming into view fetches the next page.
|
|
396
|
+
@ViewBuilder private var more: some View {
|
|
397
|
+
if host.paging[node.id] == .failed {
|
|
398
|
+
Button("Could not load more. Tap to retry") { host.loadMore(node.id, query: node.prop("query")) }
|
|
399
|
+
.font(.footnote)
|
|
400
|
+
.fontDesign(theme.ui)
|
|
401
|
+
.accessibilityIdentifier("more-failed")
|
|
402
|
+
} else {
|
|
403
|
+
HStack {
|
|
404
|
+
Spacer()
|
|
405
|
+
ProgressView().tint(theme.muted)
|
|
406
|
+
Spacer()
|
|
407
|
+
}
|
|
408
|
+
.accessibilityIdentifier("more")
|
|
409
|
+
.onAppear { host.loadMore(node.id, query: node.prop("query")) }
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// A view the app wrote in SwiftUI, by the name it registered. A name
|
|
414
|
+
// nobody registered says so on screen.
|
|
415
|
+
@ViewBuilder private var swift: some View {
|
|
416
|
+
if let view = host.views.view(node.text, Props(node), Actions(host: host, route: route)) {
|
|
417
|
+
view
|
|
418
|
+
} else {
|
|
419
|
+
Label(NodeView.unregistered(node.text), systemImage: "exclamationmark.triangle").foregroundStyle(.orange)
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
static func unregistered(_ name: String) -> String { "No Swift view is registered as \(name)" }
|
|
424
|
+
|
|
425
|
+
private var font: Font {
|
|
426
|
+
switch node.prop("style") {
|
|
427
|
+
case "title": .title
|
|
428
|
+
case "headline": .headline
|
|
429
|
+
case "subheadline": .subheadline
|
|
430
|
+
case "caption": .caption
|
|
431
|
+
default: .body
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// A text with no style is running text, set in the theme's reading
|
|
436
|
+
// face. Every other style is a label, in the face of the controls.
|
|
437
|
+
private var face: Font.Design { node.prop("style").isEmpty ? theme.reading : theme.ui }
|
|
438
|
+
|
|
439
|
+
private var tone: Color {
|
|
440
|
+
switch node.prop("tone") {
|
|
441
|
+
case "secondary": theme.muted
|
|
442
|
+
case "accent": theme.tint
|
|
443
|
+
default: theme.ink
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/// A navigation title: large on the first screen, inline on pushed ones. A
|
|
449
|
+
/// screen with no navigation of its own keeps whatever its Swift view sets.
|
|
450
|
+
private struct Titled: ViewModifier {
|
|
451
|
+
let title: String?
|
|
452
|
+
let large: Bool
|
|
453
|
+
|
|
454
|
+
func body(content: Content) -> some View {
|
|
455
|
+
if let title {
|
|
456
|
+
content
|
|
457
|
+
.navigationTitle(title)
|
|
458
|
+
#if os(iOS)
|
|
459
|
+
.navigationBarTitleDisplayMode(large ? .large : .inline)
|
|
460
|
+
#endif
|
|
461
|
+
} else {
|
|
462
|
+
content
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/// A moment the app wrote as ISO 8601, in the phone's language and time zone:
|
|
468
|
+
/// the time when it is today, or else the day.
|
|
469
|
+
enum Moment {
|
|
470
|
+
static func date(_ iso8601: String) -> Date? {
|
|
471
|
+
let parser = ISO8601DateFormatter()
|
|
472
|
+
parser.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
|
|
473
|
+
return parser.date(from: iso8601) ?? ISO8601DateFormatter().date(from: iso8601)
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
static func written(_ iso8601: String, now: Date = Date(), calendar: Calendar = .current) -> String {
|
|
477
|
+
guard let date = date(iso8601) else { return iso8601 }
|
|
478
|
+
if calendar.isDate(date, inSameDayAs: now) { return date.formatted(date: .omitted, time: .shortened) }
|
|
479
|
+
return date.formatted(.dateTime.month(.abbreviated).day())
|
|
480
|
+
}
|
|
481
|
+
}
|