use of com.faltenreich.diaguard.shared.data.database.entity.Category in project Diaguard by Faltenreich.
the class CsvImport method importFromVersion3_0.
@SuppressWarnings("ParameterCanBeLocal")
private void importFromVersion3_0(CSVReader reader, String[] nextLine) throws Exception {
Entry lastEntry = null;
Meal lastMeal = null;
while ((nextLine = reader.readNext()) != null) {
switch(nextLine[0]) {
case Tag.BACKUP_KEY:
if (nextLine.length >= 2) {
String tagName = nextLine[1];
if (TagDao.getInstance().getByName(tagName) == null) {
Tag tag = new Tag();
tag.setName(nextLine[1]);
TagDao.getInstance().createOrUpdate(tag);
}
}
break;
case Food.BACKUP_KEY:
if (nextLine.length >= 5) {
String foodName = nextLine[1];
Food food = FoodDao.getInstance().get(foodName);
if (food == null) {
food = new Food();
food.setName(foodName);
food.setBrand(nextLine[2]);
food.setIngredients(nextLine[3]);
food.setCarbohydrates(FloatUtils.parseNumber(nextLine[4]));
FoodDao.getInstance().createOrUpdate(food);
} else if (food.isDeleted()) {
// Reactivate previously deleted food that is being re-imported
food.setDeletedAt(null);
FoodDao.getInstance().createOrUpdate(food);
}
}
break;
case Entry.BACKUP_KEY:
lastMeal = null;
if (nextLine.length >= 3) {
DateTime parsedDateTime = DateTimeFormat.forPattern(Export.BACKUP_DATE_FORMAT).parseDateTime(nextLine[1]);
DateStrategy dateStrategy = this.dateStrategy != null ? this.dateStrategy : defaultDateStrategy;
DateTime dateTime = dateStrategy.convertDate(parsedDateTime);
lastEntry = new Entry();
lastEntry.setDate(dateTime);
String note = nextLine[2];
lastEntry.setNote(note != null && note.length() > 0 ? note : null);
lastEntry = EntryDao.getInstance().createOrUpdate(lastEntry);
break;
}
case Measurement.BACKUP_KEY:
if (lastEntry != null && nextLine.length >= 3) {
Category category = Helper.valueOf(Category.class, nextLine[1]);
if (category != null) {
try {
Measurement measurement = category.toClass().newInstance();
List<Float> valueList = new ArrayList<>();
for (int position = 2; position < nextLine.length; position++) {
String valueString = nextLine[position];
try {
valueList.add(FloatUtils.parseNumber(valueString));
} catch (NumberFormatException exception) {
Log.e(TAG, exception.toString());
}
}
float[] values = new float[valueList.size()];
for (int position = 0; position < valueList.size(); position++) {
values[position] = valueList.get(position);
}
measurement.setValues(values);
measurement.setEntry(lastEntry);
MeasurementDao.getInstance(category.toClass()).createOrUpdate(measurement);
if (measurement instanceof Meal) {
lastMeal = (Meal) measurement;
}
} catch (InstantiationException exception) {
Log.e(TAG, exception.toString());
} catch (IllegalAccessException exception) {
Log.e(TAG, exception.toString());
}
}
}
break;
case EntryTag.BACKUP_KEY:
if (lastEntry != null && nextLine.length >= 2) {
Tag tag = TagDao.getInstance().getByName(nextLine[1]);
if (tag != null) {
EntryTag entryTag = new EntryTag();
entryTag.setEntry(lastEntry);
entryTag.setTag(tag);
EntryTagDao.getInstance().createOrUpdate(entryTag);
}
}
break;
case FoodEaten.BACKUP_KEY:
if (lastMeal != null && nextLine.length >= 3) {
Food food = FoodDao.getInstance().get(nextLine[1]);
if (food != null) {
FoodEaten foodEaten = new FoodEaten();
foodEaten.setMeal(lastMeal);
foodEaten.setFood(food);
foodEaten.setAmountInGrams(FloatUtils.parseNumber(nextLine[2]));
FoodEatenDao.getInstance().createOrUpdate(foodEaten);
}
}
break;
}
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.Category in project Diaguard by Faltenreich.
the class CsvImport method importFromVersion1_0.
private void importFromVersion1_0(CSVReader reader, String[] nextLine) throws Exception {
while (nextLine != null) {
Entry 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);
EntryDao.getInstance().createOrUpdate(entry);
try {
CategoryDeprecated categoryDeprecated = Helper.valueOf(CategoryDeprecated.class, nextLine[2]);
Category category = categoryDeprecated.toUpdate();
Measurement measurement = category.toClass().newInstance();
measurement.setValues(FloatUtils.parseNumber(nextLine[0]));
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());
}
nextLine = reader.readNext();
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.Category in project Diaguard by Faltenreich.
the class StatisticTest method onCategorySelected_showsCorrectContent.
@Test
public void onCategorySelected_showsCorrectContent() {
for (Category category : Category.values()) {
selectCategory(category);
String label = ApplicationProvider.getApplicationContext().getString(category.getStringResId());
Espresso.onView(ViewMatchers.withId(R.id.category_spinner)).check(ViewAssertions.matches(ViewMatchers.withSpinnerText(label)));
Espresso.onView(ViewMatchers.withText(R.string.average)).check(ViewAssertions.matches(ViewMatchers.withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
Espresso.onView(ViewMatchers.withText(R.string.trend)).check(ViewAssertions.matches(ViewMatchers.withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
if (category == Category.BLOODSUGAR) {
Espresso.onView(ViewMatchers.withText(R.string.distribution)).check(ViewAssertions.matches(ViewMatchers.withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
}
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.Category in project Diaguard by Faltenreich.
the class CategorySerializer method deserialize.
@Override
@Nullable
public Category[] deserialize(String string) {
if (string != null) {
String[] ids = string.split(";");
List<Category> categories = new ArrayList<>();
for (String id : ids) {
try {
int stableId = Integer.parseInt(id);
Category category = Category.fromStableId(stableId);
if (category != null) {
categories.add(category);
}
} catch (NumberFormatException exception) {
Log.e(TAG, exception.getMessage());
}
}
return categories.toArray(new Category[categories.size()]);
} else {
return null;
}
}
use of com.faltenreich.diaguard.shared.data.database.entity.Category in project Diaguard by Faltenreich.
the class PdfTable method init.
private void init() {
PdfExportConfig config = cache.getConfig();
Context context = config.getContext();
List<List<Cell>> data = new ArrayList<>();
List<Cell> cells = new ArrayList<>();
Cell headerCell = new CellBuilder(new Cell(cache.getFontBold())).setWidth(getLabelWidth()).setText(DateTimeUtils.toWeekDayAndDate(cache.getDateTime())).build();
cells.add(headerCell);
float cellWidth = (cache.getPage().getWidth() - getLabelWidth()) / (DateTimeConstants.HOURS_PER_DAY / 2f);
for (int hour = 0; hour < DateTimeConstants.HOURS_PER_DAY; hour += PdfTable.HOURS_TO_SKIP) {
Cell hourCell = new CellBuilder(new Cell(cache.getFontNormal())).setWidth(cellWidth).setText(Integer.toString(hour)).setForegroundColor(Color.gray).setTextAlignment(Align.CENTER).build();
cells.add(hourCell);
}
data.add(cells);
LinkedHashMap<Category, CategoryValueListItem[]> values = EntryDao.getInstance().getAverageDataTable(cache.getDateTime(), config.getCategories(), HOURS_TO_SKIP);
int rowIndex = 0;
for (Category category : values.keySet()) {
CategoryValueListItem[] items = values.get(category);
if (items != null) {
String label = context.getString(category.getStringAcronymResId());
int backgroundColor = rowIndex % 2 == 0 ? cache.getColorDivider() : Color.white;
switch(category) {
case INSULIN:
if (config.splitInsulin()) {
data.add(createMeasurementRows(cache, items, cellWidth, 0, label + " " + context.getString(R.string.bolus), backgroundColor));
data.add(createMeasurementRows(cache, items, cellWidth, 1, label + " " + context.getString(R.string.correction), backgroundColor));
data.add(createMeasurementRows(cache, items, cellWidth, 2, label + " " + context.getString(R.string.basal), backgroundColor));
} else {
data.add(createMeasurementRows(cache, items, cellWidth, -1, label, backgroundColor));
}
break;
case PRESSURE:
data.add(createMeasurementRows(cache, items, cellWidth, 0, label + " " + context.getString(R.string.systolic_acronym), backgroundColor));
data.add(createMeasurementRows(cache, items, cellWidth, 1, label + " " + context.getString(R.string.diastolic_acronym), backgroundColor));
break;
default:
data.add(createMeasurementRows(cache, items, cellWidth, 0, label, backgroundColor));
break;
}
rowIndex++;
}
}
if (config.exportNotes() || config.exportTags() || config.exportFood()) {
List<PdfNote> pdfNotes = new ArrayList<>();
for (Entry entry : entriesOfDay) {
PdfNote pdfNote = PdfNoteFactory.createNote(config, entry);
if (pdfNote != null) {
pdfNotes.add(pdfNote);
}
}
data.addAll(CellFactory.createRowsForNotes(cache, pdfNotes, getLabelWidth()));
}
boolean hasData = data.size() > 1;
if (!hasData) {
data.add(CellFactory.createEmptyRow(cache));
}
try {
table.setData(data);
} catch (Exception exception) {
Log.e(TAG, exception.toString());
}
}
Aggregations