|
|
|
@ -8,10 +8,8 @@ import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart';
|
|
|
|
|
import 'package:fit_kit/fit_kit.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:health/health.dart';
|
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
|
|
|
|
|
|
class syncHealthDataButton extends StatefulWidget {
|
|
|
|
|
double marginTop;
|
|
|
|
@ -27,8 +25,6 @@ class syncHealthDataButton extends StatefulWidget {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _syncHealthDataButtonState extends State<syncHealthDataButton> {
|
|
|
|
|
List<DataType> dataTypes = List();
|
|
|
|
|
|
|
|
|
|
List<HealthDataPoint> _healthDataList = [];
|
|
|
|
|
|
|
|
|
|
List<healthData> Med_InsertTransactionsInputsList = new List();
|
|
|
|
@ -44,12 +40,6 @@ class _syncHealthDataButtonState extends State<syncHealthDataButton> {
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
dataTypes.add(DataType.DISTANCE);
|
|
|
|
|
dataTypes.add(DataType.STEP_COUNT);
|
|
|
|
|
dataTypes.add(DataType.HEART_RATE);
|
|
|
|
|
// dataTypes.add(DataType.SLEEP);
|
|
|
|
|
// dataTypes.add(DataType.ENERGY);
|
|
|
|
|
|
|
|
|
|
super.initState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -61,53 +51,13 @@ class _syncHealthDataButtonState extends State<syncHealthDataButton> {
|
|
|
|
|
padding: const EdgeInsets.all(20.0),
|
|
|
|
|
child: DefaultButton(TranslationBase.of(context).syncHealthData, () {
|
|
|
|
|
print("ReadAll");
|
|
|
|
|
fetchData();
|
|
|
|
|
// readAll();
|
|
|
|
|
// fetchData();
|
|
|
|
|
readAll();
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future fetchData() async {
|
|
|
|
|
// get everything from midnight until now
|
|
|
|
|
DateTime startDate = DateTime(2021, 11, 01, 0, 0, 0);
|
|
|
|
|
DateTime endDate = DateTime(2021, 12, 06, 23, 59, 59);
|
|
|
|
|
|
|
|
|
|
HealthFactory health = HealthFactory();
|
|
|
|
|
|
|
|
|
|
// define the types to get
|
|
|
|
|
List<HealthDataType> types = [
|
|
|
|
|
HealthDataType.STEPS,
|
|
|
|
|
HealthDataType.HEART_RATE,
|
|
|
|
|
Platform.isAndroid ? HealthDataType.DISTANCE_DELTA : HealthDataType.DISTANCE_WALKING_RUNNING
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// you MUST request access to the data types before reading them
|
|
|
|
|
// bool accessWasGranted = await health.requestAuthorization(types);
|
|
|
|
|
await health.requestAuthorization(types);
|
|
|
|
|
|
|
|
|
|
int steps = 0;
|
|
|
|
|
try {
|
|
|
|
|
List<HealthDataPoint> healthData = await health.getHealthDataFromTypes(startDate, endDate, types);
|
|
|
|
|
_healthDataList.addAll(healthData);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
print("Caught exception in getHealthDataFromTypes: $e");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// filter out duplicates
|
|
|
|
|
_healthDataList = HealthFactory.removeDuplicates(_healthDataList);
|
|
|
|
|
|
|
|
|
|
// print the results
|
|
|
|
|
_healthDataList.forEach((x) {
|
|
|
|
|
if(x.type == HealthDataType.STEPS) {
|
|
|
|
|
print("Data point: $x");
|
|
|
|
|
steps += x.value.round();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
print("Steps: $steps");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void readAll() async {
|
|
|
|
|
TransactionsListID = 1;
|
|
|
|
|
var MedSubCategoryID = 0;
|
|
|
|
@ -115,91 +65,37 @@ class _syncHealthDataButtonState extends State<syncHealthDataButton> {
|
|
|
|
|
|
|
|
|
|
GifLoaderDialogUtils.showMyDialog(context);
|
|
|
|
|
Med_InsertTransactionsInputsList.clear();
|
|
|
|
|
var date;
|
|
|
|
|
var differenceInHours;
|
|
|
|
|
String strDifferenceInHours;
|
|
|
|
|
var differenceInMinutes;
|
|
|
|
|
String strDifferenceInMinutes;
|
|
|
|
|
var today = DateTime.now();
|
|
|
|
|
var yearNum = DateFormat('y').format(today).toString();
|
|
|
|
|
|
|
|
|
|
var firstDayOfTheYear = DateTime.parse(yearNum + "-" + "01" + "-" + "01");
|
|
|
|
|
|
|
|
|
|
if (await FitKit.requestPermissions(dataTypes)) {
|
|
|
|
|
for (DataType type in dataTypes) {
|
|
|
|
|
final results = await FitKit.read(
|
|
|
|
|
type,
|
|
|
|
|
dateFrom: firstDayOfTheYear,
|
|
|
|
|
dateTo: DateTime.now(),
|
|
|
|
|
limit: 1000,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (type == DataType.DISTANCE) {
|
|
|
|
|
MedCategoryID = 7;
|
|
|
|
|
} else if (type == DataType.STEP_COUNT) {
|
|
|
|
|
MedCategoryID = 6;
|
|
|
|
|
} else if (type == DataType.HEART_RATE) {
|
|
|
|
|
MedCategoryID = 3;
|
|
|
|
|
}
|
|
|
|
|
DateTime startDate = DateTime.now().subtract(new Duration(days: 30));
|
|
|
|
|
|
|
|
|
|
results.forEach((result) {
|
|
|
|
|
// print("in forEach");
|
|
|
|
|
date = result.dateTo;
|
|
|
|
|
if (result.value.runtimeType.toString() == "int") {
|
|
|
|
|
value = double.parse(result.value.toString());
|
|
|
|
|
} else {
|
|
|
|
|
value = result.value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MedSubCategoryID = 0;
|
|
|
|
|
|
|
|
|
|
if (MedCategoryID == 4) {
|
|
|
|
|
differenceInHours = null;
|
|
|
|
|
var sleepDate = result.dateFrom.toString().substring(0, 10);
|
|
|
|
|
|
|
|
|
|
DateTime dateFrom = result.dateFrom;
|
|
|
|
|
DateTime dateTo = result.dateTo;
|
|
|
|
|
|
|
|
|
|
differenceInMinutes = dateTo.difference(dateFrom).inMinutes;
|
|
|
|
|
differenceInMinutes > 59 ? differenceInHours = differenceInMinutes / 60 : differenceInMinutes = differenceInMinutes;
|
|
|
|
|
|
|
|
|
|
if (differenceInHours != null && differenceInHours != 0) {
|
|
|
|
|
differenceInHours = differenceInHours.toInt();
|
|
|
|
|
differenceInMinutes = differenceInMinutes % 60;
|
|
|
|
|
|
|
|
|
|
differenceInHours < 10 ? strDifferenceInHours = "0" + differenceInHours.toString() : strDifferenceInHours = differenceInHours.toString();
|
|
|
|
|
} else {
|
|
|
|
|
strDifferenceInHours = "00";
|
|
|
|
|
differenceInHours = 00;
|
|
|
|
|
}
|
|
|
|
|
differenceInMinutes < 10 ? strDifferenceInMinutes = "0" + differenceInMinutes.toString() : strDifferenceInMinutes = differenceInMinutes.toString();
|
|
|
|
|
|
|
|
|
|
sleepDate = DateTime.parse(sleepDate + " " + strDifferenceInHours + ":" + strDifferenceInMinutes + ":" + "00.00").toString();
|
|
|
|
|
String hours = sleepDate.substring(11, sleepDate.indexOf(":"));
|
|
|
|
|
print(hours);
|
|
|
|
|
|
|
|
|
|
String minutes = sleepDate.substring(sleepDate.indexOf(":") + 1, sleepDate.indexOf(":") + 3);
|
|
|
|
|
print(minutes);
|
|
|
|
|
|
|
|
|
|
if (value == 0 || value == 109) {
|
|
|
|
|
print("in Bed");
|
|
|
|
|
MedSubCategoryID = 1;
|
|
|
|
|
value = double.parse(hours + "." + minutes);
|
|
|
|
|
print("value");
|
|
|
|
|
print(value);
|
|
|
|
|
} else if (value == 1 || value == 110) {
|
|
|
|
|
print("Sleep");
|
|
|
|
|
MedSubCategoryID = 2;
|
|
|
|
|
value = double.parse(hours + "." + minutes);
|
|
|
|
|
print("value");
|
|
|
|
|
print(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
date = DateUtil.convertDateToString(date);
|
|
|
|
|
Med_InsertTransactionsInputsList.add(healthData(MedCategoryID: MedCategoryID, MedSubCategoryID: MedSubCategoryID, MachineDate: date, Value: value, TransactionsListID: TransactionsListID++));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
HealthFactory health = HealthFactory();
|
|
|
|
|
List<HealthDataType> types = [HealthDataType.STEPS, HealthDataType.HEART_RATE, Platform.isAndroid ? HealthDataType.DISTANCE_DELTA : HealthDataType.DISTANCE_WALKING_RUNNING];
|
|
|
|
|
|
|
|
|
|
await health.requestAuthorization(types);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
List<HealthDataPoint> healthData = await health.getHealthDataFromTypes(startDate, DateTime.now(), types);
|
|
|
|
|
_healthDataList.addAll(healthData);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
print("Caught exception in getHealthDataFromTypes: $e");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_healthDataList = HealthFactory.removeDuplicates(_healthDataList);
|
|
|
|
|
|
|
|
|
|
_healthDataList.forEach((x) {
|
|
|
|
|
if (x.type == HealthDataType.STEPS) {
|
|
|
|
|
Med_InsertTransactionsInputsList.add(
|
|
|
|
|
healthData(MedCategoryID: 6, MedSubCategoryID: MedSubCategoryID, MachineDate: DateUtil.convertDateToString(x.dateFrom), Value: x.value, TransactionsListID: TransactionsListID++));
|
|
|
|
|
}
|
|
|
|
|
if (x.type == HealthDataType.HEART_RATE) {
|
|
|
|
|
Med_InsertTransactionsInputsList.add(
|
|
|
|
|
healthData(MedCategoryID: 3, MedSubCategoryID: MedSubCategoryID, MachineDate: DateUtil.convertDateToString(x.dateFrom), Value: x.value, TransactionsListID: TransactionsListID++));
|
|
|
|
|
}
|
|
|
|
|
if (x.type == HealthDataType.DISTANCE_DELTA || x.type == HealthDataType.DISTANCE_WALKING_RUNNING) {
|
|
|
|
|
Med_InsertTransactionsInputsList.add(
|
|
|
|
|
healthData(MedCategoryID: 7, MedSubCategoryID: MedSubCategoryID, MachineDate: DateUtil.convertDateToString(x.dateFrom), Value: x.value, TransactionsListID: TransactionsListID++));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
getAllHealthDataLists();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -238,10 +134,8 @@ class _syncHealthDataButtonState extends State<syncHealthDataButton> {
|
|
|
|
|
totalSteps += element['Value'];
|
|
|
|
|
} else if (element['MedCategoryID'] == 7) {
|
|
|
|
|
MedCategoryID = 7;
|
|
|
|
|
// to convert from meter to km
|
|
|
|
|
totalDistance += (element['Value'] * 0.001);
|
|
|
|
|
} else if (element['MedCategoryID'] == 3) {
|
|
|
|
|
// print("HeartRate");
|
|
|
|
|
MedCategoryID = 3;
|
|
|
|
|
counter++;
|
|
|
|
|
totalHeartRate += element['Value'];
|
|
|
|
|