growme/app/lib/components/edit_metric.dart
2022-09-30 10:19:50 +02:00

383 lines
12 KiB
Dart

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:async/async.dart';
import 'package:grow_me_app/components/bottom_sheet.dart';
import 'package:grow_me_app/components/device_model.dart';
import 'package:grow_me_app/components/device_view.dart';
import 'package:health/health.dart';
import 'package:permission_handler/permission_handler.dart';
class EditMetricView extends StatefulWidget {
const EditMetricView({
required this.device,
required this.metric,
super.key,
});
final LinkedDevice device;
final DeviceMetric? metric;
@override
EditMetricViewState createState() => EditMetricViewState();
}
class EditMetricViewState extends State<EditMetricView> {
List<HealthDataPoint> _healthDataList = [];
AppState _state = AppState.DATA_NOT_FETCHED;
int _nofSteps = 10;
double _mgdl = 10.0;
CancelableOperation? dataFetch;
// create a HealthFactory for use in the app
HealthFactory health = HealthFactory();
@override
void initState() {
super.initState();
if (widget.metric != null) {
if (widget.metric!.type == MetricType.steps) {
dataFetch = CancelableOperation.fromFuture(fetchStepData());
} else {
dataFetch = CancelableOperation.fromFuture(fetchData());
}
}
}
@override
void dispose() {
// your dispose part
super.dispose();
if (dataFetch != null && !dataFetch!.isCompleted) {
dataFetch!.cancel();
}
}
/// Fetch data points from the health plugin and show them in the app.
Future fetchData() async {
setState(() => _state = AppState.FETCHING_DATA);
// define the types to get
final types = [
HealthDataType.STEPS,
HealthDataType.WEIGHT,
HealthDataType.HEIGHT,
HealthDataType.BLOOD_GLUCOSE,
HealthDataType.WORKOUT,
HealthDataType.WATER,
HealthDataType.SLEEP_ASLEEP,
// Uncomment these lines on iOS - only available on iOS
// HealthDataType.AUDIOGRAM
];
// with coresponsing permissions
final permissions = [
HealthDataAccess.READ,
HealthDataAccess.READ,
HealthDataAccess.READ,
HealthDataAccess.READ,
HealthDataAccess.READ,
HealthDataAccess.READ,
HealthDataAccess.READ,
// HealthDataAccess.READ,
];
// get data within the last 24 hours
final now = DateTime.now();
final yesterday = now.subtract(const Duration(days: 1));
// requesting access to the data types before reading them
// note that strictly speaking, the [permissions] are not
// needed, since we only want READ access.
bool requested =
await health.requestAuthorization(types, permissions: permissions);
print('requested: $requested');
// If we are trying to read Step Count, Workout, Sleep or other data that requires
// the ACTIVITY_RECOGNITION permission, we need to request the permission first.
// This requires a special request authorization call.
//
// The location permission is requested for Workouts using the Distance information.
await Permission.activityRecognition.request();
await Permission.location.request();
if (requested) {
try {
// fetch health data
List<HealthDataPoint> healthData =
await health.getHealthDataFromTypes(yesterday, now, types);
// save all the new data points (only the first 100)
_healthDataList.addAll((healthData.length < 100)
? healthData
: healthData.sublist(0, 100));
} catch (error) {
print("Exception in getHealthDataFromTypes: $error");
}
// filter out duplicates
_healthDataList = HealthFactory.removeDuplicates(_healthDataList);
// print the results
_healthDataList.forEach((x) => print(x));
// update the UI to display the results
setState(() {
_state =
_healthDataList.isEmpty ? AppState.NO_DATA : AppState.DATA_READY;
});
} else {
print("Authorization not granted");
setState(() => _state = AppState.DATA_NOT_FETCHED);
}
}
/// Add some random health data.
Future addData() async {
final now = DateTime.now();
final earlier = now.subtract(Duration(minutes: 20));
final types = [
HealthDataType.STEPS,
HealthDataType.HEIGHT,
HealthDataType.BLOOD_GLUCOSE,
HealthDataType.WORKOUT, // Requires Google Fit on Android
// Uncomment these lines on iOS - only available on iOS
// HealthDataType.AUDIOGRAM,
];
final rights = [
HealthDataAccess.WRITE,
HealthDataAccess.WRITE,
HealthDataAccess.WRITE,
HealthDataAccess.WRITE,
// HealthDataAccess.WRITE
];
final permissions = [
HealthDataAccess.READ_WRITE,
HealthDataAccess.READ_WRITE,
HealthDataAccess.READ_WRITE,
HealthDataAccess.READ_WRITE,
// HealthDataAccess.READ_WRITE,
];
late bool perm;
bool? hasPermissions =
await HealthFactory.hasPermissions(types, permissions: rights);
if (hasPermissions == false) {
perm = await health.requestAuthorization(types, permissions: permissions);
}
// Store a count of steps taken
_nofSteps = Random().nextInt(10);
bool success = await health.writeHealthData(
_nofSteps.toDouble(), HealthDataType.STEPS, earlier, now);
// Store a height
success &=
await health.writeHealthData(1.93, HealthDataType.HEIGHT, earlier, now);
// Store a Blood Glucose measurement
_mgdl = Random().nextInt(10) * 1.0;
success &= await health.writeHealthData(
_mgdl, HealthDataType.BLOOD_GLUCOSE, now, now);
// Store a workout eg. running
success &= await health.writeWorkoutData(
HealthWorkoutActivityType.RUNNING, earlier, now,
// The following are optional parameters
// and the UNITS are functional on iOS ONLY!
totalEnergyBurned: 230,
totalEnergyBurnedUnit: HealthDataUnit.KILOCALORIE,
totalDistance: 1234,
totalDistanceUnit: HealthDataUnit.FOOT,
);
// Store an Audiogram
// Uncomment these on iOS - only available on iOS
// const frequencies = [125.0, 500.0, 1000.0, 2000.0, 4000.0, 8000.0];
// const leftEarSensitivities = [49.0, 54.0, 89.0, 52.0, 77.0, 35.0];
// const rightEarSensitivities = [76.0, 66.0, 90.0, 22.0, 85.0, 44.5];
// success &= await health.writeAudiogram(
// frequencies,
// leftEarSensitivities,
// rightEarSensitivities,
// now,
// now,
// metadata: {
// "HKExternalUUID": "uniqueID",
// "HKDeviceName": "bluetooth headphone",
// },
// );
setState(() {
_state = success ? AppState.DATA_ADDED : AppState.DATA_NOT_ADDED;
});
}
/// Fetch steps from the health plugin and show them in the app.
Future fetchStepData() async {
int? steps;
// get steps for today (i.e., since midnight)
final now = DateTime.now();
final midnight = DateTime(now.year, now.month, now.day);
bool requested = await health.requestAuthorization([HealthDataType.STEPS]);
if (requested) {
try {
steps = await health.getTotalStepsInInterval(midnight, now);
} catch (error) {
print("Caught exception in getTotalStepsInInterval: $error");
}
print('Total number of steps: $steps');
setState(() {
_nofSteps = (steps == null) ? 0 : steps;
_state = (steps == null) ? AppState.NO_DATA : AppState.STEPS_READY;
});
} else {
print("Authorization not granted - error in authorization");
setState(() => _state = AppState.DATA_NOT_FETCHED);
}
}
Widget _contentFetchingData() {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
padding: EdgeInsets.all(20),
child: CircularProgressIndicator(
strokeWidth: 10,
)),
Text('Fetching data...')
],
);
}
Widget _contentDataReady() {
return ListView.builder(
itemCount: _healthDataList.length,
itemBuilder: (_, index) {
HealthDataPoint p = _healthDataList[index];
if (p.value is AudiogramHealthValue) {
return ListTile(
title: Text("${p.typeString}: ${p.value}"),
trailing: Text('${p.unitString}'),
subtitle: Text('${p.dateFrom} - ${p.dateTo}'),
);
}
if (p.value is WorkoutHealthValue) {
return ListTile(
title: Text(
"${p.typeString}: ${(p.value as WorkoutHealthValue).totalEnergyBurned} ${(p.value as WorkoutHealthValue).totalEnergyBurnedUnit?.typeToString()}"),
trailing: Text(
'${(p.value as WorkoutHealthValue).workoutActivityType.typeToString()}'),
subtitle: Text('${p.dateFrom} - ${p.dateTo}'),
);
}
return ListTile(
title: Text("${p.typeString}: ${p.value}"),
trailing: Text('${p.unitString}'),
subtitle: Text('${p.dateFrom} - ${p.dateTo}'),
);
});
}
Widget _contentNoData() {
return Text('No Data to show');
}
Widget _contentNotFetched() {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
);
}
Widget _authorizationNotGranted() {
return const Text('Authorization not given. '
'For Android please check your OAUTH2 client ID is correct in Google Developer Console. '
'For iOS check your permissions in Apple Health.');
}
Widget _dataAdded() {
return Text('Data points inserted successfully!');
}
Widget _stepsFetched() {
return Text('Total number of steps: $_nofSteps');
}
Widget _dataNotAdded() {
return Text('Failed to add data');
}
Widget _content() {
if (_state == AppState.DATA_READY)
switch (widget.metric!.type) {
case MetricType.steps:
case MetricType.water:
return Text("Steps: $_nofSteps");
}
else if (_state == AppState.NO_DATA)
return _contentNoData();
else if (_state == AppState.FETCHING_DATA)
return _contentFetchingData();
else if (_state == AppState.AUTH_NOT_GRANTED)
return _authorizationNotGranted();
else if (_state == AppState.DATA_ADDED)
return _dataAdded();
else if (_state == AppState.STEPS_READY)
return _stepsFetched();
else if (_state == AppState.DATA_NOT_ADDED) return _dataNotAdded();
return _contentNotFetched();
}
@override
Widget build(BuildContext context) {
if (widget.metric == null) {
return Flex(direction: Axis.vertical, children: [
MetricIcon(widget.metric, (_) {}),
const SizedBox(height: 25),
const Text("Link to other metrics, coming soon!", textScaleFactor: 1.5),
const SizedBox(height: 25),
]);
}
return Column(children: [
MetricIcon(widget.metric, (_) {}),
const SizedBox(height: 25),
_content(),
const SizedBox(height: 25),
const Text(
"Progress",
textScaleFactor: 1.25,
),
const SizedBox(height: 5),
Text(
"${widget.metric!.progress}/${widget.metric!.target} ${widget.metric!.unit}",
textScaleFactor: 2,
),
const SizedBox(height: 25),
const Text(
"Further configuration options coming soon",
textScaleFactor: 1.5,
),
]);
}
}
Future showEditMetricModal(
BuildContext context, LinkedDevice device, DeviceMetric? metric) {
return showCustomModalBottomSheet(
context,
Padding(
padding: const EdgeInsets.all(25),
child: EditMetricView(
metric: metric,
device: device,
)));
}