Last active
April 30, 2018 12:29
-
-
Save tpakis/a8a18f3a8ac927a45fe360685db67049 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void getRecipesFromWeb(){ | |
Timber.d("getRecipesFromWeb"); | |
recipeApi.getRecipesFromWeb().enqueue(new Callback<List<Recipe>>() { | |
@Override | |
public void onResponse(Call<List<Recipe>> call, Response<List<Recipe>> response) { | |
if (response.isSuccessful()) { | |
setRecipesListObservableStatus(Status.SUCCESS,null); | |
addRecipesToDB(response.body()); | |
} else { | |
// error case | |
setRecipesListObservableStatus(Status.ERROR,String.valueOf(response.code())); | |
switch (response.code()) { | |
case 404: | |
Timber.d("not found"); | |
break; | |
case 500: | |
Timber.d("not logged in or server broken"); | |
break; | |
default: | |
Timber.d("unknown error"); | |
break; | |
} | |
} | |
} | |
@Override | |
public void onFailure(Call<List<Recipe>> call, Throwable t) { | |
Log.d("asd","asd"); | |
setRecipesListObservableStatus(Status.ERROR, t.getMessage()); | |
} | |
}); | |
} | |
private void addRecipesToDB(List<Recipe> items) { | |
Timber.d("addRecipesToDB"); | |
new AsyncTask<List<Recipe>, Void, Boolean>() { | |
@Override | |
protected Boolean doInBackground(List<Recipe>... params) { | |
boolean needsUpdate = false; | |
for (Recipe item : params[0]) { | |
//upsert implementation for future use | |
Long inserted = recipesDAO.insertEntry(item); //-1 if not inserted | |
if (inserted == -1){ | |
int updated = recipesDAO.update(item); | |
if (updated > 0){ | |
needsUpdate = true; | |
} | |
}else{ | |
needsUpdate = true; | |
} | |
} | |
return needsUpdate; | |
} | |
@Override | |
protected void onPostExecute(Boolean needUpdate) { | |
if (needUpdate) { | |
loadAllRecipesFromDB(); | |
} | |
} | |
}.execute(items); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment