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);
}
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);
}
Aggregations