ReviewOS

also looking at this

craft-native/craft

fix(ios): hand the page the answers native was already sending

#222
Merged glennmichael123 wants to merge fix/ios-unanswered-posts into main
18 files +597 -164
packages/ios/templates/CraftApp.swiftmodified+69-14
Changes to packages/ios/templates/CraftApp.swift
@@ -667,16 +667,23 @@ struct CraftWebView: UIViewRepresentable {
667667 switch action {
668668 case "startListening":
669669 if config.enableSpeechRecognition {
670 // Answers that the request was taken. Authorization and the
671 // recognizer answer later, as craftSpeech* events; waiting
672 // for them here would leave the promise open on every path
673 // that ends without one.
670674 startSpeechRecognition()
675 resolveCallback(callbackId, result: true)
671676 } else {
672677 rejectCallback(callbackId, error: "Speech recognition is disabled", code: "CAPABILITY_DISABLED")
673678 }
674679 case "stopListening":
675680 stopSpeechRecognition()
681 resolveCallback(callbackId, result: true)
676682 case "haptic":
677683 if config.enableHaptics {
678684 let style = body["style"] as? String ?? "medium"
679685 triggerHaptic(style: style)
686 resolveCallback(callbackId, result: true)
680687 } else {
681688 rejectCallback(callbackId, error: "Haptics is disabled", code: "CAPABILITY_DISABLED")
682689 }
@@ -2013,16 +2020,37 @@ struct CraftWebView: UIViewRepresentable {
20132020 return Promise.reject(new Error('Android fitness APIs are unavailable on iOS'));
20142021 },
20152022
2023 // Resolves true once UIKit has taken the haptic, and rejects
2024 // CAPABILITY_DISABLED when enableHaptics is off.
20162025 haptic: function(style) {
2017 window.webkit.messageHandlers.craft.postMessage({action: 'haptic', style: style || 'medium'});
2026 var self = window.craft;
2027 var id = 'cb_' + (++self._callbackId);
2028 window.webkit.messageHandlers.craft.postMessage({action: 'haptic', style: style || 'medium', callbackId: id});
2029 return new Promise(function(resolve, reject) {
2030 self._callbacks[id] = {resolve: resolve, reject: reject};
2031 });
20182032 },
20192033
2034 // Resolves true once native has taken the request, not once
2035 // audio is flowing. What happens next (a prompt declined, no
2036 // recognizer, a transcript) still arrives as craftSpeech*
2037 // events, because none of it is known when this answers.
20202038 startListening: function() {
2021 window.webkit.messageHandlers.craft.postMessage({action: 'startListening'});
2039 var self = window.craft;
2040 var id = 'cb_' + (++self._callbackId);
2041 window.webkit.messageHandlers.craft.postMessage({action: 'startListening', callbackId: id});
2042 return new Promise(function(resolve, reject) {
2043 self._callbacks[id] = {resolve: resolve, reject: reject};
2044 });
20222045 },
20232046
20242047 stopListening: function() {
2025 window.webkit.messageHandlers.craft.postMessage({action: 'stopListening'});
2048 var self = window.craft;
2049 var id = 'cb_' + (++self._callbackId);
2050 window.webkit.messageHandlers.craft.postMessage({action: 'stopListening', callbackId: id});
2051 return new Promise(function(resolve, reject) {
2052 self._callbacks[id] = {resolve: resolve, reject: reject};
2053 });
20262054 },
20272055
20282056 share: function(text) {
@@ -2169,12 +2197,25 @@ struct CraftWebView: UIViewRepresentable {
21692197 }
21702198 });
21712199 },
2200 // Resolves true once updates have started, before any
2201 // authorization answer. A later refusal arrives as a
2202 // craftLocationError event.
21722203 watchPosition: function(callback) {
2204 var self = window.craft;
2205 var id = 'cb_' + (++self._callbackId);
21732206 window.addEventListener('craftLocationUpdate', function(e) { callback(e.detail); });
2174 window.webkit.messageHandlers.craft.postMessage({action: 'watchPosition'});
2207 window.webkit.messageHandlers.craft.postMessage({action: 'watchPosition', callbackId: id});
2208 return new Promise(function(resolve, reject) {
2209 self._callbacks[id] = {resolve: resolve, reject: reject};
2210 });
21752211 },
21762212 clearWatch: function() {
2177 window.webkit.messageHandlers.craft.postMessage({action: 'clearWatch'});
2213 var self = window.craft;
2214 var id = 'cb_' + (++self._callbackId);
2215 window.webkit.messageHandlers.craft.postMessage({action: 'clearWatch', callbackId: id});
2216 return new Promise(function(resolve, reject) {
2217 self._callbacks[id] = {resolve: resolve, reject: reject};
2218 });
21782219 }
21792220 },
21802221
@@ -2274,7 +2315,12 @@ struct CraftWebView: UIViewRepresentable {
22742315
22752316 // Vibrate
22762317 vibrate: function(pattern) {
2277 window.webkit.messageHandlers.craft.postMessage({action: 'vibrate', pattern: pattern});
2318 var self = window.craft;
2319 var id = 'cb_' + (++self._callbackId);
2320 window.webkit.messageHandlers.craft.postMessage({action: 'vibrate', pattern: pattern, callbackId: id});
2321 return new Promise(function(resolve, reject) {
2322 self._callbacks[id] = {resolve: resolve, reject: reject};
2323 });
22782324 },
22792325
22802326 // Open URL
@@ -2924,14 +2970,25 @@ struct CraftWebView: UIViewRepresentable {
29242970 getInfo: function() { return craft.getDeviceInfo(); },
29252971 getCapabilities: function() { return Promise.resolve(Object.assign({}, craft.capabilities)); }
29262972 };
2973 // Feedback, as on Android and the web, where there is no
2974 // motor to fire: an app that left enableHaptics off gets
2975 // nothing played and a settled promise, so `await
2976 // haptics.selection()` in the middle of a flow does not stop
2977 // the flow. A native failure still rejects, and
2978 // craft.haptic() itself reports the refusal.
2979 function hapticFeedback(answer) {
2980 return answer.then(function() {}, function(error) {
2981 if (error && error.code === 'CAPABILITY_DISABLED') return;
2982 throw error;
2983 });
2984 }
29272985 craft.haptics = {
2928 impact: function(style) { craft.haptic(style || 'medium'); return Promise.resolve(); },
2986 impact: function(style) { return hapticFeedback(craft.haptic(style || 'medium')); },
29292987 notification: function(type) {
2930 craft.haptic(type === 'error' ? 'heavy' : type === 'warning' ? 'medium' : 'light');
2931 return Promise.resolve();
2988 return hapticFeedback(craft.haptic(type === 'error' ? 'heavy' : type === 'warning' ? 'medium' : 'light'));
29322989 },
2933 selection: function() { craft.haptic('soft'); return Promise.resolve(); },
2934 vibrate: function(pattern) { craft.vibrate(pattern || []); return Promise.resolve(); }
2990 selection: function() { return hapticFeedback(craft.haptic('soft')); },
2991 vibrate: function(pattern) { return hapticFeedback(craft.vibrate(pattern || [])); }
29352992 };
29362993 craft.permissions = {
29372994 check: function(permission) { return craft._invoke('checkPermission', {permission: permission}); },
@@ -2971,9 +3028,7 @@ struct CraftWebView: UIViewRepresentable {
29713028 },
29723029 clearWatch: function(id) {
29733030 locationWatchCallbacks.delete(id);
2974 if (locationWatchCallbacks.size === 0) {
2975 window.webkit.messageHandlers.craft.postMessage({action: 'clearWatch'});
2976 }
3031 if (locationWatchCallbacks.size === 0) void craft._invoke('clearWatch');
29773032 },
29783033 startRecording: function() { return craft._invoke('startLocationRecording'); },
29793034 pauseRecording: function() { return craft._invoke('pauseLocationRecording'); },