@zapiack/react-native 0.1.0 → 0.2.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.
- package/CHANGELOG.md +5 -0
- package/README.md +90 -0
- package/android/build.gradle +1 -1
- package/android/src/main/java/com/zapiack/reactnative/ZapiackPushModule.kt +3 -0
- package/dist/calls.d.mts +198 -0
- package/dist/calls.d.ts +198 -0
- package/dist/calls.js +793 -0
- package/dist/calls.mjs +406 -0
- package/dist/chunk-CCLICX3L.mjs +371 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +389 -6
- package/dist/index.mjs +21 -2
- package/dist/realtime-BAcafCSr.d.mts +173 -0
- package/dist/realtime-BAcafCSr.d.ts +173 -0
- package/ios/Zapiack/Api.swift +6 -2
- package/ios/Zapiack/LayoutModel.swift +128 -0
- package/ios/Zapiack/LayoutView.swift +206 -0
- package/ios/Zapiack/Models.swift +1 -1
- package/ios/Zapiack/PushClient.swift +9 -1
- package/ios/Zapiack/ZapiackLiveActivityWidget.swift +170 -0
- package/ios/Zapiack/ZapiackLiveAttributes.swift +78 -0
- package/ios/Zapiack/ZapiackPush.swift +27 -0
- package/ios/ZapiackPushModule.m +1 -0
- package/ios/ZapiackPushModule.swift +5 -0
- package/package.json +32 -5
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zapiack Realtime: live messages over one WebSocket.
|
|
3
|
+
*
|
|
4
|
+
* Your server mints a short-lived token with its API key (`POST /v1/realtime/tokens`) and hands it to the client, so the API key never
|
|
5
|
+
* reaches a browser or an app. The client asks for a token every time it connects, so a token that expires is simply replaced.
|
|
6
|
+
*
|
|
7
|
+
* It reconnects by itself with a growing, jittered pause, re-joins every channel and asks for what it missed since the last persisted
|
|
8
|
+
* message it saw, so a lost signal costs no messages. Nothing here touches push, so it works in any JavaScript runtime that has a
|
|
9
|
+
* WebSocket: browsers, React Native, Node 22+.
|
|
10
|
+
*/
|
|
11
|
+
type RealtimeState = 'idle' | 'connecting' | 'connected' | 'reconnecting' | 'closed';
|
|
12
|
+
interface RealtimeMessage<T = unknown> {
|
|
13
|
+
msgId: string;
|
|
14
|
+
ts: number;
|
|
15
|
+
channel: string;
|
|
16
|
+
event: string;
|
|
17
|
+
data: T;
|
|
18
|
+
/** The user id (or connection id) of the sender. */
|
|
19
|
+
from: string | null;
|
|
20
|
+
/** True when it was sent while this client was away and is being replayed. */
|
|
21
|
+
replayed?: boolean;
|
|
22
|
+
}
|
|
23
|
+
interface PresenceMember {
|
|
24
|
+
connId: string;
|
|
25
|
+
user: string | null;
|
|
26
|
+
data: unknown;
|
|
27
|
+
at: number;
|
|
28
|
+
}
|
|
29
|
+
interface PublishOptions {
|
|
30
|
+
/** The name of the event. Default `message`. */
|
|
31
|
+
event?: string;
|
|
32
|
+
/** Keep it in the channel's history, so clients that were away can replay it. Default false. */
|
|
33
|
+
persist?: boolean;
|
|
34
|
+
/** Wait for the server to confirm. Default true. Pass false to fire and forget. */
|
|
35
|
+
ack?: boolean;
|
|
36
|
+
}
|
|
37
|
+
interface PublishResult {
|
|
38
|
+
msgId: string;
|
|
39
|
+
/** How many connections were subscribed when it was sent. */
|
|
40
|
+
delivered: number;
|
|
41
|
+
}
|
|
42
|
+
declare class ZapiackRealtimeError extends Error {
|
|
43
|
+
/** The server's code: 401 token, 403 not allowed, 413 too large, 429 slow down, 503 try again. 0 for a problem on this side. */
|
|
44
|
+
readonly code: number;
|
|
45
|
+
/** For a failed request (`request()`): the service's own word for it: `forbidden`, `invalid`, `no_route`, `timeout`, `rate_limited` or `internal`. */
|
|
46
|
+
readonly reason?: string | undefined;
|
|
47
|
+
constructor(message: string,
|
|
48
|
+
/** The server's code: 401 token, 403 not allowed, 413 too large, 429 slow down, 503 try again. 0 for a problem on this side. */
|
|
49
|
+
code?: number,
|
|
50
|
+
/** For a failed request (`request()`): the service's own word for it: `forbidden`, `invalid`, `no_route`, `timeout`, `rate_limited` or `internal`. */
|
|
51
|
+
reason?: string | undefined);
|
|
52
|
+
}
|
|
53
|
+
interface SocketLike {
|
|
54
|
+
readyState: number;
|
|
55
|
+
send(data: string): void;
|
|
56
|
+
close(code?: number, reason?: string): void;
|
|
57
|
+
onopen: ((ev?: unknown) => void) | null;
|
|
58
|
+
onmessage: ((ev: {
|
|
59
|
+
data: unknown;
|
|
60
|
+
}) => void) | null;
|
|
61
|
+
onclose: ((ev: {
|
|
62
|
+
code: number;
|
|
63
|
+
}) => void) | null;
|
|
64
|
+
onerror: ((ev?: unknown) => void) | null;
|
|
65
|
+
}
|
|
66
|
+
interface RealtimeEnv {
|
|
67
|
+
socket(url: string): SocketLike;
|
|
68
|
+
setTimeout(fn: () => void, ms: number): unknown;
|
|
69
|
+
clearTimeout(id: unknown): void;
|
|
70
|
+
setInterval(fn: () => void, ms: number): unknown;
|
|
71
|
+
clearInterval(id: unknown): void;
|
|
72
|
+
random(): number;
|
|
73
|
+
}
|
|
74
|
+
interface RealtimeOptions {
|
|
75
|
+
/** Returns a fresh token from your server. Called on every connect, so an expired token is replaced. */
|
|
76
|
+
getToken: () => Promise<string>;
|
|
77
|
+
/** Default `wss://rt.zapiack.com`. */
|
|
78
|
+
url?: string;
|
|
79
|
+
/** How often to check the line is alive. Default 25 seconds. */
|
|
80
|
+
heartbeatMs?: number;
|
|
81
|
+
/** The shortest and longest pause before reconnecting. Default 1s and 30s. */
|
|
82
|
+
backoffMs?: {
|
|
83
|
+
min: number;
|
|
84
|
+
max: number;
|
|
85
|
+
};
|
|
86
|
+
/** How long to wait for a confirmation. Default 10 seconds. */
|
|
87
|
+
ackTimeoutMs?: number;
|
|
88
|
+
}
|
|
89
|
+
declare const DEFAULT_REALTIME_URL = "wss://rt.zapiack.com";
|
|
90
|
+
declare const browserRealtimeEnv: () => RealtimeEnv;
|
|
91
|
+
type Handler = (m: RealtimeMessage) => void;
|
|
92
|
+
declare class ZapiackRealtime {
|
|
93
|
+
private readonly env;
|
|
94
|
+
private readonly now;
|
|
95
|
+
private readonly o;
|
|
96
|
+
private ws;
|
|
97
|
+
private _state;
|
|
98
|
+
private readonly subs;
|
|
99
|
+
private readonly presenceHandlers;
|
|
100
|
+
private readonly members;
|
|
101
|
+
private readonly waiting;
|
|
102
|
+
private readonly pending;
|
|
103
|
+
private rpcSeq;
|
|
104
|
+
private readonly stateHandlers;
|
|
105
|
+
private readonly errorHandlers;
|
|
106
|
+
private outbox;
|
|
107
|
+
private attempt;
|
|
108
|
+
private wanted;
|
|
109
|
+
private retry;
|
|
110
|
+
private beat;
|
|
111
|
+
private heard;
|
|
112
|
+
private ackSeq;
|
|
113
|
+
private first;
|
|
114
|
+
/** Set by an error the server marks fatal, for runtimes that cannot see a custom close code. */
|
|
115
|
+
private refused;
|
|
116
|
+
private _connId;
|
|
117
|
+
constructor(options: RealtimeOptions, env?: RealtimeEnv, now?: () => number);
|
|
118
|
+
get state(): RealtimeState;
|
|
119
|
+
/** This connection's id, once connected. Other members use it to signal you. */
|
|
120
|
+
get connectionId(): string | null;
|
|
121
|
+
onState(fn: (s: RealtimeState) => void): () => void;
|
|
122
|
+
onError(fn: (e: ZapiackRealtimeError) => void): () => void;
|
|
123
|
+
/** Connects and resolves once the server has said hello. It keeps itself connected afterwards until `disconnect()`. */
|
|
124
|
+
connect(): Promise<void>;
|
|
125
|
+
private chain;
|
|
126
|
+
/** Leaves for good: closes the socket, forgets the pending sends. Subscriptions stay, for a later `connect()`. */
|
|
127
|
+
disconnect(): void;
|
|
128
|
+
/** Calls `handler` for every message on the channel, now and after reconnects. Returns how to stop. */
|
|
129
|
+
subscribe(channel: string, handler: Handler, options?: {
|
|
130
|
+
resumeFrom?: string;
|
|
131
|
+
}): () => void;
|
|
132
|
+
/** Sends a message. Resolves when the server confirms, unless `ack: false`. */
|
|
133
|
+
publish(channel: string, data: unknown, options?: PublishOptions): Promise<PublishResult | void>;
|
|
134
|
+
/**
|
|
135
|
+
* Asks a Zapiack service something over this connection and waits for the answer: the way a call client talks to the media server.
|
|
136
|
+
* Needs a token minted for a call. It is never queued for later: if the line is down it fails at once, because an answer to an old
|
|
137
|
+
* question is worse than none.
|
|
138
|
+
*/
|
|
139
|
+
request<T = unknown>(method: string, data?: unknown): Promise<T>;
|
|
140
|
+
/** Sends to one connection in a channel you are subscribed to. Used for peer-to-peer setup such as WebRTC. */
|
|
141
|
+
signal(channel: string, to: string, event: string, data: unknown): void;
|
|
142
|
+
/** Presence in a channel you are subscribed to. */
|
|
143
|
+
presence(channel: string): {
|
|
144
|
+
enter: (data?: unknown) => boolean;
|
|
145
|
+
update: (data: unknown) => boolean;
|
|
146
|
+
leave: () => boolean;
|
|
147
|
+
/** Who is here now, as last told. */
|
|
148
|
+
members: () => PresenceMember[];
|
|
149
|
+
onChange: (fn: (e: {
|
|
150
|
+
action: "enter" | "update" | "leave";
|
|
151
|
+
member: Partial<PresenceMember>;
|
|
152
|
+
}) => void) => () => boolean;
|
|
153
|
+
};
|
|
154
|
+
private set;
|
|
155
|
+
private fail;
|
|
156
|
+
private settle;
|
|
157
|
+
private failAll;
|
|
158
|
+
private failRequests;
|
|
159
|
+
/** Writes now if the line is open, otherwise keeps it (a short while) for when it is. False when there is no room. */
|
|
160
|
+
private send;
|
|
161
|
+
private open;
|
|
162
|
+
private onFrame;
|
|
163
|
+
private onMessage;
|
|
164
|
+
private onPresence;
|
|
165
|
+
private rejoin;
|
|
166
|
+
private startBeat;
|
|
167
|
+
private stopBeat;
|
|
168
|
+
private onClose;
|
|
169
|
+
private later;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export { DEFAULT_REALTIME_URL as D, ZapiackRealtime as Z, ZapiackRealtimeError as a, browserRealtimeEnv as g };
|
|
173
|
+
export type { PresenceMember as P, RealtimeEnv as R, SocketLike as S, PublishOptions as b, PublishResult as c, RealtimeMessage as d, RealtimeOptions as e, RealtimeState as f };
|
package/ios/Zapiack/Api.swift
CHANGED
|
@@ -46,8 +46,10 @@ public struct DeviceRegistration: Encodable {
|
|
|
46
46
|
public var permission: String?
|
|
47
47
|
public var environment: String?
|
|
48
48
|
public var appVersion: String?
|
|
49
|
+
/// iOS 17.2+: the push-to-start token for Zapiack Live Activities, as hex. Absent leaves the one on file alone.
|
|
50
|
+
public var liveActivityToken: String?
|
|
49
51
|
|
|
50
|
-
public init(installationId: String, platform: String = "ios", token: String, externalUserId: String?? = nil, tags: [String: JSONValue]? = nil, permission: String? = nil, environment: String? = nil, appVersion: String? = nil) {
|
|
52
|
+
public init(installationId: String, platform: String = "ios", token: String, externalUserId: String?? = nil, tags: [String: JSONValue]? = nil, permission: String? = nil, environment: String? = nil, appVersion: String? = nil, liveActivityToken: String? = nil) {
|
|
51
53
|
self.installationId = installationId
|
|
52
54
|
self.platform = platform
|
|
53
55
|
self.token = token
|
|
@@ -56,10 +58,11 @@ public struct DeviceRegistration: Encodable {
|
|
|
56
58
|
self.permission = permission
|
|
57
59
|
self.environment = environment
|
|
58
60
|
self.appVersion = appVersion
|
|
61
|
+
self.liveActivityToken = liveActivityToken
|
|
59
62
|
}
|
|
60
63
|
|
|
61
64
|
private enum CodingKeys: String, CodingKey {
|
|
62
|
-
case installationId, platform, token, externalUserId, tags, permission, environment, appVersion
|
|
65
|
+
case installationId, platform, token, externalUserId, tags, permission, environment, appVersion, liveActivityToken
|
|
63
66
|
}
|
|
64
67
|
|
|
65
68
|
// An absent user leaves the link alone, a string links it, and an explicit null unlinks it, so all three are kept apart.
|
|
@@ -77,6 +80,7 @@ public struct DeviceRegistration: Encodable {
|
|
|
77
80
|
try c.encodeIfPresent(permission, forKey: .permission)
|
|
78
81
|
try c.encodeIfPresent(environment, forKey: .environment)
|
|
79
82
|
try c.encodeIfPresent(appVersion, forKey: .appVersion)
|
|
83
|
+
try c.encodeIfPresent(liveActivityToken, forKey: .liveActivityToken)
|
|
80
84
|
}
|
|
81
85
|
}
|
|
82
86
|
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
/// A push template's grid layout, as Zapiack sends it in `zp_layout`: rows and stacks that share their space by weight, cut from a
|
|
4
|
+
/// 24-column grid on the server so every platform draws the same thing.
|
|
5
|
+
///
|
|
6
|
+
/// Elements are named by short references: `t` the title, `b` the body, `i` the small picture, `p` the big picture, `l<n>` the
|
|
7
|
+
/// n-th live element (`zp_live`) and `a<n>` the n-th button.
|
|
8
|
+
public struct ZapiackLayoutNode: Decodable, Hashable {
|
|
9
|
+
public let row: Bool
|
|
10
|
+
public let children: [Child]
|
|
11
|
+
|
|
12
|
+
public enum Child: Hashable {
|
|
13
|
+
case sub(weight: Int, node: ZapiackLayoutNode)
|
|
14
|
+
case element(weight: Int, ref: String, rows: Int)
|
|
15
|
+
case space(weight: Int)
|
|
16
|
+
|
|
17
|
+
public var weight: Int {
|
|
18
|
+
switch self {
|
|
19
|
+
case let .sub(w, _), let .element(w, _, _), let .space(w): return w
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/// How tall one grid row is drawn, in points.
|
|
25
|
+
public static let rowPoints: CGFloat = 8
|
|
26
|
+
|
|
27
|
+
private enum Keys: String, CodingKey { case d, c }
|
|
28
|
+
private enum ChildKeys: String, CodingKey { case w, n, e, h, s }
|
|
29
|
+
|
|
30
|
+
public init(from decoder: Decoder) throws {
|
|
31
|
+
let c = try decoder.container(keyedBy: Keys.self)
|
|
32
|
+
row = (try c.decode(String.self, forKey: .d)) == "r"
|
|
33
|
+
var list = try c.nestedUnkeyedContainer(forKey: .c)
|
|
34
|
+
var out: [Child] = []
|
|
35
|
+
while !list.isAtEnd {
|
|
36
|
+
let item = try list.nestedContainer(keyedBy: ChildKeys.self)
|
|
37
|
+
let w = min(max(try item.decodeIfPresent(Int.self, forKey: .w) ?? 0, 0), 80)
|
|
38
|
+
guard w > 0 else { continue }
|
|
39
|
+
if let n = try item.decodeIfPresent(ZapiackLayoutNode.self, forKey: .n) {
|
|
40
|
+
out.append(.sub(weight: w, node: n))
|
|
41
|
+
} else if let e = try item.decodeIfPresent(String.self, forKey: .e), e.range(of: "^(t|b|i|p|l[0-9]{1,2}|a[0-9])$", options: .regularExpression) != nil {
|
|
42
|
+
out.append(.element(weight: w, ref: e, rows: min(max(try item.decodeIfPresent(Int.self, forKey: .h) ?? 1, 1), 80)))
|
|
43
|
+
} else {
|
|
44
|
+
out.append(.space(weight: w))
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
children = out
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/// Reads the tree from the JSON text Zapiack sends, or nil.
|
|
51
|
+
public static func parse(_ json: String?) -> ZapiackLayoutNode? {
|
|
52
|
+
guard let json, let data = json.data(using: .utf8) else { return nil }
|
|
53
|
+
return try? JSONDecoder().decode(ZapiackLayoutNode.self, from: data)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// How tall it draws, in grid rows: a stack adds its parts up, a row is as tall as its tallest part.
|
|
57
|
+
public var rows: Int {
|
|
58
|
+
row ? children.map(Self.rows(of:)).max() ?? 0 : children.map(Self.rows(of:)).reduce(0, +)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private static func rows(of child: Child) -> Int {
|
|
62
|
+
switch child {
|
|
63
|
+
case let .sub(_, n): return n.rows
|
|
64
|
+
case let .element(_, _, r): return r
|
|
65
|
+
case let .space(w): return w
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// One live element (`zp_live`): a timer, progress, counter, date or badge, as the server filled it in.
|
|
71
|
+
public struct ZapiackLiveItem: Decodable, Hashable {
|
|
72
|
+
public let type: String
|
|
73
|
+
public let mode: String?
|
|
74
|
+
public let source: String?
|
|
75
|
+
public let format: String?
|
|
76
|
+
public let look: String?
|
|
77
|
+
public let color: String?
|
|
78
|
+
public let label: String?
|
|
79
|
+
public let onEnd: String?
|
|
80
|
+
public let expiredText: String?
|
|
81
|
+
public let steps: [String]?
|
|
82
|
+
public let current: Int?
|
|
83
|
+
public let value: String?
|
|
84
|
+
public let style: String?
|
|
85
|
+
public let text: String?
|
|
86
|
+
|
|
87
|
+
private enum Keys: String, CodingKey { case type, mode, source, format, look, color, label, onEnd, expiredText, steps, current, value, style, text }
|
|
88
|
+
|
|
89
|
+
public init(from decoder: Decoder) throws {
|
|
90
|
+
let c = try decoder.container(keyedBy: Keys.self)
|
|
91
|
+
type = try c.decode(String.self, forKey: .type)
|
|
92
|
+
mode = try c.decodeIfPresent(String.self, forKey: .mode)
|
|
93
|
+
source = try c.decodeIfPresent(String.self, forKey: .source)
|
|
94
|
+
format = try c.decodeIfPresent(String.self, forKey: .format)
|
|
95
|
+
look = try c.decodeIfPresent(String.self, forKey: .look)
|
|
96
|
+
color = try c.decodeIfPresent(String.self, forKey: .color)
|
|
97
|
+
label = try c.decodeIfPresent(String.self, forKey: .label)
|
|
98
|
+
onEnd = try c.decodeIfPresent(String.self, forKey: .onEnd)
|
|
99
|
+
expiredText = try c.decodeIfPresent(String.self, forKey: .expiredText)
|
|
100
|
+
steps = try c.decodeIfPresent([String].self, forKey: .steps)
|
|
101
|
+
current = (try? c.decodeIfPresent(Int.self, forKey: .current)) ?? (try? c.decodeIfPresent(Double.self, forKey: .current)).flatMap { $0.map(Int.init) }
|
|
102
|
+
// A counter's value is a number; a date's is text. Both are kept as text to show.
|
|
103
|
+
if let n = try? c.decodeIfPresent(Double.self, forKey: .value) {
|
|
104
|
+
value = n.rounded() == n ? String(Int(n)) : String(n)
|
|
105
|
+
} else {
|
|
106
|
+
value = try c.decodeIfPresent(String.self, forKey: .value)
|
|
107
|
+
}
|
|
108
|
+
style = try c.decodeIfPresent(String.self, forKey: .style)
|
|
109
|
+
text = try c.decodeIfPresent(String.self, forKey: .text)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
public static func parse(_ json: String?) -> [ZapiackLiveItem] {
|
|
113
|
+
guard let json, let data = json.data(using: .utf8) else { return [] }
|
|
114
|
+
return (try? JSONDecoder().decode([ZapiackLiveItem].self, from: data)) ?? []
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// When a timer ends: its date for a fixed timer, its minutes after `received` for the others.
|
|
118
|
+
public func timerEnd(received: Date) -> Date? {
|
|
119
|
+
guard type == "timer", let source else { return nil }
|
|
120
|
+
if mode == "fixed" {
|
|
121
|
+
let f = ISO8601DateFormatter()
|
|
122
|
+
f.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
|
|
123
|
+
return f.date(from: source) ?? ISO8601DateFormatter().date(from: source)
|
|
124
|
+
}
|
|
125
|
+
guard let minutes = Double(source), minutes > 0 else { return nil }
|
|
126
|
+
return received.addingTimeInterval(minutes * 60)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
#if canImport(SwiftUI) && canImport(UIKit)
|
|
2
|
+
import SwiftUI
|
|
3
|
+
import UIKit
|
|
4
|
+
|
|
5
|
+
/// Everything a layout's references point at.
|
|
6
|
+
public struct ZapiackLayoutContent {
|
|
7
|
+
public var title: String
|
|
8
|
+
public var body: String?
|
|
9
|
+
public var live: [ZapiackLiveItem]
|
|
10
|
+
public var actions: [String]
|
|
11
|
+
public var background: String?
|
|
12
|
+
public var thumbnail: UIImage?
|
|
13
|
+
public var picture: UIImage?
|
|
14
|
+
/// When the push arrived: per-user timers count from here.
|
|
15
|
+
public var received: Date
|
|
16
|
+
/// Set by a Live Activity, which knows the timer's end exactly.
|
|
17
|
+
public var timerEnd: Date?
|
|
18
|
+
/// Leaves buttons out, where the system draws them itself (the notification content extension).
|
|
19
|
+
public var hideActions: Bool
|
|
20
|
+
|
|
21
|
+
public init(title: String, body: String? = nil, live: [ZapiackLiveItem] = [], actions: [String] = [], background: String? = nil, thumbnail: UIImage? = nil, picture: UIImage? = nil, received: Date = Date(), timerEnd: Date? = nil, hideActions: Bool = false) {
|
|
22
|
+
self.title = title
|
|
23
|
+
self.body = body
|
|
24
|
+
self.live = live
|
|
25
|
+
self.actions = actions
|
|
26
|
+
self.background = background
|
|
27
|
+
self.thumbnail = thumbnail
|
|
28
|
+
self.picture = picture
|
|
29
|
+
self.received = received
|
|
30
|
+
self.timerEnd = timerEnd
|
|
31
|
+
self.hideActions = hideActions
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
var dark: Bool {
|
|
35
|
+
guard let bg = background.flatMap(Color.zapiackRGB) else { return true }
|
|
36
|
+
return (bg.r * 299 + bg.g * 587 + bg.b * 114) / 1000 < 140
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// Draws a Zapiack grid layout: rows share their width by weight, stacks take each element's height in grid rows. It is the same
|
|
41
|
+
/// tree Android and the dashboard draw, so a design looks the same everywhere.
|
|
42
|
+
@available(iOS 16.0, *)
|
|
43
|
+
public struct ZapiackLayoutView: View {
|
|
44
|
+
let node: ZapiackLayoutNode
|
|
45
|
+
let content: ZapiackLayoutContent
|
|
46
|
+
|
|
47
|
+
public init(node: ZapiackLayoutNode, content: ZapiackLayoutContent) {
|
|
48
|
+
self.node = node
|
|
49
|
+
self.content = content
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public var body: some View {
|
|
53
|
+
GeometryReader { g in
|
|
54
|
+
NodeView(node: node, width: g.size.width, content: content)
|
|
55
|
+
}
|
|
56
|
+
.frame(minHeight: CGFloat(node.rows) * ZapiackLayoutNode.rowPoints, alignment: .topLeading)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@available(iOS 16.0, *)
|
|
61
|
+
struct NodeView: View {
|
|
62
|
+
let node: ZapiackLayoutNode
|
|
63
|
+
let width: CGFloat
|
|
64
|
+
let content: ZapiackLayoutContent
|
|
65
|
+
|
|
66
|
+
var body: some View {
|
|
67
|
+
let total = max(1, node.children.map(\.weight).reduce(0, +))
|
|
68
|
+
if node.row {
|
|
69
|
+
HStack(alignment: .top, spacing: 0) {
|
|
70
|
+
ForEach(Array(node.children.enumerated()), id: \.offset) { _, child in
|
|
71
|
+
cell(child, width: width * CGFloat(child.weight) / CGFloat(total))
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
VStack(alignment: .leading, spacing: 0) {
|
|
76
|
+
ForEach(Array(node.children.enumerated()), id: \.offset) { _, child in
|
|
77
|
+
cell(child, width: width)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
@ViewBuilder
|
|
84
|
+
func cell(_ child: ZapiackLayoutNode.Child, width: CGFloat) -> some View {
|
|
85
|
+
switch child {
|
|
86
|
+
case let .sub(_, n):
|
|
87
|
+
NodeView(node: n, width: width, content: content).frame(width: width, alignment: .topLeading)
|
|
88
|
+
case let .space(w):
|
|
89
|
+
Color.clear.frame(width: width, height: node.row ? 1 : CGFloat(w) * ZapiackLayoutNode.rowPoints)
|
|
90
|
+
case let .element(_, ref, rows):
|
|
91
|
+
ElementView(ref: ref, content: content)
|
|
92
|
+
.padding(2)
|
|
93
|
+
.frame(width: width, alignment: .topLeading)
|
|
94
|
+
.frame(minHeight: CGFloat(rows) * ZapiackLayoutNode.rowPoints, alignment: .topLeading)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@available(iOS 16.0, *)
|
|
100
|
+
struct ElementView: View {
|
|
101
|
+
let ref: String
|
|
102
|
+
let content: ZapiackLayoutContent
|
|
103
|
+
|
|
104
|
+
private var ink: Color { content.dark ? .white : Color(red: 0.07, green: 0.07, blue: 0.07) }
|
|
105
|
+
private var muted: Color { ink.opacity(0.7) }
|
|
106
|
+
|
|
107
|
+
var body: some View {
|
|
108
|
+
switch ref.first {
|
|
109
|
+
case "t":
|
|
110
|
+
Text(content.title).font(.headline).foregroundStyle(ink).lineLimit(2)
|
|
111
|
+
case "b":
|
|
112
|
+
Text(content.body ?? "").font(.subheadline).foregroundStyle(muted).lineLimit(4)
|
|
113
|
+
case "i", "p":
|
|
114
|
+
if let image = ref == "i" ? content.thumbnail : content.picture {
|
|
115
|
+
Image(uiImage: image).resizable().scaledToFill().frame(maxWidth: .infinity, maxHeight: .infinity).clipped().clipShape(RoundedRectangle(cornerRadius: 10))
|
|
116
|
+
}
|
|
117
|
+
case "a":
|
|
118
|
+
if !content.hideActions, let i = Int(ref.dropFirst()), i < content.actions.count {
|
|
119
|
+
Text(content.actions[i])
|
|
120
|
+
.font(.footnote.weight(.semibold))
|
|
121
|
+
.lineLimit(1)
|
|
122
|
+
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
123
|
+
.padding(.vertical, 8)
|
|
124
|
+
.foregroundStyle(i == 0 ? (content.dark ? Color.black : Color.white) : ink)
|
|
125
|
+
.background(RoundedRectangle(cornerRadius: 10).fill(i == 0 ? ink : ink.opacity(0.14)))
|
|
126
|
+
}
|
|
127
|
+
case "l":
|
|
128
|
+
if let i = Int(ref.dropFirst()), i < content.live.count {
|
|
129
|
+
LiveItemView(item: content.live[i], content: content, ink: ink, muted: muted)
|
|
130
|
+
}
|
|
131
|
+
default:
|
|
132
|
+
EmptyView()
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
@available(iOS 16.0, *)
|
|
138
|
+
struct LiveItemView: View {
|
|
139
|
+
let item: ZapiackLiveItem
|
|
140
|
+
let content: ZapiackLayoutContent
|
|
141
|
+
let ink: Color
|
|
142
|
+
let muted: Color
|
|
143
|
+
|
|
144
|
+
var body: some View {
|
|
145
|
+
let color = item.color.flatMap(Color.init(zapiackHex:)) ?? ink
|
|
146
|
+
switch item.type {
|
|
147
|
+
case "timer":
|
|
148
|
+
if let end = content.timerEnd ?? item.timerEnd(received: content.received) {
|
|
149
|
+
VStack(alignment: .leading, spacing: 2) {
|
|
150
|
+
if end <= Date() {
|
|
151
|
+
switch item.onEnd {
|
|
152
|
+
case "hide": EmptyView()
|
|
153
|
+
case "expired": Text(item.expiredText ?? "").font(.subheadline.weight(.semibold)).foregroundStyle(color)
|
|
154
|
+
default: Text(item.format == "mmss" ? "00:00" : "0:00:00").font(.title.bold()).monospacedDigit().foregroundStyle(color)
|
|
155
|
+
}
|
|
156
|
+
} else if item.format == "dhm" {
|
|
157
|
+
Text(end, style: .relative).font(.title3.bold()).foregroundStyle(color)
|
|
158
|
+
} else {
|
|
159
|
+
Text(timerInterval: Date()...end, countsDown: true).font(.title.bold()).monospacedDigit().foregroundStyle(color)
|
|
160
|
+
}
|
|
161
|
+
if let label = item.label, !label.isEmpty { Text(label).font(.caption.weight(.medium)).foregroundStyle(item.look == "pills" ? muted : color).lineLimit(2) }
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
case "progress":
|
|
165
|
+
if let steps = item.steps, steps.count >= 2 {
|
|
166
|
+
let done = min(max(item.current ?? 0, 0), steps.count)
|
|
167
|
+
VStack(alignment: .leading, spacing: 4) {
|
|
168
|
+
HStack(spacing: 4) {
|
|
169
|
+
ForEach(0..<steps.count, id: \.self) { i in Capsule().fill(i < done ? color : ink.opacity(0.18)).frame(height: 4) }
|
|
170
|
+
}
|
|
171
|
+
HStack(spacing: 4) {
|
|
172
|
+
ForEach(0..<steps.count, id: \.self) { i in
|
|
173
|
+
Text(steps[i]).font(.caption2).foregroundStyle(muted).lineLimit(1).frame(maxWidth: .infinity, alignment: i == 0 ? .leading : i == steps.count - 1 ? .trailing : .center)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
case "counter":
|
|
179
|
+
HStack(alignment: .firstTextBaseline, spacing: 4) {
|
|
180
|
+
Text(item.value ?? "").font(.title3.bold()).foregroundStyle(color)
|
|
181
|
+
Text(item.label ?? "").font(.footnote).foregroundStyle(muted)
|
|
182
|
+
}
|
|
183
|
+
case "badge":
|
|
184
|
+
Text(item.text ?? "").font(.caption2.weight(.semibold)).padding(.horizontal, 8).padding(.vertical, 2).background(Capsule().fill(color)).foregroundStyle(.white)
|
|
185
|
+
case "date":
|
|
186
|
+
Text(item.value ?? "").font(.footnote).foregroundStyle(muted)
|
|
187
|
+
default:
|
|
188
|
+
EmptyView()
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
extension Color {
|
|
194
|
+
/// A #RRGGBB colour, or nil.
|
|
195
|
+
public init?(zapiackHex hex: String) {
|
|
196
|
+
guard let rgb = Color.zapiackRGB(hex) else { return nil }
|
|
197
|
+
self.init(red: rgb.r / 255, green: rgb.g / 255, blue: rgb.b / 255)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
static func zapiackRGB(_ hex: String) -> (r: Double, g: Double, b: Double)? {
|
|
201
|
+
let s = hex.trimmingCharacters(in: .whitespaces)
|
|
202
|
+
guard s.count == 7, s.hasPrefix("#"), let n = UInt32(s.dropFirst(), radix: 16) else { return nil }
|
|
203
|
+
return (Double((n >> 16) & 0xFF), Double((n >> 8) & 0xFF), Double(n & 0xFF))
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
#endif
|
package/ios/Zapiack/Models.swift
CHANGED
|
@@ -44,7 +44,7 @@ public struct PushBlock: Codable, Equatable {
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
/// A value of the app's own, sent along with an event.
|
|
47
|
-
public enum JSONValue: Codable, Equatable {
|
|
47
|
+
public enum JSONValue: Codable, Equatable, Sendable {
|
|
48
48
|
case string(String)
|
|
49
49
|
case number(Double)
|
|
50
50
|
case bool(Bool)
|
|
@@ -22,6 +22,13 @@ public final class PushClient {
|
|
|
22
22
|
|
|
23
23
|
public var deviceToken: String? { storage.string(forKey: "zapiack.token") }
|
|
24
24
|
|
|
25
|
+
/// The push-to-start token for Live Activities, when the app turned them on (iOS 17.2+).
|
|
26
|
+
public var liveActivityToken: String? { storage.string(forKey: "zapiack.live-token") }
|
|
27
|
+
|
|
28
|
+
public func setLiveActivityToken(_ data: Data) {
|
|
29
|
+
storage.set(data.map { String(format: "%02x", $0) }.joined(), forKey: "zapiack.live-token")
|
|
30
|
+
}
|
|
31
|
+
|
|
25
32
|
public func setDeviceToken(_ data: Data) {
|
|
26
33
|
storage.set(data.map { String(format: "%02x", $0) }.joined(), forKey: "zapiack.token")
|
|
27
34
|
}
|
|
@@ -74,7 +81,8 @@ public final class PushClient {
|
|
|
74
81
|
tags: tags.isEmpty ? nil : tags,
|
|
75
82
|
permission: permission,
|
|
76
83
|
environment: environment,
|
|
77
|
-
appVersion: appVersion
|
|
84
|
+
appVersion: appVersion,
|
|
85
|
+
liveActivityToken: liveActivityToken
|
|
78
86
|
)
|
|
79
87
|
try await api.register(registration)
|
|
80
88
|
// Only forget the change if it is still the one that was sent.
|