Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package marian.dndhelper.repositories;
- import android.arch.lifecycle.LiveData;
- import android.util.Log;
- import java.nio.file.Path;
- import java.util.List;
- import java.util.concurrent.Executor;
- import javax.inject.Inject;
- import marian.dndhelper.api.Resources;
- import marian.dndhelper.database.CharacterSheetDao;
- import marian.dndhelper.vo.CharacterSheet;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- public class Repository {
- private LiveData<List<CharacterSheet>> characterSheets;
- private static final String TAG = "Repository";
- private static int FRESH_TIMEOUT_IN_MINUTES = 1;
- private final Resources resources;
- private final CharacterSheetDao csDao;
- private final Executor executor;
- private Integer id;
- @Inject
- public Repository(Resources webservice, CharacterSheetDao characterSheetDao, Executor executor) {
- this.resources = webservice;
- this.csDao = characterSheetDao;
- this.executor = executor;
- this.id = -1;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public LiveData<List<CharacterSheet>> getAll(Integer pleyer) {
- characterSheets = csDao.loadLiveData(pleyer);
- executor.execute(this::refreshCharacterSheets);
- return characterSheets;
- }
- // OPTIMISTIC IMPLEMENTATION WE ONLY TRUST THE CLIENT
- private void refreshCharacterSheets() {
- List<CharacterSheet> localList = csDao.loadList(this.id);
- for (CharacterSheet cs : localList) {
- if (cs.getStatus().equals(0)) {
- removeFromServer(cs);
- }
- if (cs.getStatus().equals(1)) {
- addToServer(cs);
- }
- }
- // WE NOW GET WHAT THE SERVER HAS AND COMPARE IT WITH WHAT WE HAVE
- executor.execute(() -> resources.getAll(this.id).enqueue(new Callback<List<CharacterSheet>>() {
- @Override
- public void onResponse(Call<List<CharacterSheet>> call, Response<List<CharacterSheet>> response) {
- List<CharacterSheet> responseList = response.body();
- if (responseList != null && localList.isEmpty())
- responseList.forEach(rCS -> executor.execute(() -> {
- rCS.setStatus(-1);
- csDao.save(rCS);
- }));
- }
- @Override
- public void onFailure(Call<List<CharacterSheet>> call, Throwable t) {
- Log.e(TAG, "The server is offline and we can't access it's data:\n" + t.getMessage());
- }
- }));
- }
- private void addToServer(CharacterSheet cs) {
- executor.execute(() -> resources.save(cs.getStr(), cs.getDex(), cs.getCon(), cs.getIntel(),
- cs.getWis(), cs.getCha(), cs.getName(), cs.getDescription(), cs.getImageUrl(),
- this.id).enqueue(new Callback<CharacterSheet>() {
- @Override
- public void onResponse(Call<CharacterSheet> call, Response<CharacterSheet> response) {
- cs.setStatus(-1);
- }
- @Override
- public void onFailure(Call<CharacterSheet> call, Throwable t) {
- Log.e(TAG, "Error sending CS to the server:\n" + t.getMessage());
- }
- }));
- }
- private void removeFromServer(CharacterSheet cs) {
- executor.execute(() -> resources.delete(cs.getId()).enqueue(new Callback<Boolean>() {
- @Override
- public void onResponse(Call<Boolean> call, Response<Boolean> response) {
- executor.execute(() -> {
- csDao.delete(cs);
- });
- }
- @Override
- public void onFailure(Call<Boolean> call, Throwable t) {
- Log.e(TAG, "Error deleting CS on the server: " + t.getMessage());
- }
- }));
- }
- public void remove(CharacterSheet cs) {
- // WE TAKE CARE OF THE LOCAL DATABASE AND JUST SOFTDELETE THE ITEM
- executor.execute(() -> {
- cs.setStatus(0);
- csDao.softDelete(cs);
- refreshCharacterSheets();
- });
- }
- public void save(CharacterSheet cs, int anInt) {
- // WE TAKE CARE OF THE LOCAL DATABASE
- executor.execute(() -> {
- cs.setPlayer(this.id);
- cs.setStatus(1);
- csDao.save(cs);
- refreshCharacterSheets();
- });
- }
- public Resources getResources() {
- return resources;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement