Search in sources :

Example 1 with IFavItem

use of forpdateam.ru.forpda.api.favorites.interfaces.IFavItem in project ForPDA by RadiationX.

the class FavoritesPresenter method handleEvent.

@Override
public void handleEvent(TabNotification event, Sorting sorting, int count) {
    if (event.isWebSocket() && event.getEvent().isNew())
        return;
    RealmResults<FavItemBd> results = realm.where(FavItemBd.class).findAll();
    ArrayList<IFavItem> currentItems = new ArrayList<>();
    for (FavItemBd itemBd : results) {
        currentItems.add(new FavItem(itemBd));
    }
    NotificationEvent loadedEvent = event.getEvent();
    int id = loadedEvent.getSourceId();
    boolean isRead = loadedEvent.isRead();
    if (isRead) {
        count--;
        for (IFavItem item : currentItems) {
            if (item.getTopicId() == id) {
                item.setNew(false);
                break;
            }
        }
    } else {
        count = event.getLoadedEvents().size();
        for (IFavItem item : currentItems) {
            if (item.getTopicId() == id) {
                if (item.getLastUserId() != ClientHelper.getUserId())
                    item.setNew(true);
                item.setLastUserNick(loadedEvent.getUserNick());
                item.setLastUserId(loadedEvent.getUserId());
                item.setPin(loadedEvent.isImportant());
                break;
            }
        }
        if (sorting.getKey().equals(Sorting.Key.TITLE)) {
            Collections.sort(currentItems, (o1, o2) -> {
                if (sorting.getOrder().equals(Sorting.Order.ASC))
                    return o1.getTopicTitle().compareToIgnoreCase(o2.getTopicTitle());
                return o2.getTopicTitle().compareToIgnoreCase(o1.getTopicTitle());
            });
        }
        if (sorting.getKey().equals(Sorting.Key.LAST_POST)) {
            for (IFavItem item : currentItems) {
                if (item.getTopicId() == id) {
                    currentItems.remove(item);
                    int index = 0;
                    if (sorting.getOrder().equals(Sorting.Order.ASC)) {
                        index = currentItems.size();
                    }
                    currentItems.add(index, item);
                    break;
                }
            }
        }
    }
    view.onHandleEvent(count);
}
Also used : IFavItem(forpdateam.ru.forpda.api.favorites.interfaces.IFavItem) FavItem(forpdateam.ru.forpda.api.favorites.models.FavItem) IFavItem(forpdateam.ru.forpda.api.favorites.interfaces.IFavItem) ArrayList(java.util.ArrayList) NotificationEvent(forpdateam.ru.forpda.api.events.models.NotificationEvent) FavItemBd(forpdateam.ru.forpda.data.realm.favorites.FavItemBd)

Aggregations

NotificationEvent (forpdateam.ru.forpda.api.events.models.NotificationEvent)1 IFavItem (forpdateam.ru.forpda.api.favorites.interfaces.IFavItem)1 FavItem (forpdateam.ru.forpda.api.favorites.models.FavItem)1 FavItemBd (forpdateam.ru.forpda.data.realm.favorites.FavItemBd)1 ArrayList (java.util.ArrayList)1