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-09-13 09:17:59 +02:00
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
|
|
import io.reactivex.functions.Consumer;
|
|
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
|
|
2019-05-13 03:19:17 +02:00
|
|
|
import org.mercury_im.messenger.MercuryImApplication;
|
2019-08-19 02:36:31 +02:00
|
|
|
import org.mercury_im.messenger.persistence.model.ContactModel;
|
|
|
|
import org.mercury_im.messenger.persistence.repository.RosterRepository;
|
2019-08-01 00:17:27 +02:00
|
|
|
import org.mercury_im.messenger.persistence.room.model.RoomContactModel;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
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-08-19 02:36:31 +02:00
|
|
|
RosterRepository rosterRepository;
|
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-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-08-19 02:36:31 +02:00
|
|
|
compositeDisposable.add(rosterRepository.getAllContacts()
|
2019-08-03 19:05:50 +02:00
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
2019-08-19 02:36:31 +02:00
|
|
|
.subscribe((Consumer<List<? extends ContactModel>>) o -> {
|
2019-09-23 23:20:43 +02:00
|
|
|
Log.d("ContactListViewModel", "Room changed contacts: " + o.size());
|
2019-08-19 02:36:31 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|