diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessageAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessageAdapter.java new file mode 100644 index 0000000..99ffc7f --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessageAdapter.java @@ -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 { + + /** + * Public class constructor + * + * @param context The context of execution of the application + * @param list The dataset + */ + public ConversationMessageAdapter(Context context, ArrayList 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; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationRefreshRunnable.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationRefreshRunnable.java index 1f036b4..ae5281d 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationRefreshRunnable.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationRefreshRunnable.java @@ -91,9 +91,10 @@ public class ConversationRefreshRunnable implements Runnable { /** * 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 */ - void onAddMessage(@NonNull ArrayList messages); + void onAddMessage(int lastID, @NonNull ArrayList messages); /** * 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."); synchronized (object) { + //Loop that execute indefinitely until the fragment is stopped while (!quit) { @@ -135,7 +137,7 @@ public class ConversationRefreshRunnable implements Runnable { } //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 (lastMessageInDb > last_message_id) { @@ -166,7 +168,7 @@ public class ConversationRefreshRunnable implements Runnable { mActivity.runOnUiThread(new Runnable() { @Override public void run() { - listener.onAddMessage(newMessages); + listener.onAddMessage(lastMessageInDb, newMessages); } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java index 7ca95b6..8d31fc7 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java @@ -7,11 +7,13 @@ import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ListView; import android.widget.Toast; import org.communiquons.android.comunic.client.R; 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.ConversationMessageAdapter; import org.communiquons.android.comunic.client.data.conversations.ConversationMessagesHelper; import org.communiquons.android.comunic.client.data.conversations.ConversationRefreshRunnable; @@ -50,9 +52,9 @@ public class ConversationFragment extends Fragment private int last_message_id = 0; /** - * The list of messages + * The list of messages of the conversation */ - private ArrayList messagesList; + private ArrayList messagesList = new ArrayList<>(); /** * Conversation refresh runnable @@ -64,6 +66,11 @@ public class ConversationFragment extends Fragment */ private ConversationMessagesHelper convMessHelper; + /** + * Conversation messages adapter + */ + private ConversationMessageAdapter convMessAdapter; + @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -93,6 +100,13 @@ public class ConversationFragment extends Fragment @Override public void onViewCreated(View view, @Nullable Bundle 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 @@ -119,8 +133,16 @@ public class ConversationFragment extends Fragment } @Override - public void onAddMessage(@NonNull ArrayList messages) { + public void onAddMessage(int lastID, @NonNull ArrayList 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 diff --git a/app/src/main/res/layout/fragment_conversation.xml b/app/src/main/res/layout/fragment_conversation.xml index 6714e1f..d9dcc30 100644 --- a/app/src/main/res/layout/fragment_conversation.xml +++ b/app/src/main/res/layout/fragment_conversation.xml @@ -3,9 +3,10 @@ android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> - + \ No newline at end of file