Skip to content

Commit f4083c1

Browse files
authored
Update pedometer sample for jni 0.8.0 (#2237)
Also clean up some nearby Dart code :)
1 parent 5149510 commit f4083c1

File tree

8 files changed

+18761
-8167
lines changed

8 files changed

+18761
-8167
lines changed

experimental/pedometer/example/lib/main.dart

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -188,28 +188,21 @@ class _HomeState extends State<Home> {
188188
}
189189
}
190190

191-
// Axis labels for bottom of chart
191+
// Axis labels for bottom of chart.
192192
Widget getBottomTitles(double value, TitleMeta meta) {
193-
String text;
194-
switch (value.toInt()) {
195-
case 0:
196-
text = '12AM';
197-
break;
198-
case 6:
199-
text = '6AM';
200-
break;
201-
case 12:
202-
text = '12PM';
203-
break;
204-
case 18:
205-
text = '6PM';
206-
break;
207-
default:
208-
text = '';
209-
}
193+
final timeText = switch (value.toInt()) {
194+
0 => '12AM',
195+
6 => '6AM',
196+
12 => '12PM',
197+
18 => '6PM',
198+
_ => ''
199+
};
210200
return SideTitleWidget(
211201
axisSide: meta.axisSide,
212202
space: 4,
213-
child: Text(text, style: TextStyle(fontSize: 14, color: Colors.blue[900])),
203+
child: Text(
204+
timeText,
205+
style: TextStyle(fontSize: 14, color: Colors.blue[900]),
206+
),
214207
);
215208
}

experimental/pedometer/example/lib/steps_repo.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import 'package:pedometer/health_connect.dart' as hc;
1111

1212
/// Class to hold the information needed for the chart
1313
class Steps {
14-
String startHour;
15-
int steps;
14+
final String startHour;
15+
final int steps;
16+
1617
Steps(this.startHour, this.steps);
1718
}
1819

@@ -30,9 +31,9 @@ class _IOSStepsRepo implements StepsRepo {
3031
static const _dylibPath =
3132
'/System/Library/Frameworks/CoreMotion.framework/CoreMotion';
3233

33-
// Bindings for the CMPedometer class
34+
// Bindings for the CMPedometer class.
3435
final lib = pd.PedometerBindings(ffi.DynamicLibrary.open(_dylibPath));
35-
// Bindings for the helper function
36+
// Bindings for the helper function.
3637
final helpLib = pd.PedometerBindings(ffi.DynamicLibrary.process());
3738

3839
late final pd.CMPedometer client;
@@ -68,7 +69,8 @@ class _IOSStepsRepo implements StepsRepo {
6869
pd.NSDate dateConverter(DateTime dartDate) {
6970
// Format dart date to string.
7071
final formattedDate = DateFormat(StepsRepo._formatString).format(dartDate);
71-
// Get current timezone. If eastern african change to AST to follow with NSDate.
72+
// Get current timezone.
73+
// If eastern african change to AST to follow with NSDate.
7274
final tz = dartDate.timeZoneName == "EAT" ? "AST" : dartDate.timeZoneName;
7375

7476
// Create a new NSString with the formatted date and timezone.
@@ -112,7 +114,7 @@ class _IOSStepsRepo implements StepsRepo {
112114
start, end, handler);
113115
}
114116

115-
return (await Future.wait(futures)).nonNulls.toList();
117+
return (await futures.wait).nonNulls.toList();
116118
}
117119
}
118120

@@ -122,10 +124,9 @@ class _AndroidStepsRepo implements StepsRepo {
122124
late final hc.HealthConnectClient client;
123125

124126
_AndroidStepsRepo() {
125-
jni.Jni.initDLApi();
126-
activity = hc.Activity.fromRef(jni.Jni.getCurrentActivity());
127+
activity = hc.Activity.fromReference(jni.Jni.getCurrentActivity());
127128
applicationContext =
128-
hc.Context.fromRef(jni.Jni.getCachedApplicationContext());
129+
hc.Context.fromReference(jni.Jni.getCachedApplicationContext());
129130
client = hc.HealthConnectClient.getOrCreate1(applicationContext);
130131
}
131132

experimental/pedometer/example/pubspec.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,9 @@ dependencies:
3939
# the parent directory to use the current plugin's version.
4040
path: ../
4141

42-
# The following adds the Cupertino Icons font to your application.
43-
# Use with the CupertinoIcons class for iOS style icons.
44-
cupertino_icons: ^1.0.2
45-
ffi: ^2.1.0
42+
ffi: ^2.1.2
4643
intl: ^0.19.0
47-
jni: ^0.7.0
44+
jni: ^0.8.0
4845
fl_chart: ^0.67.0
4946

5047
dev_dependencies:

0 commit comments

Comments
 (0)