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

114 lines
3.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
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;
2019-12-21 16:30:14 +01:00
import java.util.UUID;
2019-08-29 00:22:26 +02:00
import javax.inject.Inject;
import io.reactivex.Completable;
2019-12-21 05:34:19 +01:00
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
2019-12-21 05:34:19 +01:00
import io.reactivex.schedulers.Schedulers;
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);
}
2019-12-21 16:30:14 +01:00
public void init(UUID 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) {
2019-12-21 05:34:19 +01:00
Observable<List<Message>> observable = query.isEmpty() ?
messageRepository.observeMessages(chat.getValue()) :
messageRepository.findMessagesWithBody(chat.getValue(), query);
2019-12-21 05:34:19 +01:00
disposable.add(observable.subscribe(messages ->
ChatViewModel.this.messages.setValue(messages)));
}
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
}