-
Notifications
You must be signed in to change notification settings - Fork 0
/
HealthKitService.cs
224 lines (194 loc) · 7.9 KB
/
HealthKitService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Foundation;
using HealthKit;
namespace HealthKitPoc
{
public class HealthKitService
{
private HKHealthStore HealthStore;
private List<WeightData> data;
private List<HKSample> results;
NSSet DataTypesToWrite
{
get
{
return NSSet.MakeNSObjectSet<HKObjectType>(new HKObjectType[] {
HKQuantityType.Create (HKQuantityTypeIdentifier.BodyMass),
HKObjectType.GetWorkoutType()
});
}
}
NSSet DataTypesToRead
{
get
{
return NSSet.MakeNSObjectSet<HKObjectType>(new HKObjectType[] {
HKQuantityType.Create (HKQuantityTypeIdentifier.BodyMass),
HKObjectType.GetWorkoutType()
});
}
}
public HealthKitService()
{
HealthStore = new HKHealthStore();
}
public async Task Init(){
if (HKHealthStore.IsHealthDataAvailable)
{
var success = await HealthStore.RequestAuthorizationToShareAsync(DataTypesToWrite, DataTypesToRead);
/*if (!success.Item1)
{
Console.WriteLine("You didn't allow HealthKit to access these read/write data types. " +
"In your app, try to handle this error gracefully when a user decides not to provide access. " +
"If you're using a simulator, try it on a device.");
return;
}*/
}
}
private void FetchMostRecentData(HKQuantityType quantityType, Action<List<HKSample>, NSError> completion,int days)
{
var timeSortDescriptor = new NSSortDescriptor(HKSample.SortIdentifierEndDate, false);
DateTime startDay = DateTime.Now;
DateTime endDay = startDay.AddDays(-days).AddHours(-startDay.Hour).AddMinutes(-startDay.Minute).AddSeconds(-startDay.Second).AddMilliseconds(-startDay.Millisecond);
var predicate = HKQuery.GetPredicateForSamples(DateUtil.DateTimeToNSDate(endDay), DateUtil.DateTimeToNSDate(startDay), HKQueryOptions.None);
var query = new HKSampleQuery(quantityType, predicate, 0, new NSSortDescriptor[] { timeSortDescriptor },
(HKSampleQuery resultQuery, HKSample[] results, NSError error) =>
{
if (completion != null && error != null)
{
completion(null, error);
return;
}
completion?.Invoke(results.ToList(), error);
});
HealthStore.ExecuteQuery(query);
}
public Task<List<WeightData>> GetWeigths(){
var weightType = HKQuantityType.Create(HKQuantityTypeIdentifier.BodyMass);
if (HealthStore.GetAuthorizationStatus(weightType) == HKAuthorizationStatus.SharingAuthorized)
{
var completionSource = new TaskCompletionSource<List<WeightData>>();
FetchMostRecentData(weightType, (QuantityResults, error) =>
{
if (error != null)
{
//Console.WriteLine("An error occured fetching the user's age information. " +
//"In your app, try to handle this gracefully. The error was: {0}", error.LocalizedDescription);
completionSource.SetResult(null);
}
data = new List<WeightData>();
if (QuantityResults != null)
{
results = QuantityResults;
for (int i = 0; i < QuantityResults.Count; i++)
{
WeightData h = new WeightData();
var weightUnit = HKUnit.Gram;
h.Value = ((HKQuantitySample)QuantityResults[i]).Quantity.GetDoubleValue(weightUnit) / 1000f;
h.Unit = "kg";
h.Date = DateUtil.NSDateToDateTime(((HKQuantitySample)QuantityResults[i]).StartDate);
data.Add(h);
}
}
completionSource.SetResult(data);
}, 365 * 2
);
return completionSource.Task;
}
else
return null;
}
public Task<bool> SaveWeightIntoHealthStore(WeightData weight)
{
var weightQuantity = HKQuantity.FromQuantity(HKUnit.Gram, weight.Value * 1000);
var weightType = HKQuantityType.Create(HKQuantityTypeIdentifier.BodyMass);
var weightSample = HKQuantitySample.FromType(weightType, weightQuantity, DateUtil.DateTimeToNSDate(weight.Date), DateUtil.DateTimeToNSDate(weight.Date), new NSDictionary());
var completionSource = new TaskCompletionSource<bool>();
HealthStore.SaveObject(weightSample, (success, error) =>
{
if (!success)
{
completionSource.SetResult(false);
}
else
{
completionSource.SetResult(true);
}
});
return completionSource.Task;
}
public Task<bool> DeleteWeightIntoHealthStore(WeightData weight)
{
int index = data.FindIndex(x => Math.Abs(x.Value - weight.Value) < 0.01 && weight.Date.Ticks == x.Date.Ticks);
var sample = results[index];
var completionSource = new TaskCompletionSource<bool>();
HealthStore.DeleteObject(sample, (success, error) =>
{
if (!success)
{
/*InvokeOnMainThread(delegate
{
new UIAlertView("HealthKit", "No puede borrar este registro, ha sido creado por otra app", null, "OK", null).Show();
});
Console.WriteLine("An error occured saving the weight sample {0}. " +
"In your app, try to handle this gracefully. The error was: {1}.", sample, error.LocalizedDescription);
return;*/
completionSource.SetResult(false);
}else{
completionSource.SetResult(true);
}
});
return completionSource.Task;
}
public Task <ExerciseData> AddExercise (ExerciseData data)
{
var completionSource = new TaskCompletionSource<ExerciseData>();
var metadata = new HKMetadata()
{
GroupFitness = true,
IndoorWorkout = true,
CoachedWorkout = true,
};
if (data.Time.TotalMinutes == 0)
{
DateTime start = ((DateTime)data.DateInit).ToLocalTime();
DateTime end = ((DateTime)data.DateEnd).ToLocalTime();
data.Time= end.Subtract(start);
}
HKQuantity calories = getCaloriesFromData(data);
HKWorkout workOut = HKWorkout.Create(HKWorkoutActivityType.TraditionalStrengthTraining,
data.DateInit,
data.DateEnd,
null,
calories,
null,
metadata);
HealthStore.SaveObject(workOut,(succes, error) => {
if (succes)
{
data.Kilocalories = calories.GetDoubleValue(HKUnit.Kilocalorie);
completionSource.SetResult(data);
}
else
{
completionSource.SetResult(null);
}
});
return completionSource.Task;
}
private HKQuantity getCaloriesFromData(ExerciseData data)
{
//0,029 x(peso corporal en kg) x 2,2 x Total de minutos practicados = cantidad de calorías aproximadas quemadas
double userWeight = 88.0;
double weigthEdit = 0.029;
double timeEdit = 2.2;
var kilocalories = weigthEdit * userWeight;
kilocalories = kilocalories * (timeEdit * data.Time.TotalMinutes);
kilocalories = Math.Round(kilocalories, 2);
return HKQuantity.FromQuantity(HKUnit.Kilocalorie, kilocalories);
}
}
}