Skip to content

Commit 1515035

Browse files
author
llaske
committed
Fix FractionBounce accelerometer on iOS
1 parent 7deebd7 commit 1515035

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

activities/FractionBounce.activity/js/activity.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,7 @@ let app = new Vue({
485485
this.acceleration = {
486486
x: acceleration.x,
487487
y: acceleration.y,
488-
z: acceleration.z,
489-
type: acceleration.type
488+
z: acceleration.z
490489
};
491490
this.accelerationUpdate();
492491
},

activities/FractionBounce.activity/js/components/SugarDevice.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ Vue.component('sugar-device', {
33
template: `<div style="display: none">{{ watchId }}</div>`,
44
data: function () {
55
return {
6-
watchId: null
6+
watchId: null,
7+
readyToWatch: false,
8+
frequencyExpected: 0
79
}
810
},
9-
created: function () {
10-
var cordovaScript = document.createElement('script');
11-
cordovaScript.setAttribute('type', 'text/javascript');
12-
cordovaScript.setAttribute('src', '../../cordova.js');
13-
document.head.appendChild(cordovaScript);
11+
mounted: function () {
12+
var vm = this;
13+
requirejs(['domReady!'], function () {
14+
document.addEventListener('deviceready', function() {
15+
vm.readyToWatch = true;
16+
if (vm.frequencyExpected > 0) {
17+
vm.watchAcceleration(vm.frequencyExpected);
18+
}
19+
}, false);
20+
});
1421
},
1522
methods: {
1623
isMobile() {
@@ -85,13 +92,22 @@ Vue.component('sugar-device', {
8592
accelerometer.start();
8693
}
8794
} else if (navigator.accelerometer) {
88-
watchId = navigator.accelerometer.watchAcceleration(function () {
89-
vm.accelerationCallback(accelerometer);
90-
}, null, { frequency: frequency });
95+
if (vm.readyToWatch) {
96+
vm.watchId = navigator.accelerometer.watchAcceleration(function (accelerometer) {
97+
vm.accelerationCallback(accelerometer);
98+
}, null, { frequency: frequency });
99+
} else {
100+
vm.frequencyExpected = frequency;
101+
}
102+
} else {
103+
vm.frequencyExpected = frequency;
91104
}
92105
},
93106

94107
accelerationCallback: function (acceleration) {
108+
if (this.readyToWatch) {
109+
this.readyToWatch = false;
110+
}
95111
this.$emit('acceleration-callback', acceleration);
96112
},
97113

0 commit comments

Comments
 (0)