1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-27 19:15:50 +01:00

Calories: Fix some crashes

This commit is contained in:
José Rebelo 2024-11-17 00:00:46 +00:00
parent b722969a68
commit 3ad2e827c2
2 changed files with 12 additions and 9 deletions

View File

@ -29,7 +29,6 @@ import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.GarminRestingMetabolicRateSample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
@ -121,7 +120,7 @@ public class CaloriesDailyFragment extends AbstractChartFragment<CaloriesDailyFr
return device.getDeviceCoordinator().supportsActiveCalories();
}
protected TimeSample getRestingMetabolicRate(DBHandler db, GBDevice device) {
protected RestingMetabolicRateSample getRestingMetabolicRate(DBHandler db, GBDevice device) {
TimeSampleProvider<? extends RestingMetabolicRateSample> provider = device.getDeviceCoordinator().getRestingMetabolicRateProvider(device, db.getDaoSession());
return provider.getLatestSample();
}
@ -155,7 +154,10 @@ public class CaloriesDailyFragment extends AbstractChartFragment<CaloriesDailyFr
String formattedDate = new SimpleDateFormat("E, MMM dd").format(date);
dateView.setText(formattedDate);
List<? extends ActivitySample> samples = getActivitySamples(db, device, startTs, endTs);
TimeSample metabolicRate = getRestingMetabolicRate(db, device);
RestingMetabolicRateSample metabolicRate = getRestingMetabolicRate(db, device);
if (metabolicRate == null) {
return new CaloriesData(0, 0, 0);
}
int totalBurnt;
int activeBurnt = 0;
boolean sameDay = calendar.get(Calendar.DAY_OF_YEAR) == day.get(Calendar.DAY_OF_YEAR) &&
@ -164,7 +166,7 @@ public class CaloriesDailyFragment extends AbstractChartFragment<CaloriesDailyFr
if (sameDay) {
passedDayProportion = (double) (calendar.getTimeInMillis() - day.getTimeInMillis()) / (24L * 60 * 60 * 1000);
}
int restingBurnt = (int) ((double) ((GarminRestingMetabolicRateSample) metabolicRate).getRestingMetabolicRate() * passedDayProportion);
int restingBurnt = (int) (metabolicRate.getRestingMetabolicRate() * passedDayProportion);
for (int i = 0; i <= samples.size() - 1; i++) {
ActivitySample sample = samples.get(i);

View File

@ -24,7 +24,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
@ -35,7 +34,6 @@ import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.TimeSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.GarminRestingMetabolicRateSample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
@ -162,21 +160,24 @@ public class DailyTotals implements Serializable {
day.set(Calendar.MINUTE, 0);
day.set(Calendar.SECOND, 0);
day.add(Calendar.HOUR, 0);
TimeSample metabolicRate = getRestingMetabolicRate(db, device);
RestingMetabolicRateSample metabolicRate = getRestingMetabolicRate(db, device);
if (metabolicRate == null) {
return 0;
}
double passedDayProportion = 1;
boolean sameDay = calendar.get(Calendar.DAY_OF_YEAR) == day.get(Calendar.DAY_OF_YEAR) &&
calendar.get(Calendar.YEAR) == day.get(Calendar.YEAR);
if (sameDay) {
passedDayProportion = (double) (calendar.getTimeInMillis() - day.getTimeInMillis()) / (24L * 60 * 60 * 1000);
}
return (int) ((double) ((GarminRestingMetabolicRateSample) metabolicRate).getRestingMetabolicRate() * passedDayProportion);
return (int) (metabolicRate.getRestingMetabolicRate() * passedDayProportion);
}
public static List<? extends ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
return getAllSamples(db, device, tsFrom, tsTo);
}
protected static TimeSample getRestingMetabolicRate(DBHandler db, GBDevice device) {
protected static RestingMetabolicRateSample getRestingMetabolicRate(DBHandler db, GBDevice device) {
TimeSampleProvider<? extends RestingMetabolicRateSample> provider = device.getDeviceCoordinator().getRestingMetabolicRateProvider(device, db.getDaoSession());
return provider.getLatestSample();
}