Added user account image on conversations

This commit is contained in:
Pierre 2017-12-19 18:38:54 +01:00
parent c0545c5db5
commit 0f2f28ec36
3 changed files with 131 additions and 35 deletions

View File

@ -1,11 +1,9 @@
package org.communiquons.android.comunic.client.data.conversations; package org.communiquons.android.comunic.client.data.conversations;
import android.content.Context; import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log; import android.util.ArrayMap;
import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -14,6 +12,8 @@ import android.widget.ImageView;
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.ImageLoad.ImageLoadManager;
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo;
import java.util.ArrayList; import java.util.ArrayList;
@ -36,6 +36,11 @@ public class ConversationMessageAdapter extends ArrayAdapter<ConversationMessage
*/ */
private int userID; private int userID;
/**
* Information about users
*/
private ArrayMap<Integer, UserInfo> usersInfo;
/** /**
* Public class constructor * Public class constructor
* *
@ -44,12 +49,15 @@ public class ConversationMessageAdapter extends ArrayAdapter<ConversationMessage
* @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, ArrayList<ConversationMessage> list,
int userID){ int userID, ArrayMap<Integer, UserInfo> usersInfo){
super(context, 0, list); super(context, 0, list);
//Set user ID //Set user ID
this.userID = userID; this.userID = userID;
//Set user information list
this.usersInfo = usersInfo;
} }
@ -99,6 +107,14 @@ public class ConversationMessageAdapter extends ArrayAdapter<ConversationMessage
findViewById(R.id.fragment_conversation_message_item_accountimage); findViewById(R.id.fragment_conversation_message_item_accountimage);
} }
/*
Check for user information
*/
UserInfo user = null;
if(usersInfo.containsKey(message.getUser_id())){
user = usersInfo.get(message.getUser_id());
}
/* /*
Update message content Update message content
@ -110,7 +126,17 @@ public class ConversationMessageAdapter extends ArrayAdapter<ConversationMessage
/* /*
Update account image Update account image
*/ */
//Cancel any load pending operation
ImageLoadManager.remove(accountImage);
//Set the default image
accountImage.setImageResource(R.drawable.default_account_image);
//Check if we can load a specific image
if(user != null) {
String imageURL = user.getAcountImageURL();
ImageLoadManager.load(getContext(), imageURL, accountImage);
}
return convertView; return convertView;

View File

@ -1,9 +1,11 @@
package org.communiquons.android.comunic.client.fragments; package org.communiquons.android.comunic.client.fragments;
import android.app.Fragment; import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.ArrayMap;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -13,11 +15,14 @@ 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.Account.AccountUtils; import org.communiquons.android.comunic.client.data.Account.AccountUtils;
import org.communiquons.android.comunic.client.data.DatabaseHelper; 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.ConversationMessage; import org.communiquons.android.comunic.client.data.conversations.ConversationMessage;
import org.communiquons.android.comunic.client.data.conversations.ConversationMessageAdapter; import org.communiquons.android.comunic.client.data.conversations.ConversationMessageAdapter;
import org.communiquons.android.comunic.client.data.conversations.ConversationMessagesHelper; import org.communiquons.android.comunic.client.data.conversations.ConversationMessagesHelper;
import org.communiquons.android.comunic.client.data.conversations.ConversationRefreshRunnable; import org.communiquons.android.comunic.client.data.conversations.ConversationRefreshRunnable;
import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
@ -57,6 +62,11 @@ public class ConversationFragment extends Fragment
*/ */
private ArrayList<ConversationMessage> messagesList = new ArrayList<>(); private ArrayList<ConversationMessage> messagesList = new ArrayList<>();
/**
* Informations about the users of the conversation
*/
private ArrayMap<Integer, UserInfo> users = new ArrayMap<>();
/** /**
* Conversation refresh runnable * Conversation refresh runnable
*/ */
@ -72,6 +82,11 @@ public class ConversationFragment extends Fragment
*/ */
private ConversationMessageAdapter convMessAdapter; private ConversationMessageAdapter convMessAdapter;
/**
* Get user helper
*/
private GetUsersHelper getUsersHelper;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -85,6 +100,9 @@ public class ConversationFragment extends Fragment
//Get the conversation ID //Get the conversation ID
conversation_id = getArguments().getInt(ARG_CONVERSATION_ID); conversation_id = getArguments().getInt(ARG_CONVERSATION_ID);
//Get user helper
getUsersHelper = new GetUsersHelper(getActivity(), dbHelper);
if(conversation_id < 1){ if(conversation_id < 1){
throw new RuntimeException(TAG + " requires a valid conversation ID when created !"); throw new RuntimeException(TAG + " requires a valid conversation ID when created !");
} }
@ -105,8 +123,14 @@ public class ConversationFragment extends Fragment
//Conversation messages listView //Conversation messages listView
ListView convMessListView = view.findViewById(R.id.fragment_conversation_messageslist); ListView convMessListView = view.findViewById(R.id.fragment_conversation_messageslist);
//Need user ID
int userID = new AccountUtils(getActivity()).get_current_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); convMessListView.setAdapter(convMessAdapter);
} }
@ -137,14 +161,34 @@ public class ConversationFragment extends Fragment
@Override @Override
public void onAddMessage(int lastID, @NonNull ArrayList<ConversationMessage> newMessages) { public void onAddMessage(int lastID, @NonNull ArrayList<ConversationMessage> newMessages) {
final ArrayList<Integer> usersToFetch = new ArrayList<>();
//Add the messages to the the main list of messages //Add the messages to the the main list of messages
for(ConversationMessage message : newMessages){ for(ConversationMessage message : newMessages){
messagesList.add(message); messagesList.add(message);
if(!users.containsKey(message.getUser_id()))
usersToFetch.add(message.getUser_id());
} }
convMessAdapter.notifyDataSetChanged(); convMessAdapter.notifyDataSetChanged();
last_message_id = lastID; last_message_id = lastID;
//Fetch user information if required
if(usersToFetch.size() > 0){
new AsyncTask<Void, Void, ArrayMap<Integer, UserInfo>>(){
@Override
protected ArrayMap<Integer, UserInfo> doInBackground(Void... params) {
//Get the users list
return getUsersHelper.getMultiple(usersToFetch);
}
@Override
protected void onPostExecute(ArrayMap<Integer, UserInfo> usersInfo) {
onGotUserInfo(usersInfo);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
} }
@Override @Override
@ -153,4 +197,28 @@ public class ConversationFragment extends Fragment
Toast.makeText(getActivity(), R.string.fragment_conversation_err_load_message, Toast.makeText(getActivity(), R.string.fragment_conversation_err_load_message,
Toast.LENGTH_SHORT).show(); 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<Integer, UserInfo> 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();
}
} }

View File

@ -1,15 +1,16 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingTop="5dp" android:orientation="vertical"
android:paddingBottom="5dp" android:paddingBottom="5dp"
android:paddingEnd="5dp"
android:paddingStart="5dp" android:paddingStart="5dp"
android:paddingEnd="5dp"> android:paddingTop="5dp">
<LinearLayout <!-- Messages on the left -->
<RelativeLayout
android:id="@+id/fragment_conversation_message_left" android:id="@+id/fragment_conversation_message_left"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -17,8 +18,8 @@
<ImageView <ImageView
android:id="@+id/fragment_conversation_message_item_accountimage" android:id="@+id/fragment_conversation_message_item_accountimage"
android:layout_width="24dp" android:layout_width="26dp"
android:layout_height="24dp" android:layout_height="26dp"
android:layout_gravity="center" android:layout_gravity="center"
android:layout_marginEnd="5dp" android:layout_marginEnd="5dp"
android:src="@drawable/default_account_image" /> android:src="@drawable/default_account_image" />
@ -27,46 +28,47 @@
android:id="@+id/fragment_conversation_message_item_content" android:id="@+id/fragment_conversation_message_item_content"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="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:background="@color/conversation_otheruser_messages_background"
android:padding="5dp" android:padding="5dp"
android:paddingStart="15dp"
android:paddingEnd="15dp" android:paddingEnd="15dp"
tools:text="A message" android:paddingStart="15dp"
android:layout_gravity="center" android:textColor="@color/conversation_otheruser_messages_textColor"
android:textColor="@color/conversation_otheruser_messages_textColor"/> tools:text="A message"/>
</LinearLayout> </RelativeLayout>
<LinearLayout <!-- Messages on the right -->
<RelativeLayout
android:id="@+id/fragment_conversation_message_right" android:id="@+id/fragment_conversation_message_right"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="1dp"> android:padding="1dp">
<ImageView
android:id="@+id/fragment_conversation_message_item_accountimage_right"
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_alignParentEnd="true"
android:layout_gravity="end"
android:layout_marginStart="5dp"
android:src="@drawable/default_account_image" />
<TextView <TextView
android:id="@+id/fragment_conversation_message_item_content_right" android:id="@+id/fragment_conversation_message_item_content_right"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_toStartOf="@id/fragment_conversation_message_item_accountimage_right"
android:background="@color/conversation_user_messages_background" android:background="@color/conversation_user_messages_background"
android:padding="5dp" android:padding="5dp"
android:paddingStart="15dp"
android:paddingEnd="15dp" android:paddingEnd="15dp"
tools:text="A message" android:paddingStart="15dp"
android:layout_gravity="center" android:textColor="@color/conversation_user_messages_textColor"
android:textColor="@color/conversation_user_messages_textColor"/> tools:text="A message"/>
<ImageView
android:id="@+id/fragment_conversation_message_item_accountimage_right"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:layout_marginStart="5dp"
android:src="@drawable/default_account_image" />
</LinearLayout>
</RelativeLayout>
</LinearLayout> </LinearLayout>