User informations can be successfully cached locally.

This commit is contained in:
Pierre 2017-11-05 18:22:43 +01:00
parent 7d8816912f
commit dca9924fc9
2 changed files with 24 additions and 5 deletions

View File

@ -58,12 +58,31 @@ public class GetUsersInfos {
} }
/**
* Get and return informations about a user
*
* @param id The ID of the user to get the informations
* @param callback What to do once we got the response
*/
public void get(int id, getUserInfosCallback callback){
//Check if the user is already present in the database or not
if(!udbHelper.exists(id))
//Perform a request on the server
getOnServer(id, callback);
//Else we can retrieve user informations from the local database
callback.callback(udbHelper.get(id));
}
/** /**
* Get and return the informations about a user on the server * Get and return the informations about a user on the server
* *
* @param id The ID of the user to get informations from * @param id The ID of the user to get informations from
* @param callback What to do once the request is done
*/ */
public void get(final int id, final getUserInfosCallback callback){ private void getOnServer(final int id, final getUserInfosCallback callback){
//Perform a request on the API server //Perform a request on the API server
//Setup the request //Setup the request
@ -89,7 +108,7 @@ public class GetUsersInfos {
userInfos = parse_user_json(userObject); userInfos = parse_user_json(userObject);
} }
//Save user in the local database in case of success //Save user information in the local database in case of success
if(userInfos != null) if(userInfos != null)
udbHelper.insertOrUpdate(userInfos); udbHelper.insertOrUpdate(userInfos);

View File

@ -16,7 +16,7 @@ import org.communiquons.android.comunic.client.data.DatabaseHelper;
* Created by pierre on 11/2/17. * Created by pierre on 11/2/17.
*/ */
public class UsersInfosDbHelper { class UsersInfosDbHelper {
/** /**
* Database helper * Database helper
@ -28,7 +28,7 @@ public class UsersInfosDbHelper {
* *
* @param dbHelper Database helper object * @param dbHelper Database helper object
*/ */
public UsersInfosDbHelper(DatabaseHelper dbHelper){ UsersInfosDbHelper(DatabaseHelper dbHelper){
this.dbHelper = dbHelper; this.dbHelper = dbHelper;
} }
@ -56,7 +56,7 @@ public class UsersInfosDbHelper {
* @param userID The user to research on the database * @param userID The user to research on the database
* @return boolean True if the user exists / false else * @return boolean True if the user exists / false else
*/ */
public boolean exists(int userID){ boolean exists(int userID){
//Get the database //Get the database
SQLiteDatabase db = dbHelper.getReadableDatabase(); SQLiteDatabase db = dbHelper.getReadableDatabase();