Better navigation and editing contact names

This commit is contained in:
Paul Schaub 2020-06-05 00:22:48 +02:00
parent 71e16d0fef
commit 441cf7c902
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
6 changed files with 85 additions and 28 deletions

View File

@ -1,9 +1,11 @@
package org.mercury_im.messenger.ui.chat;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
@ -19,6 +21,7 @@ import org.jxmpp.jid.impl.JidCreate;
import org.mercury_im.messenger.MercuryImApplication;
import org.mercury_im.messenger.R;
import org.mercury_im.messenger.entity.contact.Peer;
import org.mercury_im.messenger.ui.roster.contacts.detail.ContactDetailActivity;
import java.util.UUID;
@ -82,6 +85,17 @@ public class ChatActivity extends AppCompatActivity
// Listen for updates to contact information and messages
observeViewModel(chatViewModel);
}
toolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ChatActivity.this, ContactDetailActivity.class);
intent.putExtra(ContactDetailActivity.EXTRA_JID, jidString);
intent.putExtra(ContactDetailActivity.EXTRA_ACCOUNT, accountId.toString());
ChatActivity.this.startActivity(intent);
}
});
}
public void observeViewModel(ChatViewModel viewModel) {
@ -175,23 +189,7 @@ public class ChatActivity extends AppCompatActivity
return;
}
/*
// TODO: Improve by using rx
new Thread() {
@Override
public void run() {
try {
ChatManager.getInstanceFor(connectionCenter.getConnection(accountId).getConnection())
.chatWith(jid).send(msg);
} catch (SmackException.NotConnectedException e) {
Logger.getAnonymousLogger().log(Level.SEVERE,"NotConnectedException : \n" + e.getStackTrace().toString());
} catch (InterruptedException e) {
Logger.getAnonymousLogger().log(Level.SEVERE,"InterruptedException" + e.getStackTrace().toString());
}
}
}.start();
*/
chatViewModel.sendMessage(msg);
}
@Override

View File

@ -130,4 +130,9 @@ public class ChatViewModel extends ViewModel {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
}));
}
public void sendMessage(String body) {
disposable.add(messenger.sendMessage(getContact().getValue(), body)
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe());
}
}

View File

@ -1,11 +1,14 @@
package org.mercury_im.messenger.ui.roster.contacts.detail;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
@ -18,12 +21,17 @@ import androidx.lifecycle.ViewModelStoreOwner;
import com.google.android.material.chip.ChipGroup;
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;
import org.mercury_im.messenger.Messenger;
import org.mercury_im.messenger.R;
import org.mercury_im.messenger.ui.chat.ChatActivity;
import org.mercury_im.messenger.util.ColorUtil;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
public class ContactDetailFragment extends Fragment {
@ -71,6 +79,25 @@ public class ContactDetailFragment extends Fragment {
});
}
contactName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Edit Contact Name");
final EditText editText = new EditText(getContext());
editText.setText(contactName.getText());
builder.setView(editText)
.setPositiveButton("Save", (dialog, which) -> {
Completable.fromAction(() -> viewModel.changeContactName(editText.getText().toString())).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> {}, e -> Log.e(Messenger.TAG, "Error changing contact name", e));
})
.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());
builder.show();
}
});
return view;
}

View File

@ -8,9 +8,12 @@ import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import org.jivesoftware.smack.PresenceListener;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.roster.PresenceEventListener;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.impl.JidCreate;
import org.mercury_im.messenger.MercuryImApplication;
@ -126,4 +129,13 @@ public class ContactDetailViewModel extends ViewModel {
}
}
};
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);
}
}
}

View File

@ -3,6 +3,8 @@ package org.mercury_im.messenger;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.chat2.ChatManager;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jxmpp.jid.BareJid;
@ -78,10 +80,7 @@ public class Messenger {
throws ConnectionNotFoundException, XmppStringprepException, ContactAlreadyAddedException,
SmackException.NotLoggedInException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
MercuryConnection connection = getConnectionManager().getConnection(accountId);
if (connection == null) {
throw new ConnectionNotFoundException(accountId);
}
MercuryConnection connection = getConnection(accountId);
EntityBareJid jid = JidCreate.entityBareFrom(contactAddress);
Roster roster = Roster.getInstanceFor(connection.getConnection());
@ -109,10 +108,7 @@ public class Messenger {
throws ConnectionNotFoundException, XmppStringprepException,
SmackException.NotLoggedInException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
MercuryConnection connection = getConnectionManager().getConnection(contact.getAccount().getId());
if (connection == null) {
throw new ConnectionNotFoundException(contact.getAccount().getId());
}
MercuryConnection connection = getConnection(contact.getAccount().getId());
Roster roster = Roster.getInstanceFor(connection.getConnection());
EntityBareJid jid = JidCreate.entityBareFrom(contact.getAddress());
@ -123,4 +119,24 @@ public class Messenger {
throw new IllegalStateException("Contact " + jid.toString() + " not in roster!");
}
}
public Completable sendMessage(Peer peer, String body) {
return Completable.fromAction(() -> doSendMessage(peer, body));
}
private void doSendMessage(Peer contact, String body) throws ConnectionNotFoundException,
SmackException.NotConnectedException, InterruptedException {
MercuryConnection connection = getConnection(contact.getAccount().getId());
ChatManager chatManager = ChatManager.getInstanceFor(connection.getConnection());
chatManager.chatWith(JidCreate.entityBareFromOrThrowUnchecked(contact.getAddress()))
.send(body);
}
private MercuryConnection getConnection(UUID accountId) throws ConnectionNotFoundException {
MercuryConnection connection = getConnectionManager().getConnection(accountId);
if (connection == null) {
throw new ConnectionNotFoundException(accountId);
}
return connection;
}
}

View File

@ -5,7 +5,6 @@ import org.mercury_im.messenger.entity.Account;
import java.util.UUID;
import lombok.Data;
import lombok.With;
/**
* Defines a user on the network (eg. a contact, chat partner, group chat member etc).
@ -26,9 +25,9 @@ public class Peer {
}
public String getDisplayName() {
if (name != null) {
if (name != null && !name.trim().isEmpty()) {
return name;
}
return address;
return address.substring(0, address.indexOf('@'));
}
}