mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 13:59:29 +00:00
Created ConversationMessagesList class
This commit is contained in:
parent
12bdee1617
commit
a0feee9516
@ -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;
|
||||
}
|
||||
}
|
@ -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<Integer, UserInfo> usersInfo;
|
||||
|
||||
/**
|
||||
* Activity context
|
||||
*/
|
||||
@ -55,22 +51,20 @@ public class ConversationMessageAdapter extends RecyclerView.Adapter {
|
||||
/**
|
||||
* Conversation messages
|
||||
*/
|
||||
private ArrayList<ConversationMessage> 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<ConversationMessage> list,
|
||||
int userID, ArrayMap<Integer, UserInfo> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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<ConversationMessage> messagesList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Informations about the users of the conversation
|
||||
*/
|
||||
private ArrayMap<Integer, UserInfo> 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<Integer> 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<Integer> 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());
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user