Users informations are cached in the database.

This commit is contained in:
Pierre 2017-11-05 18:15:37 +01:00
parent fe945c32e6
commit 7d8816912f
3 changed files with 60 additions and 6 deletions

View File

@ -42,7 +42,7 @@ public class MainActivity extends AppCompatActivity {
account = new Account(this); account = new Account(this);
//DEVELOPMENT : Try to get information about a user over the network //DEVELOPMENT : Try to get information about a user over the network
GetUsersInfos uInfos = new GetUsersInfos(this, null); GetUsersInfos uInfos = new GetUsersInfos(this, new DatabaseHelper(this));
//Get infos... about me! :) //Get infos... about me! :)
final int uID = 1; final int uID = 1;

View File

@ -19,9 +19,9 @@ import org.json.JSONObject;
public class GetUsersInfos { public class GetUsersInfos {
/** /**
* Database helper * User informations database helper
*/ */
private DatabaseHelper dbHelper = null; private UsersInfosDbHelper udbHelper = null;
/** /**
* Operations context * Operations context
@ -40,7 +40,7 @@ public class GetUsersInfos {
this.context = context; this.context = context;
//Save database helper object //Save database helper object
this.dbHelper = dbHelper; this.udbHelper = new UsersInfosDbHelper(dbHelper);
} }
@ -59,7 +59,7 @@ public class GetUsersInfos {
} }
/** /**
* Get and return the informations about a user * 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
*/ */
@ -89,6 +89,10 @@ public class GetUsersInfos {
userInfos = parse_user_json(userObject); userInfos = parse_user_json(userObject);
} }
//Save user in the local database in case of success
if(userInfos != null)
udbHelper.insertOrUpdate(userInfos);
} catch (JSONException e){ } catch (JSONException e){
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -32,6 +32,24 @@ public class UsersInfosDbHelper {
this.dbHelper = dbHelper; this.dbHelper = dbHelper;
} }
/**
* Insert or update a user entry
* * Insertion if the user isn't already present in the database
* * Update if the user is already present in the database
*
* @param userInfo The user insert or update
* @return True for a success / false else
*/
boolean insertOrUpdate(UserInfo userInfo){
//Check if the user is already present in the database or not
if(!exists(userInfo.getId()))
return insert(userInfo) > 0;
else
return update(userInfo);
}
/** /**
* Check wether a user is present in the database or not * Check wether a user is present in the database or not
* *
@ -81,7 +99,7 @@ public class UsersInfosDbHelper {
* @param user The user to insert in the database * @param user The user to insert in the database
* @return -1 in case of failure / the id of the new entry else * @return -1 in case of failure / the id of the new entry else
*/ */
public int insert(UserInfo user){ private int insert(UserInfo user){
//Get a database with write access //Get a database with write access
SQLiteDatabase db = dbHelper.getWritableDatabase(); SQLiteDatabase db = dbHelper.getWritableDatabase();
@ -197,4 +215,36 @@ public class UsersInfosDbHelper {
return result > 0; return result > 0;
} }
/**
* Update a user entry
*
* @param userInfo New informations about the user
* @return True if the operation seems to be a success / false else
*/
private boolean update(UserInfo userInfo){
SQLiteDatabase db = dbHelper.getWritableDatabase();
//Prepare the request
//Set the new values
ContentValues newValues = new ContentValues();
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME, userInfo.getFirstName());
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_LASTNAME, userInfo.getLastName());
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE, userInfo.getAcountImageURL());
//Set the condition
String conditions = UsersInfoSchema.COLUMN_NAME_USER_ID + " = ?";
String[] conditionArgs = {
""+userInfo.getId()
};
//Perform the request
int result = db.update(UsersInfoSchema.TABLE_NAME, newValues, conditions, conditionArgs);
db.close();
return result > 0;
}
} }