Display briefly the messages of a conversation

This commit is contained in:
Pierre 2017-12-18 17:55:33 +01:00
parent fd3ba109fc
commit 3a70883eff
4 changed files with 81 additions and 10 deletions

View File

@ -0,0 +1,46 @@
package org.communiquons.android.comunic.client.data.conversations;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Conversation messages adapter
*
* @author Pierre HUBERT
* Created by pierre on 12/18/17.
*/
public class ConversationMessageAdapter extends ArrayAdapter<ConversationMessage> {
/**
* Public class constructor
*
* @param context The context of execution of the application
* @param list The dataset
*/
public ConversationMessageAdapter(Context context, ArrayList<ConversationMessage> list){
super(context, 0, list);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if(convertView == null)
convertView = LayoutInflater.from(getContext()).
inflate(android.R.layout.simple_list_item_1, parent, false);
((TextView) convertView).setText(getItem(position).getContent());
return convertView;
}
}

View File

@ -91,9 +91,10 @@ public class ConversationRefreshRunnable implements Runnable {
/** /**
* Add new messages to a previous list of messages * Add new messages to a previous list of messages
* *
* @param lastID The ID of the latest message downloaded from server
* @param messages The new messagess * @param messages The new messagess
*/ */
void onAddMessage(@NonNull ArrayList<ConversationMessage> messages); void onAddMessage(int lastID, @NonNull ArrayList<ConversationMessage> messages);
/** /**
* This method is called when there is not any message in the conversation * This method is called when there is not any message in the conversation
@ -116,6 +117,7 @@ public class ConversationRefreshRunnable implements Runnable {
Log.v(TAG, "Started conversation refresh runnable."); Log.v(TAG, "Started conversation refresh runnable.");
synchronized (object) { synchronized (object) {
//Loop that execute indefinitely until the fragment is stopped //Loop that execute indefinitely until the fragment is stopped
while (!quit) { while (!quit) {
@ -135,7 +137,7 @@ public class ConversationRefreshRunnable implements Runnable {
} }
//Get the ID of the last message available in the local database //Get the ID of the last message available in the local database
int lastMessageInDb = convMessHelper.getLastIDFromDb(conversation_id); final int lastMessageInDb = convMessHelper.getLastIDFromDb(conversation_id);
//If the last message in the database is newer than the last message already read //If the last message in the database is newer than the last message already read
if (lastMessageInDb > last_message_id) { if (lastMessageInDb > last_message_id) {
@ -166,7 +168,7 @@ public class ConversationRefreshRunnable implements Runnable {
mActivity.runOnUiThread(new Runnable() { mActivity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
listener.onAddMessage(newMessages); listener.onAddMessage(lastMessageInDb, newMessages);
} }

View File

@ -7,11 +7,13 @@ import android.support.annotation.Nullable;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ListView;
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.DatabaseHelper; import org.communiquons.android.comunic.client.data.DatabaseHelper;
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.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;
@ -50,9 +52,9 @@ public class ConversationFragment extends Fragment
private int last_message_id = 0; private int last_message_id = 0;
/** /**
* The list of messages * The list of messages of the conversation
*/ */
private ArrayList<ConversationMessage> messagesList; private ArrayList<ConversationMessage> messagesList = new ArrayList<>();
/** /**
* Conversation refresh runnable * Conversation refresh runnable
@ -64,6 +66,11 @@ public class ConversationFragment extends Fragment
*/ */
private ConversationMessagesHelper convMessHelper; private ConversationMessagesHelper convMessHelper;
/**
* Conversation messages adapter
*/
private ConversationMessageAdapter convMessAdapter;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -93,6 +100,13 @@ public class ConversationFragment extends Fragment
@Override @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
//Conversation messages listView
ListView convMessListView = view.findViewById(R.id.fragment_conversation_messageslist);
convMessAdapter = new ConversationMessageAdapter(getActivity(), messagesList);
convMessListView.setAdapter(convMessAdapter);
} }
@Override @Override
@ -119,8 +133,16 @@ public class ConversationFragment extends Fragment
} }
@Override @Override
public void onAddMessage(@NonNull ArrayList<ConversationMessage> messages) { public void onAddMessage(int lastID, @NonNull ArrayList<ConversationMessage> newMessages) {
//Add the messages to the the main list of messages
for(ConversationMessage message : newMessages){
messagesList.add(message);
}
convMessAdapter.notifyDataSetChanged();
last_message_id = lastID;
} }
@Override @Override

View File

@ -3,9 +3,10 @@
android:orientation="vertical" android:layout_width="match_parent" android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<TextView <ListView
android:layout_width="wrap_content" android:id="@+id/fragment_conversation_messageslist"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:text="hello conversation"/> android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout> </LinearLayout>