From b8ca813d09d3f75ae60e142ec46e20c93eb6cc24 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 10 Dec 2017 13:46:13 +0100 Subject: [PATCH] Generate conversations displayed name --- .../client/data/UsersInfo/GetUsersHelper.java | 2 +- .../data/conversations/ConversationsInfo.java | 14 +-- .../ConversationsListHelper.java | 1 + .../fragments/ConversationsListFragment.java | 91 +++++++++++++++++-- 4 files changed, 92 insertions(+), 16 deletions(-) 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 1269027..2d1e0c9 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 @@ -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 !"); return null; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsInfo.java index 64e0c46..ee636af 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsInfo.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsInfo.java @@ -52,7 +52,7 @@ public class ConversationsInfo { * * @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; } @@ -70,7 +70,7 @@ public class ConversationsInfo { * * @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; } @@ -88,7 +88,7 @@ public class ConversationsInfo { * * @param name The name of the conversation */ - public void setName(@Nullable String name) { + public void setName(@Nullable String name) { //Check the validity of the name if(name == "false" || name == "null" || name == null) @@ -112,7 +112,7 @@ public class ConversationsInfo { * @return True if the conversation has a name / false else */ 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 */ - public void setFollowing(boolean following) { + void setFollowing(boolean 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 */ - public void setSaw_last_message(boolean saw_last_message) { + void setSaw_last_message(boolean 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 */ - public void addMember(Integer id){ + void addMember(Integer id){ if(members == null) members = new ArrayList<>(); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java index 6f4d5c6..8f61da8 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java @@ -44,6 +44,7 @@ public class ConversationsListHelper { * * @return The list of conversations */ + @Nullable public ArrayList download(){ ArrayList list = new ArrayList<>(); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java index f43c27f..e3ca998 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java @@ -4,12 +4,17 @@ import android.app.Fragment; import android.os.AsyncTask; 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.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.ConversationsListHelper; @@ -36,6 +41,11 @@ public class ConversationsListFragment extends Fragment { */ private ArrayList convList; + /** + * User information helper + */ + private GetUsersHelper userHelper; + /** * The conversation list helper */ @@ -51,6 +61,12 @@ public class ConversationsListFragment extends Fragment { public void onViewCreated(View view, @Nullable Bundle 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 conversationsListHelper = new ConversationsListHelper(getActivity()); @@ -60,13 +76,15 @@ public class ConversationsListFragment extends Fragment { protected ArrayList doInBackground(Void... params) { //Get the list of conversations - return conversationsListHelper.download(); + ArrayList list = conversationsListHelper.download(); + process_conversations_list(list); + return list; } @Override protected void onPostExecute(ArrayList list) { - process_conversations_list(list); + display_conversations_list(list); } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); @@ -82,14 +100,12 @@ public class ConversationsListFragment extends Fragment { //Check if got the list if(list == null){ - Toast.makeText(getActivity(), R.string.fragment_conversationslist_err_get_list, - Toast.LENGTH_LONG).show(); - return; + return; //Nothing to be done } //Process the list of conversation ArrayList usersToGet = new ArrayList<>(); - ArrayList convToUpdate = new ArrayList<>(); + ArrayList convToUpdateName = new ArrayList<>(); for(ConversationsInfo conv : list){ //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 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 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()); + } + } }