Search in sources :

Example 16 with NotificationEvent

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;
}
Also used : ArrayList(java.util.ArrayList) NotificationEvent(forpdateam.ru.forpda.api.events.models.NotificationEvent)

Example 17 with NotificationEvent

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);
        }
    }
}
Also used : TabNotification(forpdateam.ru.forpda.data.models.TabNotification) ArrayList(java.util.ArrayList) NotificationEvent(forpdateam.ru.forpda.api.events.models.NotificationEvent) SuppressLint(android.annotation.SuppressLint)

Example 18 with NotificationEvent

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);
}
Also used : QmsThemesBd(forpdateam.ru.forpda.data.realm.qms.QmsThemesBd) SparseIntArray(android.util.SparseIntArray) QmsTheme(forpdateam.ru.forpda.api.qms.models.QmsTheme) IQmsTheme(forpdateam.ru.forpda.api.qms.interfaces.IQmsTheme) ArrayList(java.util.ArrayList) QmsThemeBd(forpdateam.ru.forpda.data.realm.qms.QmsThemeBd) NotificationEvent(forpdateam.ru.forpda.api.events.models.NotificationEvent)

Aggregations

NotificationEvent (forpdateam.ru.forpda.api.events.models.NotificationEvent)18 ArrayList (java.util.ArrayList)10 SuppressLint (android.annotation.SuppressLint)4 TabNotification (forpdateam.ru.forpda.data.models.TabNotification)3 SparseIntArray (android.util.SparseIntArray)2 QmsContactBd (forpdateam.ru.forpda.data.realm.qms.QmsContactBd)2 QmsThemeBd (forpdateam.ru.forpda.data.realm.qms.QmsThemeBd)2 QmsThemesBd (forpdateam.ru.forpda.data.realm.qms.QmsThemesBd)2 Matcher (java.util.regex.Matcher)2 NotificationChannel (android.app.NotificationChannel)1 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 ArraySet (android.support.v4.util.ArraySet)1 IFavItem (forpdateam.ru.forpda.api.favorites.interfaces.IFavItem)1 FavItem (forpdateam.ru.forpda.api.favorites.models.FavItem)1 IQmsContact (forpdateam.ru.forpda.api.qms.interfaces.IQmsContact)1 IQmsTheme (forpdateam.ru.forpda.api.qms.interfaces.IQmsTheme)1 QmsContact (forpdateam.ru.forpda.api.qms.models.QmsContact)1