2019-05-13 03:19:17 +02:00
|
|
|
package org.mercury_im.messenger.ui.roster;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
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-08-01 00:17:27 +02:00
|
|
|
import org.mercury_im.messenger.persistence.repository.ContactRepository;
|
|
|
|
import org.mercury_im.messenger.persistence.room.model.RoomContactModel;
|
2019-08-04 04:22:08 +02:00
|
|
|
import org.mercury_im.messenger.persistence.room.repository.IContactRepository;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-08-03 19:05:50 +02:00
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
|
|
|
|
|
public class RosterViewModel extends ViewModel {
|
2019-05-04 00:27:02 +02:00
|
|
|
|
|
|
|
@Inject
|
2019-08-04 04:22:08 +02:00
|
|
|
ContactRepository contactRepository;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-08-03 19:05:50 +02:00
|
|
|
private final MutableLiveData<List<RoomContactModel>> rosterEntryList = new MutableLiveData<>();
|
|
|
|
private final CompositeDisposable compositeDisposable = new CompositeDisposable();
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2019-08-03 19:05:50 +02:00
|
|
|
public RosterViewModel() {
|
|
|
|
super();
|
2019-05-13 03:19:17 +02:00
|
|
|
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
2019-08-03 19:05:50 +02:00
|
|
|
|
|
|
|
// Subscribe to changes to the contacts table and update the LiveData object for the UI.
|
|
|
|
compositeDisposable.add(contactRepository.getAllContacts()
|
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
2019-08-04 04:22:08 +02:00
|
|
|
.subscribe(o -> rosterEntryList.setValue((List<RoomContactModel>) o)));
|
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-08-01 00:17:27 +02:00
|
|
|
public LiveData<List<RoomContactModel>> getRosterEntryList() {
|
2019-05-04 00:27:02 +02:00
|
|
|
return rosterEntryList;
|
|
|
|
}
|
|
|
|
}
|