Created ConversationMessagesList class

This commit is contained in:
Pierre HUBERT 2018-08-15 19:18:12 +02:00
parent 12bdee1617
commit a0feee9516
3 changed files with 95 additions and 47 deletions

View File

@ -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<ConversationMessage> {
/**
* Information about the related users
*/
private ArrayMap<Integer, UserInfo> mUsersInfo = new ArrayMap<>();
public ArrayMap<Integer, UserInfo> getUsersInfo() {
return mUsersInfo;
}
public void setUsersInfo(ArrayMap<Integer, UserInfo> 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<Integer> getMissingUsersList(){
ArrayList<Integer> 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;
}
}

View File

@ -11,6 +11,7 @@ import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import org.communiquons.android.comunic.client.R; 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.UserInfo;
import org.communiquons.android.comunic.client.data.models.ConversationMessage; import org.communiquons.android.comunic.client.data.models.ConversationMessage;
import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage; import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage;
@ -42,11 +43,6 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter {
*/ */
private int userID; private int userID;
/**
* Information about users
*/
private ArrayMap<Integer, UserInfo> usersInfo;
/** /**
* Activity context * Activity context
*/ */
@ -55,22 +51,20 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter {
/** /**
* Conversation messages * Conversation messages
*/ */
private ArrayList<ConversationMessage> mList; private ConversationMessagesList mList;
/** /**
* Public class constructor * Public class constructor
* *
* @param context The context of execution of the application * @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 * @param userID The ID of the current user
*/ */
public ConversationMessageAdapter(Context context, ArrayList<ConversationMessage> list, public ConversationMessageAdapter(Context context, ConversationMessagesList list, int userID){
int userID, ArrayMap<Integer, UserInfo> usersInfo){
super(); super();
//Set values //Set values
this.userID = userID; this.userID = userID;
this.usersInfo = usersInfo;
this.mContext = context; this.mContext = context;
this.mList = list; this.mList = list;
@ -81,10 +75,6 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter {
return mList.size(); return mList.size();
} }
public ConversationMessage getAt(int pos){
return mList.get(pos);
}
@Override @Override
public int getItemViewType(int position) { public int getItemViewType(int position) {
return mList.get(position).getUser_id() == userID ? VIEW_TYPE_MESSAGE_SENT return mList.get(position).getUser_id() == userID ? VIEW_TYPE_MESSAGE_SENT
@ -145,7 +135,7 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter {
*/ */
@CallSuper @CallSuper
void bind(int pos){ 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(); mUserAccountImage.removeUser();
mUserName.setText(""); mUserName.setText("");
if(usersInfo.containsKey(getAt(pos).getUser_id())){ if(mList.hasUserForMessage(pos)){
UserInfo info = usersInfo.get(getAt(pos).getUser_id()); UserInfo info = mList.getUserForMessage(pos);
mUserAccountImage.setUser(info); mUserAccountImage.setUser(info);
mUserName.setText(info.getDisplayFullName()); mUserName.setText(info.getDisplayFullName());
} }
@ -195,7 +185,7 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter {
if(pos < 2) if(pos < 2)
setUserInfoVisibility(true); setUserInfoVisibility(true);
else 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); setUserInfoVisibility(false);
} }
} }

View File

@ -24,6 +24,7 @@ import android.widget.TextView;
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.arrays.ConversationMessagesList;
import org.communiquons.android.comunic.client.data.helpers.ConversationMessagesHelper; 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.ConversationsListHelper;
import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper; 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.listeners.OnScrollChangeDetectListener;
import org.communiquons.android.comunic.client.ui.utils.BitmapUtils; 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.utils.UiUtils;
import org.communiquons.android.comunic.client.ui.views.ScrollListView;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
@ -90,12 +90,7 @@ public class ConversationFragment extends Fragment
/** /**
* The list of messages of the conversation * The list of messages of the conversation
*/ */
private ArrayList<ConversationMessage> messagesList = new ArrayList<>(); private ConversationMessagesList messagesList = new ConversationMessagesList();
/**
* Informations about the users of the conversation
*/
private ArrayMap<Integer, UserInfo> users = new ArrayMap<>();
/** /**
* Conversation refresh runnable * Conversation refresh runnable
@ -181,6 +176,7 @@ public class ConversationFragment extends Fragment
convListHelper = new ConversationsListHelper(getActivity(), dbHelper); convListHelper = new ConversationsListHelper(getActivity(), dbHelper);
//Get the conversation ID //Get the conversation ID
assert getArguments() != null;
conversation_id = getArguments().getInt(ARG_CONVERSATION_ID); conversation_id = getArguments().getInt(ARG_CONVERSATION_ID);
//Get user helper //Get user helper
@ -232,7 +228,7 @@ public class ConversationFragment extends Fragment
//Create the adapter //Create the adapter
convMessAdapter = new ConversationMessageAdapter(getActivity(), convMessAdapter = new ConversationMessageAdapter(getActivity(),
messagesList, userID, users); messagesList, userID);
//Apply adapter //Apply adapter
convMessRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); convMessRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
@ -319,12 +315,12 @@ public class ConversationFragment extends Fragment
@Override @Override
protected void onPostExecute(ConversationsInfo conversationsInfo) { protected void onPostExecute(ConversationsInfo conversationsInfo) {
onGotConversationInfos(conversationsInfo); onGotConversationInfo(conversationsInfo);
} }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
else else
onGotConversationInfos(conversationInfo); onGotConversationInfo(conversationInfo);
} }
@Override @Override
@ -378,17 +374,7 @@ public class ConversationFragment extends Fragment
if(messagesList == null) if(messagesList == null)
return; return;
final ArrayList<Integer> usersToFetch = new ArrayList<>(); final ArrayList<Integer> usersToFetch = messagesList.getMissingUsersList();
//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());
}
}
//Fetch user information if required //Fetch user information if required
@ -424,7 +410,7 @@ public class ConversationFragment extends Fragment
//Process the list of users //Process the list of users
for(UserInfo user : info.values()){ for(UserInfo user : info.values()){
if(user != null){ 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 //Check for errors
if(infos == null){ if(info == null){
Toast.makeText(getActivity(), R.string.fragment_conversation_err_getconvinfos, Toast.makeText(getActivity(), R.string.fragment_conversation_err_getconvinfos,
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
return; return;
} }
//Save conversation informations //Save conversation information
conversationInfo = infos; conversationInfo = info;
//Update the name of the conversation //Update the name of the conversation
getActivity().setTitle(infos.getDisplayName()); getActivity().setTitle(info.getDisplayName());
} }