Not working thread versionned get user infos

This commit is contained in:
Pierre 2017-11-12 14:49:54 +01:00
parent 53e60228fe
commit 42913de841
2 changed files with 82 additions and 67 deletions

View File

@ -1,6 +1,8 @@
package org.communiquons.android.comunic.client.data.UsersInfo; package org.communiquons.android.comunic.client.data.UsersInfo;
import android.content.Context; import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import org.communiquons.android.comunic.client.api.APIRequestParameters; import org.communiquons.android.comunic.client.api.APIRequestParameters;
import org.communiquons.android.comunic.client.api.APIRequestTask; import org.communiquons.android.comunic.client.api.APIRequestTask;
@ -9,6 +11,8 @@ import org.communiquons.android.comunic.client.data.DatabaseHelper;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.concurrent.ExecutionException;
/** /**
* This class handles informations requests about user informations * This class handles informations requests about user informations
* *
@ -16,7 +20,7 @@ import org.json.JSONObject;
* Created by pierre on 11/5/17. * Created by pierre on 11/5/17.
*/ */
public class GetUsersInfos { public abstract class GetUsersInfos extends AsyncTask<Void, Void, UserInfo> {
/** /**
* User informations database helper * User informations database helper
@ -28,13 +32,22 @@ public class GetUsersInfos {
*/ */
private Context context; private Context context;
/**
* UserID to retrieve
*/
private int id;
/** /**
* Public constructor * Public constructor
* *
* @param id The ID of the user to get the information about
* @param context The context of the application * @param context The context of the application
* @param dbHelper Database helper object * @param dbHelper Database helper object
*/ */
public GetUsersInfos(Context context, DatabaseHelper dbHelper){ public GetUsersInfos(int id, Context context, DatabaseHelper dbHelper){
//Save user ID
this.id = id;
//Save context //Save context
this.context = context; this.context = context;
@ -45,50 +58,38 @@ public class GetUsersInfos {
} }
/** /**
* This interface must be implemented to perform an API request * Each script must implement specifically what will be done once the request is done
*
* @param info Informations about the user / null in case of failure
*/ */
public interface getUserInfosCallback{ @Override
abstract protected void onPostExecute(UserInfo info);
/** /**
* Callback function called when we got informations about user * Get and return information about a user
*
* @param info Information about the user
*/ */
void callback(UserInfo info); @Override
protected UserInfo doInBackground(Void... params) {
}
/**
* 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 ID is positive, error else //Check if the ID is positive, error else
if(id < 1){ if(id < 1)
callback.callback(null); //This is an error return null;
}
//Check if the user is already present in the database or not //Check if the user is already present in the database or not
if(!udbHelper.exists(id)) if(!udbHelper.exists(id))
//Perform a request on the server //Perform a request on the server
getOnServer(id, callback); return getOnServer(id);
//Else we can retrieve user informations from the local database
else else
callback.callback(udbHelper.get(id)); //Return the cached values about the user
return 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
*/ */
private void getOnServer(final int id, final getUserInfosCallback callback){ private UserInfo getOnServer(int id){
//Perform a request on the API server //Perform a request on the API server
//Setup the request //Setup the request
@ -96,12 +97,21 @@ public class GetUsersInfos {
requestParameters.addParameter("userID", ""+id); requestParameters.addParameter("userID", ""+id);
//Do it. //Do it.
new APIRequestTask(){ APIRequestTask req = new APIRequestTask(){
@Override @Override
protected void onPostExecute(APIResponse result) { protected void onPostExecute(APIResponse result) {
//Nothing
}
};
req.execute(requestParameters);
//Get the result and process it when it becomes available
try {
APIResponse result = req.get();
UserInfo userInfos = null; UserInfo userInfos = null;
Log.v("GetUsersInfos", "test2 test test");
try { try {
if(result != null) { if(result != null) {
@ -131,13 +141,14 @@ public class GetUsersInfos {
e.printStackTrace(); e.printStackTrace();
} }
//Go to the next function return userInfos;
callback.callback(userInfos);
} catch (Exception e){
e.printStackTrace();
//Failure
return null;
} }
}.execute(requestParameters);
} }
/** /**

View File

@ -5,6 +5,7 @@ import android.app.Fragment;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -58,9 +59,12 @@ public class UserInfosFragment extends Fragment {
//Retrieve user informations in order to display them //Retrieve user informations in order to display them
int user_id = new AccountUtils(mContext).get_current_user_id(); int user_id = new AccountUtils(mContext).get_current_user_id();
new GetUsersInfos(mContext, dbHelper).get(user_id, new GetUsersInfos.getUserInfosCallback() { new GetUsersInfos(user_id, mContext, dbHelper){
@Override @Override
public void callback(UserInfo info) { protected void onPostExecute(UserInfo info) {
if(info != null)
return;
//Set the name of the user //Set the name of the user
userNameView.setText(info.getFullName()); userNameView.setText(info.getFullName());
@ -68,7 +72,7 @@ public class UserInfosFragment extends Fragment {
//Get and show the user account image //Get and show the user account image
new ImageLoadTask(mContext, info.getAcountImageURL(), imageView).execute(); new ImageLoadTask(mContext, info.getAcountImageURL(), imageView).execute();
} }
}); }.execute();
return result; return result;
} }