Skip to content

Commit 42f7cf7

Browse files
author
Volodymyr Oliinyk
committed
updated shake detector
1 parent 705eea7 commit 42f7cf7

File tree

5 files changed

+44
-48
lines changed

5 files changed

+44
-48
lines changed

.flutter-plugins

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# This is a generated file; do not edit or check into version control.
2-
sensors=/Users/volodymyroliinyk/.pub-cache/hosted/pub.dartlang.org/sensors-0.4.2+6/
2+
sensors_plus=/Users/volodymyroliinyk/.pub-cache/hosted/pub.dartlang.org/sensors_plus-2.0.1/

.flutter-plugins-dependencies

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"sensors","path":"/Users/volodymyroliinyk/.pub-cache/hosted/pub.dartlang.org/sensors-0.4.2+6/","native_build":true,"dependencies":[]}],"android":[{"name":"sensors","path":"/Users/volodymyroliinyk/.pub-cache/hosted/pub.dartlang.org/sensors-0.4.2+6/","native_build":true,"dependencies":[]}],"macos":[],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"sensors","dependencies":[]}],"date_created":"2022-11-28 17:02:23.593622","version":"3.3.9"}
1+
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"sensors_plus","path":"/Users/volodymyroliinyk/.pub-cache/hosted/pub.dartlang.org/sensors_plus-2.0.1/","native_build":true,"dependencies":[]}],"android":[{"name":"sensors_plus","path":"/Users/volodymyroliinyk/.pub-cache/hosted/pub.dartlang.org/sensors_plus-2.0.1/","native_build":true,"dependencies":[]}],"macos":[],"linux":[],"windows":[],"web":[{"name":"sensors_plus","path":"/Users/volodymyroliinyk/.pub-cache/hosted/pub.dartlang.org/sensors_plus-2.0.1/","dependencies":[]}]},"dependencyGraph":[{"name":"sensors_plus","dependencies":[]}],"date_created":"2022-11-28 17:07:22.023896","version":"3.3.9"}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"sensors","path":"/Users/volodymyroliinyk/.pub-cache/hosted/pub.dartlang.org/sensors-0.4.2+6/","native_build":true,"dependencies":[]}],"android":[{"name":"sensors","path":"/Users/volodymyroliinyk/.pub-cache/hosted/pub.dartlang.org/sensors-0.4.2+6/","native_build":true,"dependencies":[]}],"macos":[],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"sensors","dependencies":[]}],"date_created":"2022-11-28 17:02:24.205567","version":"3.3.9"}
1+
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"sensors_plus","path":"/Users/volodymyroliinyk/.pub-cache/hosted/pub.dartlang.org/sensors_plus-2.0.1/","native_build":true,"dependencies":[]}],"android":[{"name":"sensors_plus","path":"/Users/volodymyroliinyk/.pub-cache/hosted/pub.dartlang.org/sensors_plus-2.0.1/","native_build":true,"dependencies":[]}],"macos":[],"linux":[],"windows":[],"web":[{"name":"sensors_plus","path":"/Users/volodymyroliinyk/.pub-cache/hosted/pub.dartlang.org/sensors_plus-2.0.1/","dependencies":[]}]},"dependencyGraph":[{"name":"sensors_plus","dependencies":[]}],"date_created":"2022-11-28 17:07:22.978430","version":"3.3.9"}

lib/src/shake_detector.dart

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,63 @@ import 'dart:async';
22
import 'dart:math';
33

44
import 'package:flutter/material.dart';
5-
import 'package:sensors/sensors.dart';
5+
import 'package:sensors_plus/sensors_plus.dart';
66

77
class ShakeDetector {
8-
final VoidCallback? onPhoneShake;
9-
10-
final double shakeThresholdGravity;
11-
12-
final int minTimeBetweenShakes;
13-
14-
final int shakeCountResetTime;
15-
16-
final int minShakeCount;
17-
18-
int shakeCount = 0;
19-
20-
int lastShakeTimestamp = DateTime.now().millisecondsSinceEpoch;
21-
22-
StreamSubscription? streamSubscription;
23-
248
ShakeDetector({
25-
this.onPhoneShake,
9+
required this.onPhoneShake,
2610
this.shakeThresholdGravity = 1.25,
2711
this.minTimeBetweenShakes = 160,
2812
this.shakeCountResetTime = 1500,
2913
this.minShakeCount = 2,
3014
});
3115

16+
final VoidCallback onPhoneShake;
17+
18+
final double shakeThresholdGravity;
19+
final int minTimeBetweenShakes;
20+
final int shakeCountResetTime;
21+
final int minShakeCount;
22+
23+
int _shakeCount = 0;
24+
int _lastShakeTimestamp = DateTime.now().millisecondsSinceEpoch;
25+
StreamSubscription? _streamSubscription;
26+
3227
/// Starts listening to accerelometer events
3328
void startListening() {
34-
streamSubscription = accelerometerEvents.listen((event) {
35-
var gX = event.x / 9.81;
36-
var gY = event.y / 9.81;
37-
var gZ = event.z / 9.81;
29+
_streamSubscription = accelerometerEvents
30+
.map((event) {
31+
final gX = event.x / 9.81;
32+
final gY = event.y / 9.81;
33+
final gZ = event.z / 9.81;
34+
35+
// gForce will be close to 1 when there is no movement.
36+
final gForce = sqrt(gX * gX + gY * gY + gZ * gZ);
3837

39-
// gForce will be close to 1 when there is no movement.
40-
var gForce = sqrt(gX * gX + gY * gY + gZ * gZ);
41-
if (gForce > shakeThresholdGravity) {
42-
var now = DateTime.now().millisecondsSinceEpoch;
43-
// ignore shake events too close to each other
44-
if (lastShakeTimestamp + minTimeBetweenShakes > now) {
45-
return;
46-
}
38+
return gForce;
39+
})
40+
.where((gForce) => gForce > shakeThresholdGravity)
41+
.where((_) {
42+
var now = DateTime.now().millisecondsSinceEpoch;
43+
// ignore shake events too close to each other
44+
return _lastShakeTimestamp + minTimeBetweenShakes < now;
45+
})
46+
.map((_) => _lastShakeTimestamp)
47+
.map((lastShakeTimestamp) {
48+
var now = DateTime.now().millisecondsSinceEpoch;
4749

48-
// reset the shake count after 1.5 seconds of no shakes
49-
if (lastShakeTimestamp + shakeCountResetTime < now) {
50-
shakeCount = 0;
51-
}
50+
_shakeCount = lastShakeTimestamp + shakeCountResetTime < now ? 0 : _shakeCount + 1;
51+
_lastShakeTimestamp = now;
5252

53-
lastShakeTimestamp = now;
54-
if (++shakeCount >= minShakeCount) {
55-
shakeCount = 0;
56-
onPhoneShake?.call();
57-
}
58-
}
59-
});
53+
return _shakeCount;
54+
})
55+
.where((shakeCount) => shakeCount >= minShakeCount)
56+
.listen((event) => onPhoneShake());
6057
}
6158

6259
/// Stops listening to accelerometer events
6360
void stopListening() {
64-
if (streamSubscription != null) {
65-
streamSubscription?.cancel();
66-
}
61+
_streamSubscription?.cancel();
62+
_streamSubscription = null;
6763
}
6864
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ dependencies:
1212
sdk: flutter
1313

1414
logger: "0.7.0"
15-
sensors: ^0.4.0+1
15+
sensors_plus: ^2.0.1

0 commit comments

Comments
 (0)