Search in sources :

Example 1 with DayChartData

use of com.faltenreich.diaguard.feature.timeline.chart.DayChartData in project Diaguard by Faltenreich.

the class TimelineDayFragment method invalidateData.

private void invalidateData() {
    if (getContext() == null) {
        return;
    }
    if (data.needsChartData()) {
        Log.d(TAG, "Invalidating data for chart on " + data.getDay().toString());
        DataLoader.getInstance().load(getContext(), new DataLoaderListener<DayChartData>() {

            @Override
            public DayChartData onShouldLoad(Context context) {
                List<Measurement> values = new ArrayList<>();
                List<Entry> entries = EntryDao.getInstance().getEntriesOfDay(data.getDay());
                if (entries != null && entries.size() > 0) {
                    for (Entry entry : entries) {
                        // TODO: Improve performance by using transaction / bulk fetch
                        List<Measurement> measurements = EntryDao.getInstance().getMeasurements(entry, new Category[] { Category.BLOODSUGAR });
                        values.addAll(measurements);
                    }
                }
                return new DayChartData(context, PreferenceStore.getInstance().showDotsInTimeline(), PreferenceStore.getInstance().showLinesInTimeline(), values);
            }

            @Override
            public void onDidLoad(DayChartData chartData) {
                data.setChartData(chartData);
                invalidateChart();
            }
        });
    } else {
        invalidateChart();
    }
    if (data.needsListData()) {
        Log.d(TAG, "Invalidating data for list on " + data.getDay().toString());
        DataLoader.getInstance().load(getContext(), new DataLoaderListener<List<CategoryValueListItem>>() {

            @Override
            public List<CategoryValueListItem> onShouldLoad(Context context) {
                List<CategoryValueListItem> listItems = new ArrayList<>();
                LinkedHashMap<Category, CategoryValueListItem[]> values = EntryDao.getInstance().getAverageDataTable(data.getDay(), categories, SKIP_EVERY_X_HOUR);
                for (Map.Entry<Category, CategoryValueListItem[]> mapEntry : values.entrySet()) {
                    Collections.addAll(listItems, mapEntry.getValue());
                }
                return listItems;
            }

            @Override
            public void onDidLoad(List<CategoryValueListItem> listData) {
                data.setListData(listData);
                invalidateList();
            }
        });
    } else {
        invalidateList();
    }
}
Also used : Context(android.content.Context) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) DayChartData(com.faltenreich.diaguard.feature.timeline.chart.DayChartData) ArrayList(java.util.ArrayList) List(java.util.List) CategoryValueListItem(com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with DayChartData

use of com.faltenreich.diaguard.feature.timeline.chart.DayChartData in project Diaguard by Faltenreich.

the class TimelineDayFragment method invalidateChart.

private void invalidateChart() {
    if (isAdded() && !data.needsChartData()) {
        Log.d(TAG, "Invalidating view for chart on " + data.getDay().toString());
        DayChartData chartData = data.getChartData();
        chartView.setData(chartData);
        chartView.getAxisLeft().setAxisMaximum(chartData.getYAxisMaximum());
        // Workaround: Fixes invalidation within ViewPager
        // https://github.com/PhilJay/MPAndroidChart/issues/1274
        // FIXME: Leads to jumping chart view
        chartView.invalidate();
    }
}
Also used : DayChartData(com.faltenreich.diaguard.feature.timeline.chart.DayChartData)

Aggregations

DayChartData (com.faltenreich.diaguard.feature.timeline.chart.DayChartData)2 Context (android.content.Context)1 CategoryValueListItem (com.faltenreich.diaguard.feature.timeline.table.CategoryValueListItem)1 Category (com.faltenreich.diaguard.shared.data.database.entity.Category)1 Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1