Search in sources :

Example 6 with NotificationEvent

use of forpdateam.ru.forpda.api.events.models.NotificationEvent 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 7 with NotificationEvent

use of forpdateam.ru.forpda.api.events.models.NotificationEvent in project ForPDA by RadiationX.

the class NotificationsService method sendNotifications.

public void sendNotifications(List<NotificationEvent> events, NotificationEvent.Source tSource) {
    if (events.isEmpty()) {
        return;
    }
    if (events.size() <= STACKED_MAX) {
        for (NotificationEvent event : events) {
            sendNotification(event);
        }
        return;
    }
    if (!checkNotify(null, tSource)) {
        return;
    }
    String title = createStackedTitle(events);
    CharSequence text = createStackedContent(events);
    String summaryText = createStackedSummary(events);
    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle.setBigContentTitle(title);
    bigTextStyle.bigText(text);
    bigTextStyle.setSummaryText(summaryText);
    String channelId = getChannelId(events.get(0));
    String channelName = getChannelName(events.get(0));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
        getSystemService(NotificationManager.class).createNotificationChannel(channel);
    }
    NotificationCompat.Builder builder;
    builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(createStackedSmallIcon(events));
    builder.setContentTitle(title);
    builder.setContentText(text);
    builder.setStyle(bigTextStyle);
    builder.setChannelId(channelId);
    Intent notifyIntent = new Intent(this, MainActivity.class);
    notifyIntent.setData(Uri.parse(createStackedIntentUrl(events)));
    notifyIntent.setAction(Intent.ACTION_VIEW);
    PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
    builder.setContentIntent(notifyPendingIntent);
    configureNotification(builder);
    int id = 0;
    NotificationEvent event = events.get(0);
    if (event.fromQms()) {
        id = NOTIFY_STACKED_QMS_ID;
    } else if (event.fromTheme()) {
        id = NOTIFY_STACKED_FAV_ID;
    }
    mNotificationManager.notify(id, builder.build());
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationManager(android.app.NotificationManager) NotificationCompat(android.support.v4.app.NotificationCompat) NotificationEvent(forpdateam.ru.forpda.api.events.models.NotificationEvent) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) SuppressLint(android.annotation.SuppressLint)

Example 8 with NotificationEvent

use of forpdateam.ru.forpda.api.events.models.NotificationEvent in project ForPDA by RadiationX.

the class NotificationsService method handlePendingEvents.

private void handlePendingEvents(NotificationEvent.Source source) {
    SparseArray<NotificationEvent> pending = pendingEvents.get(source);
    if (pending != null && pending.size() > 0) {
        List<NotificationEvent> events = new ArrayList<>();
        for (int i = 0; i < pending.size(); i++) {
            events.add(pending.valueAt(i));
        }
        hardHandleEvent(events, source);
        pending.clear();
    }
}
Also used : ArrayList(java.util.ArrayList) NotificationEvent(forpdateam.ru.forpda.api.events.models.NotificationEvent) SuppressLint(android.annotation.SuppressLint)

Example 9 with NotificationEvent

use of forpdateam.ru.forpda.api.events.models.NotificationEvent in project ForPDA by RadiationX.

the class NotificationsService method saveEvents.

private void saveEvents(List<NotificationEvent> loadedEvents, NotificationEvent.Source source) {
    String prefKey = "";
    if (NotificationEvent.fromQms(source)) {
        prefKey = Preferences.Notifications.Data.QMS_EVENTS;
    } else if (NotificationEvent.fromTheme(source)) {
        prefKey = Preferences.Notifications.Data.FAVORITES_EVENTS;
    }
    Set<String> savedEvents = new ArraySet<>();
    for (NotificationEvent event : loadedEvents) {
        savedEvents.add(event.getSourceEventText());
    }
    App.get().getPreferences().edit().putStringSet(prefKey, savedEvents).apply();
}
Also used : ArraySet(android.support.v4.util.ArraySet) NotificationEvent(forpdateam.ru.forpda.api.events.models.NotificationEvent)

Example 10 with NotificationEvent

use of forpdateam.ru.forpda.api.events.models.NotificationEvent in project ForPDA by RadiationX.

the class NotificationEvents method getQmsEvent.

public NotificationEvent getQmsEvent(Matcher matcher) {
    NotificationEvent event = new NotificationEvent();
    event.setSourceEventText(matcher.group());
    event.setSource(NotificationEvent.Source.QMS);
    event.setType(NotificationEvent.Type.NEW);
    event.setSourceId(Integer.parseInt(matcher.group(1)));
    event.setSourceTitle(ApiUtils.fromHtml(matcher.group(2)));
    event.setUserId(Integer.parseInt(matcher.group(3)));
    event.setUserNick(ApiUtils.fromHtml(matcher.group(4)));
    event.setTimeStamp(Integer.parseInt(matcher.group(5)));
    event.setMsgCount(Integer.parseInt(matcher.group(6)));
    if (event.getUserNick().isEmpty() && event.getSourceId() == 0) {
        event.setUserNick("Сообщения 4PDA");
    }
    return event;
}
Also used : 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