Search in sources :

Example 1 with ChatDataRealm

use of com.xabber.android.data.database.realm.ChatDataRealm in project xabber-android by redsolution.

the class ChatManager method clearUnusedNotificationStateFromRealm.

public void clearUnusedNotificationStateFromRealm() {
    final long startTime = System.currentTimeMillis();
    // TODO: 13.03.18 ANR - WRITE
    Realm realm = RealmManager.getInstance().getNewRealm();
    realm.beginTransaction();
    RealmResults<NotificationStateRealm> results = realm.where(NotificationStateRealm.class).findAll();
    for (NotificationStateRealm notificationState : results) {
        ChatDataRealm chatDataRealm = realm.where(ChatDataRealm.class).equalTo("notificationState.id", notificationState.getId()).findFirst();
        if (chatDataRealm == null)
            notificationState.deleteFromRealm();
    }
    realm.commitTransaction();
    realm.close();
    LogManager.d("REALM", Thread.currentThread().getName() + " clear unused notif. state: " + (System.currentTimeMillis() - startTime));
}
Also used : NotificationStateRealm(com.xabber.android.data.database.realm.NotificationStateRealm) NotificationStateRealm(com.xabber.android.data.database.realm.NotificationStateRealm) Realm(io.realm.Realm) ChatDataRealm(com.xabber.android.data.database.realm.ChatDataRealm) ChatDataRealm(com.xabber.android.data.database.realm.ChatDataRealm)

Example 2 with ChatDataRealm

use of com.xabber.android.data.database.realm.ChatDataRealm in project xabber-android by redsolution.

the class ChatManager method loadChatDataFromRealm.

@Nullable
public ChatData loadChatDataFromRealm(AbstractChat chat) {
    String accountJid = chat.getAccount().toString();
    String userJid = chat.getUser().toString();
    ChatData chatData = null;
    Realm realm = RealmManager.getInstance().getNewRealm();
    ChatDataRealm realmChat = realm.where(ChatDataRealm.class).equalTo("accountJid", accountJid).equalTo("userJid", userJid).findFirst();
    if (realmChat != null) {
        NotificationState notificationState;
        if (realmChat.getNotificationState() != null) {
            notificationState = new NotificationState(realmChat.getNotificationState().getMode(), realmChat.getNotificationState().getTimestamp());
        } else
            notificationState = new NotificationState(NotificationState.NotificationMode.bydefault, 0);
        chatData = new ChatData(realmChat.getSubject(), realmChat.getAccountJid(), realmChat.getUserJid(), realmChat.getUnreadCount(), realmChat.isArchived(), notificationState);
    }
    realm.close();
    return chatData;
}
Also used : ChatData(com.xabber.android.data.message.ChatData) NotificationStateRealm(com.xabber.android.data.database.realm.NotificationStateRealm) Realm(io.realm.Realm) ChatDataRealm(com.xabber.android.data.database.realm.ChatDataRealm) ChatDataRealm(com.xabber.android.data.database.realm.ChatDataRealm) NotificationState(com.xabber.android.data.message.NotificationState) Nullable(android.support.annotation.Nullable)

Example 3 with ChatDataRealm

use of com.xabber.android.data.database.realm.ChatDataRealm in project xabber-android by redsolution.

the class ChatManager method saveOrUpdateChatDataToRealm.

public void saveOrUpdateChatDataToRealm(final AbstractChat chat) {
    final long startTime = System.currentTimeMillis();
    Application.getInstance().runInBackground(new Runnable() {

        @Override
        public void run() {
            Realm realm = RealmManager.getInstance().getNewRealm();
            realm.executeTransaction(new Realm.Transaction() {

                @Override
                public void execute(Realm realm) {
                    String accountJid = chat.getAccount().toString();
                    String userJid = chat.getUser().toString();
                    ChatDataRealm chatRealm = realm.where(ChatDataRealm.class).equalTo("accountJid", accountJid).equalTo("userJid", userJid).findFirst();
                    if (chatRealm == null)
                        chatRealm = new ChatDataRealm(accountJid, userJid);
                    chatRealm.setUnreadCount(chat.getUnreadMessageCount());
                    chatRealm.setArchived(chat.isArchived());
                    NotificationStateRealm notificationStateRealm = chatRealm.getNotificationState();
                    if (notificationStateRealm == null)
                        notificationStateRealm = new NotificationStateRealm();
                    notificationStateRealm.setMode(chat.getNotificationState().getMode());
                    notificationStateRealm.setTimestamp(chat.getNotificationState().getTimestamp());
                    chatRealm.setNotificationState(notificationStateRealm);
                    RealmObject realmObject = realm.copyToRealmOrUpdate(chatRealm);
                }
            });
        }
    });
    LogManager.d("REALM", Thread.currentThread().getName() + " save chat data: " + (System.currentTimeMillis() - startTime));
}
Also used : NotificationStateRealm(com.xabber.android.data.database.realm.NotificationStateRealm) RealmObject(io.realm.RealmObject) NotificationStateRealm(com.xabber.android.data.database.realm.NotificationStateRealm) Realm(io.realm.Realm) ChatDataRealm(com.xabber.android.data.database.realm.ChatDataRealm) ChatDataRealm(com.xabber.android.data.database.realm.ChatDataRealm)

Aggregations

ChatDataRealm (com.xabber.android.data.database.realm.ChatDataRealm)3 NotificationStateRealm (com.xabber.android.data.database.realm.NotificationStateRealm)3 Realm (io.realm.Realm)3 Nullable (android.support.annotation.Nullable)1 ChatData (com.xabber.android.data.message.ChatData)1 NotificationState (com.xabber.android.data.message.NotificationState)1 RealmObject (io.realm.RealmObject)1