Search in sources :

Example 1 with ContactGroup

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

the class RosterCacheManager method saveContact.

public static void saveContact(AccountJid accountJid, Collection<RosterContact> contacts) {
    Realm realm = MessageDatabaseManager.getInstance().getRealmUiThread();
    realm.beginTransaction();
    if (contacts.size() > 1) {
        RealmResults<ContactRealm> results = realm.where(ContactRealm.class).equalTo(ContactRealm.Fields.ACCOUNT, accountJid.getFullJid().asBareJid().toString()).findAll();
        results.deleteAllFromRealm();
    }
    List<ContactRealm> newContacts = new ArrayList<>();
    for (RosterContact contact : contacts) {
        String account = contact.getAccount().getFullJid().asBareJid().toString();
        String user = contact.getUser().getBareJid().toString();
        ContactRealm contactRealm = realm.where(ContactRealm.class).equalTo(ContactRealm.Fields.ID, account + "/" + user).findFirst();
        if (contactRealm == null) {
            contactRealm = new ContactRealm(account + "/" + user);
        }
        RealmList<ContactGroup> groups = new RealmList<>();
        for (String groupName : contact.getGroupNames()) {
            ContactGroup group = realm.copyToRealmOrUpdate(new ContactGroup(groupName));
            if (group.isManaged() && group.isValid())
                groups.add(group);
        }
        contactRealm.setGroups(groups);
        contactRealm.setAccount(account);
        contactRealm.setUser(user);
        contactRealm.setName(contact.getName());
        contactRealm.setAccountResource(contact.getAccount().getFullJid().getResourcepart().toString());
        newContacts.add(contactRealm);
    }
    realm.copyToRealmOrUpdate(newContacts);
    realm.commitTransaction();
}
Also used : RealmList(io.realm.RealmList) ContactRealm(com.xabber.android.data.database.realm.ContactRealm) ArrayList(java.util.ArrayList) ContactGroup(com.xabber.android.data.database.realm.ContactGroup) Realm(io.realm.Realm) ContactRealm(com.xabber.android.data.database.realm.ContactRealm)

Example 2 with ContactGroup

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

the class RosterManager method onPreInitialize.

public void onPreInitialize() {
    List<ContactRealm> contacts = RosterCacheManager.loadContacts();
    for (ContactRealm contactRealm : contacts) {
        try {
            AccountJid account = AccountJid.from(contactRealm.getAccount() + "/" + contactRealm.getAccountResource());
            UserJid userJid = UserJid.from(contactRealm.getUser());
            RosterContact contact = RosterContact.getRosterContact(account, userJid, contactRealm.getName());
            for (ContactGroup group : contactRealm.getGroups()) {
                contact.addGroupReference(new RosterGroupReference(new RosterGroup(account, group.getGroupName())));
            }
            rosterContacts.put(contact.getAccount().toString(), contact.getUser().getBareJid().toString(), contact);
            MessageItem lastMessage = contactRealm.getLastMessage();
            if (lastMessage != null) {
                MessageManager.getInstance().getOrCreateChat(contact.getAccount(), contact.getUser(), lastMessage);
            } else
                MessageManager.getInstance().getOrCreateChat(contact.getAccount(), contact.getUser());
        } catch (UserJid.UserJidCreateException e) {
            e.printStackTrace();
        } catch (XmppStringprepException e) {
            e.printStackTrace();
        }
    }
    onContactsChanged(Collections.<RosterContact>emptyList());
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) ContactRealm(com.xabber.android.data.database.realm.ContactRealm) AccountJid(com.xabber.android.data.entity.AccountJid) UserJid(com.xabber.android.data.entity.UserJid) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) ContactGroup(com.xabber.android.data.database.realm.ContactGroup)

Aggregations

ContactGroup (com.xabber.android.data.database.realm.ContactGroup)2 ContactRealm (com.xabber.android.data.database.realm.ContactRealm)2 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)1 AccountJid (com.xabber.android.data.entity.AccountJid)1 UserJid (com.xabber.android.data.entity.UserJid)1 Realm (io.realm.Realm)1 RealmList (io.realm.RealmList)1 ArrayList (java.util.ArrayList)1 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)1