From 15150357af21ae899e974d43186649e38fd8b840 Mon Sep 17 00:00:00 2001 From: llaske Date: Fri, 25 Oct 2024 23:09:59 +0200 Subject: [PATCH] Fix FractionBounce accelerometer on iOS --- .../FractionBounce.activity/js/activity.js | 3 +- .../js/components/SugarDevice.js | 34 ++++++++++++++----- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/activities/FractionBounce.activity/js/activity.js b/activities/FractionBounce.activity/js/activity.js index ee6ebb15a..a57b829a5 100644 --- a/activities/FractionBounce.activity/js/activity.js +++ b/activities/FractionBounce.activity/js/activity.js @@ -485,8 +485,7 @@ let app = new Vue({ this.acceleration = { x: acceleration.x, y: acceleration.y, - z: acceleration.z, - type: acceleration.type + z: acceleration.z }; this.accelerationUpdate(); }, diff --git a/activities/FractionBounce.activity/js/components/SugarDevice.js b/activities/FractionBounce.activity/js/components/SugarDevice.js index 3a3b39a19..082cdf972 100644 --- a/activities/FractionBounce.activity/js/components/SugarDevice.js +++ b/activities/FractionBounce.activity/js/components/SugarDevice.js @@ -3,14 +3,21 @@ Vue.component('sugar-device', { template: `
{{ watchId }}
`, data: function () { return { - watchId: null + watchId: null, + readyToWatch: false, + frequencyExpected: 0 } }, - created: function () { - var cordovaScript = document.createElement('script'); - cordovaScript.setAttribute('type', 'text/javascript'); - cordovaScript.setAttribute('src', '../../cordova.js'); - document.head.appendChild(cordovaScript); + mounted: function () { + var vm = this; + requirejs(['domReady!'], function () { + document.addEventListener('deviceready', function() { + vm.readyToWatch = true; + if (vm.frequencyExpected > 0) { + vm.watchAcceleration(vm.frequencyExpected); + } + }, false); + }); }, methods: { isMobile() { @@ -85,13 +92,22 @@ Vue.component('sugar-device', { accelerometer.start(); } } else if (navigator.accelerometer) { - watchId = navigator.accelerometer.watchAcceleration(function () { - vm.accelerationCallback(accelerometer); - }, null, { frequency: frequency }); + if (vm.readyToWatch) { + vm.watchId = navigator.accelerometer.watchAcceleration(function (accelerometer) { + vm.accelerationCallback(accelerometer); + }, null, { frequency: frequency }); + } else { + vm.frequencyExpected = frequency; + } + } else { + vm.frequencyExpected = frequency; } }, accelerationCallback: function (acceleration) { + if (this.readyToWatch) { + this.readyToWatch = false; + } this.$emit('acceleration-callback', acceleration); },