Distribute carbs strictly within timeframe

This commit is contained in:
AdrianLxM 2018-04-13 00:48:57 +02:00 committed by GitHub
parent bf02edd871
commit 10b9171afe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -401,13 +401,12 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, C
createCarb(carbsAfterConstraints, time, finalNotes);
} else {
long remainingCarbs = carbsAfterConstraints;
long carbTime = time;
long smallCarbAmount = Math.round(remainingCarbs / (editDuration.getValue() * 4));
if (smallCarbAmount == 0) smallCarbAmount = 1;
while (remainingCarbs > 0) {
createCarb(Math.min(smallCarbAmount, remainingCarbs), carbTime, finalNotes);
int ticks = (duration * 4); //duration guaranteed to be integer greater zero
for (int i = 0; i < ticks; i++){
long carbTime = time + i * 15 * 60 * 1000;
long smallCarbAmount = Math.round((1d * remainingCarbs) / (ticks-i)); //on last iteration (ticks-i) is 1 -> smallCarbAmount == remainingCarbs
remainingCarbs -= smallCarbAmount;
carbTime += 15 * 60 * 1000;
createCarb(smallCarbAmount, carbTime, finalNotes);
}
}
}