Better observation of Rosternames
This commit is contained in:
parent
8e8060b8ee
commit
f414f3a6d5
|
@ -84,10 +84,13 @@ public class ChatActivity extends AppCompatActivity
|
|||
String jidString = savedInstanceState.getString(EXTRA_JID);
|
||||
if (jidString != null) {
|
||||
jid = JidCreate.entityBareFromOrThrowUnchecked(jidString);
|
||||
// JID will never change, so just set it once
|
||||
getSupportActionBar().setSubtitle(jid.asUnescapedString());
|
||||
accountId = savedInstanceState.getLong(EXTRA_ACCOUNT);
|
||||
|
||||
chatViewModel = ViewModelProviders.of(this).get(ChatViewModel.class);
|
||||
chatViewModel.init(accountId, jid);
|
||||
// Listen for updates to contact information and messages
|
||||
observeViewModel(chatViewModel);
|
||||
}
|
||||
}
|
||||
|
@ -96,9 +99,6 @@ public class ChatActivity extends AppCompatActivity
|
|||
viewModel.getContactDisplayName().observe(this,
|
||||
name -> getSupportActionBar().setTitle(name));
|
||||
|
||||
viewModel.getContact().observe(this, contactModel ->
|
||||
getSupportActionBar().setSubtitle(contactModel.getEntity().getJid().asUnescapedString()));
|
||||
|
||||
viewModel.getMessages().observe(this, messageModels -> {
|
||||
recyclerViewAdapter.updateMessages(messageModels);
|
||||
recyclerView.scrollToPosition(messageModels.size() - 1);
|
||||
|
@ -138,7 +138,7 @@ public class ChatActivity extends AppCompatActivity
|
|||
case R.id.action_notification_settings:
|
||||
case R.id.action_delete_chat:
|
||||
|
||||
// long_click_message
|
||||
// long_click_message
|
||||
case R.id.action_edit_message:
|
||||
case R.id.action_reply_message:
|
||||
case R.id.action_copy_message:
|
||||
|
@ -146,7 +146,7 @@ public class ChatActivity extends AppCompatActivity
|
|||
case R.id.action_delete_message:
|
||||
case R.id.action_message_details:
|
||||
|
||||
// short_click_message
|
||||
// short_click_message
|
||||
case R.id.action_react_message:
|
||||
|
||||
Toast.makeText(this, R.string.not_yet_implemented, Toast.LENGTH_SHORT).show();
|
||||
|
|
|
@ -7,6 +7,7 @@ import androidx.lifecycle.ViewModel;
|
|||
import org.jxmpp.jid.EntityBareJid;
|
||||
import org.mercury_im.messenger.MercuryImApplication;
|
||||
import org.mercury_im.messenger.core.centers.ConnectionCenter;
|
||||
import org.mercury_im.messenger.core.util.ContactNameUtil;
|
||||
import org.mercury_im.messenger.persistence.model.ChatModel;
|
||||
import org.mercury_im.messenger.persistence.model.ContactModel;
|
||||
import org.mercury_im.messenger.persistence.model.MessageModel;
|
||||
|
@ -64,12 +65,7 @@ public class ChatViewModel extends ViewModel {
|
|||
.subscribe((Consumer<ContactModel>)
|
||||
contactModel -> {
|
||||
ChatViewModel.this.contact.setValue(contactModel);
|
||||
|
||||
String name = contactModel.getRosterName();
|
||||
if (name == null) {
|
||||
name = jid.getLocalpart().asUnescapedString();
|
||||
}
|
||||
contactDisplayName.setValue(name);
|
||||
contactDisplayName.setValue(ContactNameUtil.displayableNameFrom(contactModel));
|
||||
}));
|
||||
|
||||
disposable.add(messageRepository.getAllMessagesOfChat(accountId, jid)
|
||||
|
@ -120,15 +116,12 @@ public class ChatViewModel extends ViewModel {
|
|||
}
|
||||
|
||||
public Completable requestMamMessages() {
|
||||
return Completable.fromAction(new Action() {
|
||||
@Override
|
||||
public void run() throws Exception {
|
||||
ChatModel chatModel = ChatViewModel.this.chat.getValue();
|
||||
if (chatModel == null) {
|
||||
return;
|
||||
}
|
||||
connectionCenter.requestMamMessagesFor(chatModel);
|
||||
return Completable.fromAction(() -> {
|
||||
ChatModel chatModel = ChatViewModel.this.chat.getValue();
|
||||
if (chatModel == null) {
|
||||
return;
|
||||
}
|
||||
connectionCenter.requestMamMessagesFor(chatModel);
|
||||
});
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package org.mercury_im.messenger.core.util;
|
||||
|
||||
import org.mercury_im.messenger.persistence.model.ContactModel;
|
||||
|
||||
public class ContactNameUtil {
|
||||
|
||||
public static String displayableNameFrom(ContactModel contactModel) {
|
||||
if (contactModel.getRosterName() != null) {
|
||||
return contactModel.getRosterName();
|
||||
}
|
||||
if (contactModel.getEntity() != null) {
|
||||
return contactModel.getEntity().getJid().getLocalpart().asUnescapedString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -23,7 +23,7 @@ import io.reactivex.disposables.Disposable;
|
|||
public class ExampleInstrumentedTest extends AbstractDatabaseTest {
|
||||
|
||||
@Test
|
||||
public void testUpsertContact() {
|
||||
public void testUpsertContact1() {
|
||||
RoomAccountModel accountModel = new RoomAccountModel();
|
||||
accountModel.setJid(TEST_JID_JULIET);
|
||||
accountRepository.insertAccount(accountModel).test().assertValue(1L).dispose();
|
||||
|
@ -35,12 +35,12 @@ public class ExampleInstrumentedTest extends AbstractDatabaseTest {
|
|||
contactModel.setEntity(entityModel);
|
||||
|
||||
rosterRepository.upsertContact(contactModel).test().assertValue(1L).dispose();
|
||||
rosterRepository.getContact(1L).subscribe(contact -> {
|
||||
rosterRepository.maybeGetContact(1L).subscribe(contact -> {
|
||||
System.out.println(contact.getEntity().getAccountId() + " " + contact.getEntity().getJid().toString());
|
||||
}).dispose();
|
||||
|
||||
rosterRepository.upsertContact(contactModel).test().assertValue(1L).dispose();
|
||||
rosterRepository.getContact(1L).subscribe(contact -> {
|
||||
rosterRepository.maybeGetContact(1L).subscribe(contact -> {
|
||||
System.out.println(contact.getEntity().getAccountId() + " " + contact.getEntity().getJid().toString());
|
||||
}).dispose();
|
||||
}
|
||||
|
@ -83,4 +83,29 @@ public class ExampleInstrumentedTest extends AbstractDatabaseTest {
|
|||
|
||||
Thread.sleep(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpsertContact() throws InterruptedException {
|
||||
RoomAccountModel accountModel = new RoomAccountModel();
|
||||
accountModel.setJid(TEST_JID_ROMEO);
|
||||
accountRepository.insertAccount(accountModel).test().assertValue(1L).dispose();
|
||||
|
||||
RoomContactModel contact = new RoomContactModel();
|
||||
contact.setRosterName("A");
|
||||
RoomEntityModel entity = new RoomEntityModel();
|
||||
entity.setJid(TEST_JID_JULIET);
|
||||
entity.setAccountId(1L);
|
||||
contact.setEntity(entity);
|
||||
rosterRepository.upsertContact(contact).test().assertValue(1L).dispose();
|
||||
Thread.sleep(100);
|
||||
contact.setId(0);
|
||||
contact.setRosterName("B");
|
||||
contact.getEntity().setId(0);
|
||||
contact.setEntityId(0);
|
||||
rosterRepository.upsertContact(contact).test().assertValue(1L).dispose();
|
||||
|
||||
rosterRepository.getAllContacts().subscribe(System.out::println);
|
||||
|
||||
Thread.sleep(200);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,10 @@ public interface ContactDao extends BaseDao<RoomContactModel> {
|
|||
Single<Long> insert(RoomContactModel entity);
|
||||
|
||||
@Query("SELECT * FROM contacts WHERE pk_contact_id = :id")
|
||||
Maybe<RoomContactModel> getContact(long id);
|
||||
Observable<RoomContactModel> getContact(long id);
|
||||
|
||||
@Query("SELECT * FROM contacts WHERE pk_contact_id = :id")
|
||||
Maybe<RoomContactModel> maybeGetContact(long id);
|
||||
|
||||
@Query("SELECT * FROM contacts WHERE fk_entity_id = :entityId")
|
||||
Maybe<RoomContactModel> getContactForEntityId(long entityId);
|
||||
|
@ -38,7 +41,11 @@ public interface ContactDao extends BaseDao<RoomContactModel> {
|
|||
|
||||
@Query("SELECT contacts.* FROM contacts JOIN entities " +
|
||||
"WHERE entities.fk_account_id = :accountId AND jid = :jid")
|
||||
Maybe<RoomContactModel> getContactByJid(long accountId, EntityBareJid jid);
|
||||
Maybe<RoomContactModel> maybeGetContactByJid(long accountId, EntityBareJid jid);
|
||||
|
||||
@Query("SELECT contacts.* FROM contacts JOIN entities " +
|
||||
"WHERE entities.fk_account_id = :accountId AND jid = :jid")
|
||||
Observable<RoomContactModel> getContactByJid(long accountId, EntityBareJid jid);
|
||||
|
||||
@Query("SELECT contacts.* FROM contacts JOIN entities " +
|
||||
"WHERE entities.fk_account_id = :accountId")
|
||||
|
|
|
@ -12,7 +12,6 @@ import org.mercury_im.messenger.persistence.room.model.RoomEntityModel;
|
|||
import org.mercury_im.messenger.persistence.room.model.RoomRosterInformationModel;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
|
@ -26,6 +25,12 @@ import static org.mercury_im.messenger.persistence.room.AppDatabase.TAG;
|
|||
public class IRosterRepository extends RosterRepository<RoomEntityModel, RoomContactModel, RoomRosterInformationModel> {
|
||||
|
||||
private final EntityDao entityDao;
|
||||
|
||||
@Override
|
||||
public Observable getContact(long accountId, EntityBareJid jid) {
|
||||
return contactDao.getContactByJid(accountId, jid);
|
||||
}
|
||||
|
||||
private final ContactDao contactDao;
|
||||
private final RosterInformationDao rosterInformationDao;
|
||||
|
||||
|
@ -82,6 +87,12 @@ public class IRosterRepository extends RosterRepository<RoomEntityModel, RoomCon
|
|||
long entityId = insertEntity(existingEntityModel).blockingGet();
|
||||
existingEntityModel.setId(entityId);
|
||||
contact.setEntity(existingEntityModel);
|
||||
} else {
|
||||
contact.getEntity().setId(existingEntityModel.getId());
|
||||
contact.setEntityId(existingEntityModel.getId());
|
||||
entityDao.update((RoomEntityModel) contact.getEntity())
|
||||
.doOnSubscribe(ignore -> Log.v(TAG, "Updating entity " + contact.getEntity()))
|
||||
.blockingAwait();
|
||||
}
|
||||
|
||||
RoomContactModel existingContactModel = contactDao.getContactForEntityId(existingEntityModel.getId()).blockingGet();
|
||||
|
@ -90,7 +101,11 @@ public class IRosterRepository extends RosterRepository<RoomEntityModel, RoomCon
|
|||
existingContactModel = contact;
|
||||
return insertContact(existingContactModel).blockingGet();
|
||||
} else {
|
||||
contactDao.update(contact);
|
||||
contact.setId(existingContactModel.getId());
|
||||
contactDao.update(contact)
|
||||
.doOnSubscribe(ignore -> Log.v(TAG, "Updating contact " + contact))
|
||||
.doOnComplete(() -> Log.v(TAG, "Update complete"))
|
||||
.blockingAwait();
|
||||
}
|
||||
|
||||
return existingContactModel.getId();
|
||||
|
@ -116,8 +131,18 @@ public class IRosterRepository extends RosterRepository<RoomEntityModel, RoomCon
|
|||
}
|
||||
|
||||
@Override
|
||||
public Maybe<RoomContactModel> getContact(long id) {
|
||||
return contactDao.getContact(id)
|
||||
public Observable<RoomContactModel> getContact(long contactId) {
|
||||
return contactDao.getContact(contactId)
|
||||
.map(contact -> {
|
||||
contact.setEntity(getEntityForContact(contactId).toMaybe().blockingGet());
|
||||
contact.setEntityId(contact.getEntity().getId());
|
||||
return contact;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Maybe<RoomContactModel> maybeGetContact(long id) {
|
||||
return contactDao.maybeGetContact(id)
|
||||
// Set the entity
|
||||
.zipWith(getEntityForContact(id).toMaybe(),
|
||||
(contact, entity) -> {
|
||||
|
|
|
@ -79,12 +79,16 @@ public abstract class RosterRepository<E extends EntityModel, C extends ContactM
|
|||
* @param id ID of the contact
|
||||
* @return {@link Maybe} wrapping the {@link ContactModel}
|
||||
*/
|
||||
public abstract Maybe<C> getContact(long id);
|
||||
public abstract Maybe<C> maybeGetContact(long id);
|
||||
|
||||
public Maybe<C> getContact(long accountId, EntityBareJid jid) {
|
||||
public abstract Observable getContact(long accountId, EntityBareJid jid);
|
||||
|
||||
public Maybe<C> maybeGetContact(long accountId, EntityBareJid jid) {
|
||||
return getEntityForAccountAndJid(accountId, jid).flatMap(this::getContactForEntity);
|
||||
}
|
||||
|
||||
public abstract Observable<C> getContact(long contactId);
|
||||
|
||||
/**
|
||||
* Return the {@link ContactModel} which belongs to the given entityId, wrapped in a {@link Maybe}.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue