Skip to content

Commit

Permalink
Versión v0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlaizg committed Jun 8, 2019
2 parents b58b1a9 + 64b3a9e commit 81f2520
Show file tree
Hide file tree
Showing 31 changed files with 319 additions and 203 deletions.
Binary file modified android-studio-settings/settings.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
applicationId 'com.martinlaizg.geofind'
minSdkVersion 24
targetSdkVersion 28
versionCode 5
versionName 'v0.2'
versionCode 6
versionName 'v0.2.1'
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

public class RetrofitInstance {

// private static final String BASE_URL = "https://geofind1.herokuapp.com/api/";
private static final String BASE_URL = "http://192.168.1.44:8000/api/";
private static final String BASE_URL = "https://geofind1.herokuapp.com/api/";
// private static final String BASE_URL = "http://192.168.1.44:8000/api/";
private static Retrofit retrofitInstance;

public static RestClient getRestClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.martinlaizg.geofind.data.access.api.service.exceptions.APIException;
import com.martinlaizg.geofind.data.access.database.entities.Tour;

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

Expand All @@ -31,6 +30,13 @@ public static TourService getInstance() {
return tourService;
}

/**
* Retrieve all tours form de server
*
* @return the list of tours
* @throws APIException
* server errors
*/
public List<Tour> getAllTours() throws APIException {
Response<List<Tour>> response;
APIException apiException;
Expand All @@ -41,13 +47,22 @@ public List<Tour> getAllTours() throws APIException {
}
apiException = ErrorUtils.parseError(response);

} catch(IOException e) {
} catch(Exception e) {
apiException = new APIException(ErrorType.NETWORK, e.getMessage());
Log.e(TAG, "getTours: ", e);
}
throw apiException;
}

/**
* Create de tour in the server
*
* @param tour
* the tour to create
* @return the created tour
* @throws APIException
* server errors
*/
public Tour create(Tour tour) throws APIException {
Response<Tour> response;
APIException apiException;
Expand All @@ -58,13 +73,22 @@ public Tour create(Tour tour) throws APIException {
}
apiException = ErrorUtils.parseError(response);

} catch(IOException e) {
} catch(Exception e) {
apiException = new APIException(ErrorType.NETWORK, e.getMessage());
Log.e(TAG, "create: ", e);
}
throw apiException;
}

/**
* Get a single tour by id
*
* @param id
* the id of the Tour to get
* @return the tour
* @throws APIException
* server errors
*/
public Tour getTour(Integer id) throws APIException {
Response<Tour> response;
APIException apiException;
Expand All @@ -74,13 +98,22 @@ public Tour getTour(Integer id) throws APIException {
return response.body();
}
apiException = ErrorUtils.parseError(response);
} catch(IOException e) {
} catch(Exception e) {
apiException = new APIException(ErrorType.NETWORK, e.getMessage());
Log.e(TAG, "getTour: ", e);
}
throw apiException;
}

/**
* Update the tour with id=tour.getId() with the data in tour
*
* @param tour
* the new data
* @return the new tour
* @throws APIException
* server errors
*/
public Tour update(Tour tour) throws APIException {
Response<Tour> response;
APIException apiException;
Expand All @@ -90,7 +123,7 @@ public Tour update(Tour tour) throws APIException {
return response.body();
}
apiException = ErrorUtils.parseError(response);
} catch(IOException e) {
} catch(Exception e) {
apiException = new APIException(ErrorType.NETWORK, e.getMessage());
Log.e(TAG, "update: ", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.martinlaizg.geofind.data.access.api.service.exceptions.APIException;
import com.martinlaizg.geofind.data.access.database.entities.User;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -53,7 +52,7 @@ public User login(Login login) throws APIException {
return response.body();
}
apiException = ErrorUtils.parseError(response);
} catch(IOException e) {
} catch(Exception e) {
apiException = new APIException(ErrorType.NETWORK, e.getMessage());
Log.e(TAG, "login: ", e);
}
Expand All @@ -69,7 +68,7 @@ public User registry(Login login) throws APIException {
return response.body();
}
apiException = ErrorUtils.parseError(response);
} catch(IOException e) {
} catch(Exception e) {
apiException = new APIException(ErrorType.NETWORK, e.getMessage());
Log.e(TAG, "registry: ", e);
}
Expand All @@ -89,7 +88,7 @@ public boolean sendMessage(String title, String message) throws APIException {
return true;
}
throw ErrorUtils.parseError(response);
} catch(IOException e) {
} catch(Exception e) {
apiException = new APIException(ErrorType.NETWORK, e.getMessage());
Log.e(TAG, "registry: ", e);
throw apiException;
Expand All @@ -106,7 +105,7 @@ public User update(Login login, User user) throws APIException {
return response.body();
}
throw ErrorUtils.parseError(response);
} catch(IOException e) {
} catch(Exception e) {
apiException = new APIException(ErrorType.NETWORK, e.getMessage());
Log.e(TAG, "registry: ", e);
throw apiException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,48 @@
@Dao
public interface TourDAO {

/**
* Insert a tour into the database
*
* @param tour
* the tour to be inserted
*/
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Tour tour);

/**
* Update a tour
*
* @param tour
* the tour to be updated
*/
@Update
void update(Tour tour);

/**
* Get a single tour by id
*
* @param tour_id
* the id of the tour to retrieve
* @return the tour
*/
@Query("SELECT * FROM tours WHERE id = :tour_id")
Tour getTour(Integer tour_id);

/**
* Delete a single tour by id
*
* @param tour_id
* the id of the tour to delete
*/
@Query("DELETE FROM tours WHERE id = :tour_id")
void delete(int tour_id);

/**
* Get all the tours
*
* @return the list of tours
*/
@Query("SELECT * FROM tours")
List<Tour> getAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,57 +30,61 @@ public class TourRepository {
}

/**
* Get the TourCreatorPlaces list from local and update each item from server
* Get the list of tours from local and removes the outdated ones
* At the same time refresh the local tours with the server
*
* @return the list of elements
*/
public List<Tour> getAllTours() {
public List<Tour> getAllTours() throws APIException {
List<Tour> tours = tourDAO.getAll();
for(int i = 0; i < tours.size(); i++) {
if(tours.get(i).isOutOfDate()) {
Tour newTour = refresh(tours.get(i));
if(newTour == null) {
tours.remove(i);
i--;
}
tours.set(i, newTour);
tours.remove(i);
i--;
} else {
Tour t = tours.get(i);
t.setCreator(userRepo.getUser(t.getCreator_id()));
t.setPlaces(placeRepo.getTourPlaces(t.getId()));
tours.set(i, t);
}
}

// If the list of tours is empty wait for refresh
if(tours.isEmpty()) {
tours = refreshTours();
} else {
// start the refresh on new thread to do it in background
new Thread(() -> {
try {
refreshTours();
} catch(APIException e) {
Log.e(TAG, "getAllTours: ", e);
}
}).start();
}
return tours;
}

/**
* Refresh the tour from the server and insert into the local database
* Load tours from server and insert into de local database
*
* @param tour the tour to refresh, return null on error
* @return the tour refreshed
* @throws APIException
* the server exception
*/
private Tour refresh(Tour tour) {
int tour_id = tour.getId();
try {
tour = tourService.getTour(tour.getId());
if(tour != null) {
insert(tour);
} else {
tourDAO.delete(tour_id);
}
return tour;
} catch(APIException e) {
Log.e(TAG, "refresh: ", e);
private List<Tour> refreshTours() throws APIException {
List<Tour> tours = tourService.getAllTours();
for(Tour t : tours) {
insert(t);
}
return null;
return tours;
}

/**
* Insert a Tour to the local database
* Insert the User creator and the list of Place recursively
*
* @param tour Tour to insert
* @param tour
* Tour to insert
*/
public void insert(Tour tour) {
if(tour != null) {
Expand All @@ -95,13 +99,38 @@ public void insert(Tour tour) {
}
}

/**
* Refresh the tour from the server and insert into the local database
*
* @param tour
* the tour to refresh, return null on error
* @return the tour refreshed
*/
private Tour refresh(Tour tour) {
int tour_id = tour.getId();
try {
tour = tourService.getTour(tour.getId());
if(tour != null) {
insert(tour);
} else {
tourDAO.delete(tour_id);
}
return tour;
} catch(APIException e) {
Log.e(TAG, "refresh: ", e);
}
return null;
}

/**
* Update the tour on server and local
* If the tour is removed from server, return null
*
* @param tour tour to update
* @param tour
* tour to update
* @return tour updated or null if no exist on server
* @throws APIException exception from server
* @throws APIException
* exception from server
*/
public Tour update(Tour tour) throws APIException {
int tour_id = tour.getId();
Expand All @@ -117,9 +146,11 @@ public Tour update(Tour tour) throws APIException {
/**
* Create a tour on server and insert into local database
*
* @param tour tour to insert
* @param tour
* tour to insert
* @return inserted tour
* @throws APIException the exception from API
* @throws APIException
* the exception from API
*/
public Tour create(Tour tour) throws APIException {
tour = tourService.create(tour);
Expand All @@ -130,9 +161,11 @@ public Tour create(Tour tour) throws APIException {
/**
* Get the tour with this id
*
* @param id the tour id
* @param id
* the tour id
* @return the tour
* @throws APIException the server exception
* @throws APIException
* the server exception
*/
public Tour getTour(Integer id) throws APIException {
Tour t = tourDAO.getTour(id);
Expand All @@ -145,16 +178,4 @@ public Tour getTour(Integer id) throws APIException {
}
return t;
}

/**
* Load tours from server and insert into de local database
*
* @throws APIException the server exception
*/
public void refreshTours() throws APIException {
List<Tour> tours = tourService.getAllTours();
for(Tour t : tours) {
insert(t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class DateUtils {

public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final long TIME_TO_EXPIRE = 5 * 60 * 1000; // 15min * 60s * 1000ms
public static final long TIME_TO_EXPIRE = 15 * 60 * 1000; // 15min * 60s * 1000ms

public static boolean isDateExpire(Date fromDate) {
return Calendar.getInstance().getTime().getTime() - fromDate.getTime() > TIME_TO_EXPIRE;
Expand Down
Loading

0 comments on commit 81f2520

Please sign in to comment.