also looking at this
fix(ios): hand the page the answers native was already sending
#222
18 files
+597
-164
| @@ -667,16 +667,23 @@ struct CraftWebView: UIViewRepresentable { | ||
| 667 | 667 | switch action { |
| 668 | 668 | case "startListening": |
| 669 | 669 | 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. | |
| 670 | 674 | startSpeechRecognition() |
| 675 | resolveCallback(callbackId, result: true) | |
| 671 | 676 | } else { |
| 672 | 677 | rejectCallback(callbackId, error: "Speech recognition is disabled", code: "CAPABILITY_DISABLED") |
| 673 | 678 | } |
| 674 | 679 | case "stopListening": |
| 675 | 680 | stopSpeechRecognition() |
| 681 | resolveCallback(callbackId, result: true) | |
| 676 | 682 | case "haptic": |
| 677 | 683 | if config.enableHaptics { |
| 678 | 684 | let style = body["style"] as? String ?? "medium" |
| 679 | 685 | triggerHaptic(style: style) |
| 686 | resolveCallback(callbackId, result: true) | |
| 680 | 687 | } else { |
| 681 | 688 | rejectCallback(callbackId, error: "Haptics is disabled", code: "CAPABILITY_DISABLED") |
| 682 | 689 | } |
| @@ -2013,16 +2020,37 @@ struct CraftWebView: UIViewRepresentable { | ||
| 2013 | 2020 | return Promise.reject(new Error('Android fitness APIs are unavailable on iOS')); |
| 2014 | 2021 | }, |
| 2015 | 2022 | |
| 2023 | // Resolves true once UIKit has taken the haptic, and rejects | |
| 2024 | // CAPABILITY_DISABLED when enableHaptics is off. | |
| 2016 | 2025 | 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 | }); | |
| 2018 | 2032 | }, |
| 2019 | 2033 | |
| 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. | |
| 2020 | 2038 | 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 | }); | |
| 2022 | 2045 | }, |
| 2023 | 2046 | |
| 2024 | 2047 | 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 | }); | |
| 2026 | 2054 | }, |
| 2027 | 2055 | |
| 2028 | 2056 | share: function(text) { |
| @@ -2169,12 +2197,25 @@ struct CraftWebView: UIViewRepresentable { | ||
| 2169 | 2197 | } |
| 2170 | 2198 | }); |
| 2171 | 2199 | }, |
| 2200 | // Resolves true once updates have started, before any | |
| 2201 | // authorization answer. A later refusal arrives as a | |
| 2202 | // craftLocationError event. | |
| 2172 | 2203 | watchPosition: function(callback) { |
| 2204 | var self = window.craft; | |
| 2205 | var id = 'cb_' + (++self._callbackId); | |
| 2173 | 2206 | 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 | }); | |
| 2175 | 2211 | }, |
| 2176 | 2212 | 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 | }); | |
| 2178 | 2219 | } |
| 2179 | 2220 | }, |
| 2180 | 2221 | |
| @@ -2274,7 +2315,12 @@ struct CraftWebView: UIViewRepresentable { | ||
| 2274 | 2315 | |
| 2275 | 2316 | // Vibrate |
| 2276 | 2317 | 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 | }); | |
| 2278 | 2324 | }, |
| 2279 | 2325 | |
| 2280 | 2326 | // Open URL |
| @@ -2924,14 +2970,25 @@ struct CraftWebView: UIViewRepresentable { | ||
| 2924 | 2970 | getInfo: function() { return craft.getDeviceInfo(); }, |
| 2925 | 2971 | getCapabilities: function() { return Promise.resolve(Object.assign({}, craft.capabilities)); } |
| 2926 | 2972 | }; |
| 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 | } | |
| 2927 | 2985 | craft.haptics = { |
| 2928 | impact: function(style) { craft.haptic(style || 'medium'); return Promise.resolve(); }, | |
| 2986 | impact: function(style) { return hapticFeedback(craft.haptic(style || 'medium')); }, | |
| 2929 | 2987 | 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')); | |
| 2932 | 2989 | }, |
| 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 || [])); } | |
| 2935 | 2992 | }; |
| 2936 | 2993 | craft.permissions = { |
| 2937 | 2994 | check: function(permission) { return craft._invoke('checkPermission', {permission: permission}); }, |
| @@ -2971,9 +3028,7 @@ struct CraftWebView: UIViewRepresentable { | ||
| 2971 | 3028 | }, |
| 2972 | 3029 | clearWatch: function(id) { |
| 2973 | 3030 | 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'); | |
| 2977 | 3032 | }, |
| 2978 | 3033 | startRecording: function() { return craft._invoke('startLocationRecording'); }, |
| 2979 | 3034 | pauseRecording: function() { return craft._invoke('pauseLocationRecording'); }, |