Skip to content

Commit

Permalink
Add checks for Health Connect availability on Android when configurin…
Browse files Browse the repository at this point in the history
…g and reading/writing data
  • Loading branch information
SlimShadyIAm committed Sep 19, 2024
1 parent a57e5dd commit 783bfba
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/health/lib/src/health_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Health {

String? _deviceId;
final _deviceInfo = DeviceInfoPlugin();
HealthConnectSdkStatus _healthConnectSdkStatus =
HealthConnectSdkStatus.sdkUnavailable;

Health._() {
_registerFromJsonFunctions();
Expand All @@ -48,11 +50,27 @@ class Health {

/// Configure the health plugin. Must be called before using the plugin.
Future<void> configure() async {
if (Platform.isAndroid) {
await _checkIfHealthConnectAvailable();
}

_deviceId = Platform.isAndroid
? (await _deviceInfo.androidInfo).id
: (await _deviceInfo.iosInfo).identifierForVendor;
}

Future<void> _checkIfHealthConnectAvailable() async {
if (!Platform.isAndroid) return;

_healthConnectSdkStatus = await getHealthConnectSdkStatus() ??
HealthConnectSdkStatus.sdkUnavailable;

if (_healthConnectSdkStatus == HealthConnectSdkStatus.sdkUnavailable || _healthConnectSdkStatus == HealthConnectSdkStatus.sdkUnavailableProviderUpdateRequired) {
throw UnsupportedError(
'Health Connect is not available on this device, prompt the user to install it using installHealthConnect.');
}
}

/// Check if a given data type is available on the platform
bool isDataTypeAvailable(HealthDataType dataType) => Platform.isAndroid
? dataTypeKeysAndroid.contains(dataType)
Expand Down Expand Up @@ -86,6 +104,7 @@ class Health {
List<HealthDataType> types, {
List<HealthDataAccess>? permissions,
}) async {
await _checkIfHealthConnectAvailable();
if (permissions != null && permissions.length != types.length) {
throw ArgumentError(
"The lists of types and permissions must be of same length.");
Expand All @@ -111,6 +130,7 @@ class Health {
/// NOTE: The app must be completely killed and restarted for the changes to take effect.
/// Not implemented on iOS as there is no way to programmatically remove access.
Future<void> revokePermissions() async {
await _checkIfHealthConnectAvailable();
try {
if (Platform.isIOS) {
throw UnsupportedError(
Expand Down Expand Up @@ -183,6 +203,7 @@ class Health {
List<HealthDataType> types, {
List<HealthDataAccess>? permissions,
}) async {
await _checkIfHealthConnectAvailable();
if (permissions != null && permissions.length != types.length) {
throw ArgumentError(
'The length of [types] must be same as that of [permissions].');
Expand Down Expand Up @@ -311,6 +332,7 @@ class Health {
DateTime? endTime,
RecordingMethod recordingMethod = RecordingMethod.automatic,
}) async {
await _checkIfHealthConnectAvailable();
if (Platform.isIOS &&
[RecordingMethod.active, RecordingMethod.unknown]
.contains(recordingMethod)) {
Expand Down Expand Up @@ -386,6 +408,7 @@ class Health {
required DateTime startTime,
DateTime? endTime,
}) async {
await _checkIfHealthConnectAvailable();
endTime ??= startTime;
if (startTime.isAfter(endTime)) {
throw ArgumentError("startTime must be equal or earlier than endTime");
Expand Down Expand Up @@ -421,6 +444,7 @@ class Health {
DateTime? endTime,
RecordingMethod recordingMethod = RecordingMethod.automatic,
}) async {
await _checkIfHealthConnectAvailable();
if (Platform.isIOS &&
[RecordingMethod.active, RecordingMethod.unknown]
.contains(recordingMethod)) {
Expand Down Expand Up @@ -461,6 +485,7 @@ class Health {
DateTime? endTime,
RecordingMethod recordingMethod = RecordingMethod.automatic,
}) async {
await _checkIfHealthConnectAvailable();
if (Platform.isIOS &&
[RecordingMethod.active, RecordingMethod.unknown]
.contains(recordingMethod)) {
Expand Down Expand Up @@ -594,6 +619,7 @@ class Health {
double? zinc,
RecordingMethod recordingMethod = RecordingMethod.automatic,
}) async {
await _checkIfHealthConnectAvailable();
if (Platform.isIOS &&
[RecordingMethod.active, RecordingMethod.unknown]
.contains(recordingMethod)) {
Expand Down Expand Up @@ -674,6 +700,7 @@ class Health {
required bool isStartOfCycle,
RecordingMethod recordingMethod = RecordingMethod.automatic,
}) async {
await _checkIfHealthConnectAvailable();
if (Platform.isIOS &&
[RecordingMethod.active, RecordingMethod.unknown]
.contains(recordingMethod)) {
Expand Down Expand Up @@ -804,6 +831,7 @@ class Health {
required DateTime endTime,
List<RecordingMethod> recordingMethodsToFilter = const [],
}) async {
await _checkIfHealthConnectAvailable();
List<HealthDataPoint> dataPoints = [];

for (var type in types) {
Expand All @@ -829,6 +857,7 @@ class Health {
required List<HealthDataType> types,
required int interval,
List<RecordingMethod> recordingMethodsToFilter = const []}) async {
await _checkIfHealthConnectAvailable();
List<HealthDataPoint> dataPoints = [];

for (var type in types) {
Expand All @@ -848,6 +877,7 @@ class Health {
int activitySegmentDuration = 1,
bool includeManualEntry = true,
}) async {
await _checkIfHealthConnectAvailable();
List<HealthDataPoint> dataPoints = [];

final result = await _prepareAggregateQuery(
Expand Down Expand Up @@ -1095,6 +1125,7 @@ class Health {
String? title,
RecordingMethod recordingMethod = RecordingMethod.automatic,
}) async {
await _checkIfHealthConnectAvailable();
if (Platform.isIOS &&
[RecordingMethod.active, RecordingMethod.unknown]
.contains(recordingMethod)) {
Expand Down

0 comments on commit 783bfba

Please sign in to comment.