Skip to content

Commit

Permalink
feat: request sync from watch
Browse files Browse the repository at this point in the history
Request sync from watch when connecting to it.
Log received data items.
  • Loading branch information
Turtlepaw committed Sep 1, 2024
1 parent 913261c commit 0a71fa9
Show file tree
Hide file tree
Showing 3 changed files with 465 additions and 19 deletions.
21 changes: 5 additions & 16 deletions lib/routes/settings.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:fitness_challenges/constants.dart';
import 'package:fitness_challenges/routes/profile.dart';
import 'package:fitness_challenges/utils/common.dart';
Expand Down Expand Up @@ -148,19 +146,10 @@ class _SettingsPageState extends State<SettingsPage> {
});

await _flutterWearOsConnectivity.configureWearableAPI();
final _connectedDevices =
await _flutterWearOsConnectivity.getConnectedDevices();
for (var device in _connectedDevices) {
await _flutterWearOsConnectivity
.sendMessage(Uint8List(1),
deviceId: device.id,
path: "/request-sync",
priority: MessagePriority.high)
.then((d) => print(d));
}
List<DataItem> _allDataItems =

List<DataItem> allDataItems =
await _flutterWearOsConnectivity.getAllDataItems();
print(_allDataItems.map((e) => e.mapData));
debugPrint(allDataItems.map((e) => e.mapData).join(","));
if (mounted) {
Provider.of<HealthManager>(context, listen: false)
.fetchHealthData(context: context);
Expand All @@ -170,7 +159,7 @@ class _SettingsPageState extends State<SettingsPage> {
await Future.delayed(const Duration(seconds: 1));
setState(() {
healthType = HealthType.watch;
if (_allDataItems.isNotEmpty) _isHealthConnected = true;
if (allDataItems.isNotEmpty) _isHealthConnected = true;
_isWatchLoading = false;
});
return true;
Expand Down Expand Up @@ -329,7 +318,7 @@ class _SettingsPageState extends State<SettingsPage> {
? _connectSystemHealthPlatform
: null),
label: Text(health.capabilities.contains(HealthType.systemManaged)
? ((_isHealthConnected &&
? ((health.isConnected &&
healthType == HealthType.systemManaged)
? "Connected"
: "System")
Expand Down
17 changes: 14 additions & 3 deletions lib/utils/health.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:fitness_challenges/constants.dart';
import 'package:fitness_challenges/types/challenges.dart';
Expand Down Expand Up @@ -56,7 +57,7 @@ class HealthManager with ChangeNotifier {
await health.hasPermissions(types, permissions: permissions);

_isConnected = isAvailable && (hasPermissions ?? false);
} else if(type == HealthType.watch){
} else if (type == HealthType.watch) {
_isConnected = caps.containsKey("verify_wear_app");
}
}
Expand Down Expand Up @@ -92,15 +93,25 @@ class HealthManager with ChangeNotifier {
final FlutterWearOsConnectivity flutterWearOsConnectivity =
FlutterWearOsConnectivity();

await flutterWearOsConnectivity.configureWearableAPI();
var devices = await flutterWearOsConnectivity.getConnectedDevices();

for (var device in devices) {
await flutterWearOsConnectivity
.sendMessage(Uint8List(1),
deviceId: device.id,
path: "/request-sync",
priority: MessagePriority.high)
.then((d) => debugPrint(d.toString()));
}

await Future.delayed(const Duration(seconds: 2));
// Fetches most recent data, even if it's from yesterday
flutterWearOsConnectivity.configureWearableAPI();
var data = await flutterWearOsConnectivity.getAllDataItems();
const id = "com.turtlepaw.fitness_challenges.steps";
const timeId = "com.turtlepaw.fitness_challenges.timestamp";
debugPrint(data.toString());

var devices = await flutterWearOsConnectivity.getConnectedDevices();
if (devices.isNotEmpty) _isConnected = true;

if (data.isEmpty) {
Expand Down
Loading

0 comments on commit 0a71fa9

Please sign in to comment.