2020-06-06 18:45:20 +02:00
|
|
|
package org.mercury_im.messenger.android.ui.roster.contacts;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2020-07-13 01:39:26 +02:00
|
|
|
import android.app.Application;
|
2019-08-19 02:36:31 +02:00
|
|
|
|
2020-07-13 01:39:26 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.lifecycle.AndroidViewModel;
|
2019-05-18 10:06:16 +02:00
|
|
|
import androidx.lifecycle.LiveData;
|
2019-08-03 19:05:50 +02:00
|
|
|
import androidx.lifecycle.MutableLiveData;
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2020-06-06 18:45:20 +02:00
|
|
|
import org.mercury_im.messenger.android.MercuryImApplication;
|
2020-07-13 01:39:26 +02:00
|
|
|
import org.mercury_im.messenger.android.ui.MercuryAndroidViewModel;
|
2020-06-06 18:45:20 +02:00
|
|
|
import org.mercury_im.messenger.core.Messenger;
|
2020-07-13 01:39:26 +02:00
|
|
|
import org.mercury_im.messenger.core.viewmodel.roster.ContactListViewModel;
|
2020-06-04 00:04:20 +02:00
|
|
|
import org.mercury_im.messenger.entity.Account;
|
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;
|
2020-06-04 23:49:03 +02:00
|
|
|
import lombok.Getter;
|
2019-10-28 14:56:56 +01:00
|
|
|
|
2019-08-03 19:05:50 +02:00
|
|
|
|
2020-07-13 01:39:26 +02:00
|
|
|
public class AndroidContactListViewModel extends AndroidViewModel
|
|
|
|
implements MercuryAndroidViewModel<ContactListViewModel> {
|
2019-05-04 00:27:02 +02:00
|
|
|
|
|
|
|
@Inject
|
2020-07-13 01:39:26 +02:00
|
|
|
ContactListViewModel commonViewModel;
|
2020-06-04 00:04:20 +02:00
|
|
|
|
2020-06-04 23:49:03 +02:00
|
|
|
@Inject
|
|
|
|
@Getter
|
|
|
|
Messenger messenger;
|
|
|
|
|
2019-12-06 15:52:50 +01:00
|
|
|
private final MutableLiveData<List<Peer>> rosterEntryList = new MutableLiveData<>();
|
2020-06-04 00:04:20 +02:00
|
|
|
private final MutableLiveData<List<Account>> accounts = new MutableLiveData<>();
|
2019-08-03 19:05:50 +02:00
|
|
|
private final CompositeDisposable compositeDisposable = new CompositeDisposable();
|
2019-05-04 00:27:02 +02:00
|
|
|
|
2020-07-13 01:39:26 +02:00
|
|
|
public AndroidContactListViewModel(@NonNull Application application) {
|
|
|
|
super(application);
|
|
|
|
((MercuryImApplication) application).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.
|
2020-07-13 01:39:26 +02:00
|
|
|
compositeDisposable.add(getCommonViewModel().getContacts()
|
2020-06-24 22:50:26 +02:00
|
|
|
.subscribe(rosterEntryList::postValue));
|
2020-07-13 01:39:26 +02:00
|
|
|
compositeDisposable.add(getCommonViewModel().getAccounts()
|
2020-06-24 22:50:26 +02:00
|
|
|
.subscribe(accounts::postValue));
|
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;
|
|
|
|
}
|
2020-06-04 00:04:20 +02:00
|
|
|
|
|
|
|
public LiveData<List<Account>> getAccounts() {
|
|
|
|
return accounts;
|
|
|
|
}
|
2020-07-13 01:39:26 +02:00
|
|
|
|
|
|
|
public void onContactSearchQueryTextChanged(String query) {
|
|
|
|
getCommonViewModel().onContactSearchQueryChanged(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ContactListViewModel getCommonViewModel() {
|
|
|
|
return commonViewModel;
|
|
|
|
}
|
2019-05-04 00:27:02 +02:00
|
|
|
}
|