use of forpdateam.ru.forpda.data.realm.qms.QmsThemesBd 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);
}
use of forpdateam.ru.forpda.data.realm.qms.QmsThemesBd 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();
}
use of forpdateam.ru.forpda.data.realm.qms.QmsThemesBd in project ForPDA by RadiationX.
the class QmsThemesFragment method onLoadThemes.
private void onLoadThemes(QmsThemes themes) {
setRefreshing(false);
recyclerView.scrollToPosition(0);
currentThemes = themes;
setTabTitle(String.format(getString(R.string.dialogs_Nick), currentThemes.getNick()));
setTitle(currentThemes.getNick());
if (currentThemes.getThemes().isEmpty() && currentThemes.getNick() != null) {
Bundle args = new Bundle();
args.putInt(QmsChatFragment.USER_ID_ARG, currentThemes.getUserId());
args.putString(QmsChatFragment.USER_NICK_ARG, currentThemes.getNick());
args.putString(QmsChatFragment.USER_AVATAR_ARG, avatarUrl);
TabManager.get().add(QmsChatFragment.class, args);
// new Handler().postDelayed(() -> TabManager.get().remove(getTag()), 500);
}
if (realm.isClosed())
return;
realm.executeTransactionAsync(r -> {
r.where(QmsThemesBd.class).equalTo("userId", currentThemes.getUserId()).findAll().deleteAllFromRealm();
QmsThemesBd qmsThemesBd = new QmsThemesBd(currentThemes);
r.copyToRealmOrUpdate(qmsThemesBd);
qmsThemesBd.getThemes().clear();
}, this::bindView);
}
use of forpdateam.ru.forpda.data.realm.qms.QmsThemesBd 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