Search in sources :

Example 1 with LogEntryListItem

use of com.faltenreich.diaguard.feature.log.entry.LogEntryListItem in project Diaguard by Faltenreich.

the class LogFragment method updateHeaderSection.

private void updateHeaderSection(DateTime dateTime) {
    int position = listAdapter.getFirstListItemEntryOfDayPosition(dateTime);
    if (position >= 0) {
        LogEntryListItem firstListItemEntry = (LogEntryListItem) listAdapter.getItem(position);
        while (listAdapter.getItem(position).getDateTime().withTimeAtStartOfDay().isEqual(dateTime.withTimeAtStartOfDay()) && listAdapter.getItem(position) instanceof LogEntryListItem) {
            LogEntryListItem listItem = (LogEntryListItem) listAdapter.getItem(position);
            listItem.setFirstListItemEntryOfDay(firstListItemEntry);
            position++;
        }
    }
    invalidateSections();
}
Also used : LogEntryListItem(com.faltenreich.diaguard.feature.log.entry.LogEntryListItem)

Example 2 with LogEntryListItem

use of com.faltenreich.diaguard.feature.log.entry.LogEntryListItem in project Diaguard by Faltenreich.

the class LogFragment method addEntry.

private void addEntry(Entry entry, List<EntryTag> entryTags, List<FoodEaten> foodEatenList) {
    if (entry != null) {
        DateTime date = entry.getDate();
        int position = listAdapter.getNextDateTimePosition(date);
        if (position >= 0) {
            // Remove any existing empty view
            int previousPosition = position - 1;
            LogListItem previousListItem = listAdapter.getItem(previousPosition);
            if (previousListItem instanceof LogEmptyListItem && previousListItem.getDateTime().getDayOfYear() == date.getDayOfYear()) {
                listAdapter.removeItem(previousPosition);
                listAdapter.notifyItemRemoved(previousPosition);
                position = previousPosition;
            }
            entry.setMeasurementCache(EntryDao.getInstance().getMeasurements(entry));
            LogEntryListItem listItemEntry = new LogEntryListItem(entry, entryTags, foodEatenList);
            listAdapter.addItem(position, listItemEntry);
            listAdapter.notifyItemInserted(position);
            updateHeaderSection(date);
        }
    }
}
Also used : LogEmptyListItem(com.faltenreich.diaguard.feature.log.empty.LogEmptyListItem) LogEntryListItem(com.faltenreich.diaguard.feature.log.entry.LogEntryListItem) DateTime(org.joda.time.DateTime)

Example 3 with LogEntryListItem

use of com.faltenreich.diaguard.feature.log.entry.LogEntryListItem in project Diaguard by Faltenreich.

the class LogSwipeCallback method onSwiped.

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    Object item = adapter.getItem(viewHolder.getAdapterPosition());
    if (item instanceof LogEntryListItem) {
        LogEntryListItem listItem = (LogEntryListItem) item;
        Entry entry = listItem.getEntry();
        EntryDao.getInstance().delete(entry);
        Events.post(new EntryDeletedEvent(entry, listItem.getEntryTags(), listItem.getFoodEatenList()));
    }
}
Also used : Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) LogEntryListItem(com.faltenreich.diaguard.feature.log.entry.LogEntryListItem) EntryDeletedEvent(com.faltenreich.diaguard.shared.event.data.EntryDeletedEvent)

Example 4 with LogEntryListItem

use of com.faltenreich.diaguard.feature.log.entry.LogEntryListItem in project Diaguard by Faltenreich.

the class LogFetchDataTask method getListItems.

private List<LogListItem> getListItems(DateTime startDate, boolean scrollingDown) {
    List<LogListItem> listItems = new ArrayList<>();
    DateTime date = startDate;
    boolean loadMore = true;
    while (loadMore) {
        List<Entry> entries = EntryDao.getInstance().getEntriesOfDay(date);
        for (Entry entry : entries) {
            List<Measurement> measurements = EntryDao.getInstance().getMeasurements(entry);
            entry.setMeasurementCache(measurements);
        }
        if (entries.size() > 0) {
            LogEntryListItem firstListItemEntryOfDay = null;
            for (int entryIndex = 0; entryIndex < entries.size(); entryIndex++) {
                Entry entry = entries.get(entryIndex);
                List<EntryTag> entryTags = EntryTagDao.getInstance().getAll(entry);
                List<FoodEaten> foodEaten = new ArrayList<>();
                for (Measurement measurement : entry.getMeasurementCache()) {
                    if (measurement instanceof Meal) {
                        foodEaten.addAll(FoodEatenDao.getInstance().getAll((Meal) measurement));
                    }
                }
                LogEntryListItem listItemEntry = new LogEntryListItem(entry, entryTags, foodEaten);
                if (entryIndex == 0) {
                    firstListItemEntryOfDay = listItemEntry;
                }
                listItemEntry.setFirstListItemEntryOfDay(firstListItemEntryOfDay);
                listItems.add(scrollingDown ? listItems.size() : entryIndex, listItemEntry);
            }
        } else {
            listItems.add(scrollingDown ? listItems.size() : 0, new LogEmptyListItem(date));
        }
        boolean isFirstDayOfMonth = date.dayOfMonth().get() == 1;
        if (isFirstDayOfMonth) {
            listItems.add(scrollingDown ? listItems.size() - 1 : 0, new LogMonthListItem(date));
        }
        loadMore = listItems.size() < (EndlessAdapter.BULK_SIZE);
        date = scrollingDown ? date.plusDays(1) : date.minusDays(1);
    }
    return listItems;
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) LogEmptyListItem(com.faltenreich.diaguard.feature.log.empty.LogEmptyListItem) EntryTag(com.faltenreich.diaguard.shared.data.database.entity.EntryTag) Meal(com.faltenreich.diaguard.shared.data.database.entity.Meal) ArrayList(java.util.ArrayList) FoodEaten(com.faltenreich.diaguard.shared.data.database.entity.FoodEaten) LogMonthListItem(com.faltenreich.diaguard.feature.log.month.LogMonthListItem) DateTime(org.joda.time.DateTime) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) LogEntryListItem(com.faltenreich.diaguard.feature.log.entry.LogEntryListItem)

Example 5 with LogEntryListItem

use of com.faltenreich.diaguard.feature.log.entry.LogEntryListItem in project Diaguard by Faltenreich.

the class LogFragment method updateEntry.

private void updateEntry(Entry entry, List<EntryTag> entryTags, List<FoodEaten> foodEatenList) {
    if (entry != null) {
        int originalPosition = listAdapter.getEntryPosition(entry);
        if (originalPosition >= 0) {
            int updatedPosition = listAdapter.getNextDateTimePosition(entry.getDate()) - 1;
            if (originalPosition == updatedPosition) {
                Object listItem = listAdapter.getItem(originalPosition);
                if (listItem instanceof LogEntryListItem) {
                    LogEntryListItem listItemEntry = (LogEntryListItem) listItem;
                    listItemEntry.setEntry(entry);
                    listItemEntry.setEntryTags(entryTags);
                    listItemEntry.setFoodEatenList(foodEatenList);
                    listAdapter.notifyItemChanged(originalPosition);
                }
            } else {
                removeEntry(originalPosition);
                addEntry(entry, entryTags, foodEatenList);
            }
        }
    }
}
Also used : LogEntryListItem(com.faltenreich.diaguard.feature.log.entry.LogEntryListItem)

Aggregations

LogEntryListItem (com.faltenreich.diaguard.feature.log.entry.LogEntryListItem)5 LogEmptyListItem (com.faltenreich.diaguard.feature.log.empty.LogEmptyListItem)2 Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)2 DateTime (org.joda.time.DateTime)2 LogMonthListItem (com.faltenreich.diaguard.feature.log.month.LogMonthListItem)1 EntryTag (com.faltenreich.diaguard.shared.data.database.entity.EntryTag)1 FoodEaten (com.faltenreich.diaguard.shared.data.database.entity.FoodEaten)1 Meal (com.faltenreich.diaguard.shared.data.database.entity.Meal)1 Measurement (com.faltenreich.diaguard.shared.data.database.entity.Measurement)1 EntryDeletedEvent (com.faltenreich.diaguard.shared.event.data.EntryDeletedEvent)1 ArrayList (java.util.ArrayList)1