use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.
the class LogEntryViewHolder method onBind.
@Override
public void onBind(LogEntryListItem item) {
Entry entry = item.getEntry();
List<EntryTag> entryTags = item.getEntryTags();
List<FoodEaten> foodEatenList = item.getFoodEatenList();
getBinding().dateLabel.setText(entry.getDate().toString("HH:mm"));
TextView noteLabel = getBinding().noteLabel;
if (entry.getNote() != null && entry.getNote().length() > 0) {
noteLabel.setVisibility(View.VISIBLE);
noteLabel.setText(entry.getNote());
} else {
noteLabel.setVisibility(View.GONE);
}
TextView foodLabel = getBinding().foodLabel;
if (foodEatenList != null && foodEatenList.size() > 0) {
List<String> foodNotes = new ArrayList<>();
for (FoodEaten foodEaten : foodEatenList) {
String foodEatenAsString = foodEaten.print();
if (foodEatenAsString != null) {
foodNotes.add(foodEatenAsString);
}
}
if (foodNotes.size() > 0) {
foodLabel.setVisibility(View.VISIBLE);
foodLabel.setText(TextUtils.join("\n", foodNotes));
} else {
foodLabel.setVisibility(View.GONE);
}
} else {
foodLabel.setVisibility(View.GONE);
}
ChipGroup entryTagChipGroup = getBinding().entryTagChipGroup;
entryTagChipGroup.setVisibility(entryTags != null && entryTags.size() > 0 ? View.VISIBLE : View.GONE);
entryTagChipGroup.removeAllViews();
if (entryTags != null) {
for (EntryTag entryTag : entryTags) {
Tag tag = entryTag.getTag();
if (tag != null) {
ChipView chipView = new ChipView(getContext());
chipView.setText(tag.getName());
chipView.setOnClickListener(view -> listener.onTagSelected(tag, view));
entryTagChipGroup.addView(chipView);
}
}
}
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout measurementsLayout = getBinding().measurementsLayout;
if (inflater != null) {
measurementsLayout.removeAllViews();
List<Measurement> measurements = entry.getMeasurementCache();
if (measurements.size() > 0) {
measurementsLayout.setVisibility(View.VISIBLE);
for (Measurement measurement : measurements) {
Category category = measurement.getCategory();
View viewMeasurement = inflater.inflate(R.layout.list_item_log_measurement, measurementsLayout, false);
ImageView categoryImage = viewMeasurement.findViewById(R.id.image);
int imageResourceId = category.getIconImageResourceId();
categoryImage.setImageDrawable(ContextCompat.getDrawable(getContext(), imageResourceId));
categoryImage.setColorFilter(ContextCompat.getColor(getContext(), R.color.gray_dark));
TextView value = viewMeasurement.findViewById(R.id.value);
value.setText(measurement.print(getContext()));
if (category == Category.BLOODSUGAR) {
BloodSugar bloodSugar = (BloodSugar) measurement;
if (PreferenceStore.getInstance().limitsAreHighlighted()) {
int backgroundColor = ContextCompat.getColor(getContext(), R.color.green);
if (bloodSugar.getMgDl() > PreferenceStore.getInstance().getLimitHyperglycemia()) {
backgroundColor = ContextCompat.getColor(getContext(), R.color.red);
} else if (bloodSugar.getMgDl() < PreferenceStore.getInstance().getLimitHypoglycemia()) {
backgroundColor = ContextCompat.getColor(getContext(), R.color.blue);
}
categoryImage.setColorFilter(backgroundColor);
}
}
measurementsLayout.addView(viewMeasurement);
}
} else {
measurementsLayout.setVisibility(View.GONE);
}
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.
the class PdfLog method init.
private void init() {
PdfExportConfig config = cache.getConfig();
Context context = config.getContext();
List<List<Cell>> data = new ArrayList<>();
List<Cell> headerRow = new ArrayList<>();
Cell headerCell = new CellBuilder(new Cell(cache.getFontBold())).setWidth(getLabelWidth()).setText(DateTimeUtils.toWeekDayAndDate(cache.getDateTime())).build();
headerRow.add(headerCell);
data.add(headerRow);
for (Entry entry : entriesOfDay) {
List<Measurement> measurements = EntryDao.getInstance().getMeasurements(entry, cache.getConfig().getCategories());
entry.setMeasurementCache(measurements);
}
int rowIndex = 0;
for (Entry entry : entriesOfDay) {
int backgroundColor = rowIndex % 2 == 0 ? cache.getColorDivider() : Color.white;
int oldSize = data.size();
String time = entry.getDate().toString("HH:mm");
for (Measurement measurement : entry.getMeasurementCache()) {
Category category = measurement.getCategory();
int textColor = Color.black;
if (category == Category.BLOODSUGAR && config.highlightLimits()) {
BloodSugar bloodSugar = (BloodSugar) measurement;
float value = bloodSugar.getMgDl();
if (value > PreferenceStore.getInstance().getLimitHyperglycemia()) {
textColor = cache.getColorHyperglycemia();
} else if (value < PreferenceStore.getInstance().getLimitHypoglycemia()) {
textColor = cache.getColorHypoglycemia();
}
}
String measurementText = measurement.print(context);
if (category == Category.MEAL && config.exportFood()) {
List<String> foodOfDay = new ArrayList<>();
Meal meal = (Meal) MeasurementDao.getInstance(Meal.class).getMeasurement(entry);
if (meal != null) {
for (FoodEaten foodEaten : FoodEatenDao.getInstance().getAll(meal)) {
String foodNote = foodEaten.print();
if (foodNote != null) {
foodOfDay.add(foodNote);
}
}
}
if (!foodOfDay.isEmpty()) {
String foodText = TextUtils.join(", ", foodOfDay);
measurementText = String.format("%s\n%s", measurementText, foodText);
}
}
data.add(getRow(cache, data.size() == oldSize ? time : null, context.getString(category.getStringAcronymResId()), measurementText, backgroundColor, textColor));
}
if (config.exportTags()) {
List<EntryTag> entryTags = EntryTagDao.getInstance().getAll(entry);
if (!entryTags.isEmpty()) {
List<String> tagNames = new ArrayList<>();
for (EntryTag entryTag : entryTags) {
Tag tag = entryTag.getTag();
if (tag != null) {
String tagName = tag.getName();
if (!StringUtils.isBlank(tagName)) {
tagNames.add(tagName);
}
}
}
data.add(getRow(cache, data.size() == oldSize ? time : null, context.getString(R.string.tags), TextUtils.join(", ", tagNames), backgroundColor));
}
}
if (config.exportNotes()) {
if (!StringUtils.isBlank(entry.getNote())) {
data.add(getRow(cache, data.size() == oldSize ? time : null, context.getString(R.string.note), entry.getNote(), backgroundColor));
}
}
rowIndex++;
}
boolean hasData = data.size() > 1;
if (!hasData) {
data.add(CellFactory.createEmptyRow(cache));
}
try {
table.setData(data);
} catch (Exception exception) {
Log.e(TAG, exception.toString());
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.
the class CsvImport method importFromVersion1_1.
@SuppressWarnings("ParameterCanBeLocal")
private void importFromVersion1_1(CSVReader reader, String[] nextLine) throws Exception {
Entry entry = null;
while ((nextLine = reader.readNext()) != null) {
String key = nextLine[0];
if (key.equalsIgnoreCase(Entry.BACKUP_KEY)) {
entry = new Entry();
entry.setDate(DateTimeFormat.forPattern(Export.BACKUP_DATE_FORMAT).parseDateTime(nextLine[1]));
String note = nextLine[2];
entry.setNote(note != null && note.length() > 0 ? note : null);
entry = EntryDao.getInstance().createOrUpdate(entry);
} else if (key.equalsIgnoreCase(Measurement.BACKUP_KEY) && entry != null) {
try {
CategoryDeprecated categoryDeprecated = Helper.valueOf(CategoryDeprecated.class, nextLine[2]);
Category category = categoryDeprecated.toUpdate();
Measurement measurement = category.toClass().newInstance();
measurement.setValues(FloatUtils.parseNumber(nextLine[1]));
measurement.setEntry(entry);
MeasurementDao.getInstance(category.toClass()).createOrUpdate(measurement);
} catch (InstantiationException exception) {
Log.e(TAG, exception.toString());
} catch (IllegalAccessException exception) {
Log.e(TAG, exception.toString());
}
}
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.
the class MeasurementAverageTask method doInBackground.
@Override
protected LineData doInBackground(Void... params) {
List<Entry> entries = new ArrayList<>();
DateTime endDateTime = DateTimeUtils.atEndOfDay(DateTime.now());
DateTime startDateTime = timeSpan.getInterval(endDateTime, -1).getStart();
startDateTime = startDateTime.withTimeAtStartOfDay();
int index = 0;
float highestValue = 0;
DateTime intervalStart = startDateTime;
while (!intervalStart.isAfter(endDateTime)) {
DateTime intervalEnd = timeSpan.getStep(intervalStart, 1).minusMillis(1);
Measurement measurement = MeasurementDao.getInstance(category.toClass()).getAvgMeasurement(category, new Interval(intervalStart, intervalEnd));
if (measurement != null) {
for (Float avg : measurement.getValues()) {
if (FloatUtils.isValid(avg) && avg > 0) {
float yValue = PreferenceStore.getInstance().formatDefaultToCustomUnit(category, avg);
entries.add(new Entry(index, yValue));
if (yValue > highestValue) {
highestValue = yValue;
}
}
}
}
intervalStart = timeSpan.getStep(intervalStart, 1);
index++;
}
ArrayList<ILineDataSet> dataSets = new ArrayList<>();
if (entries.size() > 0) {
LineDataSet dataSet = new LineDataSet(entries, "Blood sugar average per day");
dataSet.setColor(dataSetColor);
dataSet.setLineWidth(fillDrawing ? 0 : ChartUtils.LINE_WIDTH);
dataSet.setCircleColor(dataSetColor);
dataSet.setCircleRadius(CIRCLE_RADIUS);
dataSet.setDrawCircles(entries.size() <= 1);
dataSet.setDrawValues(false);
dataSet.setDrawFilled(fillDrawing);
dataSet.setFillColor(dataSetColor);
dataSets.add(dataSet);
}
// Workaround to set visible area
if (forceDrawing) {
if (category == Category.BLOODSUGAR) {
float targetValue = PreferenceStore.getInstance().formatDefaultToCustomUnit(Category.BLOODSUGAR, PreferenceStore.getInstance().getTargetValue());
targetValue = targetValue * 2;
if (highestValue == 0 || highestValue < targetValue) {
highestValue = targetValue;
}
}
List<com.github.mikephil.charting.data.Entry> entriesMaximum = new ArrayList<>();
entriesMaximum.add(new com.github.mikephil.charting.data.Entry(-1, highestValue));
LineDataSet dataSetMaximum = new LineDataSet(entriesMaximum, "Fake");
dataSetMaximum.setColor(Color.TRANSPARENT);
dataSets.add(dataSetMaximum);
}
try {
return new LineData(dataSets);
} catch (IllegalArgumentException exception) {
Log.e(TAG, exception.getMessage());
return null;
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.Measurement in project Diaguard by Faltenreich.
the class DatabaseHelper method upgradeToVersion19.
private <M extends Measurement> void upgradeToVersion19(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
List<Entry> entries = new ArrayList<>();
String entryQuery = String.format("SELECT * FROM %s", DatabaseHelper.ENTRY);
Cursor cursor = sqliteDatabase.rawQuery(entryQuery, null);
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
try {
Entry entry = new Entry();
entry.setId(Long.parseLong(cursor.getString(0)));
entry.setDate(DateTimeFormat.forPattern(DATE_TIME_FORMAT_1_1).parseDateTime(cursor.getString(1)));
entry.setNote(cursor.getString(2));
entries.add(entry);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
cursor.moveToNext();
}
}
cursor.close();
HashMap<Entry, List<M>> entities = new HashMap<>();
for (Entry entry : entries) {
List<M> measurements = new ArrayList<>();
String measurementQuery = String.format("SELECT * FROM %s WHERE %s = %d", DatabaseHelper.MEASUREMENT, ENTRY_ID, entry.getId());
cursor = sqliteDatabase.rawQuery(measurementQuery, null);
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
try {
String categoryString = cursor.getString(2);
CategoryDeprecated categoryDeprecated = Helper.valueOf(CategoryDeprecated.class, categoryString);
Category category = categoryDeprecated.toUpdate();
M measurement = (M) category.toClass().newInstance();
float value = FloatUtils.parseNumber(cursor.getString(1));
float[] values = new float[measurement.getValues().length];
values[0] = value;
measurement.setValues(values);
measurements.add(measurement);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
cursor.moveToNext();
}
}
entities.put(entry, measurements);
cursor.close();
}
sqliteDatabase.execSQL("DROP TABLE IF EXISTS " + ENTRY);
sqliteDatabase.execSQL("DROP TABLE IF EXISTS " + MEASUREMENT);
sqliteDatabase.execSQL("DROP TABLE IF EXISTS " + FOOD);
sqliteDatabase.execSQL("DROP TABLE IF EXISTS " + FOOD_EATEN);
onCreate(sqliteDatabase, connectionSource);
for (Map.Entry<Entry, List<M>> mapEntry : entities.entrySet()) {
Entry tempEntry = mapEntry.getKey();
tempEntry.setId(-1);
Entry entry = EntryDao.getInstance().createOrUpdate(tempEntry);
for (Measurement measurement : mapEntry.getValue()) {
measurement.setId(-1);
measurement.setEntry(entry);
MeasurementDao.getInstance(measurement.getClass()).createOrUpdate(measurement);
}
}
}
Aggregations