2020-06-06 18:45:20 +02:00
|
|
|
package org.mercury_im.messenger.android.ui.roster.contacts.detail;
|
2020-06-04 23:49:03 +02:00
|
|
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import androidx.lifecycle.LiveData;
|
|
|
|
import androidx.lifecycle.MutableLiveData;
|
|
|
|
import androidx.lifecycle.ViewModel;
|
|
|
|
|
2020-06-05 00:22:48 +02:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
|
|
|
import org.jivesoftware.smack.XMPPException;
|
2020-06-04 23:49:03 +02:00
|
|
|
import org.jivesoftware.smack.packet.Presence;
|
|
|
|
import org.jivesoftware.smack.roster.PresenceEventListener;
|
|
|
|
import org.jivesoftware.smack.roster.Roster;
|
2020-06-05 00:22:48 +02:00
|
|
|
import org.jivesoftware.smack.roster.RosterEntry;
|
2020-06-05 13:45:53 +02:00
|
|
|
import org.jivesoftware.smack.roster.RosterGroup;
|
|
|
|
import org.jxmpp.jid.BareJid;
|
2020-06-04 23:49:03 +02:00
|
|
|
import org.jxmpp.jid.Jid;
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
2020-06-06 18:45:20 +02:00
|
|
|
import org.mercury_im.messenger.android.MercuryImApplication;
|
|
|
|
import org.mercury_im.messenger.core.Messenger;
|
|
|
|
import org.mercury_im.messenger.core.data.repository.PeerRepository;
|
2020-06-04 23:49:03 +02:00
|
|
|
import org.mercury_im.messenger.entity.contact.Peer;
|
2020-06-06 18:45:20 +02:00
|
|
|
import org.mercury_im.messenger.android.ui.avatar.AvatarDrawable;
|
|
|
|
import org.mercury_im.messenger.core.util.CombinedPresenceListener;
|
2020-06-04 23:49:03 +02:00
|
|
|
|
2020-06-05 13:45:53 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2020-06-04 23:49:03 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2020-06-05 13:45:53 +02:00
|
|
|
import io.reactivex.Completable;
|
2020-06-04 23:49:03 +02:00
|
|
|
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");
|
2020-06-05 13:45:53 +02:00
|
|
|
private MutableLiveData<List<String>> contactGroups = new MutableLiveData<>(Collections.emptyList());
|
2020-06-04 23:49:03 +02:00
|
|
|
|
|
|
|
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!");
|
2020-06-05 13:45:53 +02:00
|
|
|
roster = Roster.getInstanceFor(messenger.getConnectionManager().getConnection(accountId).getConnection());
|
|
|
|
roster.addPresenceEventListener(presenceEventListener);
|
2020-06-04 23:49:03 +02:00
|
|
|
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());
|
2020-06-05 13:45:53 +02:00
|
|
|
|
|
|
|
RosterEntry entry = roster.getEntry(JidCreate.entityBareFromOrThrowUnchecked(peerAddress));
|
|
|
|
if (entry != null) {
|
|
|
|
List<RosterGroup> groups = entry.getGroups();
|
|
|
|
List<String> groupNames = new ArrayList<>(groups.size());
|
|
|
|
for (RosterGroup g : groups) {
|
|
|
|
groupNames.add(g.getName());
|
|
|
|
}
|
|
|
|
contactGroups.postValue(groupNames);
|
|
|
|
}
|
2020-06-04 23:49:03 +02:00
|
|
|
}));
|
|
|
|
|
2020-06-05 13:45:53 +02:00
|
|
|
BareJid jid = JidCreate.entityBareFromOrThrowUnchecked(peerAddress);
|
2020-06-04 23:49:03 +02:00
|
|
|
|
2020-06-05 13:45:53 +02:00
|
|
|
Presence presence = roster.getPresence(jid);
|
2020-06-04 23:49:03 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-05 13:45:53 +02:00
|
|
|
public LiveData<List<String>> getContactGroups() {
|
|
|
|
return contactGroups;
|
|
|
|
}
|
|
|
|
|
2020-06-04 23:49:03 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-06-05 00:22:48 +02:00
|
|
|
|
|
|
|
public void changeContactName(String newName)
|
|
|
|
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException,
|
|
|
|
InterruptedException, SmackException.NoResponseException {
|
|
|
|
if (!newName.trim().isEmpty()) {
|
|
|
|
RosterEntry entry = roster.getEntry(JidCreate.entityBareFromOrThrowUnchecked(getContactAddress().getValue()));
|
|
|
|
entry.setName(newName);
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 13:45:53 +02:00
|
|
|
|
|
|
|
public Completable addContactToRosterGroup() {
|
|
|
|
return Completable.fromAction(() -> doAddContactToRosterGroup());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void doAddContactToRosterGroup() throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
|
|
|
|
String groupName = "Mercury Seven";
|
|
|
|
RosterGroup group = roster.getGroup(groupName);
|
|
|
|
if (group == null) {
|
|
|
|
group = roster.createGroup(groupName);
|
|
|
|
}
|
|
|
|
BareJid jid = JidCreate.entityBareFromOrThrowUnchecked(getContactAddress().getValue());
|
|
|
|
if (group.contains(jid)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RosterEntry entry = roster.getEntry(jid);
|
|
|
|
group.addEntry(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Completable removeContactFromRosterGroup(String group) {
|
|
|
|
return Completable.fromAction(() -> roster.getGroup(group).removeEntry(roster.getEntry(JidCreate.entityBareFromOrThrowUnchecked(getContactAddress().getValue()))));
|
|
|
|
|
|
|
|
}
|
2020-06-04 23:49:03 +02:00
|
|
|
}
|