77 lines
2.1 KiB
Java
77 lines
2.1 KiB
Java
package org.mercury_im.messenger.persistence.repository.account;
|
|
|
|
import androidx.lifecycle.LiveData;
|
|
import android.os.AsyncTask;
|
|
|
|
import org.mercury_im.messenger.persistence.database.dao.AccountDao;
|
|
import org.mercury_im.messenger.persistence.database.model.AccountModel;
|
|
|
|
import java.util.List;
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
public class AccountRepositoryImpl implements AccountRepository {
|
|
|
|
private final AccountDao accountDao;
|
|
|
|
@Inject
|
|
public AccountRepositoryImpl(AccountDao accountDao) {
|
|
this.accountDao = accountDao;
|
|
}
|
|
|
|
@Override
|
|
public LiveData<AccountModel> getAccount(long accountId) {
|
|
return accountDao.getAccountById(accountId);
|
|
}
|
|
|
|
@Override
|
|
public LiveData<List<AccountModel>> getAllAccounts() {
|
|
return accountDao.getAllAccounts();
|
|
}
|
|
|
|
@Override
|
|
public long insertAccount(AccountModel accountModel) {
|
|
InsertAsyncTask task = new InsertAsyncTask(accountDao);
|
|
try {
|
|
return task.execute(accountModel).get();
|
|
} catch (ExecutionException e) {
|
|
e.printStackTrace();
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
private static class InsertAsyncTask extends AsyncTask<AccountModel, Void, Long> {
|
|
|
|
private final AccountDao accountDao;
|
|
|
|
private InsertAsyncTask(AccountDao accountDao) {
|
|
this.accountDao = accountDao;
|
|
}
|
|
|
|
@Override
|
|
protected Long doInBackground(AccountModel... accountModels) {
|
|
for (AccountModel accountModel : accountModels) {
|
|
return accountDao.insertAccount(accountModel);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static class QueryAsyncTask extends AsyncTask<Long, Void, LiveData<AccountModel>> {
|
|
|
|
private final AccountDao accountDao;
|
|
|
|
private QueryAsyncTask(AccountDao accountDao) {
|
|
this.accountDao = accountDao;
|
|
}
|
|
|
|
@Override
|
|
protected LiveData<AccountModel> doInBackground(Long... longs) {
|
|
return accountDao.getAccountById(longs[0]);
|
|
}
|
|
}
|
|
}
|