Display search user result

This commit is contained in:
Pierre 2018-01-01 19:03:28 +01:00
parent ae5f851101
commit 352627067e
11 changed files with 371 additions and 6 deletions

View File

@ -2,12 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.communiquons.android.comunic.client">
<!-- Internet access is required to access the API -->
<!-- Internet access is required to access the API and download medias -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:fullBackupContent="@xml/backup_scheme"
@ -16,6 +14,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- Main activity of the application -->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@ -23,8 +23,17 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity"
android:label="@string/activity_login_header"></activity>
<!-- Login activity -->
<activity
android:name=".LoginActivity"
android:label="@string/activity_login_header" />
<!-- Search user activity -->
<activity
android:name=".SearchUserActivity"
android:label="@string/activity_searchuser_title"/>
</application>
</manifest>

View File

@ -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<UserInfo> 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<String, Void, ArrayMap<Integer, UserInfo>>(){
@Override
protected ArrayMap<Integer, UserInfo> doInBackground(String... params) {
return getUsersHelper.search_users(params[0]);
}
@Override
protected void onPostExecute(ArrayMap<Integer, UserInfo> result) {
searchCallback(result);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, query);
}
/**
* Search callback function
*
* @param result The result of the research
*/
private void searchCallback(@Nullable ArrayMap<Integer, UserInfo> 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);
}
}

View File

@ -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<Integer, UserInfo> search_users(String query){
//Fetch users online
ArrayList<Integer> 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<Integer> 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<Integer> 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

View File

@ -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
*

View File

@ -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<UserInfo> {
/**
* Public constructor
*
* @param context The context of the activity
* @param list The dataset
*/
public UsersBasicAdapter(@NonNull Context context, @NonNull ArrayList<UserInfo> 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;
}
}

View File

@ -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());
}
}

View File

@ -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();
}
}
}

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Enter user name -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/activity_search_user_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/activity_searchuser_text_placeholder"
android:maxLines="1"/>
</android.support.design.widget.TextInputLayout>
<!-- Results list -->
<ListView
android:id="@+id/activity_search_user_results"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/user_account_image"
android:layout_width="@dimen/account_image_default_width"
android:layout_height="@dimen/account_image_default_height"
android:src="@drawable/default_account_image" />
<TextView
android:id="@+id/user_name"
style="?textAppearanceListItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="User name"
android:layout_gravity="center"
android:paddingStart="5dp"
android:paddingEnd="1dp"/>
</LinearLayout>

View File

@ -79,4 +79,7 @@
<string name="fragment_update_conversation_button_create">Create</string>
<string name="fragment_update_conversation_follow">Follow the conversation</string>
<string name="fragment_update_conversation_addmember">Add a member</string>
<string name="activity_searchuser_title">Search user</string>
<string name="activity_searchuser_text_placeholder">User name</string>
<string name="err_search_user">Could not search user !</string>
</resources>