use of forpdateam.ru.forpda.api.events.models.NotificationEvent in project ForPDA by RadiationX.
the class NotificationsService method compareEvents.
private List<NotificationEvent> compareEvents(List<NotificationEvent> savedEvents, List<NotificationEvent> loadedEvents, List<NotificationEvent> events, NotificationEvent.Source source) {
List<NotificationEvent> newEvents = new ArrayList<>();
for (NotificationEvent loaded : loadedEvents) {
boolean isNew = true;
for (NotificationEvent saved : savedEvents) {
if (loaded.getSourceId() == saved.getSourceId() && loaded.getTimeStamp() <= saved.getTimeStamp()) {
isNew = false;
break;
}
}
if (isNew) {
newEvents.add(loaded);
}
}
if (NotificationEvent.fromTheme(source) && Preferences.Notifications.Favorites.isOnlyImportant(getApplicationContext())) {
List<NotificationEvent> toRemove = new ArrayList<>();
for (NotificationEvent newEvent : newEvents) {
boolean remove = false;
for (NotificationEvent event : events) {
if (!event.isMention() && !newEvent.isImportant()) {
remove = true;
break;
}
}
if (!newEvent.isImportant()) {
remove = true;
}
if (remove) {
toRemove.add(newEvent);
}
}
for (NotificationEvent removeEvent : toRemove) {
newEvents.remove(removeEvent);
}
toRemove.clear();
}
return newEvents;
}
use of forpdateam.ru.forpda.api.events.models.NotificationEvent in project ForPDA by RadiationX.
the class NotificationsService method checkOldEvents.
private void checkOldEvents(List<NotificationEvent> loadedEvents, NotificationEvent.Source source) {
List<NotificationEvent> oldEvents = new ArrayList<>();
for (int i = 0; i < eventsHistory.size(); i++) {
NotificationEvent event = eventsHistory.valueAt(i);
if (event.getSource() == source) {
oldEvents.add(event);
}
}
for (NotificationEvent oldEvent : oldEvents) {
boolean exist = false;
for (NotificationEvent loadedEvent : loadedEvents) {
if (oldEvent.getSourceId() == loadedEvent.getSourceId()) {
exist = true;
break;
}
}
if (!exist) {
mNotificationManager.cancel(oldEvent.notifyId());
eventsHistory.remove(oldEvent.notifyId(NotificationEvent.Type.NEW));
TabNotification tabNotification = new TabNotification();
tabNotification.setSource(oldEvent.getSource());
tabNotification.setEvent(oldEvent);
tabNotification.setType(NotificationEvent.Type.READ);
tabNotification.setWebSocket(true);
notifyTabs(tabNotification);
}
}
}
use of forpdateam.ru.forpda.api.events.models.NotificationEvent in project ForPDA by RadiationX.
the class QmsThemesFragment method handleEvent.
private void handleEvent(TabNotification event) {
if (realm.isClosed())
return;
bindView();
if (true)
return;
RealmResults<QmsThemesBd> results = realm.where(QmsThemesBd.class).equalTo("userId", currentThemes.getUserId()).findAll();
QmsThemesBd qmsThemesBdLast = results.last(null);
if (qmsThemesBdLast == null) {
return;
}
ArrayList<QmsTheme> currentItems = new ArrayList<>();
for (QmsThemeBd qmsThemeBd : qmsThemesBdLast.getThemes()) {
QmsTheme qmsTheme = new QmsTheme(qmsThemeBd);
currentItems.add(qmsTheme);
}
if (event.getType() == NotificationEvent.Type.READ) {
for (QmsTheme item : currentItems) {
if (item.getId() == event.getEvent().getSourceId()) {
item.setCountNew(0);
break;
}
}
} else {
SparseIntArray sparseArray = new SparseIntArray();
for (NotificationEvent loadedEvent : event.getLoadedEvents()) {
int count = sparseArray.get(loadedEvent.getSourceId());
count += loadedEvent.getMsgCount();
sparseArray.put(loadedEvent.getSourceId(), count);
}
for (int i = sparseArray.size() - 1; i >= 0; i--) {
int id = sparseArray.keyAt(i);
int count = sparseArray.valueAt(i);
for (QmsTheme item : currentItems) {
if (item.getId() == id) {
item.setCountMessages(item.getCountMessages() + count);
item.setCountNew(count);
Collections.swap(currentItems, currentItems.indexOf(item), 0);
break;
}
}
}
}
QmsContactsFragment contactsFragment = (QmsContactsFragment) TabManager.get().getByClass(QmsContactsFragment.class);
if (contactsFragment != null) {
int count = 0;
for (QmsTheme qmsTheme : currentItems) {
count += qmsTheme.getCountNew();
}
contactsFragment.updateCount(currentThemes.getUserId(), count);
}
if (realm.isClosed())
return;
realm.executeTransactionAsync(r -> {
r.where(QmsThemesBd.class).equalTo("userId", currentThemes.getUserId()).findAll().deleteAllFromRealm();
currentThemes.getThemes().clear();
currentThemes.getThemes().addAll(currentItems);
QmsThemesBd qmsThemesBd = new QmsThemesBd(currentThemes);
r.copyToRealmOrUpdate(qmsThemesBd);
qmsThemesBd.getThemes().clear();
}, this::bindView);
}
Aggregations