mirror of
				https://github.com/pierre42100/ComunicAndroid
				synced 2025-10-31 01:24:43 +00:00 
			
		
		
		
	Made messages callback live
This commit is contained in:
		| @@ -53,9 +53,9 @@ public class ConversationMessagesHelper { | ||||
|      * Get the latest messages of a conversation | ||||
|      * | ||||
|      * @param conversation_id The ID of the conversation to refresh | ||||
|      * @return The ID of the last message available in the database | ||||
|      * @return TRUE in case of success / FALSE else | ||||
|      */ | ||||
|     int refresh_conversation(int conversation_id){ | ||||
|     boolean refresh_conversation(int conversation_id){ | ||||
|  | ||||
|         //Get the ID of the last message available in the database | ||||
|         int last_message_id = getLastIDFromDb(conversation_id); | ||||
| @@ -66,7 +66,7 @@ public class ConversationMessagesHelper { | ||||
|         //Check for errors | ||||
|         if(new_messages == null){ | ||||
|             //An error occurred | ||||
|             return -1; | ||||
|             return false; | ||||
|         } | ||||
|  | ||||
|         //Add the new messages to the database (if any) | ||||
| @@ -75,7 +75,7 @@ public class ConversationMessagesHelper { | ||||
|         } | ||||
|  | ||||
|         //Get the last message ID from database again | ||||
|         return getLastIDFromDb(conversation_id); | ||||
|         return true; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -99,7 +99,7 @@ public class ConversationMessagesHelper { | ||||
|      * @param conversation_id Target conversation | ||||
|      * @return The ID of the last message available in the database or 0 in case of failure | ||||
|      */ | ||||
|     private int getLastIDFromDb(int conversation_id){ | ||||
|     int getLastIDFromDb(int conversation_id){ | ||||
|  | ||||
|         //Get the id of the last message available in the database | ||||
|         ConversationMessage last_message = mDbHelper.getLast(conversation_id); | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| package org.communiquons.android.comunic.client.data.conversations; | ||||
|  | ||||
| import android.content.Context; | ||||
| import android.app.Activity; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.util.Log; | ||||
|  | ||||
| @@ -35,6 +35,16 @@ public class ConversationRefreshRunnable implements Runnable { | ||||
|      */ | ||||
|     private ConversationMessagesHelper convMessHelper; | ||||
|  | ||||
|     /** | ||||
|      * The activity executing the application | ||||
|      */ | ||||
|     private Activity mActivity; | ||||
|  | ||||
|     /** | ||||
|      * Messages change listener | ||||
|      */ | ||||
|     private onMessagesChangeListener listener; | ||||
|  | ||||
|     /** | ||||
|      * Set to true to make the thread exit | ||||
|      */ | ||||
| @@ -53,12 +63,19 @@ public class ConversationRefreshRunnable implements Runnable { | ||||
|      *                        for no message) | ||||
|      * @param conversationMessagesHelper Conversation message helper to get access to the database | ||||
|      *                                   and to be able to query the API through helper | ||||
|      * @param activity The activity of the application | ||||
|      * @param listener The listener to get the new messages | ||||
|      */ | ||||
|     public ConversationRefreshRunnable(int conversation_id, int last_message_id, | ||||
|                                        @NonNull ConversationMessagesHelper conversationMessagesHelper){ | ||||
|     public ConversationRefreshRunnable(int conversation_id, | ||||
|                                        int last_message_id, | ||||
|                                        @NonNull ConversationMessagesHelper conversationMessagesHelper, | ||||
|                                        Activity activity, | ||||
|                                        onMessagesChangeListener listener){ | ||||
|         this.conversation_id = conversation_id; | ||||
|         this.last_message_id = last_message_id; | ||||
|         this.convMessHelper = conversationMessagesHelper; | ||||
|         this.mActivity = activity; | ||||
|         this.listener = listener; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -89,7 +106,7 @@ public class ConversationRefreshRunnable implements Runnable { | ||||
|          * This method is called when an error occur on a request on the database and / or on the | ||||
|          * remote server | ||||
|          */ | ||||
|         void onError(); | ||||
|         void onLoadError(); | ||||
|  | ||||
|     } | ||||
|  | ||||
| @@ -102,15 +119,29 @@ public class ConversationRefreshRunnable implements Runnable { | ||||
|             //Loop that execute indefinitely until the fragment is stopped | ||||
|             while (!quit) { | ||||
|  | ||||
|                 //Refresh the list of message from the server - the function return the ID of the last | ||||
|                 // message available | ||||
|                 int lastMessageInDb = convMessHelper.refresh_conversation(conversation_id); | ||||
|                 //Refresh the list of messages from the server | ||||
|                 if(!convMessHelper.refresh_conversation(conversation_id)){ | ||||
|                     //Callback : an error occurred | ||||
|                     Log.e(TAG, "Couldn't get the list of new messages !"); | ||||
|  | ||||
|                 //If the last message in the database is newer than the last message of the caller | ||||
|                     mActivity.runOnUiThread(new Runnable() { | ||||
|                         @Override | ||||
|                         public void run() { | ||||
|                             listener.onLoadError(); | ||||
|                         } | ||||
|                     }); | ||||
|  | ||||
|                     continue; | ||||
|                 } | ||||
|  | ||||
|                 //Get the ID of the last message available in the local database | ||||
|                 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) { | ||||
|  | ||||
|                     //Fetch all the messages available in the database since the last request | ||||
|                     ArrayList<ConversationMessage> newMessages = convMessHelper.getInDb( | ||||
|                     final ArrayList<ConversationMessage> newMessages = convMessHelper.getInDb( | ||||
|                             conversation_id, | ||||
|                             last_message_id + 1, | ||||
|                             lastMessageInDb | ||||
| @@ -122,11 +153,24 @@ public class ConversationRefreshRunnable implements Runnable { | ||||
|                         //Callback : an error occurred. | ||||
|                         Log.e(TAG, "Couldn't get the list of new messages from local database !"); | ||||
|  | ||||
|                         mActivity.runOnUiThread(new Runnable() { | ||||
|                             @Override | ||||
|                             public void run() { | ||||
|                                 listener.onLoadError(); | ||||
|                             } | ||||
|                         }); | ||||
|                     } | ||||
|                     else { | ||||
|  | ||||
|                         //Use the callback to send the messages to the UI thread | ||||
|                         for(ConversationMessage message: newMessages) | ||||
|                             Log.v(TAG, "Message: " + message.getContent()); | ||||
|                         mActivity.runOnUiThread(new Runnable() { | ||||
|                             @Override | ||||
|                             public void run() { | ||||
|                                 listener.onAddMessage(newMessages); | ||||
|                             } | ||||
|  | ||||
|  | ||||
|                         }); | ||||
|  | ||||
|                         //Update the ID of the last message fetched | ||||
|                         last_message_id = lastMessageInDb; | ||||
| @@ -134,11 +178,14 @@ public class ConversationRefreshRunnable implements Runnable { | ||||
|  | ||||
|                 } | ||||
|  | ||||
|                 if(lastMessageInDb == -1){ | ||||
|  | ||||
|                     //Callback : an error occurred | ||||
|                     Log.e(TAG, "Couldn't get the list of new messages !"); | ||||
|  | ||||
|                 //Check if there isn't any message | ||||
|                 if(last_message_id == 0){ | ||||
|                     mActivity.runOnUiThread(new Runnable() { | ||||
|                         @Override | ||||
|                         public void run() { | ||||
|                             listener.onNoMessage(); | ||||
|                         } | ||||
|                     }); | ||||
|                 } | ||||
|  | ||||
|                 //Make a small break | ||||
|   | ||||
| @@ -2,6 +2,7 @@ package org.communiquons.android.comunic.client.fragments; | ||||
|  | ||||
| import android.app.Fragment; | ||||
| import android.os.Bundle; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.support.annotation.Nullable; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| @@ -25,7 +26,8 @@ import java.util.ArrayList; | ||||
|  * Created by pierre on 12/16/17. | ||||
|  */ | ||||
|  | ||||
| public class ConversationFragment extends Fragment { | ||||
| public class ConversationFragment extends Fragment | ||||
|         implements ConversationRefreshRunnable.onMessagesChangeListener { | ||||
|  | ||||
|     /** | ||||
|      * Debug tag | ||||
| @@ -98,7 +100,7 @@ public class ConversationFragment extends Fragment { | ||||
|         super.onResume(); | ||||
|  | ||||
|         refreshRunnable = new ConversationRefreshRunnable(conversation_id, last_message_id, | ||||
|                 convMessHelper); | ||||
|                 convMessHelper, getActivity(), this); | ||||
|  | ||||
|         //Create and start the thread | ||||
|         new Thread(refreshRunnable).start(); | ||||
| @@ -110,4 +112,21 @@ public class ConversationFragment extends Fragment { | ||||
|  | ||||
|         refreshRunnable.quitSafely(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onNoMessage() { | ||||
|  | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onAddMessage(@NonNull ArrayList<ConversationMessage> messages) { | ||||
|  | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onLoadError() { | ||||
|         //Display a toast | ||||
|         Toast.makeText(getActivity(), R.string.fragment_conversation_err_load_message, | ||||
|                 Toast.LENGTH_SHORT).show(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -51,4 +51,5 @@ | ||||
|     <string name="date_s">s</string> | ||||
|     <string name="date_hours">date_hours</string> | ||||
|     <string name="err_get_user_info">Couldn\'t get user information !</string> | ||||
|     <string name="fragment_conversation_err_load_message">Could not load messages!</string> | ||||
| </resources> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Pierre
					Pierre