Reverted modifications

This commit is contained in:
Pierre 2017-11-12 14:54:17 +01:00
parent 42913de841
commit c8b64f7e53
2 changed files with 54 additions and 69 deletions

View File

@ -1,8 +1,6 @@
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;
@ -11,8 +9,6 @@ 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
* *
@ -20,7 +16,7 @@ import java.util.concurrent.ExecutionException;
* Created by pierre on 11/5/17. * Created by pierre on 11/5/17.
*/ */
public abstract class GetUsersInfos extends AsyncTask<Void, Void, UserInfo> { public class GetUsersInfos {
/** /**
* User informations database helper * User informations database helper
@ -32,22 +28,13 @@ public abstract class GetUsersInfos extends AsyncTask<Void, Void, UserInfo> {
*/ */
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(int id, Context context, DatabaseHelper dbHelper){ public GetUsersInfos(Context context, DatabaseHelper dbHelper){
//Save user ID
this.id = id;
//Save context //Save context
this.context = context; this.context = context;
@ -58,38 +45,50 @@ public abstract class GetUsersInfos extends AsyncTask<Void, Void, UserInfo> {
} }
/** /**
* Each script must implement specifically what will be done once the request is done * This interface must be implemented to perform an API request
*
* @param info Informations about the user / null in case of failure
*/ */
@Override public interface getUserInfosCallback{
abstract protected void onPostExecute(UserInfo info);
/** /**
* Get and return information about a user * Callback function called when we got informations about user
*
* @param info Information about the user
*/ */
@Override void callback(UserInfo info);
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){
return null; callback.callback(null); //This is an error
}
//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
return getOnServer(id); getOnServer(id, callback);
//Else we can retrieve user informations from the local database
else else
//Return the cached values about the user callback.callback(udbHelper.get(id));
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 UserInfo getOnServer(int id){ 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
@ -97,21 +96,12 @@ public abstract class GetUsersInfos extends AsyncTask<Void, Void, UserInfo> {
requestParameters.addParameter("userID", ""+id); requestParameters.addParameter("userID", ""+id);
//Do it. //Do it.
APIRequestTask req = new APIRequestTask(){ 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) {
@ -141,14 +131,13 @@ public abstract class GetUsersInfos extends AsyncTask<Void, Void, UserInfo> {
e.printStackTrace(); e.printStackTrace();
} }
return userInfos; //Go to the next function
callback.callback(userInfos);
} catch (Exception e){
e.printStackTrace();
//Failure
return null;
} }
}.execute(requestParameters);
} }
/** /**

View File

@ -5,7 +5,6 @@ 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;
@ -59,12 +58,9 @@ 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(user_id, mContext, dbHelper){ new GetUsersInfos(mContext, dbHelper).get(user_id, new GetUsersInfos.getUserInfosCallback() {
@Override @Override
protected void onPostExecute(UserInfo info) { public void callback(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());
@ -72,7 +68,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;
} }