Search in sources :

Example 1 with FavItemBd

use of forpdateam.ru.forpda.data.realm.favorites.FavItemBd 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)

Example 2 with FavItemBd

use of forpdateam.ru.forpda.data.realm.favorites.FavItemBd in project ForPDA by RadiationX.

the class FavoritesPresenter method saveFavorites.

@Override
public void saveFavorites(List<FavItem> items) {
    if (realm.isClosed())
        return;
    realm.executeTransactionAsync(r -> {
        r.delete(FavItemBd.class);
        List<FavItemBd> bdList = new ArrayList<>();
        for (FavItem item : items) {
            bdList.add(new FavItemBd(item));
        }
        r.copyToRealmOrUpdate(bdList);
        bdList.clear();
    }, this::showFavorites);
}
Also used : IFavItem(forpdateam.ru.forpda.api.favorites.interfaces.IFavItem) FavItem(forpdateam.ru.forpda.api.favorites.models.FavItem) ArrayList(java.util.ArrayList) FavItemBd(forpdateam.ru.forpda.data.realm.favorites.FavItemBd)

Aggregations

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