Advertisement
ostyleo

Untitled

Jan 3rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. package marian.dndhelper.repositories;
  2.  
  3. import android.arch.lifecycle.LiveData;
  4. import android.util.Log;
  5.  
  6. import java.nio.file.Path;
  7. import java.util.List;
  8. import java.util.concurrent.Executor;
  9.  
  10. import javax.inject.Inject;
  11.  
  12. import marian.dndhelper.api.Resources;
  13. import marian.dndhelper.database.CharacterSheetDao;
  14. import marian.dndhelper.vo.CharacterSheet;
  15. import retrofit2.Call;
  16. import retrofit2.Callback;
  17. import retrofit2.Response;
  18.  
  19. public class Repository {
  20.     private LiveData<List<CharacterSheet>> characterSheets;
  21.     private static final String TAG = "Repository";
  22.     private static int FRESH_TIMEOUT_IN_MINUTES = 1;
  23.  
  24.     private final Resources resources;
  25.     private final CharacterSheetDao csDao;
  26.     private final Executor executor;
  27.     private Integer id;
  28.  
  29.     @Inject
  30.     public Repository(Resources webservice, CharacterSheetDao characterSheetDao, Executor executor) {
  31.         this.resources = webservice;
  32.         this.csDao = characterSheetDao;
  33.         this.executor = executor;
  34.         this.id = -1;
  35.     }
  36.  
  37.     public void setId(Integer id) {
  38.         this.id = id;
  39.     }
  40.  
  41.     public LiveData<List<CharacterSheet>> getAll(Integer pleyer) {
  42.         characterSheets = csDao.loadLiveData(pleyer);
  43.         executor.execute(this::refreshCharacterSheets);
  44.         return characterSheets;
  45.     }
  46.  
  47.     // OPTIMISTIC IMPLEMENTATION WE ONLY TRUST THE CLIENT
  48.     private void refreshCharacterSheets() {
  49.         List<CharacterSheet> localList = csDao.loadList(this.id);
  50.         for (CharacterSheet cs : localList) {
  51.             if (cs.getStatus().equals(0)) {
  52.                 removeFromServer(cs);
  53.             }
  54.  
  55.             if (cs.getStatus().equals(1)) {
  56.                 addToServer(cs);
  57.             }
  58.         }
  59.  
  60.         // WE NOW GET WHAT THE SERVER HAS AND COMPARE IT WITH WHAT WE HAVE
  61.         executor.execute(() -> resources.getAll(this.id).enqueue(new Callback<List<CharacterSheet>>() {
  62.             @Override
  63.             public void onResponse(Call<List<CharacterSheet>> call, Response<List<CharacterSheet>> response) {
  64.                 List<CharacterSheet> responseList = response.body();
  65.                 if (responseList != null && localList.isEmpty())
  66.                     responseList.forEach(rCS -> executor.execute(() -> {
  67.                         rCS.setStatus(-1);
  68.                         csDao.save(rCS);
  69.                     }));
  70.             }
  71.  
  72.             @Override
  73.             public void onFailure(Call<List<CharacterSheet>> call, Throwable t) {
  74.                 Log.e(TAG, "The server is offline and we can't access it's data:\n" + t.getMessage());
  75.             }
  76.         }));
  77.     }
  78.  
  79.     private void addToServer(CharacterSheet cs) {
  80.         executor.execute(() -> resources.save(cs.getStr(), cs.getDex(), cs.getCon(), cs.getIntel(),
  81.                 cs.getWis(), cs.getCha(), cs.getName(), cs.getDescription(), cs.getImageUrl(),
  82.                 this.id).enqueue(new Callback<CharacterSheet>() {
  83.  
  84.             @Override
  85.             public void onResponse(Call<CharacterSheet> call, Response<CharacterSheet> response) {
  86.                 cs.setStatus(-1);
  87.             }
  88.  
  89.             @Override
  90.             public void onFailure(Call<CharacterSheet> call, Throwable t) {
  91.                 Log.e(TAG, "Error sending CS to the server:\n" + t.getMessage());
  92.             }
  93.         }));
  94.     }
  95.  
  96.     private void removeFromServer(CharacterSheet cs) {
  97.         executor.execute(() -> resources.delete(cs.getId()).enqueue(new Callback<Boolean>() {
  98.             @Override
  99.             public void onResponse(Call<Boolean> call, Response<Boolean> response) {
  100.                 executor.execute(() -> {
  101.                     csDao.delete(cs);
  102.                 });
  103.             }
  104.  
  105.             @Override
  106.             public void onFailure(Call<Boolean> call, Throwable t) {
  107.                 Log.e(TAG, "Error deleting CS on the server: " + t.getMessage());
  108.             }
  109.         }));
  110.     }
  111.  
  112.     public void remove(CharacterSheet cs) {
  113.         // WE TAKE CARE OF THE LOCAL DATABASE AND JUST SOFTDELETE THE ITEM
  114.         executor.execute(() -> {
  115.             cs.setStatus(0);
  116.             csDao.softDelete(cs);
  117.  
  118.             refreshCharacterSheets();
  119.         });
  120.     }
  121.  
  122.     public void save(CharacterSheet cs, int anInt) {
  123.         // WE TAKE CARE OF THE LOCAL DATABASE
  124.         executor.execute(() -> {
  125.             cs.setPlayer(this.id);
  126.             cs.setStatus(1);
  127.             csDao.save(cs);
  128.  
  129.             refreshCharacterSheets();
  130.         });
  131.     }
  132.  
  133.  
  134.     public Resources getResources() {
  135.         return resources;
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement