81 lines
2.5 KiB
Dart
81 lines
2.5 KiB
Dart
import 'package:health/health.dart';
|
|
import 'package:collection/collection.dart';
|
|
import 'dart:io';
|
|
import 'package:flutter/foundation.dart';
|
|
import '../models/cycle_entry.dart';
|
|
|
|
class HealthService {
|
|
static final HealthService _instance = HealthService._internal();
|
|
factory HealthService() => _instance;
|
|
HealthService._internal();
|
|
|
|
final Health _health = Health();
|
|
|
|
// ignore: unused_field
|
|
List<HealthDataType> _requestedTypes = [];
|
|
|
|
// TODO: Fix HealthDataType for menstruation in newer health package versions
|
|
static const List<HealthDataType> _menstruationDataTypes = [
|
|
// HealthDataType.MENSTRUATION - Not found in recent versions?
|
|
HealthDataType.STEPS, // Placeholder to avoid compile error
|
|
];
|
|
|
|
Future<bool> requestAuthorization(List<HealthDataType> types) async {
|
|
_requestedTypes = types;
|
|
try {
|
|
final bool authorized = await _health.requestAuthorization(types);
|
|
return authorized;
|
|
} catch (e) {
|
|
debugPrint("Error requesting authorization: $e");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<bool> hasPermissions(List<HealthDataType> types) async {
|
|
return await _health.hasPermissions(types) ?? false;
|
|
}
|
|
|
|
Future<bool> writeMenstruationData(List<CycleEntry> entries) async {
|
|
// This feature is currently disabled until compatible HealthDataType is identified
|
|
debugPrint("writeMenstruationData: Currently disabled due to package version incompatibility.");
|
|
return false;
|
|
|
|
/*
|
|
final periodEntries = entries.where((entry) => entry.isPeriodDay).toList();
|
|
|
|
if (periodEntries.isEmpty) {
|
|
debugPrint("No period entries to write.");
|
|
return true;
|
|
}
|
|
|
|
final hasAuth = await hasPermissions([HealthDataType.STEPS]);
|
|
if (!hasAuth) {
|
|
debugPrint("Authorization not granted.");
|
|
return false;
|
|
}
|
|
|
|
bool allWrittenSuccessfully = true;
|
|
for (var entry in periodEntries) {
|
|
try {
|
|
final success = await _health.writeHealthData(
|
|
value: 0.0,
|
|
type: HealthDataType.STEPS,
|
|
startTime: entry.date,
|
|
endTime: entry.date.add(const Duration(days: 1)),
|
|
);
|
|
if (!success) {
|
|
allWrittenSuccessfully = false;
|
|
debugPrint("Failed to write data for ${entry.date}");
|
|
}
|
|
} catch (e) {
|
|
allWrittenSuccessfully = false;
|
|
debugPrint("Error writing data for ${entry.date}: $e");
|
|
}
|
|
}
|
|
return allWrittenSuccessfully;
|
|
*/
|
|
}
|
|
|
|
List<HealthDataType> get menstruationDataTypes => _menstruationDataTypes;
|
|
}
|