Search in sources :

Example 1 with QmsTheme

use of forpdateam.ru.forpda.api.qms.models.QmsTheme in project ForPDA by RadiationX.

the class Qms method parseThemes.

private QmsThemes parseThemes(String response, int id) {
    QmsThemes qmsThemes = new QmsThemes();
    Matcher matcher = threadPattern.matcher(response);
    while (matcher.find()) {
        QmsTheme thread = new QmsTheme();
        thread.setId(Integer.parseInt(matcher.group(1)));
        thread.setDate(matcher.group(2));
        thread.setName(ApiUtils.fromHtml(matcher.group(3).trim()));
        thread.setCountMessages(Integer.parseInt(matcher.group(4)));
        String countNew = matcher.group(5);
        thread.setCountNew(countNew == null || countNew.isEmpty() ? 0 : Integer.parseInt(countNew));
        qmsThemes.addTheme(thread);
    }
    matcher = threadNickPattern.matcher(response);
    if (matcher.find()) {
        qmsThemes.setNick(ApiUtils.fromHtml(matcher.group(1)));
    }
    qmsThemes.setUserId(id);
    return qmsThemes;
}
Also used : QmsThemes(forpdateam.ru.forpda.api.qms.models.QmsThemes) Matcher(java.util.regex.Matcher) QmsTheme(forpdateam.ru.forpda.api.qms.models.QmsTheme)

Example 2 with QmsTheme

use of forpdateam.ru.forpda.api.qms.models.QmsTheme in project ForPDA by RadiationX.

the class QmsThemesFragment method bindView.

private void bindView() {
    if (realm.isClosed())
        return;
    refreshToolbarMenuItems(true);
    RealmResults<QmsThemesBd> results = realm.where(QmsThemesBd.class).equalTo("userId", currentThemes.getUserId()).findAll();
    QmsThemesBd qmsThemesBd = results.last(null);
    if (qmsThemesBd == null) {
        return;
    }
    ArrayList<QmsTheme> currentItems = new ArrayList<>();
    for (QmsThemeBd qmsThemeBd : qmsThemesBd.getThemes()) {
        QmsTheme qmsTheme = new QmsTheme(qmsThemeBd);
        currentItems.add(qmsTheme);
    }
    adapter.addAll(currentItems);
    adapter.notifyDataSetChanged();
}
Also used : QmsThemesBd(forpdateam.ru.forpda.data.realm.qms.QmsThemesBd) 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)

Example 3 with QmsTheme

use of forpdateam.ru.forpda.api.qms.models.QmsTheme 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

QmsTheme (forpdateam.ru.forpda.api.qms.models.QmsTheme)3 IQmsTheme (forpdateam.ru.forpda.api.qms.interfaces.IQmsTheme)2 QmsThemeBd (forpdateam.ru.forpda.data.realm.qms.QmsThemeBd)2 QmsThemesBd (forpdateam.ru.forpda.data.realm.qms.QmsThemesBd)2 ArrayList (java.util.ArrayList)2 SparseIntArray (android.util.SparseIntArray)1 NotificationEvent (forpdateam.ru.forpda.api.events.models.NotificationEvent)1 QmsThemes (forpdateam.ru.forpda.api.qms.models.QmsThemes)1 Matcher (java.util.regex.Matcher)1