mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 13:59:29 +00:00
Can retrieve information about multiple users
This commit is contained in:
parent
c8b64f7e53
commit
f1a6ff8502
@ -1,6 +1,7 @@
|
||||
package org.communiquons.android.comunic.client.data.UsersInfo;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.ArrayMap;
|
||||
|
||||
import org.communiquons.android.comunic.client.api.APIRequestParameters;
|
||||
import org.communiquons.android.comunic.client.api.APIRequestTask;
|
||||
@ -9,6 +10,8 @@ import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This class handles informations requests about user informations
|
||||
*
|
||||
@ -58,6 +61,19 @@ public class GetUsersInfos {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This interface must be implemented to perform an API request
|
||||
*/
|
||||
public interface getMultipleUserInfosCallback{
|
||||
|
||||
/**
|
||||
* Callback function called when we got informations about user
|
||||
*
|
||||
* @param info Information about the user
|
||||
*/
|
||||
void callback(ArrayMap<Integer, UserInfo> info);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and return informations about a user
|
||||
*
|
||||
@ -82,6 +98,39 @@ public class GetUsersInfos {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get informations about multiple users
|
||||
*
|
||||
* @param IDs The ID of the user to get
|
||||
* @param callback The result once we got all the users
|
||||
*/
|
||||
public void getMultiple(ArrayList<Integer> IDs, getMultipleUserInfosCallback callback){
|
||||
|
||||
//Initializate variables
|
||||
ArrayList<Integer> usersToGet = new ArrayList<>();
|
||||
ArrayMap<Integer, UserInfo> usersInfo = new ArrayMap<>();
|
||||
|
||||
//Process each given user to check if they are available locally or not
|
||||
for(Integer id : IDs){
|
||||
//Check if the user exist or not
|
||||
if(!udbHelper.exists(id))
|
||||
usersToGet.add(id);
|
||||
else {
|
||||
//Get and save user informations
|
||||
usersInfo.put(id, udbHelper.get(id));
|
||||
}
|
||||
}
|
||||
|
||||
//Check if there are user informations to get on the server
|
||||
if(usersToGet.size() > 0){
|
||||
getMultipleOnServer(usersToGet, usersInfo, callback);
|
||||
}
|
||||
else {
|
||||
//Call the callback now with the cached user informations
|
||||
callback.callback(usersInfo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and return the informations about a user on the server
|
||||
*
|
||||
@ -140,6 +189,79 @@ public class GetUsersInfos {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and return the informations about mutliple users on the server
|
||||
*
|
||||
* @param IDs The ID of the user to get informations from
|
||||
* @param uInfos Informations about the other users (users that were already available in the db)
|
||||
* @param callback What to do once the request is done
|
||||
*/
|
||||
private void getMultipleOnServer(final ArrayList<Integer> IDs, final ArrayMap<Integer, UserInfo> uInfos,
|
||||
final getMultipleUserInfosCallback callback){
|
||||
|
||||
//Determine IDs list
|
||||
String IDs_list = "";
|
||||
for(int id : IDs){
|
||||
IDs_list += id + ",";
|
||||
}
|
||||
|
||||
//Perform a request on the API server
|
||||
//Setup the request
|
||||
APIRequestParameters requestParameters = new APIRequestParameters(context, "user/getInfosMultiple");
|
||||
requestParameters.addParameter("usersID", IDs_list);
|
||||
|
||||
//Do it.
|
||||
new APIRequestTask(){
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(APIResponse result) {
|
||||
|
||||
try {
|
||||
if(result != null) {
|
||||
|
||||
//Try to extract user informations
|
||||
JSONObject userObjectContainer = result.getJSONObject();
|
||||
|
||||
if (userObjectContainer != null) {
|
||||
|
||||
//Process each user ID
|
||||
for(int userID : IDs) {
|
||||
|
||||
UserInfo userInfos = null;
|
||||
|
||||
//Extract user object
|
||||
JSONObject userObject = userObjectContainer.getJSONObject(""+userID);
|
||||
|
||||
//Continue only if we could extract required informations
|
||||
if (userObject != null) {
|
||||
//Parse user informations
|
||||
userInfos = parse_user_json(userObject);
|
||||
}
|
||||
|
||||
//Save user information in the local database in case of success
|
||||
if (userInfos != null)
|
||||
udbHelper.insertOrUpdate(userInfos);
|
||||
|
||||
//Add the user to the list
|
||||
uInfos.put(userID, userInfos);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (JSONException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//Perform callback action
|
||||
callback.callback(uInfos);
|
||||
|
||||
}
|
||||
|
||||
}.execute(requestParameters);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a JSON object into a user object
|
||||
*
|
||||
|
@ -3,11 +3,19 @@ package org.communiquons.android.comunic.client.fragments;
|
||||
import android.app.Fragment;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.ArrayMap;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.communiquons.android.comunic.client.R;
|
||||
import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersInfos;
|
||||
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Friends list fragment
|
||||
@ -24,10 +32,38 @@ public class FriendsListFragment extends Fragment {
|
||||
Bundle savedInstanceState) {
|
||||
|
||||
//Retain the fragment
|
||||
setRetainInstance(true);
|
||||
//setRetainInstance(true);
|
||||
|
||||
//Inflate the layout for this fragment
|
||||
return inflater.inflate(R.layout.fragment_friendslist, container, false);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
||||
final ArrayList<Integer> list = new ArrayList<>();
|
||||
list.add(2);
|
||||
list.add(3);
|
||||
new GetUsersInfos(getActivity(), new DatabaseHelper(getActivity())).getMultiple(list, new GetUsersInfos.getMultipleUserInfosCallback() {
|
||||
@Override
|
||||
public void callback(ArrayMap<Integer, UserInfo> info) {
|
||||
Log.v("FriendsListFragment", "User infos callback");
|
||||
if(info == null)
|
||||
Toast.makeText(getActivity(), "Failure", Toast.LENGTH_LONG).show();
|
||||
else
|
||||
Toast.makeText(getActivity(), "Success", Toast.LENGTH_LONG).show();
|
||||
|
||||
for(int ID : list){
|
||||
UserInfo infos = info.get(ID);
|
||||
|
||||
if(infos == null)
|
||||
Log.e("FriendsListFragment", "Error with user infos for ID " + ID);
|
||||
else
|
||||
Log.v("FriendsListFragment", ID + " is " + infos.getFullName() + " !");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user