@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.
@@ -0,0 +1,170 @@
1
+ #if canImport(ActivityKit) && canImport(WidgetKit) && os(iOS)
2
+ import ActivityKit
3
+ import SwiftUI
4
+ import WidgetKit
5
+
6
+ /// Draws Zapiack Live Activities. Add it to your widget extension's bundle:
7
+ ///
8
+ /// ```swift
9
+ /// @main struct Widgets: WidgetBundle {
10
+ /// var body: some Widget { ZapiackLiveActivityWidget() }
11
+ /// }
12
+ /// ```
13
+ ///
14
+ /// The countdown is the system's own timer text, so it ticks every second on the lock screen with no work from the app and no
15
+ /// further push. When the timer ends the activity goes stale, and it shows what the template asked for at the end.
16
+ @available(iOS 16.2, *)
17
+ public struct ZapiackLiveActivityWidget: Widget {
18
+ public init() {}
19
+
20
+ public var body: some WidgetConfiguration {
21
+ ActivityConfiguration(for: ZapiackLiveAttributes.self) { context in
22
+ ActivityBody(state: context.state, stale: context.isStale)
23
+ .activityBackgroundTint(context.state.background.flatMap(Color.init(hex:)))
24
+ .widgetURL(context.attributes.link.flatMap(URL.init(string:)))
25
+ } dynamicIsland: { context in
26
+ let state = context.state
27
+ let stale = context.isStale
28
+ return DynamicIsland {
29
+ DynamicIslandExpandedRegion(.leading) {
30
+ Text(state.title).font(.headline).lineLimit(1)
31
+ }
32
+ DynamicIslandExpandedRegion(.trailing) {
33
+ TimerText(state: state, stale: stale, size: 22)
34
+ }
35
+ DynamicIslandExpandedRegion(.bottom) {
36
+ VStack(alignment: .leading, spacing: 6) {
37
+ if let label = state.timerLabel, !label.isEmpty { Text(label).font(.caption).foregroundStyle(.secondary) }
38
+ ProgressSteps(state: state)
39
+ }
40
+ }
41
+ } compactLeading: {
42
+ Image(systemName: state.timerEnd != nil ? "timer" : "shippingbox")
43
+ .foregroundStyle(Color(hex: state.timerColor ?? state.progressColor ?? "") ?? .accentColor)
44
+ } compactTrailing: {
45
+ TimerText(state: state, stale: stale, size: 14)
46
+ .frame(maxWidth: 64)
47
+ } minimal: {
48
+ Image(systemName: "timer")
49
+ }
50
+ .widgetURL(context.attributes.link.flatMap(URL.init(string:)))
51
+ }
52
+ }
53
+ }
54
+
55
+ /** A grid design draws its own layout; anything else, the standard Zapiack card. */
56
+ @available(iOS 16.2, *)
57
+ struct ActivityBody: View {
58
+ let state: ZapiackLiveAttributes.ContentState
59
+ let stale: Bool
60
+
61
+ var body: some View {
62
+ if let tree = ZapiackLayoutNode.parse(state.layout) {
63
+ ZapiackLayoutView(
64
+ node: tree,
65
+ content: ZapiackLayoutContent(title: state.title, body: state.body, live: ZapiackLiveItem.parse(state.live), actions: state.actions ?? [], background: state.background, timerEnd: state.timerEnd)
66
+ )
67
+ .padding(14)
68
+ } else {
69
+ LockScreenView(state: state, stale: stale)
70
+ }
71
+ }
72
+ }
73
+
74
+ @available(iOS 16.2, *)
75
+ struct LockScreenView: View {
76
+ let state: ZapiackLiveAttributes.ContentState
77
+ let stale: Bool
78
+
79
+ var body: some View {
80
+ HStack(alignment: .center, spacing: 12) {
81
+ VStack(alignment: .leading, spacing: 6) {
82
+ if let badge = state.badgeText, !badge.isEmpty {
83
+ Text(badge)
84
+ .font(.caption2.weight(.semibold))
85
+ .padding(.horizontal, 8).padding(.vertical, 2)
86
+ .background(Capsule().fill(Color(hex: state.badgeColor ?? "") ?? .green))
87
+ .foregroundStyle(.white)
88
+ }
89
+ Text(state.title).font(.headline).lineLimit(1)
90
+ TimerText(state: state, stale: stale, size: 34)
91
+ if let label = state.timerLabel, !label.isEmpty, !(stale && state.timerOnEnd == "expired") {
92
+ Text(label).font(.footnote.weight(.medium)).foregroundStyle(Color(hex: state.timerColor ?? "") ?? .secondary).lineLimit(2)
93
+ } else if state.timerEnd == nil, let body = state.body, !body.isEmpty {
94
+ Text(body).font(.subheadline).foregroundStyle(.secondary).lineLimit(2)
95
+ }
96
+ ProgressSteps(state: state)
97
+ if let value = state.counterValue {
98
+ Text("\(value.formatted()) \(state.counterLabel ?? "")").font(.subheadline.weight(.semibold))
99
+ }
100
+ }
101
+ Spacer(minLength: 0)
102
+ }
103
+ .padding(16)
104
+ }
105
+ }
106
+
107
+ /// The countdown. Before the end it is the system timer; after, it is the template's ended state.
108
+ @available(iOS 16.2, *)
109
+ struct TimerText: View {
110
+ let state: ZapiackLiveAttributes.ContentState
111
+ let stale: Bool
112
+ let size: CGFloat
113
+
114
+ var body: some View {
115
+ if let end = state.timerEnd {
116
+ let color = Color(hex: state.timerColor ?? "") ?? .primary
117
+ let over = stale || end <= Date()
118
+ if over {
119
+ switch state.timerOnEnd {
120
+ case "hide": EmptyView()
121
+ case "expired": Text(state.expiredText ?? "").font(.system(size: size * 0.5, weight: .semibold)).foregroundStyle(color)
122
+ default: Text(state.timerFormat == "mmss" ? "00:00" : "0:00:00").font(.system(size: size, weight: .bold)).monospacedDigit().foregroundStyle(color)
123
+ }
124
+ } else if state.timerFormat == "dhm" {
125
+ // Days are too long to tick by the second: the system writes how long is left and keeps it current.
126
+ Text(end, style: .relative).font(.system(size: size * 0.7, weight: .bold)).foregroundStyle(color)
127
+ } else {
128
+ Text(timerInterval: Date()...end, countsDown: true)
129
+ .font(.system(size: size, weight: .bold))
130
+ .monospacedDigit()
131
+ .foregroundStyle(color)
132
+ .multilineTextAlignment(.leading)
133
+ }
134
+ }
135
+ }
136
+ }
137
+
138
+ @available(iOS 16.2, *)
139
+ struct ProgressSteps: View {
140
+ let state: ZapiackLiveAttributes.ContentState
141
+
142
+ var body: some View {
143
+ if let steps = state.progressSteps, steps.count >= 2 {
144
+ let done = min(max(state.progressCurrent ?? 0, 0), steps.count)
145
+ let tint = Color(hex: state.progressColor ?? "") ?? .green
146
+ VStack(alignment: .leading, spacing: 4) {
147
+ HStack(spacing: 4) {
148
+ ForEach(0..<steps.count, id: \.self) { i in
149
+ Capsule().fill(i < done ? tint : Color.secondary.opacity(0.25)).frame(height: 4)
150
+ }
151
+ }
152
+ HStack(spacing: 4) {
153
+ ForEach(0..<steps.count, id: \.self) { i in
154
+ Text(steps[i]).font(.caption2).foregroundStyle(.secondary).lineLimit(1).frame(maxWidth: .infinity, alignment: i == 0 ? .leading : i == steps.count - 1 ? .trailing : .center)
155
+ }
156
+ }
157
+ }
158
+ }
159
+ }
160
+ }
161
+
162
+ extension Color {
163
+ /// A #RRGGBB colour, or nil for anything else.
164
+ init?(hex: String) {
165
+ let s = hex.trimmingCharacters(in: .whitespaces)
166
+ guard s.count == 7, s.hasPrefix("#"), let n = UInt32(s.dropFirst(), radix: 16) else { return nil }
167
+ self.init(red: Double((n >> 16) & 0xFF) / 255, green: Double((n >> 8) & 0xFF) / 255, blue: Double(n & 0xFF) / 255)
168
+ }
169
+ }
170
+ #endif
@@ -0,0 +1,78 @@
1
+ #if canImport(ActivityKit) && os(iOS)
2
+ import ActivityKit
3
+ import Foundation
4
+
5
+ /// The Live Activity a Zapiack template push starts: a countdown, progress or counter that stays on the lock screen and in the
6
+ /// Dynamic Island and updates on its own. Zapiack fills it in from the template when it sends; the app only has to show it, by adding
7
+ /// `ZapiackLiveActivityWidget` to its widget extension.
8
+ ///
9
+ /// The field names match what the Zapiack service sends (`liveContentState` on the server). Dates are plain numbers, seconds since
10
+ /// 1970, so they decode the same way whatever the decoder's date setting is.
11
+ @available(iOS 16.1, *)
12
+ public struct ZapiackLiveAttributes: ActivityAttributes {
13
+ public struct ContentState: Codable, Hashable {
14
+ public var title: String
15
+ public var body: String?
16
+ /// The template's background, as #RRGGBB. Absent means the system's own lock screen look.
17
+ public var background: String?
18
+
19
+ public var timerEndsAt: Double?
20
+ /// hmmss, mmss or dhm.
21
+ public var timerFormat: String?
22
+ public var timerColor: String?
23
+ public var timerLabel: String?
24
+ /// What happens when the timer ends: expired (show `expiredText`), hide (no timer) or keep (zeroes).
25
+ public var timerOnEnd: String?
26
+ public var expiredText: String?
27
+
28
+ public var progressSteps: [String]?
29
+ public var progressCurrent: Int?
30
+ public var progressColor: String?
31
+
32
+ public var counterValue: Double?
33
+ public var counterLabel: String?
34
+
35
+ public var badgeText: String?
36
+ public var badgeColor: String?
37
+
38
+ /// A grid design: the layout tree, its live elements and its button labels, so the activity draws the same layout.
39
+ public var layout: String?
40
+ public var live: String?
41
+ public var actions: [String]?
42
+
43
+ public init(title: String, body: String? = nil, background: String? = nil, timerEndsAt: Double? = nil, timerFormat: String? = nil, timerColor: String? = nil, timerLabel: String? = nil, timerOnEnd: String? = nil, expiredText: String? = nil, progressSteps: [String]? = nil, progressCurrent: Int? = nil, progressColor: String? = nil, counterValue: Double? = nil, counterLabel: String? = nil, badgeText: String? = nil, badgeColor: String? = nil) {
44
+ self.title = title
45
+ self.body = body
46
+ self.background = background
47
+ self.timerEndsAt = timerEndsAt
48
+ self.timerFormat = timerFormat
49
+ self.timerColor = timerColor
50
+ self.timerLabel = timerLabel
51
+ self.timerOnEnd = timerOnEnd
52
+ self.expiredText = expiredText
53
+ self.progressSteps = progressSteps
54
+ self.progressCurrent = progressCurrent
55
+ self.progressColor = progressColor
56
+ self.counterValue = counterValue
57
+ self.counterLabel = counterLabel
58
+ self.badgeText = badgeText
59
+ self.badgeColor = badgeColor
60
+ }
61
+
62
+ /// When the timer ends, if there is one.
63
+ public var timerEnd: Date? { timerEndsAt.map { Date(timeIntervalSince1970: $0) } }
64
+ }
65
+
66
+ /// The Zapiack message and delivery this activity came from, so a tap can be reported.
67
+ public var messageId: String
68
+ public var deliveryId: String
69
+ /// Where a tap takes the person: the template's link, already chosen for iOS.
70
+ public var link: String?
71
+
72
+ public init(messageId: String, deliveryId: String, link: String? = nil) {
73
+ self.messageId = messageId
74
+ self.deliveryId = deliveryId
75
+ self.link = link
76
+ }
77
+ }
78
+ #endif
@@ -1,5 +1,8 @@
1
1
  import Foundation
2
2
  import UserNotifications
3
+ #if canImport(ActivityKit) && os(iOS)
4
+ import ActivityKit
5
+ #endif
3
6
  #if canImport(UIKit)
4
7
  import UIKit
5
8
  #endif
@@ -89,6 +92,30 @@ public final class ZapiackPush {
89
92
  return credited
90
93
  }
91
94
 
95
+ /// Lets Zapiack start Live Activities on this phone: a template push with a timer or progress then appears on the lock screen
96
+ /// and in the Dynamic Island and counts down by itself. Needs iOS 17.2 or later, `NSSupportsLiveActivities` set to YES in the
97
+ /// app's Info.plist, and `ZapiackLiveActivityWidget` in a widget extension. On older iOS it does nothing, and those pushes
98
+ /// arrive as normal notifications.
99
+ public func enableLiveActivities() {
100
+ #if canImport(ActivityKit) && os(iOS)
101
+ if #available(iOS 17.2, *) {
102
+ guard ActivityAuthorizationInfo().areActivitiesEnabled else { return }
103
+ liveTokenTask?.cancel()
104
+ liveTokenTask = Task { [weak self] in
105
+ for await token in Activity<ZapiackLiveAttributes>.pushToStartTokenUpdates {
106
+ guard let self, let client = self.client else { continue }
107
+ if client.liveActivityToken != token.map({ String(format: "%02x", $0) }).joined() {
108
+ client.setLiveActivityToken(token)
109
+ await self.syncRegistration()
110
+ }
111
+ }
112
+ }
113
+ }
114
+ #endif
115
+ }
116
+
117
+ private var liveTokenTask: Task<Void, Never>?
118
+
92
119
  /// Sends anything waiting. Call when the app comes to the foreground.
93
120
  public func flush() async { await client?.flush() }
94
121
 
@@ -13,6 +13,7 @@ RCT_EXTERN_METHOD(removeTags : (NSArray *)keys resolver : (RCTPromiseResolveBloc
13
13
  RCT_EXTERN_METHOD(screen : (NSString *)name resolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
14
14
  RCT_EXTERN_METHOD(track : (NSString *)name properties : (NSDictionary *)properties resolver : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
15
15
  RCT_EXTERN_METHOD(flush : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
16
+ RCT_EXTERN_METHOD(enableLiveActivities : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
16
17
  RCT_EXTERN_METHOD(initialTap : (RCTPromiseResolveBlock)resolve rejecter : (RCTPromiseRejectBlock)reject)
17
18
 
18
19
  @end
@@ -72,6 +72,11 @@ final class ZapiackPushModule: RCTEventEmitter, UNUserNotificationCenterDelegate
72
72
  resolve(ZapiackPush.shared.track(name, properties: properties.map(Self.values)))
73
73
  }
74
74
 
75
+ @objc func enableLiveActivities(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
76
+ ZapiackPush.shared.enableLiveActivities()
77
+ resolve(nil)
78
+ }
79
+
75
80
  @objc func flush(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
76
81
  Task { await ZapiackPush.shared.flush(); resolve(nil) }
77
82
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zapiack/react-native",
3
- "version": "0.1.0",
4
- "description": "Push notifications for React Native with Zapiack: register devices, follow taps and see what people do next.",
3
+ "version": "0.2.0",
4
+ "description": "Push notifications, live messaging and voice & video calls for React Native with Zapiack.",
5
5
  "license": "MIT",
6
6
  "author": "Zapiack <support@zapiack.com>",
7
7
  "homepage": "https://docs.zapiack.com/push/client-sdks",
@@ -18,7 +18,12 @@
18
18
  "push",
19
19
  "notifications",
20
20
  "fcm",
21
- "apns"
21
+ "apns",
22
+ "realtime",
23
+ "calls",
24
+ "webrtc",
25
+ "voice",
26
+ "video"
22
27
  ],
23
28
  "main": "./dist/index.js",
24
29
  "module": "./dist/index.mjs",
@@ -34,7 +39,17 @@
34
39
  "default": "./dist/index.js"
35
40
  }
36
41
  },
37
- "./package.json": "./package.json"
42
+ "./package.json": "./package.json",
43
+ "./calls": {
44
+ "import": {
45
+ "types": "./dist/calls.d.mts",
46
+ "default": "./dist/calls.mjs"
47
+ },
48
+ "require": {
49
+ "types": "./dist/calls.d.ts",
50
+ "default": "./dist/calls.js"
51
+ }
52
+ }
38
53
  },
39
54
  "files": [
40
55
  "dist",
@@ -56,17 +71,29 @@
56
71
  "prepublishOnly": "npm run sync-ios && npm run typecheck && npm run build && npm test"
57
72
  },
58
73
  "peerDependencies": {
59
- "react-native": ">=0.71.0"
74
+ "react-native": ">=0.71.0",
75
+ "react-native-webrtc": ">=124.0.0",
76
+ "mediasoup-client": "^3.18.0"
60
77
  },
61
78
  "devDependencies": {
62
79
  "@types/react": "^19.0.0",
80
+ "mediasoup-client": "^3.26.0",
63
81
  "react": "^19.0.0",
64
82
  "react-native": "^0.79.0",
83
+ "react-native-webrtc": "^124.0.8",
65
84
  "tsup": "^9.0.0",
66
85
  "typescript": "~5.9.0",
67
86
  "vitest": "^5.0.5"
68
87
  },
69
88
  "publishConfig": {
70
89
  "access": "public"
90
+ },
91
+ "peerDependenciesMeta": {
92
+ "react-native-webrtc": {
93
+ "optional": true
94
+ },
95
+ "mediasoup-client": {
96
+ "optional": true
97
+ }
71
98
  }
72
99
  }