130 lines
4.7 KiB
Java
130 lines
4.7 KiB
Java
package org.mercury_im.messenger.ui.roster.contacts.detail;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
import android.util.Log;
|
|
|
|
import androidx.lifecycle.LiveData;
|
|
import androidx.lifecycle.MutableLiveData;
|
|
import androidx.lifecycle.ViewModel;
|
|
|
|
import org.jivesoftware.smack.PresenceListener;
|
|
import org.jivesoftware.smack.packet.Presence;
|
|
import org.jivesoftware.smack.roster.PresenceEventListener;
|
|
import org.jivesoftware.smack.roster.Roster;
|
|
import org.jxmpp.jid.Jid;
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
import org.mercury_im.messenger.MercuryImApplication;
|
|
import org.mercury_im.messenger.Messenger;
|
|
import org.mercury_im.messenger.data.repository.PeerRepository;
|
|
import org.mercury_im.messenger.entity.contact.Peer;
|
|
import org.mercury_im.messenger.ui.avatar.AvatarDrawable;
|
|
import org.mercury_im.messenger.util.CombinedPresenceListener;
|
|
|
|
import java.util.UUID;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
|
public class ContactDetailViewModel extends ViewModel {
|
|
|
|
@Inject
|
|
PeerRepository peerRepository;
|
|
|
|
@Inject
|
|
Messenger messenger;
|
|
|
|
private MutableLiveData<UUID> contactAccountId = new MutableLiveData<>(UUID.randomUUID());
|
|
private MutableLiveData<String> contactAddress = new MutableLiveData<>("alice@wonderland.lit");
|
|
private MutableLiveData<Drawable> contactAvatar = new MutableLiveData<>(new AvatarDrawable("Alice Wonderland", "alice@wonderland.lit"));
|
|
private MutableLiveData<Presence.Mode> contactPresenceMode = new MutableLiveData<>(Presence.Mode.available);
|
|
private MutableLiveData<String> contactPresenceStatus = new MutableLiveData<>("Going down the rabbit hole.");
|
|
private MutableLiveData<String> contactName = new MutableLiveData<>("Alice Wonderland");
|
|
private MutableLiveData<String> contactAccountAddress = new MutableLiveData<>("mad@hatter.lit");
|
|
|
|
private Roster roster;
|
|
|
|
private CompositeDisposable disposable = new CompositeDisposable();
|
|
|
|
public ContactDetailViewModel() {
|
|
super();
|
|
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
|
}
|
|
|
|
public void bind(UUID accountId, String peerAddress) {
|
|
Log.d("MMMMMM", "Bind!");
|
|
contactAddress.setValue(peerAddress);
|
|
contactAccountId.setValue(accountId);
|
|
disposable.add(peerRepository.observePeerByAddress(accountId, peerAddress)
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(peerOptional -> {
|
|
if (!peerOptional.isPresent()) {
|
|
return;
|
|
}
|
|
Peer peer = peerOptional.getItem();
|
|
contactAccountAddress.setValue(peer.getAccount().getAddress());
|
|
contactAvatar.setValue(new AvatarDrawable(peer.getDisplayName(), peer.getAddress()));
|
|
contactName.setValue(peer.getDisplayName());
|
|
}));
|
|
|
|
roster = Roster.getInstanceFor(messenger.getConnectionManager().getConnection(accountId).getConnection());
|
|
roster.addPresenceEventListener(presenceEventListener);
|
|
|
|
Presence presence = roster.getPresence(JidCreate.entityBareFromOrThrowUnchecked(peerAddress));
|
|
if (presence != null) {
|
|
contactPresenceMode.postValue(presence.getMode());
|
|
contactPresenceStatus.postValue(presence.getStatus());
|
|
}
|
|
}
|
|
|
|
public LiveData<String> getContactAddress() {
|
|
return contactAddress;
|
|
}
|
|
|
|
@Override
|
|
protected void onCleared() {
|
|
super.onCleared();
|
|
disposable.dispose();
|
|
if (roster != null) {
|
|
roster.removePresenceEventListener(presenceEventListener);
|
|
}
|
|
}
|
|
|
|
public LiveData<UUID> getAccountId() {
|
|
return contactAccountId;
|
|
}
|
|
|
|
public LiveData<Drawable> getContactAvatar() {
|
|
return contactAvatar;
|
|
}
|
|
|
|
public LiveData<Presence.Mode> getContactPresenceMode() {
|
|
return contactPresenceMode;
|
|
}
|
|
|
|
public LiveData<String> getContactName() {
|
|
return contactName;
|
|
}
|
|
|
|
public LiveData<String> getContactPresenceStatus() {
|
|
return contactPresenceStatus;
|
|
}
|
|
|
|
public LiveData<String> getContactAccountAddress() {
|
|
return contactAccountAddress;
|
|
}
|
|
|
|
private final PresenceEventListener presenceEventListener = new CombinedPresenceListener() {
|
|
@Override
|
|
public void presenceReceived(Jid address, Presence presence) {
|
|
if (presence.getFrom().asBareJid().toString().equals(getContactAddress().getValue())) {
|
|
contactPresenceMode.postValue(presence.getMode());
|
|
contactPresenceStatus.postValue(presence.getStatus());
|
|
}
|
|
}
|
|
};
|
|
}
|