Search in sources :

Example 21 with Category

use of com.faltenreich.diaguard.shared.data.database.entity.Category 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());
            }
        }
    }
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) CategoryDeprecated(com.faltenreich.diaguard.shared.data.database.entity.deprecated.CategoryDeprecated)

Example 22 with Category

use of com.faltenreich.diaguard.shared.data.database.entity.Category in project Diaguard by Faltenreich.

the class StatisticFragment method initCategory.

private void initCategory() {
    Spinner spinner = getBinding().categorySpinner;
    Category[] categories = PreferenceStore.getInstance().getActiveCategories();
    List<String> categoryNames = new ArrayList<>();
    for (Category category : categories) {
        categoryNames.add(getString(category.getStringResId()));
    }
    ArrayAdapter<String> categoryAdapter = new ArrayAdapter<>(requireContext(), android.R.layout.simple_spinner_item, categoryNames);
    categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(categoryAdapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            changeCategory(categories[position]);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
}
Also used : Category(com.faltenreich.diaguard.shared.data.database.entity.Category) Spinner(android.widget.Spinner) ArrayList(java.util.ArrayList) View(android.view.View) AdapterView(android.widget.AdapterView) AdapterView(android.widget.AdapterView) ArrayAdapter(android.widget.ArrayAdapter)

Example 23 with Category

use of com.faltenreich.diaguard.shared.data.database.entity.Category in project Diaguard by Faltenreich.

the class TimelineDayFragment method invalidateList.

public void invalidateList() {
    if (isAdded() && !data.needsListData()) {
        Log.d(TAG, "Invalidating view for list on " + data.getDay().toString());
        List<CategoryValueListItem> valueListItems = data.getListData();
        if (valueAdapter.getItemCount() > 0) {
            for (int index = 0; index < valueListItems.size(); index++) {
                CategoryValueListItem listItem = valueListItems.get(index);
                RecyclerView.ViewHolder viewHolder = valueListView.findViewHolderForAdapterPosition(index);
                if (viewHolder instanceof CategoryValueViewHolder) {
                    valueAdapter.setItem(listItem, index);
                    // We access the ViewHolder directly for better performance compared to notifyItem(Range)Changed
                    CategoryValueViewHolder categoryValueViewHolder = (CategoryValueViewHolder) viewHolder;
                    categoryValueViewHolder.bind(listItem);
                }
            }
        } else {
            for (Category category : categories) {
                imageAdapter.addItem(new CategoryImageListItem(category));
            }
            imageAdapter.notifyDataSetChanged();
            // Other notify methods lead to rendering issues on view paging
            valueAdapter.addItems(valueListItems);
            valueAdapter.notifyDataSetChanged();
        }
    }
}
Also used : CategoryImageListItem(com.faltenreich.diaguard.feature.timeline.table.CategoryImageListItem) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) RecyclerView(androidx.recyclerview.widget.RecyclerView) CategoryValueListItem(com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem) CategoryValueViewHolder(com.faltenreich.diaguard.feature.timeline.table.CategoryValueViewHolder)

Example 24 with Category

use of com.faltenreich.diaguard.shared.data.database.entity.Category 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);
        }
    }
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CategoryDeprecated(com.faltenreich.diaguard.shared.data.database.entity.deprecated.CategoryDeprecated) Cursor(android.database.Cursor) SQLException(java.sql.SQLException) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with Category

use of com.faltenreich.diaguard.shared.data.database.entity.Category in project Diaguard by Faltenreich.

the class EntryDao method getAverageDataTable.

/**
 * @return HashMap with non-null but zeroed and default values for given categories and time periods
 */
public LinkedHashMap<Category, CategoryValueListItem[]> getAverageDataTable(DateTime day, Category[] categories, int hoursToSkip) {
    if (day == null) {
        return new LinkedHashMap<>();
    }
    int indices = DateTimeConstants.HOURS_PER_DAY / hoursToSkip;
    // Key: Category, Value: Fixed-size array of values per hour-index
    LinkedHashMap<Category, CategoryValueListItem[]> values = new LinkedHashMap<>();
    for (Category category : categories) {
        values.put(category, new CategoryValueListItem[indices]);
    }
    for (Category category : categories) {
        // Key: Hour-index, Value: Values of hour-index
        LinkedHashMap<Integer, List<CategoryValueListItem>> valuesOfHours = new LinkedHashMap<>();
        for (int index = 0; index < indices; index++) {
            valuesOfHours.put(index, new ArrayList<>());
        }
        List<Measurement> measurements = MeasurementDao.getInstance(category.toClass()).getMeasurements(day);
        for (Measurement measurement : measurements) {
            int index = measurement.getEntry().getDate().hourOfDay().get() / hoursToSkip;
            CategoryValueListItem item = new CategoryValueListItem(category);
            switch(category) {
                case INSULIN:
                    Insulin insulin = (Insulin) measurement;
                    item.setValueOne(insulin.getBolus());
                    item.setValueTwo(insulin.getCorrection());
                    item.setValueThree(insulin.getBasal());
                    break;
                case PRESSURE:
                    Pressure pressure = (Pressure) measurement;
                    item.setValueOne(pressure.getSystolic());
                    item.setValueTwo(pressure.getDiastolic());
                    break;
                default:
                    float value = category.stackValues() ? ArrayUtils.sum(measurement.getValues()) : ArrayUtils.avg(measurement.getValues());
                    if (category == Category.MEAL) {
                        for (FoodEaten foodEaten : ((Meal) measurement).getFoodEaten()) {
                            value += foodEaten.getCarbohydrates();
                        }
                    }
                    item.setValueOne(value);
                    break;
            }
            List<CategoryValueListItem> valuesOfHour = valuesOfHours.get(index);
            if (valuesOfHour == null) {
                valuesOfHours.put(index, new ArrayList<>());
            }
            valuesOfHours.get(index).add(item);
        }
        // Average for old values
        for (int index = 0; index < indices; index++) {
            List<CategoryValueListItem> valuesOfHour = valuesOfHours.get(index);
            CategoryValueListItem value = category.stackValues() ? CategoryListItemUtils.sum(category, valuesOfHour) : CategoryListItemUtils.avg(category, valuesOfHour);
            values.get(category)[index] = value;
        }
    }
    return values;
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) Meal(com.faltenreich.diaguard.shared.data.database.entity.Meal) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) CategoryValueListItem(com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem) Insulin(com.faltenreich.diaguard.shared.data.database.entity.Insulin) Pressure(com.faltenreich.diaguard.shared.data.database.entity.Pressure) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Category (com.faltenreich.diaguard.shared.data.database.entity.Category)31 ArrayList (java.util.ArrayList)18 Measurement (com.faltenreich.diaguard.shared.data.database.entity.Measurement)15 Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)13 List (java.util.List)10 EntryTag (com.faltenreich.diaguard.shared.data.database.entity.EntryTag)7 FoodEaten (com.faltenreich.diaguard.shared.data.database.entity.FoodEaten)7 Meal (com.faltenreich.diaguard.shared.data.database.entity.Meal)7 Tag (com.faltenreich.diaguard.shared.data.database.entity.Tag)7 View (android.view.View)6 ImageView (android.widget.ImageView)5 CategoryValueListItem (com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem)5 Context (android.content.Context)4 Point (com.pdfjet.Point)4 DateTime (org.joda.time.DateTime)4 LayoutInflater (android.view.LayoutInflater)3 LinearLayout (android.widget.LinearLayout)3 TextView (android.widget.TextView)3 CellBuilder (com.faltenreich.diaguard.feature.export.job.pdf.view.CellBuilder)3 BloodSugar (com.faltenreich.diaguard.shared.data.database.entity.BloodSugar)3