diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessageAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessageAdapter.java index fee4aa1..1b88d2b 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessageAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessageAdapter.java @@ -1,11 +1,9 @@ package org.communiquons.android.comunic.client.data.conversations; import android.content.Context; -import android.os.Build; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.util.Log; -import android.view.Gravity; +import android.util.ArrayMap; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -14,6 +12,8 @@ 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.UsersInfo.UserInfo; import java.util.ArrayList; @@ -36,6 +36,11 @@ public class ConversationMessageAdapter extends ArrayAdapter usersInfo; + /** * Public class constructor * @@ -44,12 +49,15 @@ public class ConversationMessageAdapter extends ArrayAdapter list, - int userID){ + int userID, ArrayMap usersInfo){ super(context, 0, list); //Set user ID this.userID = userID; + //Set user information list + this.usersInfo = usersInfo; + } @@ -99,6 +107,14 @@ public class ConversationMessageAdapter extends ArrayAdapter messagesList = new ArrayList<>(); + /** + * Informations about the users of the conversation + */ + private ArrayMap users = new ArrayMap<>(); + /** * Conversation refresh runnable */ @@ -72,6 +82,11 @@ public class ConversationFragment extends Fragment */ private ConversationMessageAdapter convMessAdapter; + /** + * Get user helper + */ + private GetUsersHelper getUsersHelper; + @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -85,6 +100,9 @@ public class ConversationFragment extends Fragment //Get the conversation ID conversation_id = getArguments().getInt(ARG_CONVERSATION_ID); + //Get user helper + getUsersHelper = new GetUsersHelper(getActivity(), dbHelper); + if(conversation_id < 1){ throw new RuntimeException(TAG + " requires a valid conversation ID when created !"); } @@ -105,8 +123,14 @@ public class ConversationFragment extends Fragment //Conversation messages listView ListView convMessListView = view.findViewById(R.id.fragment_conversation_messageslist); + //Need user ID int userID = new AccountUtils(getActivity()).get_current_user_id(); - convMessAdapter = new ConversationMessageAdapter(getActivity(), messagesList, userID); + + //Create the adapter + convMessAdapter = new ConversationMessageAdapter(getActivity(), + messagesList, userID, users); + + //Apply adapter convMessListView.setAdapter(convMessAdapter); } @@ -137,14 +161,34 @@ public class ConversationFragment extends Fragment @Override public void onAddMessage(int lastID, @NonNull ArrayList newMessages) { + final ArrayList usersToFetch = new ArrayList<>(); + //Add the messages to the the main list of messages for(ConversationMessage message : newMessages){ messagesList.add(message); + + if(!users.containsKey(message.getUser_id())) + usersToFetch.add(message.getUser_id()); } convMessAdapter.notifyDataSetChanged(); - last_message_id = lastID; + + //Fetch user information if required + if(usersToFetch.size() > 0){ + new AsyncTask>(){ + @Override + protected ArrayMap doInBackground(Void... params) { + //Get the users list + return getUsersHelper.getMultiple(usersToFetch); + } + + @Override + protected void onPostExecute(ArrayMap usersInfo) { + onGotUserInfo(usersInfo); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } } @Override @@ -153,4 +197,28 @@ public class ConversationFragment extends Fragment Toast.makeText(getActivity(), R.string.fragment_conversation_err_load_message, Toast.LENGTH_SHORT).show(); } + + /** + * This method is called when we get informations about users + * + * @param info Informations about the user + */ + public void onGotUserInfo(@Nullable ArrayMap info ){ + + //Check for errors + if(info == null){ + //This is a failure + return; + } + + //Process the list of users + for(UserInfo user : info.values()){ + if(user != null){ + users.put(user.getId(), user); + } + } + + //Inform about dataset update + convMessAdapter.notifyDataSetChanged(); + } } diff --git a/app/src/main/res/layout/fragment_conversation_message_item.xml b/app/src/main/res/layout/fragment_conversation_message_item.xml index 9509c68..3d03447 100644 --- a/app/src/main/res/layout/fragment_conversation_message_item.xml +++ b/app/src/main/res/layout/fragment_conversation_message_item.xml @@ -1,15 +1,16 @@ + android:paddingTop="5dp"> - + @@ -27,46 +28,47 @@ android:id="@+id/fragment_conversation_message_item_content" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_gravity="center" + android:layout_toEndOf="@id/fragment_conversation_message_item_accountimage" android:background="@color/conversation_otheruser_messages_background" android:padding="5dp" - android:paddingStart="15dp" android:paddingEnd="15dp" - tools:text="A message" - android:layout_gravity="center" - android:textColor="@color/conversation_otheruser_messages_textColor"/> + android:paddingStart="15dp" + android:textColor="@color/conversation_otheruser_messages_textColor" + tools:text="A message"/> - + - + + + - - - - + android:paddingStart="15dp" + android:textColor="@color/conversation_user_messages_textColor" + tools:text="A message"/> + \ No newline at end of file