From a0feee9516c2c3885f00b935a899cd9477145636 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 15 Aug 2018 19:18:12 +0200 Subject: [PATCH] Created ConversationMessagesList class --- .../data/arrays/ConversationMessagesList.java | 72 +++++++++++++++++++ .../adapters/ConversationMessageAdapter.java | 26 +++---- .../ui/fragments/ConversationFragment.java | 44 ++++-------- 3 files changed, 95 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/arrays/ConversationMessagesList.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/ConversationMessagesList.java b/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/ConversationMessagesList.java new file mode 100644 index 0000000..aafb7e7 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/arrays/ConversationMessagesList.java @@ -0,0 +1,72 @@ +package org.communiquons.android.comunic.client.data.arrays; + +import android.util.ArrayMap; + +import org.communiquons.android.comunic.client.data.models.ConversationMessage; +import org.communiquons.android.comunic.client.data.models.UserInfo; + +import java.util.ArrayList; + +/** + * Conversation messages list + * + * @author Pierre HUBERT + */ +public class ConversationMessagesList extends ArrayList { + + /** + * Information about the related users + */ + private ArrayMap mUsersInfo = new ArrayMap<>(); + + + public ArrayMap getUsersInfo() { + return mUsersInfo; + } + + public void setUsersInfo(ArrayMap usersInfo) { + this.mUsersInfo = usersInfo; + } + + /** + * Check whether information about the user who posted the message at pos are present or not + * + * @param pos The position to check + * @return The result of the operation + */ + public boolean hasUserForMessage(int pos){ + return mUsersInfo.containsKey(get(pos).getUser_id()); + } + + /** + * Get the information about a user who posted a message + * + * @param pos The position of the message to get + * @return Infomration about the user + */ + public UserInfo getUserForMessage(int pos){ + return mUsersInfo.get(get(pos).getUser_id()); + } + + /** + * Get the ID of the missing users in the user information list + * + * @return Information about the user + */ + public ArrayList getMissingUsersList(){ + + ArrayList missing = new ArrayList<>(); + + //Process the list of messages + for(ConversationMessage message : this){ + + if(!getUsersInfo().containsKey(message.getUser_id())){ + if(!missing.contains(message.getUser_id())) + missing.add(message.getUser_id()); + } + + } + + return missing; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java index 50ffcc6..d05fc6c 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/ConversationMessageAdapter.java @@ -11,6 +11,7 @@ import android.view.ViewGroup; import android.widget.TextView; import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.arrays.ConversationMessagesList; import org.communiquons.android.comunic.client.data.models.UserInfo; import org.communiquons.android.comunic.client.data.models.ConversationMessage; import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage; @@ -42,11 +43,6 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter { */ private int userID; - /** - * Information about users - */ - private ArrayMap usersInfo; - /** * Activity context */ @@ -55,22 +51,20 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter { /** * Conversation messages */ - private ArrayList mList; + private ConversationMessagesList mList; /** * Public class constructor * * @param context The context of execution of the application - * @param list The dataset + * @param list The list of message * @param userID The ID of the current user */ - public ConversationMessageAdapter(Context context, ArrayList list, - int userID, ArrayMap usersInfo){ + public ConversationMessageAdapter(Context context, ConversationMessagesList list, int userID){ super(); //Set values this.userID = userID; - this.usersInfo = usersInfo; this.mContext = context; this.mList = list; @@ -81,10 +75,6 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter { return mList.size(); } - public ConversationMessage getAt(int pos){ - return mList.get(pos); - } - @Override public int getItemViewType(int position) { return mList.get(position).getUser_id() == userID ? VIEW_TYPE_MESSAGE_SENT @@ -145,7 +135,7 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter { */ @CallSuper void bind(int pos){ - mMessage.setText(getAt(pos).getContent()); + mMessage.setText(mList.get(pos).getContent()); } } @@ -186,8 +176,8 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter { mUserAccountImage.removeUser(); mUserName.setText(""); - if(usersInfo.containsKey(getAt(pos).getUser_id())){ - UserInfo info = usersInfo.get(getAt(pos).getUser_id()); + if(mList.hasUserForMessage(pos)){ + UserInfo info = mList.getUserForMessage(pos); mUserAccountImage.setUser(info); mUserName.setText(info.getDisplayFullName()); } @@ -195,7 +185,7 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter { if(pos < 2) setUserInfoVisibility(true); else - if(getAt(pos).getUser_id() == getAt(pos-1).getUser_id()) + if(mList.get(pos).getUser_id() == mList.get(pos-1).getUser_id()) setUserInfoVisibility(false); } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java index 5d2a9d6..b214cce 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java @@ -24,6 +24,7 @@ import android.widget.TextView; import android.widget.Toast; import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.arrays.ConversationMessagesList; import org.communiquons.android.comunic.client.data.helpers.ConversationMessagesHelper; import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper; import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper; @@ -37,7 +38,6 @@ import org.communiquons.android.comunic.client.ui.adapters.ConversationMessageAd import org.communiquons.android.comunic.client.ui.listeners.OnScrollChangeDetectListener; import org.communiquons.android.comunic.client.ui.utils.BitmapUtils; import org.communiquons.android.comunic.client.ui.utils.UiUtils; -import org.communiquons.android.comunic.client.ui.views.ScrollListView; import java.io.FileNotFoundException; import java.util.ArrayList; @@ -90,12 +90,7 @@ public class ConversationFragment extends Fragment /** * The list of messages of the conversation */ - private ArrayList messagesList = new ArrayList<>(); - - /** - * Informations about the users of the conversation - */ - private ArrayMap users = new ArrayMap<>(); + private ConversationMessagesList messagesList = new ConversationMessagesList(); /** * Conversation refresh runnable @@ -181,6 +176,7 @@ public class ConversationFragment extends Fragment convListHelper = new ConversationsListHelper(getActivity(), dbHelper); //Get the conversation ID + assert getArguments() != null; conversation_id = getArguments().getInt(ARG_CONVERSATION_ID); //Get user helper @@ -232,7 +228,7 @@ public class ConversationFragment extends Fragment //Create the adapter convMessAdapter = new ConversationMessageAdapter(getActivity(), - messagesList, userID, users); + messagesList, userID); //Apply adapter convMessRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); @@ -319,12 +315,12 @@ public class ConversationFragment extends Fragment @Override protected void onPostExecute(ConversationsInfo conversationsInfo) { - onGotConversationInfos(conversationsInfo); + onGotConversationInfo(conversationsInfo); } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } else - onGotConversationInfos(conversationInfo); + onGotConversationInfo(conversationInfo); } @Override @@ -378,17 +374,7 @@ public class ConversationFragment extends Fragment if(messagesList == null) return; - final ArrayList usersToFetch = new ArrayList<>(); - - //Process the list of messages - for(ConversationMessage message : messagesList){ - - if(!users.containsKey(message.getUser_id())){ - if(!usersToFetch.contains(message.getUser_id())) - usersToFetch.add(message.getUser_id()); - } - - } + final ArrayList usersToFetch = messagesList.getMissingUsersList(); //Fetch user information if required @@ -424,7 +410,7 @@ public class ConversationFragment extends Fragment //Process the list of users for(UserInfo user : info.values()){ if(user != null){ - users.put(user.getId(), user); + messagesList.getUsersInfo().put(user.getId(), user); } } @@ -433,25 +419,25 @@ public class ConversationFragment extends Fragment } /** - * What to do once we got conversation informations + * What to do once we got conversation information * - * @param infos Informations about the conversation + * @param info Information about the conversation */ - private void onGotConversationInfos(ConversationsInfo infos){ + private void onGotConversationInfo(ConversationsInfo info){ //Check for errors - if(infos == null){ + if(info == null){ Toast.makeText(getActivity(), R.string.fragment_conversation_err_getconvinfos, Toast.LENGTH_SHORT).show(); return; } - //Save conversation informations - conversationInfo = infos; + //Save conversation information + conversationInfo = info; //Update the name of the conversation - getActivity().setTitle(infos.getDisplayName()); + getActivity().setTitle(info.getDisplayName()); }