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

184 lines
6.3 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
import android.os.Bundle;
2019-06-21 03:45:33 +02:00
import android.util.Log;
2019-08-11 03:32:13 +02:00
import android.view.Menu;
2019-04-22 04:54:02 +02:00
2019-06-24 01:41:17 +02:00
import androidx.annotation.NonNull;
2019-08-04 04:22:08 +02:00
import androidx.appcompat.app.AppCompatActivity;
2019-06-24 01:41:17 +02:00
import androidx.appcompat.widget.Toolbar;
2019-06-21 03:45:33 +02:00
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2019-08-25 17:54:03 +02:00
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.chat2.ChatManager;
2019-06-21 03:45:33 +02:00
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-08-25 17:54:03 +02:00
import org.mercury_im.messenger.core.centers.ConnectionCenter;
2019-06-21 03:45:33 +02:00
import org.mercury_im.messenger.persistence.model.AccountModel;
2019-08-24 23:06:06 +02:00
import org.mercury_im.messenger.persistence.model.ContactModel;
2019-06-21 03:45:33 +02:00
import org.mercury_im.messenger.persistence.model.MessageModel;
import org.mercury_im.messenger.persistence.repository.AccountRepository;
import org.mercury_im.messenger.persistence.repository.MessageRepository;
2019-08-24 23:06:06 +02:00
import org.mercury_im.messenger.persistence.repository.RosterRepository;
2019-06-21 03:45:33 +02:00
import java.util.List;
import javax.inject.Inject;
2019-06-24 01:41:17 +02:00
import butterknife.BindView;
import butterknife.ButterKnife;
2019-08-04 04:22:08 +02:00
import io.reactivex.Maybe;
2019-08-24 23:06:06 +02:00
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
2019-06-24 01:41:17 +02:00
2019-08-04 04:22:08 +02:00
public class ChatActivity extends AppCompatActivity implements ChatInputFragment.OnChatInputActionListener {
2019-06-21 03:45:33 +02:00
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-08-24 23:06:06 +02:00
@Inject
RosterRepository rosterRepository;
2019-08-25 17:54:03 +02:00
@Inject
ConnectionCenter connectionCenter;
2019-06-24 01:41:17 +02:00
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.recyclerView)
RecyclerView recyclerView;
private ChatViewModel chatViewModel;
2019-08-24 23:06:06 +02:00
private final CompositeDisposable disposable = new CompositeDisposable();
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) {
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
2019-08-24 23:06:06 +02:00
Maybe<AccountModel> account = 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
2019-08-24 23:06:06 +02:00
disposable.add(
account.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
2019-08-25 18:32:27 +02:00
.subscribe(accountModel -> chatViewModel.init(accountModel, jid)));
2019-08-24 23:06:06 +02:00
disposable.add(
rosterRepository.getContact(accountId, jid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((Consumer<ContactModel>) o -> {
String title = o.getRosterName();
if (title == null) {
title = jidString;
}
getSupportActionBar().setTitle(title);
}));
Observable<List<MessageModel>> messages = messageRepository.getAllMessagesOfChat(accountId, jid);
disposable.add(
messages.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnNext(messageModels -> Log.d(MercuryImApplication.TAG, "Updating messages: " + messageModels.size()))
.subscribe(messageModels -> {
adapter.updateMessages(messageModels);
recyclerView.scrollToPosition(messageModels.size() - 1);
}));
ChatInputFragment composer = (ChatInputFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment_compose);
2019-08-25 17:54:03 +02:00
2019-06-21 03:45:33 +02:00
}
2019-06-06 23:32:41 +02:00
}
2019-08-24 23:06:06 +02:00
public void onStop() {
super.onStop();
disposable.clear();
}
2019-08-11 03:32:13 +02:00
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_chat, menu);
return true;
}
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-08-24 23:06:06 +02:00
2019-08-25 17:54:03 +02:00
// TODO: Improve by using rx
2019-06-21 03:45:33 +02:00
new Thread() {
@Override
public void run() {
try {
2019-08-25 17:54:03 +02:00
ChatManager.getInstanceFor(connectionCenter.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-04-22 04:54:02 +02:00
}
}