59 lines
1.7 KiB
Java
59 lines
1.7 KiB
Java
package org.mercury_im.messenger.ui.account;
|
|
|
|
import android.app.Application;
|
|
|
|
import androidx.lifecycle.AndroidViewModel;
|
|
import androidx.lifecycle.LiveData;
|
|
import androidx.lifecycle.MutableLiveData;
|
|
|
|
import org.mercury_im.messenger.MercuryImApplication;
|
|
import org.mercury_im.messenger.data.repository.AccountRepository;
|
|
import org.mercury_im.messenger.entity.Account;
|
|
|
|
import java.util.List;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
|
|
public class AccountsViewModel extends AndroidViewModel {
|
|
|
|
@Inject
|
|
AccountRepository repository;
|
|
|
|
private final MutableLiveData<List<Account>> accounts = new MutableLiveData<>();
|
|
private final CompositeDisposable compositeDisposable = new CompositeDisposable();
|
|
|
|
@Inject
|
|
public AccountsViewModel(Application application) {
|
|
super(application);
|
|
MercuryImApplication.getApplication().getAppComponent().inject(this);
|
|
compositeDisposable.add(repository.observeAllAccounts()
|
|
.subscribe(accounts::setValue));
|
|
}
|
|
|
|
@Override
|
|
protected void onCleared() {
|
|
super.onCleared();
|
|
compositeDisposable.clear();
|
|
}
|
|
|
|
public LiveData<List<Account>> getAccounts() {
|
|
return accounts;
|
|
}
|
|
|
|
public void toggleAccountEnabled(Account accountModel) {
|
|
/*
|
|
MercuryConnection connection = connectionCenter.getConnection(accountModel);
|
|
if (connection == null) {
|
|
Toast.makeText(this.getApplication(), "MercuryConnection is null!", Toast.LENGTH_LONG).show();
|
|
return;
|
|
}
|
|
*/
|
|
|
|
accountModel.setEnabled(!accountModel.isEnabled());
|
|
repository.upsertAccount(accountModel)
|
|
.subscribe();
|
|
}
|
|
}
|