mirror of
https://codeberg.org/Mercury-IM/Mercury-IM
synced 2024-06-25 21:14:50 +02:00
Move chat logic to ChatViewModel
This commit is contained in:
parent
71574298c1
commit
de39e356f0
|
@ -18,40 +18,18 @@ import org.jxmpp.jid.impl.JidCreate;
|
|||
import org.mercury_im.messenger.MercuryImApplication;
|
||||
import org.mercury_im.messenger.R;
|
||||
import org.mercury_im.messenger.core.centers.ConnectionCenter;
|
||||
import org.mercury_im.messenger.persistence.model.AccountModel;
|
||||
import org.mercury_im.messenger.persistence.model.ContactModel;
|
||||
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.persistence.repository.RosterRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import io.reactivex.Maybe;
|
||||
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;
|
||||
|
||||
public class ChatActivity extends AppCompatActivity implements ChatInputFragment.OnChatInputActionListener {
|
||||
|
||||
public static final String EXTRA_JID = "JID";
|
||||
public static final String EXTRA_ACCOUNT = "ACCOUNT";
|
||||
|
||||
@Inject
|
||||
AccountRepository accountRepository;
|
||||
|
||||
@Inject
|
||||
MessageRepository messageRepository;
|
||||
|
||||
@Inject
|
||||
RosterRepository rosterRepository;
|
||||
|
||||
@Inject
|
||||
ConnectionCenter connectionCenter;
|
||||
|
||||
|
@ -61,6 +39,8 @@ public class ChatActivity extends AppCompatActivity implements ChatInputFragment
|
|||
@BindView(R.id.recyclerView)
|
||||
RecyclerView recyclerView;
|
||||
|
||||
private final ChatRecyclerViewAdapter recyclerViewAdapter = new ChatRecyclerViewAdapter();
|
||||
|
||||
private ChatViewModel chatViewModel;
|
||||
|
||||
private final CompositeDisposable disposable = new CompositeDisposable();
|
||||
|
@ -77,10 +57,10 @@ public class ChatActivity extends AppCompatActivity implements ChatInputFragment
|
|||
ButterKnife.bind(this);
|
||||
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
||||
|
||||
ChatRecyclerViewAdapter adapter = new ChatRecyclerViewAdapter();
|
||||
recyclerView.setAdapter(adapter);
|
||||
recyclerView.setAdapter(recyclerViewAdapter);
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
|
@ -93,42 +73,22 @@ public class ChatActivity extends AppCompatActivity implements ChatInputFragment
|
|||
jid = JidCreate.entityBareFromOrThrowUnchecked(jidString);
|
||||
accountId = savedInstanceState.getLong(EXTRA_ACCOUNT);
|
||||
|
||||
Maybe<AccountModel> account = accountRepository.getAccount(accountId);
|
||||
chatViewModel = ViewModelProviders.of(this).get(ChatViewModel.class);
|
||||
|
||||
disposable.add(
|
||||
account.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(accountModel -> chatViewModel.init(accountModel, jid)));
|
||||
|
||||
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);
|
||||
|
||||
chatViewModel.init(accountId, jid);
|
||||
observeViewModel(chatViewModel);
|
||||
}
|
||||
}
|
||||
|
||||
public void observeViewModel(ChatViewModel viewModel) {
|
||||
viewModel.getContactDisplayName().observe(this,
|
||||
name -> getSupportActionBar().setTitle(name));
|
||||
|
||||
viewModel.getMessages().observe(this, messageModels -> {
|
||||
recyclerViewAdapter.updateMessages(messageModels);
|
||||
recyclerView.scrollToPosition(messageModels.size() - 1);
|
||||
});
|
||||
}
|
||||
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
disposable.clear();
|
||||
|
|
|
@ -44,6 +44,7 @@ public class ChatRecyclerViewAdapter extends RecyclerView.Adapter<ChatRecyclerVi
|
|||
holder.body.setBackgroundResource(background.getOut());
|
||||
}
|
||||
holder.body.setText(message.getBody());
|
||||
holder.itemView.invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,19 +1,81 @@
|
|||
package org.mercury_im.messenger.ui.chat;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import org.jxmpp.jid.EntityBareJid;
|
||||
import org.mercury_im.messenger.persistence.model.AccountModel;
|
||||
import org.mercury_im.messenger.MercuryImApplication;
|
||||
import org.mercury_im.messenger.persistence.model.ContactModel;
|
||||
import org.mercury_im.messenger.persistence.model.MessageModel;
|
||||
import org.mercury_im.messenger.persistence.repository.MessageRepository;
|
||||
import org.mercury_im.messenger.persistence.repository.RosterRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
import io.reactivex.functions.Consumer;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
public class ChatViewModel extends ViewModel {
|
||||
|
||||
private AccountModel account;
|
||||
private final CompositeDisposable disposable = new CompositeDisposable();
|
||||
|
||||
private EntityBareJid contact;
|
||||
@Inject
|
||||
MessageRepository messageRepository;
|
||||
|
||||
public void init(AccountModel account, EntityBareJid contact) {
|
||||
this.account = account;
|
||||
this.contact = contact;
|
||||
@Inject
|
||||
RosterRepository rosterRepository;
|
||||
|
||||
private MutableLiveData<ContactModel> contact = new MutableLiveData<>();
|
||||
private MutableLiveData<List<MessageModel>> messages = new MutableLiveData<>();
|
||||
private MutableLiveData<String> contactDisplayName = new MutableLiveData<>();
|
||||
|
||||
public ChatViewModel() {
|
||||
super();
|
||||
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
||||
}
|
||||
|
||||
public void init(long accountId, EntityBareJid jid) {
|
||||
disposable.add(rosterRepository.getContact(accountId, jid)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe((Consumer<ContactModel>)
|
||||
contactModel -> {
|
||||
ChatViewModel.this.contact.setValue(contactModel);
|
||||
|
||||
String name = contactModel.getRosterName();
|
||||
if (name == null) {
|
||||
name = jid.getLocalpart().asUnescapedString();
|
||||
}
|
||||
contactDisplayName.setValue(name);
|
||||
}));
|
||||
|
||||
disposable.add(messageRepository.getAllMessagesOfChat(accountId, jid)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe((Consumer<List<MessageModel>>)
|
||||
messages -> ChatViewModel.this.messages.setValue(messages)));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
super.onCleared();
|
||||
disposable.clear();
|
||||
}
|
||||
|
||||
public LiveData<List<MessageModel>> getMessages() {
|
||||
return messages;
|
||||
}
|
||||
|
||||
public LiveData<ContactModel> getContact() {
|
||||
return contact;
|
||||
}
|
||||
|
||||
public LiveData<String> getContactDisplayName() {
|
||||
return contactDisplayName;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@
|
|||
android:layout_marginEnd="8dp"
|
||||
android:background="@null"
|
||||
android:hint="Type your message"
|
||||
android:inputType="textMultiLine|textAutoCorrect"
|
||||
android:inputType="textMultiLine|textAutoCorrect|textCapSentences"
|
||||
android:maxLines="6"
|
||||
android:padding="8dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
|
|
Loading…
Reference in a new issue