Skip to content

Commit

Permalink
[sensors_plus] Update package dependency (flutter-tizen#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swanseo0 authored Oct 16, 2023
1 parent fd17ae4 commit b7c3071
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 43 deletions.
6 changes: 6 additions & 0 deletions packages/sensors_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.1.3

* Update sensors_plus to 3.0.2.
* Update sensors_plus_platform_interface to 1.1.3.
* Update the example app.

## 1.1.2

* Remove unnecessary `StreamHandlerError` implementation.
Expand Down
4 changes: 2 additions & 2 deletions packages/sensors_plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ This package is not an _endorsed_ implementation of 'sensors_plus'. Therefore, y

```yaml
dependencies:
sensors_plus: ^1.2.1
sensors_plus_tizen: ^1.1.2
sensors_plus: ^3.0.2
sensors_plus_tizen: ^1.1.3
```
Then you can import `sensors_plus` in your Dart code:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:integration_test/integration_test.dart';
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

testWidgets('Can subscript to accelerometerEvents and get non-null events',
testWidgets('Can subscribe to accelerometerEvents and get non-null events',
(WidgetTester tester) async {
final completer = Completer<AccelerometerEvent>();
late StreamSubscription<AccelerometerEvent> subscription;
Expand Down
94 changes: 77 additions & 17 deletions packages/sensors_plus/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@
// ignore_for_file: public_member_api_docs

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sensors_plus/sensors_plus.dart';

import 'snake.dart';

void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations(
[
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
],
);

runApp(const MyApp());
}

Expand All @@ -22,7 +32,8 @@ class MyApp extends StatelessWidget {
return MaterialApp(
title: 'Sensors Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
colorSchemeSeed: const Color(0x9f4376f8),
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
Expand All @@ -35,35 +46,36 @@ class MyHomePage extends StatefulWidget {
final String? title;

@override
_MyHomePageState createState() => _MyHomePageState();
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
static const int _snakeRows = 20;
static const int _snakeColumns = 20;
static const double _snakeCellSize = 10.0;

List<double>? _accelerometerValues;
List<double>? _userAccelerometerValues;
List<double>? _accelerometerValues;
List<double>? _gyroscopeValues;
List<double>? _magnetometerValues;
final _streamSubscriptions = <StreamSubscription<dynamic>>[];

@override
Widget build(BuildContext context) {
final userAccelerometer = _userAccelerometerValues
?.map((double v) => v.toStringAsFixed(1))
.toList();
final accelerometer =
_accelerometerValues?.map((double v) => v.toStringAsFixed(1)).toList();
final gyroscope =
_gyroscopeValues?.map((double v) => v.toStringAsFixed(1)).toList();
final userAccelerometer = _userAccelerometerValues
?.map((double v) => v.toStringAsFixed(1))
.toList();
final magnetometer =
_magnetometerValues?.map((double v) => v.toStringAsFixed(1)).toList();

return Scaffold(
appBar: AppBar(
title: const Text('Sensor Example'),
title: const Text('Sensors Plus Example'),
elevation: 4,
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
Expand All @@ -89,7 +101,7 @@ class _MyHomePageState extends State<MyHomePage> {
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Accelerometer: $accelerometer'),
Text('UserAccelerometer: $userAccelerometer'),
],
),
),
Expand All @@ -98,7 +110,7 @@ class _MyHomePageState extends State<MyHomePage> {
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('UserAccelerometer: $userAccelerometer'),
Text('Accelerometer: $accelerometer'),
],
),
),
Expand Down Expand Up @@ -136,13 +148,46 @@ class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
_streamSubscriptions.add(
userAccelerometerEvents.listen(
(UserAccelerometerEvent event) {
setState(() {
_userAccelerometerValues = <double>[event.x, event.y, event.z];
});
},
onError: (e) {
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
title: Text("Sensor Not Found"),
content: Text(
"It seems that your device doesn't support Accelerometer Sensor"),
);
});
},
cancelOnError: true,
),
);
_streamSubscriptions.add(
accelerometerEvents.listen(
(AccelerometerEvent event) {
setState(() {
_accelerometerValues = <double>[event.x, event.y, event.z];
});
},
onError: (e) {
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
title: Text("Sensor Not Found"),
content: Text(
"It seems that your device doesn't support Gyroscope Sensor"),
);
});
},
cancelOnError: true,
),
);
_streamSubscriptions.add(
Expand All @@ -152,15 +197,18 @@ class _MyHomePageState extends State<MyHomePage> {
_gyroscopeValues = <double>[event.x, event.y, event.z];
});
},
),
);
_streamSubscriptions.add(
userAccelerometerEvents.listen(
(UserAccelerometerEvent event) {
setState(() {
_userAccelerometerValues = <double>[event.x, event.y, event.z];
});
onError: (e) {
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
title: Text("Sensor Not Found"),
content: Text(
"It seems that your device doesn't support User Accelerometer Sensor"),
);
});
},
cancelOnError: true,
),
);
_streamSubscriptions.add(
Expand All @@ -170,6 +218,18 @@ class _MyHomePageState extends State<MyHomePage> {
_magnetometerValues = <double>[event.x, event.y, event.z];
});
},
onError: (e) {
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
title: Text("Sensor Not Found"),
content: Text(
"It seems that your device doesn't support Magnetometer Sensor"),
);
});
},
cancelOnError: true,
),
);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/sensors_plus/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ environment:
dependencies:
flutter:
sdk: flutter
sensors_plus: ^1.2.1
sensors_plus: ^3.0.2
sensors_plus_tizen:
path: ../

dev_dependencies:
flutter_driver:
sdk: flutter
flutter_test:
sdk: flutter
integration_test:
sdk: flutter
integration_test_tizen:
path: ../../integration_test/
flutter_lints: ^1.0.4
flutter_lints: ^2.0.1

flutter:
uses-material-design: true
6 changes: 3 additions & 3 deletions packages/sensors_plus/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: sensors_plus_tizen
description: Tizen implementation of the sensors plugin.
homepage: https://github.com/flutter-tizen/plugins
repository: https://github.com/flutter-tizen/plugins/tree/master/packages/sensors_plus
version: 1.1.2
version: 1.1.3

environment:
sdk: ">=2.18.0 <4.0.0"
Expand All @@ -18,7 +18,7 @@ flutter:
dependencies:
flutter:
sdk: flutter
sensors_plus_platform_interface: ^1.1.0
sensors_plus_platform_interface: ^1.1.3

dev_dependencies:
flutter_lints: ^1.0.4
flutter_lints: ^2.0.1
8 changes: 8 additions & 0 deletions packages/sensors_plus/tizen/src/device_sensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ sensor_type_e ToTizenSensorType(const SensorType &sensor_type) {
return SENSOR_ACCELEROMETER;
case SensorType::kGyroscope:
return SENSOR_GYROSCOPE;
case SensorType::kMagnetometer:
return SENSOR_MAGNETIC;
case SensorType::kUserAccel:
default:
return SENSOR_LINEAR_ACCELERATION;
Expand All @@ -37,6 +39,12 @@ DeviceSensor::~DeviceSensor() {
}

bool DeviceSensor::StartListen(SensorEventCallback callback) {
if (sensor_type_ == SensorType::kMagnetometer) {
LOG_ERROR("Not supported sensor type.");
last_error_ = SENSOR_ERROR_NOT_SUPPORTED;
return false;
}

if (is_listening_) {
LOG_WARN("Already listening.");
last_error_ = SENSOR_ERROR_OPERATION_FAILED;
Expand Down
2 changes: 1 addition & 1 deletion packages/sensors_plus/tizen/src/device_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <string>
#include <vector>

enum class SensorType { kAccelerometer, kGyroscope, kUserAccel };
enum class SensorType { kAccelerometer, kGyroscope, kUserAccel, kMagnetometer };

typedef std::vector<double> SensorEvent;
typedef std::function<void(SensorEvent)> SensorEventCallback;
Expand Down
44 changes: 27 additions & 17 deletions packages/sensors_plus/tizen/src/sensors_plus_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class DeviceSensorStreamHandler : public FlStreamHandler {
events_->Success(flutter::EncodableValue(sensor_event));
};
if (!sensor_.StartListen(callback)) {
events_->Error(sensor_.GetLastErrorString());
return std::make_unique<FlStreamHandlerError>(
std::to_string(sensor_.GetLastError()), sensor_.GetLastErrorString(),
nullptr);
Expand Down Expand Up @@ -67,30 +68,39 @@ class SensorsPlusPlugin : public flutter::Plugin {

private:
void SetupEventChannels(flutter::PluginRegistrar *registrar) {
accelerometer_channel_ = std::make_unique<FlEventChannel>(
registrar->messenger(),
"dev.fluttercommunity.plus/sensors/accelerometer",
&flutter::StandardMethodCodec::GetInstance());
accelerometer_channel_->SetStreamHandler(
std::unique_ptr<FlEventChannel> accelerometer_channel =
std::make_unique<FlEventChannel>(
registrar->messenger(),
"dev.fluttercommunity.plus/sensors/accelerometer",
&flutter::StandardMethodCodec::GetInstance());
accelerometer_channel->SetStreamHandler(
std::make_unique<DeviceSensorStreamHandler>(
SensorType::kAccelerometer));

gyroscope_channel_ = std::make_unique<FlEventChannel>(
registrar->messenger(), "dev.fluttercommunity.plus/sensors/gyroscope",
&flutter::StandardMethodCodec::GetInstance());
gyroscope_channel_->SetStreamHandler(
std::unique_ptr<FlEventChannel> gyroscope_channel =
std::make_unique<FlEventChannel>(
registrar->messenger(),
"dev.fluttercommunity.plus/sensors/gyroscope",
&flutter::StandardMethodCodec::GetInstance());
gyroscope_channel->SetStreamHandler(
std::make_unique<DeviceSensorStreamHandler>(SensorType::kGyroscope));

user_accel_channel_ = std::make_unique<FlEventChannel>(
registrar->messenger(), "dev.fluttercommunity.plus/sensors/user_accel",
&flutter::StandardMethodCodec::GetInstance());
user_accel_channel_->SetStreamHandler(
std::unique_ptr<FlEventChannel> user_accel_channel =
std::make_unique<FlEventChannel>(
registrar->messenger(),
"dev.fluttercommunity.plus/sensors/user_accel",
&flutter::StandardMethodCodec::GetInstance());
user_accel_channel->SetStreamHandler(
std::make_unique<DeviceSensorStreamHandler>(SensorType::kUserAccel));
}

std::unique_ptr<FlEventChannel> accelerometer_channel_;
std::unique_ptr<FlEventChannel> gyroscope_channel_;
std::unique_ptr<FlEventChannel> user_accel_channel_;
std::unique_ptr<FlEventChannel> magnetometer_channel =
std::make_unique<FlEventChannel>(
registrar->messenger(),
"dev.fluttercommunity.plus/sensors/magnetometer",
&flutter::StandardMethodCodec::GetInstance());
magnetometer_channel->SetStreamHandler(
std::make_unique<DeviceSensorStreamHandler>(SensorType::kMagnetometer));
}
};

void SensorsPlusPluginRegisterWithRegistrar(
Expand Down

0 comments on commit b7c3071

Please sign in to comment.