mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 13:59:29 +00:00
Users informations are cached in the database.
This commit is contained in:
parent
fe945c32e6
commit
7d8816912f
@ -42,7 +42,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
account = new Account(this);
|
||||
|
||||
//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! :)
|
||||
final int uID = 1;
|
||||
|
@ -19,9 +19,9 @@ import org.json.JSONObject;
|
||||
public class GetUsersInfos {
|
||||
|
||||
/**
|
||||
* Database helper
|
||||
* User informations database helper
|
||||
*/
|
||||
private DatabaseHelper dbHelper = null;
|
||||
private UsersInfosDbHelper udbHelper = null;
|
||||
|
||||
/**
|
||||
* Operations context
|
||||
@ -40,7 +40,7 @@ public class GetUsersInfos {
|
||||
this.context = context;
|
||||
|
||||
//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
|
||||
*/
|
||||
@ -89,6 +89,10 @@ public class GetUsersInfos {
|
||||
userInfos = parse_user_json(userObject);
|
||||
}
|
||||
|
||||
//Save user in the local database in case of success
|
||||
if(userInfos != null)
|
||||
udbHelper.insertOrUpdate(userInfos);
|
||||
|
||||
} catch (JSONException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -32,6 +32,24 @@ public class UsersInfosDbHelper {
|
||||
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
|
||||
*
|
||||
@ -81,7 +99,7 @@ public class UsersInfosDbHelper {
|
||||
* @param user The user to insert in the database
|
||||
* @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
|
||||
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
||||
@ -197,4 +215,36 @@ public class UsersInfosDbHelper {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user