Generate conversations displayed name

This commit is contained in:
Pierre 2017-12-10 13:46:13 +01:00
parent e669f0fa05
commit b8ca813d09
4 changed files with 92 additions and 16 deletions

View File

@ -101,7 +101,7 @@ public class GetUsersHelper {
} }
} }
//If we got there, an error occured //If we got there, an error occurred
Log.e(TAG, "Couldn't get information about a single user !"); Log.e(TAG, "Couldn't get information about a single user !");
return null; return null;

View File

@ -52,7 +52,7 @@ public class ConversationsInfo {
* *
* @param ID_owner The ID of the owner of the conversation * @param ID_owner The ID of the owner of the conversation
*/ */
public void setID_owner(int ID_owner) { void setID_owner(int ID_owner) {
this.ID_owner = ID_owner; this.ID_owner = ID_owner;
} }
@ -70,7 +70,7 @@ public class ConversationsInfo {
* *
* @param last_active The timestamp of the last activity of the conversation * @param last_active The timestamp of the last activity of the conversation
*/ */
public void setLast_active(int last_active) { void setLast_active(int last_active) {
this.last_active = last_active; this.last_active = last_active;
} }
@ -112,7 +112,7 @@ public class ConversationsInfo {
* @return True if the conversation has a name / false else * @return True if the conversation has a name / false else
*/ */
public boolean hasName(){ public boolean hasName(){
return name == null; return name != null;
} }
/** /**
@ -122,7 +122,7 @@ public class ConversationsInfo {
* *
* @param following True if the user is following the conversation * @param following True if the user is following the conversation
*/ */
public void setFollowing(boolean following) { void setFollowing(boolean following) {
this.following = following; this.following = following;
} }
@ -140,7 +140,7 @@ public class ConversationsInfo {
* *
* @param saw_last_message True if the user has seen the last message of the conversation * @param saw_last_message True if the user has seen the last message of the conversation
*/ */
public void setSaw_last_message(boolean saw_last_message) { void setSaw_last_message(boolean saw_last_message) {
this.saw_last_message = saw_last_message; this.saw_last_message = saw_last_message;
} }
@ -167,7 +167,7 @@ public class ConversationsInfo {
* *
* @param id The ID of the member to add * @param id The ID of the member to add
*/ */
public void addMember(Integer id){ void addMember(Integer id){
if(members == null) if(members == null)
members = new ArrayList<>(); members = new ArrayList<>();

View File

@ -44,6 +44,7 @@ public class ConversationsListHelper {
* *
* @return The list of conversations * @return The list of conversations
*/ */
@Nullable
public ArrayList<ConversationsInfo> download(){ public ArrayList<ConversationsInfo> download(){
ArrayList<ConversationsInfo> list = new ArrayList<>(); ArrayList<ConversationsInfo> list = new ArrayList<>();

View File

@ -4,12 +4,17 @@ import android.app.Fragment;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.ArrayMap;
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;
import android.widget.Toast; import android.widget.Toast;
import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.R;
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.UserInfo;
import org.communiquons.android.comunic.client.data.conversations.ConversationsInfo; import org.communiquons.android.comunic.client.data.conversations.ConversationsInfo;
import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper; import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper;
@ -36,6 +41,11 @@ public class ConversationsListFragment extends Fragment {
*/ */
private ArrayList<ConversationsInfo> convList; private ArrayList<ConversationsInfo> convList;
/**
* User information helper
*/
private GetUsersHelper userHelper;
/** /**
* The conversation list helper * The conversation list helper
*/ */
@ -51,6 +61,12 @@ public class ConversationsListFragment extends Fragment {
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
//Database helper
DatabaseHelper dbHelper = new DatabaseHelper(getActivity());
//Instantiate the user informations helper
userHelper = new GetUsersHelper(getActivity(), dbHelper);
//Create the conversation list helper //Create the conversation list helper
conversationsListHelper = new ConversationsListHelper(getActivity()); conversationsListHelper = new ConversationsListHelper(getActivity());
@ -60,13 +76,15 @@ public class ConversationsListFragment extends Fragment {
protected ArrayList<ConversationsInfo> doInBackground(Void... params) { protected ArrayList<ConversationsInfo> doInBackground(Void... params) {
//Get the list of conversations //Get the list of conversations
return conversationsListHelper.download(); ArrayList<ConversationsInfo> list = conversationsListHelper.download();
process_conversations_list(list);
return list;
} }
@Override @Override
protected void onPostExecute(ArrayList<ConversationsInfo> list) { protected void onPostExecute(ArrayList<ConversationsInfo> list) {
process_conversations_list(list); display_conversations_list(list);
} }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
@ -82,14 +100,12 @@ public class ConversationsListFragment extends Fragment {
//Check if got the list //Check if got the list
if(list == null){ if(list == null){
Toast.makeText(getActivity(), R.string.fragment_conversationslist_err_get_list, return; //Nothing to be done
Toast.LENGTH_LONG).show();
return;
} }
//Process the list of conversation //Process the list of conversation
ArrayList<Integer> usersToGet = new ArrayList<>(); ArrayList<Integer> usersToGet = new ArrayList<>();
ArrayList<ConversationsInfo> convToUpdate = new ArrayList<>(); ArrayList<ConversationsInfo> convToUpdateName = new ArrayList<>();
for(ConversationsInfo conv : list){ for(ConversationsInfo conv : list){
//Set the displayed names of the conversation //Set the displayed names of the conversation
@ -109,12 +125,71 @@ public class ConversationsListFragment extends Fragment {
} }
convToUpdate.add(conv); convToUpdateName.add(conv);
}
}
//Check if we have user to get information about
if(usersToGet.size() > 0){
//Get information about the users
ArrayMap<Integer, UserInfo> usersInfo = userHelper.getMultiple(usersToGet);
//Check for errors
if(usersInfo == null){
Log.e(TAG, "Couldn't get informations about some users !");
return;
}
//Process the conversation that have to be processed
for(ConversationsInfo conv : convToUpdateName){
//Get the name of the first members
String conversationName = "";
int count = 0;
for(int userID : conv.getMembers()){
if(usersInfo.containsKey(userID)){
UserInfo userInfo = usersInfo.get(userID);
if(userInfo != null){
conversationName += userInfo.getFullName() + ", ";
count++;
} }
} }
if(count == 2)
break;
}
if(conv.getMembers().size() > 3)
conversationName += "...";
//Update the displayed name of the conversation
conv.setDisplayName(conversationName);
}
}
}
/**
* Display the conversation list
*
* @param list The list to display
*/
public void display_conversations_list(ArrayList<ConversationsInfo> list){
//Check if we got a list
if(list == null) {
Toast.makeText(getActivity(), R.string.fragment_conversationslist_err_get_list,
Toast.LENGTH_LONG).show();
return;
}
for(ConversationsInfo info : list){
Log.v(TAG, "Conversation " + info.getID() + " : " + info.getDisplayName());
}
} }
} }