Skip to content

Commit

Permalink
[leancode_debug_page] Vendor shake package, bump sensors_plus depende…
Browse files Browse the repository at this point in the history
…ncy (#325)
  • Loading branch information
gogolon authored Jul 8, 2024
1 parent 32607ae commit ee4b4a2
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 46 deletions.
6 changes: 5 additions & 1 deletion packages/leancode_debug_page/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@

## 1.0.1

- Fix screenshots in README.md
- Fix screenshots in README.md

## 1.0.2

- Vendor shake package to avoid using obsolete version of sensors_plus
27 changes: 26 additions & 1 deletion packages/leancode_debug_page/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,29 @@
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.

The file shake_detector.dart contains code from the shake package
(https://pub.dev/packages/shake/license) and is governed by the original BSD-style license
included below.

Copyright 2019 Deven Joshi

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided with
the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 changes: 5 additions & 29 deletions packages/leancode_debug_page/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.0"
version: "1.0.1"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -299,34 +299,18 @@ packages:
dependency: transitive
description:
name: sensors_plus
sha256: "362c8f4f001838b90dd5206b898bbad941bc0142479eab9a3415f0f79e622908"
sha256: "6898cd4490ffc27fea4de5976585e92fae55355175d46c6c3b3d719d42f9e230"
url: "https://pub.dev"
source: hosted
version: "1.4.1"
version: "5.0.1"
sensors_plus_platform_interface:
dependency: transitive
description:
name: sensors_plus_platform_interface
sha256: "95f0cc08791b8bf0c41c5fa99c84be2a7d5bf60a811ddc17e1438b1e68caf0d3"
sha256: bc472d6cfd622acb4f020e726433ee31788b038056691ba433fec80e448a094f
url: "https://pub.dev"
source: hosted
version: "1.1.3"
sensors_plus_web:
dependency: transitive
description:
name: sensors_plus_web
sha256: fca8d7d9ab6233b2a059952666415508e252420be1ef54f092d07884da53ec5e
url: "https://pub.dev"
source: hosted
version: "1.1.2"
shake:
dependency: transitive
description:
name: shake
sha256: "107546951c6b8f5e4c2dca66dfb3aa27dd1a853b4e9a26c9aea224b167045023"
url: "https://pub.dev"
source: hosted
version: "2.2.0"
version: "1.2.0"
share_plus:
dependency: transitive
description:
Expand Down Expand Up @@ -412,14 +396,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.2"
universal_io:
dependency: transitive
description:
name: universal_io
sha256: "1722b2dcc462b4b2f3ee7d188dad008b6eb4c40bbd03a3de451d82c78bba9aad"
url: "https://pub.dev"
source: hosted
version: "2.2.2"
url_launcher_linux:
dependency: transitive
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:leancode_debug_page/src/core/logger_listener.dart';
import 'package:leancode_debug_page/src/core/logging_http_client.dart';
import 'package:leancode_debug_page/src/core/shake_detector.dart';
import 'package:leancode_debug_page/src/models/filter.dart';
import 'package:leancode_debug_page/src/models/request_log_record.dart';
import 'package:logging/logging.dart';
import 'package:rxdart/rxdart.dart';
import 'package:shake/shake.dart';
import 'package:universal_io/io.dart';

bool get _isMobile => Platform.isAndroid || Platform.isIOS;

class DebugPageController {
DebugPageController({
Expand All @@ -37,11 +34,7 @@ class DebugPageController {
() => _updateLoggerLogsStream(loggerListener.logs),
);

// Platform check is necessary due shake package dependency on an obsolete
// version of sensors_plus. Without the check, an exception is thrown on
// platforms where accelerometer is not available. Can be removed when
// https://github.com/deven98/shake/pull/32 is merged.
if (showOnShake && _isMobile) {
if (showOnShake) {
_shakeDetector = ShakeDetector.autoStart(
shakeThresholdGravity: 4,
onPhoneShake: () => visible.value = true,
Expand Down
80 changes: 80 additions & 0 deletions packages/leancode_debug_page/lib/src/core/shake_detector.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// This file mostly consists of code copied from https://pub.dev/packages/shake
// The package must have been vendored in order to bump dependency on
// sensors_plus and avoid exceptions caused by the old version of that package.
// Shake dependency can be brought back once https://github.com/deven98/shake/pull/32
// is merged

import 'dart:async';
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:sensors_plus/sensors_plus.dart';

class ShakeDetector {
/// This constructor automatically calls [startListening] and starts detection and callbacks.
ShakeDetector.autoStart({
required this.onPhoneShake,
this.shakeThresholdGravity = 2.7,
this.shakeSlopTimeMS = 500,
this.shakeCountResetTime = 3000,
this.minimumShakeCount = 1,
}) {
startListening();
}

final VoidCallback onPhoneShake;

/// Shake detection threshold
final double shakeThresholdGravity;

/// Minimum time between shake
final int shakeSlopTimeMS;

/// Time before shake count resets
final int shakeCountResetTime;

/// Number of shakes required before shake is triggered
final int minimumShakeCount;

int mShakeTimestamp = DateTime.now().millisecondsSinceEpoch;
int mShakeCount = 0;

StreamSubscription<AccelerometerEvent>? streamSubscription;

void startListening() {
streamSubscription = accelerometerEventStream().listen(
(event) {
final gX = event.x / 9.80665;
final gY = event.y / 9.80665;
final gZ = event.z / 9.80665;

// gForce will be close to 1 when there is no movement.
final gForce = sqrt(gX * gX + gY * gY + gZ * gZ);

if (gForce > shakeThresholdGravity) {
final now = DateTime.now().millisecondsSinceEpoch;
// ignore shake events too close to each other (500ms)
if (mShakeTimestamp + shakeSlopTimeMS > now) {
return;
}

// reset the shake count after [shakeCountResetTime] seconds of no shakes
if (mShakeTimestamp + shakeCountResetTime < now) {
mShakeCount = 0;
}

mShakeTimestamp = now;
mShakeCount++;

if (mShakeCount >= minimumShakeCount) {
onPhoneShake();
}
}
},
);
}

void stopListening() {
streamSubscription?.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ class DebugPageOverlay extends StatelessWidget {
child: MaterialApp(
theme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.deepPurple,
),
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: LogsInspector(
controller: _controller,
Expand Down
5 changes: 2 additions & 3 deletions packages/leancode_debug_page/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: leancode_debug_page
version: 1.0.1
version: 1.0.2
homepage: https://github.com/leancodepl/flutter_corelibrary/tree/master/packages/leancode_debug_page
repository: https://github.com/leancodepl/flutter_corelibrary
description: >-
Expand All @@ -15,9 +15,8 @@ dependencies:
http: ^1.1.0
logging: ^1.2.0
rxdart: ^0.27.7
shake: ^2.2.0
sensors_plus: ^5.0.1
share_plus: ^9.0.0
universal_io: ^2.2.2

dev_dependencies:
custom_lint: ^0.6.4
Expand Down

0 comments on commit ee4b4a2

Please sign in to comment.