Skip to content

Commit

Permalink
Carga de tours en progreso del usuario al iniciar la aplicación
Browse files Browse the repository at this point in the history
Close #72
  • Loading branch information
martinlaizg committed Aug 17, 2019
1 parent 3a6362c commit 70eb0a4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,14 @@ Call<Play> createPlacePlay(@Path("play_id") Integer play_id,
*/
@PUT("users/{user_id}")
Call<User> updateUser(@Path("user_id") Integer user_id, @Body Login login);

/**
* Get the list of plays of the user
*
* @param user_id
* the id of the user
* @return the list of plays
*/
@GET("users/{user_id}/plays")
Call<List<Play>> getUserPlays(@Path("user_id") Integer user_id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.martinlaizg.geofind.data.access.database.entities.Play;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Response;
Expand Down Expand Up @@ -113,7 +112,19 @@ public Play createPlacePlay(Integer play_id, Integer place_id) throws APIExcepti
throw apiException;
}

public List<Play> getUserPlays() {
return new ArrayList<>();
public List<Play> getUserPlays(int user_id) throws APIException {
Response<List<Play>> response;
APIException apiException;
try {
response = restClient.getUserPlays(user_id).execute();
if(response.isSuccessful()) {
return response.body();
}
apiException = ErrorUtils.parseError(response);
} catch(IOException e) {
apiException = new APIException(ErrorType.NETWORK, e.getMessage());
Log.e(TAG, "getPlace: ", e);
}
throw apiException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,40 +157,50 @@ public Play createPlay(int user_id, int tour_id) throws APIException {
* Get the list of plays of users
*
* @param user_id
* the id of the user to get plays
* the id of the user
* @return the list of plays
* @throws APIException
* the exception from the database
*/
public List<Play> getUserPlays(int user_id) throws APIException {
List<Play> plays = playDAO.getUserPlays(user_id);
// Remove out of date
for(int i = 0; i < plays.size(); i++) {
if(plays.get(i).isOutOfDate()) {
plays.remove(i);
i--;
}
}
if(plays.isEmpty()) {
plays.addAll(playService.getUserPlays());
plays.addAll(playService.getUserPlays(user_id));
for(Play p : plays) {
userRepo.insert(p.getUser());
tourRepo.insert(p.getTour());
userRepo.insert(p.getTour().getCreator());
playDAO.insert(p);
insert(p);
}
} else {
for(int i = 0; i < plays.size(); i++) {
if(plays.get(i).isOutOfDate()) {
plays.remove(i);
i--;
} else {
plays.get(i).setTour(tourRepo.getTour(plays.get(i).getTour_id()));
plays.get(i).setPlaces(placePlayDAO.getPlayPlace(plays.get(i).getId()));
}
plays.get(i).setTour(tourRepo.getTour(plays.get(i).getTour_id()));
plays.get(i).setPlaces(placePlayDAO.getPlayPlace(plays.get(i).getId()));
}
}
refreshUserPlays(user_id);
return plays;
}

private void refreshUserPlays(int user_id) {
new Thread(() -> {
List<Play> plays = playService.getUserPlays();
for(Play p : plays) {
insert(p);
}
}).start();
/**
* Refresh the list of plays of the user
* From the server into the database
*
* @param user_id
* the id of the user
* @throws APIException
* exception from the server
*/
private void refreshPlays(int user_id) throws APIException {
List<Play> plays = playService.getUserPlays(user_id);
for(Play p : plays) {
insert(p);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void onError() {
holder.tour_progress_text.setText(context.getString(R.string.div, completed, numPlaces));

Bundle b = new Bundle();
b.putInt(TourFragment.TOUR_ID, plays.get(i).getTour_id());
b.putInt(TourFragment.TOUR_ID, t.getId());
holder.tour_card
.setOnClickListener(v -> Navigation.findNavController(v).navigate(R.id.toTour, b));
}
Expand Down

0 comments on commit 70eb0a4

Please sign in to comment.