2019-05-13 03:19:17 +02:00
|
|
|
package org.mercury_im.messenger.ui.chat;
|
2019-04-22 04:54:02 +02:00
|
|
|
|
|
|
|
import android.os.Bundle;
|
2019-06-21 03:45:33 +02:00
|
|
|
import android.util.Log;
|
2019-04-22 04:54:02 +02:00
|
|
|
|
2019-06-24 01:41:17 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.appcompat.widget.Toolbar;
|
2019-06-21 03:45:33 +02:00
|
|
|
import androidx.lifecycle.LiveData;
|
|
|
|
import androidx.lifecycle.Observer;
|
|
|
|
import androidx.lifecycle.ViewModelProviders;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.SmackException;
|
|
|
|
import org.jivesoftware.smack.chat2.ChatManager;
|
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
|
|
import org.mercury_im.messenger.MercuryImApplication;
|
2019-05-13 03:19:17 +02:00
|
|
|
import org.mercury_im.messenger.R;
|
2019-06-21 03:45:33 +02:00
|
|
|
import org.mercury_im.messenger.persistence.model.AccountModel;
|
|
|
|
import org.mercury_im.messenger.persistence.model.MessageModel;
|
|
|
|
import org.mercury_im.messenger.persistence.repository.AccountRepository;
|
|
|
|
import org.mercury_im.messenger.persistence.repository.MessageRepository;
|
|
|
|
import org.mercury_im.messenger.ui.BindingActivity;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-06-24 01:41:17 +02:00
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
2019-06-21 03:45:33 +02:00
|
|
|
public class ChatActivity extends BindingActivity implements ChatInputFragment.OnChatInputActionListener {
|
|
|
|
|
2019-06-24 01:41:17 +02:00
|
|
|
public static final String EXTRA_JID = "JID";
|
|
|
|
public static final String EXTRA_ACCOUNT = "ACCOUNT";
|
|
|
|
|
2019-06-21 03:45:33 +02:00
|
|
|
@Inject
|
|
|
|
AccountRepository accountRepository;
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
MessageRepository messageRepository;
|
|
|
|
|
2019-06-24 01:41:17 +02:00
|
|
|
@BindView(R.id.toolbar)
|
|
|
|
Toolbar toolbar;
|
|
|
|
|
|
|
|
@BindView(R.id.recyclerView)
|
|
|
|
RecyclerView recyclerView;
|
|
|
|
|
|
|
|
private ChatViewModel chatViewModel;
|
|
|
|
|
2019-06-21 03:45:33 +02:00
|
|
|
private EntityBareJid jid;
|
2019-04-22 04:54:02 +02:00
|
|
|
|
2019-06-21 03:45:33 +02:00
|
|
|
private long accountId;
|
2019-04-22 04:54:02 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2019-06-24 01:41:17 +02:00
|
|
|
Log.d("Mercury", "onCreate");
|
2019-04-22 04:54:02 +02:00
|
|
|
setContentView(R.layout.activity_chat);
|
2019-06-24 01:41:17 +02:00
|
|
|
ButterKnife.bind(this);
|
2019-06-21 03:45:33 +02:00
|
|
|
|
2019-06-24 01:41:17 +02:00
|
|
|
setSupportActionBar(toolbar);
|
2019-06-21 03:45:33 +02:00
|
|
|
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
|
|
|
|
|
|
|
ChatRecyclerViewAdapter adapter = new ChatRecyclerViewAdapter();
|
|
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
|
|
|
|
|
|
|
if (savedInstanceState == null) {
|
|
|
|
savedInstanceState = getIntent().getExtras();
|
2019-06-21 04:44:59 +02:00
|
|
|
if (savedInstanceState == null) return;
|
2019-06-21 03:45:33 +02:00
|
|
|
}
|
2019-06-21 04:44:59 +02:00
|
|
|
|
2019-06-24 01:41:17 +02:00
|
|
|
String jidString = savedInstanceState.getString(EXTRA_JID);
|
2019-06-21 03:45:33 +02:00
|
|
|
if (jidString != null) {
|
2019-06-24 01:41:17 +02:00
|
|
|
getSupportActionBar().setTitle(jidString);
|
2019-06-21 03:45:33 +02:00
|
|
|
jid = JidCreate.entityBareFromOrThrowUnchecked(jidString);
|
2019-06-24 01:41:17 +02:00
|
|
|
accountId = savedInstanceState.getLong(EXTRA_ACCOUNT);
|
2019-06-21 03:45:33 +02:00
|
|
|
|
|
|
|
LiveData<AccountModel> accountModel = accountRepository.getAccount(accountId);
|
2019-06-24 01:41:17 +02:00
|
|
|
chatViewModel = ViewModelProviders.of(this).get(ChatViewModel.class);
|
2019-06-21 03:45:33 +02:00
|
|
|
|
|
|
|
accountModel.observe(this, new Observer<AccountModel>() {
|
|
|
|
@Override
|
|
|
|
public void onChanged(AccountModel accountModel) {
|
2019-06-24 01:41:17 +02:00
|
|
|
chatViewModel.init(accountModel, jid);
|
2019-06-21 03:45:33 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-21 04:44:59 +02:00
|
|
|
LiveData<List<MessageModel>> messages = messageRepository.getAllMessagesOfChat(accountId, jid);
|
2019-06-21 03:45:33 +02:00
|
|
|
messages.observe(this, new Observer<List<MessageModel>>() {
|
|
|
|
@Override
|
|
|
|
public void onChanged(List<MessageModel> messageModels) {
|
|
|
|
Log.d(MercuryImApplication.TAG, "Updating messages: " + messageModels.size());
|
|
|
|
adapter.updateMessages(messageModels);
|
2019-06-21 04:44:59 +02:00
|
|
|
recyclerView.scrollToPosition(messageModels.size() - 1);
|
2019-06-21 03:45:33 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-23 02:34:14 +02:00
|
|
|
getSupportFragmentManager().beginTransaction().replace(R.id.stub_compose, ChatInputFragment.newInstance())
|
2019-06-21 03:45:33 +02:00
|
|
|
.commitAllowingStateLoss();
|
|
|
|
}
|
2019-06-06 23:32:41 +02:00
|
|
|
}
|
|
|
|
|
2019-06-24 01:41:17 +02:00
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(@NonNull Bundle outState) {
|
|
|
|
outState.putString(EXTRA_JID, jid.toString());
|
|
|
|
outState.putLong(EXTRA_ACCOUNT, accountId);
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
}
|
|
|
|
|
2019-06-06 23:32:41 +02:00
|
|
|
@Override
|
|
|
|
public void onButtonMediaClicked() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onComposingBodyChanged(String body) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onComposingBodySend(String body) {
|
2019-07-02 00:46:40 +02:00
|
|
|
String msg = body.trim();
|
|
|
|
if (msg.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-21 03:45:33 +02:00
|
|
|
new Thread() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
ChatManager.getInstanceFor(connectionService.getConnection(accountId).getConnection())
|
2019-07-02 00:46:40 +02:00
|
|
|
.chatWith(jid).send(msg);
|
2019-06-21 03:45:33 +02:00
|
|
|
} catch (SmackException.NotConnectedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.start();
|
2019-06-06 23:32:41 +02:00
|
|
|
|
2019-04-22 04:54:02 +02:00
|
|
|
}
|
|
|
|
}
|