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

120 lines
3.8 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
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-12-06 15:52:50 +01:00
import org.mercury_im.messenger.data.repository.DirectChatRepository;
import org.mercury_im.messenger.data.repository.MessageRepository;
2019-12-06 15:52:50 +01:00
import org.mercury_im.messenger.data.repository.PeerRepository;
import org.mercury_im.messenger.entity.chat.DirectChat;
import org.mercury_im.messenger.entity.contact.Peer;
import org.mercury_im.messenger.entity.message.Message;
2019-08-29 00:22:26 +02:00
import java.util.List;
import javax.inject.Inject;
import io.reactivex.Completable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
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
2019-12-06 15:52:50 +01:00
PeerRepository contactRepository;
2019-08-29 00:22:26 +02:00
@Inject
2019-12-06 15:52:50 +01:00
DirectChatRepository chatRepository;
2019-08-29 00:22:26 +02:00
@Inject
MessageRepository messageRepository;
2019-12-06 15:52:50 +01:00
private MutableLiveData<Peer> contact = new MutableLiveData<>();
private MutableLiveData<List<Message>> messages = new MutableLiveData<>();
2019-08-29 00:22:26 +02:00
private MutableLiveData<String> contactDisplayName = new MutableLiveData<>();
2019-12-06 15:52:50 +01:00
private MutableLiveData<DirectChat> 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) {
2019-12-06 15:52:50 +01:00
disposable.add(contactRepository.getOrCreatePeer(accountId, jid.toString())
.subscribe((Consumer<Peer>) this::init));
}
2019-12-06 15:52:50 +01:00
public void init(Peer peer) {
disposable.add(chatRepository.getOrCreateChatWithPeer(peer)
.subscribe((Consumer<DirectChat>) this::init));
}
public void init(DirectChat chat) {
this.chat.setValue(chat);
2019-08-29 00:22:26 +02:00
2019-12-06 15:52:50 +01:00
// Subscribe peer
disposable.add(contactRepository.observePeer(chat.getPeer().getId())
.subscribe(peer -> contactDisplayName.setValue(peer.getItem().getName())));
2019-09-08 04:47:59 +02:00
2019-12-06 15:52:50 +01:00
// Subscribe messages
disposable.add(messageRepository.observeMessages(chat)
2019-12-06 15:52:50 +01:00
.subscribe(ChatViewModel.this.messages::setValue));
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-12-06 15:52:50 +01:00
public LiveData<List<Message>> getMessages() {
2019-08-29 00:22:26 +02:00
return messages;
}
2019-05-04 00:27:02 +02:00
2019-12-06 15:52:50 +01:00
public LiveData<Peer> getContact() {
2019-08-29 00:22:26 +02:00
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-12-06 15:52:50 +01:00
/*
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-12-06 15:52:50 +01:00
*/
return null;
2019-09-08 04:47:59 +02:00
}
2019-04-22 04:54:02 +02:00
}