Search in sources :

Example 1 with QmsContactBd

use of forpdateam.ru.forpda.data.realm.qms.QmsContactBd in project ForPDA by RadiationX.

the class QmsContactsFragment method handleEvent.

private void handleEvent(TabNotification event) {
    bindView();
    if (true)
        return;
    SparseIntArray sparseArray = new SparseIntArray();
    if (realm.isClosed())
        return;
    results = realm.where(QmsContactBd.class).findAll();
    ArrayList<QmsContact> currentItems = new ArrayList<>();
    for (QmsContactBd qmsContactBd : results) {
        QmsContact contact = new QmsContact(qmsContactBd);
        currentItems.add(contact);
    }
    for (NotificationEvent loadedEvent : event.getLoadedEvents()) {
        int count = sparseArray.get(loadedEvent.getUserId());
        count += loadedEvent.getMsgCount();
        sparseArray.put(loadedEvent.getUserId(), count);
    }
    for (int i = sparseArray.size() - 1; i >= 0; i--) {
        int id = sparseArray.keyAt(i);
        int count = sparseArray.valueAt(i);
        for (QmsContact item : currentItems) {
            if (item.getId() == id) {
                item.setCount(count);
                Collections.swap(currentItems, currentItems.indexOf(item), 0);
                break;
            }
        }
    }
    if (realm.isClosed())
        return;
    realm.executeTransactionAsync(r -> {
        r.delete(QmsContactBd.class);
        List<QmsContactBd> bdList = new ArrayList<>();
        for (QmsContact qmsContact : currentItems) {
            bdList.add(new QmsContactBd(qmsContact));
        }
        r.copyToRealmOrUpdate(bdList);
        bdList.clear();
    }, this::bindView);
// adapter.notifyDataSetChanged();
/*ArrayList<IFavItem> newItems = new ArrayList<>();
        newItems.addAll(currentItems);
        refreshList(newItems);*/
}
Also used : SparseIntArray(android.util.SparseIntArray) ArrayList(java.util.ArrayList) NotificationEvent(forpdateam.ru.forpda.api.events.models.NotificationEvent) IQmsContact(forpdateam.ru.forpda.api.qms.interfaces.IQmsContact) QmsContact(forpdateam.ru.forpda.api.qms.models.QmsContact) QmsContactBd(forpdateam.ru.forpda.data.realm.qms.QmsContactBd)

Example 2 with QmsContactBd

use of forpdateam.ru.forpda.data.realm.qms.QmsContactBd in project ForPDA by RadiationX.

the class QmsHelper method handleEvent.

private void handleEvent(TabNotification event) {
    Realm realm = Realm.getDefaultInstance();
    RealmResults<QmsThemesBd> themes = realm.where(QmsThemesBd.class).findAll();
    QmsThemeBd targetTheme = null;
    QmsThemesBd targetDialog = null;
    for (QmsThemesBd dialog : themes) {
        for (QmsThemeBd theme : dialog.getThemes()) {
            if (theme.getId() == event.getEvent().getSourceId()) {
                targetDialog = dialog;
                targetTheme = theme;
                break;
            }
        }
        if (targetTheme != null) {
            break;
        }
    }
    if (targetTheme != null) {
        QmsThemeBd finalTargetTheme = targetTheme;
        QmsThemesBd finalTargetDialog = targetDialog;
        realm.executeTransaction(realm1 -> {
            if (event.isWebSocket()) {
                if (NotificationEvent.isRead(event.getType())) {
                    finalTargetTheme.setCountNew(0);
                }
            } else {
                if (NotificationEvent.isNew(event.getType())) {
                    finalTargetTheme.setCountNew(event.getEvent().getMsgCount());
                }
            }
            QmsContactBd contact = realm1.where(QmsContactBd.class).equalTo("id", finalTargetDialog.getUserId()).findFirst();
            if (contact != null) {
                int count = 0;
                for (QmsThemeBd theme : finalTargetDialog.getThemes()) {
                    count += theme.getCountNew();
                }
                contact.setCount(count);
            }
        });
    }
    int globalCount = 0;
    for (NotificationEvent ev : event.getLoadedEvents()) {
        globalCount += ev.getMsgCount();
    }
    ClientHelper.setQmsCount(globalCount);
    ClientHelper.get().notifyCountsChanged();
    realm.close();
    notifyQms(event);
}
Also used : QmsThemesBd(forpdateam.ru.forpda.data.realm.qms.QmsThemesBd) QmsThemeBd(forpdateam.ru.forpda.data.realm.qms.QmsThemeBd) NotificationEvent(forpdateam.ru.forpda.api.events.models.NotificationEvent) Realm(io.realm.Realm) QmsContactBd(forpdateam.ru.forpda.data.realm.qms.QmsContactBd)

Example 3 with QmsContactBd

use of forpdateam.ru.forpda.data.realm.qms.QmsContactBd in project ForPDA by RadiationX.

the class QmsContactsFragment method onLoadContacts.

private void onLoadContacts(ArrayList<QmsContact> data) {
    setRefreshing(false);
    recyclerView.scrollToPosition(0);
    if (realm.isClosed())
        return;
    realm.executeTransactionAsync(r -> {
        r.delete(QmsContactBd.class);
        List<QmsContactBd> bdList = new ArrayList<>();
        for (QmsContact contact : data) {
            bdList.add(new QmsContactBd(contact));
        }
        r.copyToRealmOrUpdate(bdList);
        bdList.clear();
    }, this::bindView);
}
Also used : ArrayList(java.util.ArrayList) QmsContactBd(forpdateam.ru.forpda.data.realm.qms.QmsContactBd) IQmsContact(forpdateam.ru.forpda.api.qms.interfaces.IQmsContact) QmsContact(forpdateam.ru.forpda.api.qms.models.QmsContact)

Example 4 with QmsContactBd

use of forpdateam.ru.forpda.data.realm.qms.QmsContactBd in project ForPDA by RadiationX.

the class QmsContactsFragment method addBaseToolbarMenu.

@Override
protected void addBaseToolbarMenu(Menu menu) {
    super.addBaseToolbarMenu(menu);
    toolbar.inflateMenu(R.menu.qms_contacts_menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();
    SearchManager searchManager = (SearchManager) getMainActivity().getSystemService(Context.SEARCH_SERVICE);
    if (null != searchManager) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getMainActivity().getComponentName()));
    }
    searchView.setIconifiedByDefault(true);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        private ArrayList<QmsContactBd> searchContacts = new ArrayList<>();

        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            searchContacts.clear();
            if (!newText.isEmpty()) {
                for (QmsContactBd contact : results) {
                    if (contact.getNick().toLowerCase().contains(newText.toLowerCase()))
                        searchContacts.add(contact);
                }
                adapter.addAll(searchContacts);
            } else {
                adapter.addAll(results);
            }
            return false;
        }
    });
    searchView.setQueryHint(getString(R.string.user));
    menu.add(R.string.blacklist).setOnMenuItemClickListener(item -> {
        TabManager.get().add(QmsBlackListFragment.class);
        return false;
    });
}
Also used : SearchView(android.support.v7.widget.SearchView) SearchManager(android.app.SearchManager) ArrayList(java.util.ArrayList) MenuItem(android.view.MenuItem) QmsContactBd(forpdateam.ru.forpda.data.realm.qms.QmsContactBd)

Example 5 with QmsContactBd

use of forpdateam.ru.forpda.data.realm.qms.QmsContactBd in project ForPDA by RadiationX.

the class QmsContactsFragment method bindView.

private void bindView() {
    if (realm.isClosed())
        return;
    results = realm.where(QmsContactBd.class).findAll();
    if (results.isEmpty()) {
        if (!contentController.contains(ContentController.TAG_NO_DATA)) {
            FunnyContent funnyContent = new FunnyContent(getContext()).setImage(R.drawable.ic_contacts).setTitle(R.string.funny_contacts_nodata_title);
            contentController.addContent(funnyContent, ContentController.TAG_NO_DATA);
        }
        contentController.showContent(ContentController.TAG_NO_DATA);
    } else {
        contentController.hideContent(ContentController.TAG_NO_DATA);
    }
    ArrayList<QmsContact> currentItems = new ArrayList<>();
    for (QmsContactBd qmsContactBd : results) {
        QmsContact contact = new QmsContact(qmsContactBd);
        currentItems.add(contact);
    }
    int count = 0;
    for (QmsContact contact : currentItems) {
        if (contact.getCount() > 0) {
            count += contact.getCount();
        }
    }
    ClientHelper.setQmsCount(count);
    ClientHelper.get().notifyCountsChanged();
    adapter.addAll(currentItems);
}
Also used : FunnyContent(forpdateam.ru.forpda.ui.views.FunnyContent) ArrayList(java.util.ArrayList) IQmsContact(forpdateam.ru.forpda.api.qms.interfaces.IQmsContact) QmsContact(forpdateam.ru.forpda.api.qms.models.QmsContact) QmsContactBd(forpdateam.ru.forpda.data.realm.qms.QmsContactBd)

Aggregations

QmsContactBd (forpdateam.ru.forpda.data.realm.qms.QmsContactBd)5 ArrayList (java.util.ArrayList)4 IQmsContact (forpdateam.ru.forpda.api.qms.interfaces.IQmsContact)3 QmsContact (forpdateam.ru.forpda.api.qms.models.QmsContact)3 NotificationEvent (forpdateam.ru.forpda.api.events.models.NotificationEvent)2 SearchManager (android.app.SearchManager)1 SearchView (android.support.v7.widget.SearchView)1 SparseIntArray (android.util.SparseIntArray)1 MenuItem (android.view.MenuItem)1 QmsThemeBd (forpdateam.ru.forpda.data.realm.qms.QmsThemeBd)1 QmsThemesBd (forpdateam.ru.forpda.data.realm.qms.QmsThemesBd)1 FunnyContent (forpdateam.ru.forpda.ui.views.FunnyContent)1 Realm (io.realm.Realm)1