85 lines
2.4 KiB
Java
85 lines
2.4 KiB
Java
package org.mercury_im.messenger.persistence.room.repository;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import androidx.lifecycle.LiveData;
|
|
|
|
import org.mercury_im.messenger.persistence.repository.AccountRepository;
|
|
import org.mercury_im.messenger.persistence.room.dao.AccountDao;
|
|
import org.mercury_im.messenger.persistence.room.model.RoomAccountModel;
|
|
|
|
import java.util.List;
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
public class IAccountRepository implements AccountRepository<RoomAccountModel> {
|
|
|
|
private final AccountDao accountDao;
|
|
|
|
@Inject
|
|
public IAccountRepository(AccountDao accountDao) {
|
|
this.accountDao = accountDao;
|
|
}
|
|
|
|
@Override
|
|
public LiveData<RoomAccountModel> getAccount(long accountId) {
|
|
return accountDao.getAccountById(accountId);
|
|
}
|
|
|
|
@Override
|
|
public LiveData<List<RoomAccountModel>> getAllAccountsLive() {
|
|
return accountDao.getAllAccountsLive();
|
|
}
|
|
|
|
@Override
|
|
public List<RoomAccountModel> getAllAccounts() {
|
|
return accountDao.getAllAccounts();
|
|
}
|
|
|
|
|
|
@Override
|
|
public long insertAccount(RoomAccountModel 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<RoomAccountModel, Void, Long> {
|
|
|
|
private final AccountDao accountDao;
|
|
|
|
private InsertAsyncTask(AccountDao accountDao) {
|
|
this.accountDao = accountDao;
|
|
}
|
|
|
|
@Override
|
|
protected Long doInBackground(RoomAccountModel... accountModels) {
|
|
for (RoomAccountModel accountModel : accountModels) {
|
|
return accountDao.insert(accountModel);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static class QueryAsyncTask extends AsyncTask<Long, Void, LiveData<RoomAccountModel>> {
|
|
|
|
private final AccountDao accountDao;
|
|
|
|
private QueryAsyncTask(AccountDao accountDao) {
|
|
this.accountDao = accountDao;
|
|
}
|
|
|
|
@Override
|
|
protected LiveData<RoomAccountModel> doInBackground(Long... longs) {
|
|
return accountDao.getAccountById(longs[0]);
|
|
}
|
|
}
|
|
}
|