From 352627067ed9926c3982ca5b8c3ce0df558d8702 Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 1 Jan 2018 19:03:28 +0100 Subject: [PATCH] Display search user result --- app/src/main/AndroidManifest.xml | 19 ++- .../comunic/client/SearchUserActivity.java | 132 ++++++++++++++++++ .../data/ImageLoad/ImageDownloadRunnable.java | 2 +- .../client/data/UsersInfo/GetUsersHelper.java | 52 +++++++ .../client/data/UsersInfo/UserInfo.java | 11 ++ .../data/UsersInfo/UsersBasicAdapter.java | 69 +++++++++ .../comunic/client/data/utils/UiUtils.java | 11 ++ .../fragments/UpdateConversationFragment.java | 27 ++++ .../main/res/layout/activity_search_user.xml | 28 ++++ .../res/layout/user_basic_adapter_item.xml | 23 +++ app/src/main/res/values/strings.xml | 3 + 11 files changed, 371 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/SearchUserActivity.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersBasicAdapter.java create mode 100644 app/src/main/res/layout/activity_search_user.xml create mode 100644 app/src/main/res/layout/user_basic_adapter_item.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index dff6697..a48e3f5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,12 +2,10 @@ - + - - + + @@ -23,8 +23,17 @@ - + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/org/communiquons/android/comunic/client/SearchUserActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/SearchUserActivity.java new file mode 100644 index 0000000..0fef8cb --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/SearchUserActivity.java @@ -0,0 +1,132 @@ +package org.communiquons.android.comunic.client; + +import android.os.AsyncTask; +import android.support.annotation.Nullable; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.ArrayMap; +import android.widget.EditText; +import android.widget.ListView; +import android.widget.Toast; + +import org.communiquons.android.comunic.client.data.DatabaseHelper; +import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper; +import org.communiquons.android.comunic.client.data.UsersInfo.UsersBasicAdapter; +import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; + +import java.util.ArrayList; + +public class SearchUserActivity extends AppCompatActivity + implements TextWatcher{ + + /** + * Debug tag + */ + private static final String TAG = "SearchUserActivity"; + + /** + * Search field + */ + private EditText searchField; + + /** + * Results list + */ + private ListView resultListView; + + /** + * Search result user informations + */ + private ArrayList resultArray; + + /** + * Search results adapter + */ + private UsersBasicAdapter resultAdapter; + + /** + * Get user infos helper + */ + private GetUsersHelper getUsersHelper; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_search_user); + + //Create a get user helper object + getUsersHelper = new GetUsersHelper(this, DatabaseHelper.getInstance(this)); + + //Get view + searchField = (EditText) findViewById(R.id.activity_search_user_field); + resultListView = (ListView) findViewById(R.id.activity_search_user_results); + + //Set on key listener + searchField.addTextChangedListener(this); + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + //Do nothing + } + + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + //Do nothing + } + + @Override + public void afterTextChanged(Editable s) { + newSearch(""+searchField.getText()); + } + + /** + * Perform a new search on the server + * + * @param query The query to perform on the server + */ + private void newSearch(String query){ + + //Check if there is a query + if(query.equals("")) + return; //Cancel the request + + new AsyncTask>(){ + @Override + protected ArrayMap doInBackground(String... params) { + return getUsersHelper.search_users(params[0]); + } + + @Override + protected void onPostExecute(ArrayMap result) { + searchCallback(result); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, query); + + } + + /** + * Search callback function + * + * @param result The result of the research + */ + private void searchCallback(@Nullable ArrayMap result){ + + //Check for errors + if(result == null){ + Toast.makeText(this, R.string.err_search_user, Toast.LENGTH_SHORT).show(); + return; + } + + //Save the results + resultArray = new ArrayList<>(result.values()); + + //Display the result + resultAdapter = new UsersBasicAdapter(this, resultArray); + + //Set the adapter + resultListView.setAdapter(resultAdapter); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageDownloadRunnable.java b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageDownloadRunnable.java index bb4f38f..103857f 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageDownloadRunnable.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageDownloadRunnable.java @@ -59,7 +59,7 @@ class ImageDownloadRunnable implements Runnable { InputStream is = conn.getInputStream(); //Process image - Bitmap image =BitmapFactory.decodeStream(is); + Bitmap image = BitmapFactory.decodeStream(is); image.compress(Bitmap.CompressFormat.PNG, 100, os); os.close(); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersHelper.java index 2d1e0c9..29e476c 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/GetUsersHelper.java @@ -10,6 +10,7 @@ import org.communiquons.android.comunic.client.api.APIRequest; import org.communiquons.android.comunic.client.api.APIRequestParameters; import org.communiquons.android.comunic.client.api.APIResponse; import org.communiquons.android.comunic.client.data.DatabaseHelper; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -155,6 +156,57 @@ public class GetUsersHelper { return usersInfo; } + /** + * Search for user from online source + * + * @param query The query string + * @return A list of users / false in case of failure + */ + @Nullable + public ArrayMap search_users(String query){ + + //Fetch users online + ArrayList usersID = search_users_online(query); + + //Check for errors + if(usersID == null) + return null; + + return getMultiple(usersID); + + } + + /** + * Search for users on the API + * + * @param query The query of the research + * @return The ID of the corresponding users / false in case of failure + */ + @Nullable + private ArrayList search_users_online(String query){ + + //Make an API request + APIRequestParameters params = new APIRequestParameters(mContext, "search/user"); + params.addParameter("query", query); + + try { + + //Get and extract the response + APIResponse response = new APIRequest().exec(params); + JSONArray array = response.getJSONArray(); + + //Make response + ArrayList IDs = new ArrayList<>(); + for(int i = 0; i < array.length(); i++){ + IDs.add(array.getInt(i)); + } + return IDs; + + } catch (Exception e){ + e.printStackTrace(); + return null; + } + } /** * Get and return the information about multiple users from the server diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UserInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UserInfo.java index 2d412c5..04cb4e0 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UserInfo.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UserInfo.java @@ -1,5 +1,7 @@ package org.communiquons.android.comunic.client.data.UsersInfo; +import org.communiquons.android.comunic.client.data.utils.Utilities; + /** * This class contains the informations about a single user * @@ -80,6 +82,15 @@ public class UserInfo { return firstName + " " + lastName; } + /** + * Get the full name of the user ready to be shown + * + * @return The full name of the user + */ + public String getDisplayFullName(){ + return Utilities.prepareStringTextView(getFullName()); + } + /** * Set the image URL of the account of the user * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersBasicAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersBasicAdapter.java new file mode 100644 index 0000000..1fd3766 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersBasicAdapter.java @@ -0,0 +1,69 @@ +package org.communiquons.android.comunic.client.data.UsersInfo; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; + +import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager; +import org.communiquons.android.comunic.client.data.utils.UiUtils; + +import java.util.ArrayList; +import java.util.zip.Inflater; + +/** + * User basic adapter + * + * Allow to display basic informations about a set of users in a ListView + * + * @author Pierre HUBERT + * Created by pierre on 1/1/18. + */ + +public class UsersBasicAdapter extends ArrayAdapter { + + /** + * Public constructor + * + * @param context The context of the activity + * @param list The dataset + */ + public UsersBasicAdapter(@NonNull Context context, @NonNull ArrayList list){ + super(context, 0, list); + } + + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + + //Check if the view has to be inflated + if(convertView == null) + convertView = LayoutInflater.from(getContext()). + inflate(R.layout.user_basic_adapter_item, parent, false); + + //Get item + UserInfo userInfos = getItem(position); + + if(userInfos != null){ + + //Set user name + ((TextView) convertView.findViewById(R.id.user_name)). + setText(userInfos.getDisplayFullName()); + + //Set account image + ImageView account_image = convertView.findViewById(R.id.user_account_image); + ImageLoadManager.remove(account_image); + account_image.setImageDrawable(UiUtils.getDrawable(getContext(), + R.drawable.default_account_image)); + ImageLoadManager.load(getContext(), userInfos.getAcountImageURL(), account_image); + } + + return convertView; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/UiUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/UiUtils.java index fbd0227..b4a9992 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/UiUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/UiUtils.java @@ -1,6 +1,7 @@ package org.communiquons.android.comunic.client.data.utils; import android.content.Context; +import android.graphics.drawable.Drawable; import android.os.Build; /** @@ -28,4 +29,14 @@ public class UiUtils { return context.getResources().getColor(color_id); } } + + /** + * Get a drawable from ressources + * + * @param context The context of the application + * @param drawable_id The ID of the drawable to get + */ + public static Drawable getDrawable(Context context, int drawable_id){ + return context.getResources().getDrawable(drawable_id, context.getTheme()); + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/UpdateConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/UpdateConversationFragment.java index b9fbafa..335d052 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/UpdateConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/UpdateConversationFragment.java @@ -1,6 +1,8 @@ package org.communiquons.android.comunic.client.fragments; +import android.app.Activity; import android.app.Fragment; +import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; @@ -10,9 +12,11 @@ import android.widget.Button; import android.widget.CheckBox; import android.widget.ListView; import android.widget.TextView; +import android.widget.Toast; import org.communiquons.android.comunic.client.MainActivity; import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.SearchUserActivity; /** * Create and / or update a conversation fragment @@ -23,6 +27,16 @@ import org.communiquons.android.comunic.client.R; public class UpdateConversationFragment extends Fragment { + /** + * Debug tag + */ + private static final String TAG = "UpdateConversationFragment"; + + /** + * Find user ID intent + */ + public static final int FIND_USER_ID_INTENT = 0; + /** * The name of the conversation */ @@ -89,6 +103,19 @@ public class UpdateConversationFragment extends Fragment { */ private void requestAddMember(){ + //Make intent + Intent intent = new Intent(getActivity(), SearchUserActivity.class); + startActivityForResult(intent, FIND_USER_ID_INTENT); + } + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + //Check if it is a success + if(resultCode == Activity.RESULT_OK){ + Toast.makeText(getActivity(), "Request success", Toast.LENGTH_SHORT).show(); + } + } } diff --git a/app/src/main/res/layout/activity_search_user.xml b/app/src/main/res/layout/activity_search_user.xml new file mode 100644 index 0000000..7cb4c5d --- /dev/null +++ b/app/src/main/res/layout/activity_search_user.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/user_basic_adapter_item.xml b/app/src/main/res/layout/user_basic_adapter_item.xml new file mode 100644 index 0000000..abc80ad --- /dev/null +++ b/app/src/main/res/layout/user_basic_adapter_item.xml @@ -0,0 +1,23 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f0ee986..8b1457e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -79,4 +79,7 @@ Create Follow the conversation Add a member + Search user + User name + Could not search user !