Mercury-IM/app/src/main/java/org/mercury_im/messenger/ui/chat/ChatViewModel.java

128 lines
4.5 KiB
Java
Raw Normal View History

2019-05-13 03:19:17 +02:00
package org.mercury_im.messenger.ui.chat;
2019-04-22 04:54:02 +02:00
2019-08-29 00:22:26 +02:00
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
2019-05-18 10:06:16 +02:00
import androidx.lifecycle.ViewModel;
2019-04-22 04:54:02 +02:00
import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
2019-05-04 00:27:02 +02:00
import org.jxmpp.jid.EntityBareJid;
2019-08-29 00:22:26 +02:00
import org.mercury_im.messenger.MercuryImApplication;
2019-09-08 04:47:59 +02:00
import org.mercury_im.messenger.core.centers.ConnectionCenter;
2019-09-11 01:40:32 +02:00
import org.mercury_im.messenger.core.util.ContactNameUtil;
2019-09-08 04:47:59 +02:00
import org.mercury_im.messenger.persistence.model.ChatModel;
2019-08-29 00:22:26 +02:00
import org.mercury_im.messenger.persistence.model.ContactModel;
import org.mercury_im.messenger.persistence.model.MessageModel;
2019-09-08 04:47:59 +02:00
import org.mercury_im.messenger.persistence.repository.ChatRepository;
2019-08-29 00:22:26 +02:00
import org.mercury_im.messenger.persistence.repository.MessageRepository;
import org.mercury_im.messenger.persistence.repository.RosterRepository;
import java.util.List;
import javax.inject.Inject;
2019-04-22 04:54:02 +02:00
public class ChatViewModel extends ViewModel {
2019-05-04 00:27:02 +02:00
2019-08-29 00:22:26 +02:00
private final CompositeDisposable disposable = new CompositeDisposable();
@Inject
MessageRepository messageRepository;
@Inject
RosterRepository rosterRepository;
2019-09-08 04:47:59 +02:00
@Inject
ChatRepository chatRepository;
@Inject
ConnectionCenter connectionCenter;
private long accountId;
private EntityBareJid jid;
2019-08-29 00:22:26 +02:00
private MutableLiveData<ContactModel> contact = new MutableLiveData<>();
private MutableLiveData<List<MessageModel>> messages = new MutableLiveData<>();
private MutableLiveData<String> contactDisplayName = new MutableLiveData<>();
2019-09-08 04:47:59 +02:00
private MutableLiveData<ChatModel> chat = new MutableLiveData<>();
2019-08-29 00:22:26 +02:00
public ChatViewModel() {
super();
MercuryImApplication.getApplication().getAppComponent().inject(this);
}
public void init(long accountId, EntityBareJid jid) {
this.accountId = accountId;
this.jid = jid;
2019-08-29 00:22:26 +02:00
disposable.add(rosterRepository.getContact(accountId, jid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((Consumer<ContactModel>)
contactModel -> {
ChatViewModel.this.contact.setValue(contactModel);
2019-09-11 01:40:32 +02:00
contactDisplayName.setValue(ContactNameUtil.displayableNameFrom(contactModel));
2019-08-29 00:22:26 +02:00
}));
disposable.add(messageRepository.getAllMessagesOfChat(accountId, jid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((Consumer<List<MessageModel>>)
messages -> ChatViewModel.this.messages.setValue(messages)));
2019-09-08 04:47:59 +02:00
disposable.add(chatRepository.getOrCreateChatWith(accountId, jid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((Consumer<ChatModel>)
chatModel -> this.chat.setValue(chatModel)));
2019-08-29 00:22:26 +02:00
}
@Override
protected void onCleared() {
super.onCleared();
disposable.clear();
}
2019-06-06 23:32:41 +02:00
2019-08-29 00:22:26 +02:00
public LiveData<List<MessageModel>> getMessages() {
return messages;
}
2019-05-04 00:27:02 +02:00
2019-08-29 00:22:26 +02:00
public LiveData<ContactModel> getContact() {
return contact;
2019-05-04 00:27:02 +02:00
}
2019-08-29 00:22:26 +02:00
public LiveData<String> getContactDisplayName() {
return contactDisplayName;
}
public void queryTextChanged(String query) {
if (query.isEmpty()) {
disposable.add(messageRepository.getAllMessagesOfChat(accountId, jid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((Consumer<List<MessageModel>>)
messages -> ChatViewModel.this.messages.setValue(messages)));
}
disposable.add(messageRepository.findMessageByQuery(accountId, jid, query)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((Consumer<List<MessageModel>>) o -> {
messages.setValue(o);
}));
}
2019-09-08 04:47:59 +02:00
public Completable requestMamMessages() {
2019-09-11 01:40:32 +02:00
return Completable.fromAction(() -> {
ChatModel chatModel = ChatViewModel.this.chat.getValue();
if (chatModel == null) {
return;
2019-09-08 04:47:59 +02:00
}
2019-09-11 01:40:32 +02:00
connectionCenter.requestMamMessagesFor(chatModel);
2019-09-08 04:47:59 +02:00
});
}
2019-04-22 04:54:02 +02:00
}