2019-09-23 23:20:43 +02:00
|
|
|
package org.mercury_im.messenger.ui.roster.contacts;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-08-19 02:36:31 +02:00
|
|
|
import android.util.Log;
|
|
|
|
|
2019-05-18 10:06:16 +02:00
|
|
|
import androidx.lifecycle.LiveData;
|
2019-08-03 19:05:50 +02:00
|
|
|
import androidx.lifecycle.MutableLiveData;
|
|
|
|
import androidx.lifecycle.ViewModel;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-05-13 03:19:17 +02:00
|
|
|
import org.mercury_im.messenger.MercuryImApplication;
|
2019-12-02 00:37:35 +01:00
|
|
|
import org.mercury_im.messenger.data.repository.XmppPeerRepository;
|
2019-12-06 15:52:50 +01:00
|
|
|
import org.mercury_im.messenger.entity.contact.Peer;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-10-28 14:56:56 +01:00
|
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
|
|
|
2019-08-03 19:05:50 +02:00
|
|
|
|
2019-09-23 23:20:43 +02:00
|
|
|
public class ContactListViewModel extends ViewModel {
|
2019-05-04 00:27:02 +02:00
|
|
|
|
|
|
|
@Inject
|
2019-12-02 00:37:35 +01:00
|
|
|
XmppPeerRepository xmppContactRepository;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-12-06 15:52:50 +01:00
|
|
|
private final MutableLiveData<List<Peer>> rosterEntryList = new MutableLiveData<>();
|
2019-08-03 19:05:50 +02:00
|
|
|
private final CompositeDisposable compositeDisposable = new CompositeDisposable();
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-09-23 23:20:43 +02:00
|
|
|
public ContactListViewModel() {
|
2019-08-03 19:05:50 +02:00
|
|
|
super();
|
2019-05-13 03:19:17 +02:00
|
|
|
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
2019-09-23 23:20:43 +02:00
|
|
|
Log.d("ContactListViewModel", "Start observing database");
|
2019-08-03 19:05:50 +02:00
|
|
|
// Subscribe to changes to the contacts table and update the LiveData object for the UI.
|
2019-12-06 15:52:50 +01:00
|
|
|
compositeDisposable.add(xmppContactRepository.observeAllPeers()
|
2019-12-21 05:34:19 +01:00
|
|
|
.subscribe(rosterEntryList::setValue));
|
2019-08-03 19:05:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCleared() {
|
|
|
|
super.onCleared();
|
|
|
|
compositeDisposable.clear();
|
2019-05-04 00:27:02 +02:00
|
|
|
}
|
|
|
|
|
2019-12-06 15:52:50 +01:00
|
|
|
public LiveData<List<Peer>> getRosterEntryList() {
|
2019-05-04 00:27:02 +02:00
|
|
|
return rosterEntryList;
|
|
|
|
}
|
|
|
|
}
|