mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 13:59:29 +00:00
Use messages cache before getting messages online
This commit is contained in:
parent
882a949169
commit
4de0b62117
@ -6,6 +6,7 @@ import android.util.Log;
|
|||||||
|
|
||||||
import org.communiquons.android.comunic.client.data.helpers.ConversationMessagesHelper;
|
import org.communiquons.android.comunic.client.data.helpers.ConversationMessagesHelper;
|
||||||
import org.communiquons.android.comunic.client.data.models.ConversationMessage;
|
import org.communiquons.android.comunic.client.data.models.ConversationMessage;
|
||||||
|
import org.communiquons.android.comunic.client.ui.listeners.OnMessagesChangeListener;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@ -21,7 +22,7 @@ public class ConversationRefreshRunnable implements Runnable {
|
|||||||
/**
|
/**
|
||||||
* Debug tag
|
* Debug tag
|
||||||
*/
|
*/
|
||||||
private String TAG = "ConversationRefreshRunn";
|
private static final String TAG = ConversationRefreshRunnable.class.getSimpleName();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ID of the conversation
|
* The ID of the conversation
|
||||||
@ -36,7 +37,7 @@ public class ConversationRefreshRunnable implements Runnable {
|
|||||||
/**
|
/**
|
||||||
* Conversation message helper
|
* Conversation message helper
|
||||||
*/
|
*/
|
||||||
private ConversationMessagesHelper convMessHelper;
|
private ConversationMessagesHelper conversationMessagesHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The activity executing the application
|
* The activity executing the application
|
||||||
@ -46,7 +47,7 @@ public class ConversationRefreshRunnable implements Runnable {
|
|||||||
/**
|
/**
|
||||||
* Messages change listener
|
* Messages change listener
|
||||||
*/
|
*/
|
||||||
private onMessagesChangeListener listener;
|
private OnMessagesChangeListener listener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set to true to make the thread exit
|
* Set to true to make the thread exit
|
||||||
@ -73,47 +74,14 @@ public class ConversationRefreshRunnable implements Runnable {
|
|||||||
int last_message_id,
|
int last_message_id,
|
||||||
@NonNull ConversationMessagesHelper conversationMessagesHelper,
|
@NonNull ConversationMessagesHelper conversationMessagesHelper,
|
||||||
Activity activity,
|
Activity activity,
|
||||||
onMessagesChangeListener listener){
|
OnMessagesChangeListener listener){
|
||||||
this.conversation_id = conversation_id;
|
this.conversation_id = conversation_id;
|
||||||
this.last_message_id = last_message_id;
|
this.last_message_id = last_message_id;
|
||||||
this.convMessHelper = conversationMessagesHelper;
|
this.conversationMessagesHelper = conversationMessagesHelper;
|
||||||
this.mActivity = activity;
|
this.mActivity = activity;
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* onMessagesChangeListener
|
|
||||||
*
|
|
||||||
* This interface is used to perform callback actions on the UI Thread to add messages
|
|
||||||
* to a list for example
|
|
||||||
*
|
|
||||||
* This method also changes in the conversations
|
|
||||||
*/
|
|
||||||
public interface onMessagesChangeListener {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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(int lastID, @NonNull ArrayList<ConversationMessage> messages);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called when there is not any message in the conversation
|
|
||||||
*
|
|
||||||
* Warning ! This method may be called several time
|
|
||||||
*/
|
|
||||||
void onNoMessage();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called when an error occur on a request on the database and / or on the
|
|
||||||
* remote server
|
|
||||||
*/
|
|
||||||
void onLoadError();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
//Log action
|
//Log action
|
||||||
@ -121,77 +89,25 @@ public class ConversationRefreshRunnable implements Runnable {
|
|||||||
|
|
||||||
synchronized (object) {
|
synchronized (object) {
|
||||||
|
|
||||||
|
//Get first messages in the database
|
||||||
|
checkForMessagesInDatabase();
|
||||||
|
|
||||||
//Loop that execute indefinitely until the fragment is stopped
|
//Loop that execute indefinitely until the fragment is stopped
|
||||||
while (!quit) {
|
while (!quit) {
|
||||||
|
|
||||||
//Refresh the list of messages from the server
|
//Refresh the list of messages from the server
|
||||||
if(!convMessHelper.refresh_conversation(conversation_id)){
|
if(!conversationMessagesHelper.refresh_conversation(conversation_id)){
|
||||||
//Callback : an error occurred
|
//Callback : an error occurred
|
||||||
Log.e(TAG, "Couldn't get the list of new messages !");
|
Log.e(TAG, "Couldn't get the list of new messages !");
|
||||||
|
|
||||||
mActivity.runOnUiThread(new Runnable() {
|
mActivity.runOnUiThread(() -> listener.onLoadError());
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
listener.onLoadError();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Get the ID of the last message available in the local database
|
else
|
||||||
final int lastMessageInDb = convMessHelper.getLastIDFromDb(conversation_id);
|
mActivity.runOnUiThread(() -> listener.onGotMessageFromServer());
|
||||||
|
|
||||||
//If the last message in the database is newer than the last message already read
|
//Checkout the database to send any available new message to the client
|
||||||
if (lastMessageInDb > last_message_id) {
|
checkForMessagesInDatabase();
|
||||||
|
|
||||||
//Fetch all the messages available in the database since the last request
|
|
||||||
final ArrayList<ConversationMessage> newMessages = convMessHelper.getInDb(
|
|
||||||
conversation_id,
|
|
||||||
last_message_id + 1,
|
|
||||||
lastMessageInDb
|
|
||||||
);
|
|
||||||
|
|
||||||
//Check for errors
|
|
||||||
if(newMessages == null){
|
|
||||||
|
|
||||||
//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
|
|
||||||
mActivity.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
listener.onAddMessage(lastMessageInDb, newMessages);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
//Update the ID of the last message fetched
|
|
||||||
last_message_id = lastMessageInDb;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//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 (1 sec 200)
|
//Make a small break (1 sec 200)
|
||||||
try {
|
try {
|
||||||
@ -206,6 +122,51 @@ public class ConversationRefreshRunnable implements Runnable {
|
|||||||
Log.v(TAG, "Stopped conversation refresh runnable.");
|
Log.v(TAG, "Stopped conversation refresh runnable.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the database contains messages that have not already been sent to the client
|
||||||
|
*/
|
||||||
|
private void checkForMessagesInDatabase(){
|
||||||
|
|
||||||
|
//Get the ID of the last message available in the local database
|
||||||
|
final int lastMessageInDb = conversationMessagesHelper.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
|
||||||
|
final ArrayList<ConversationMessage> newMessages = conversationMessagesHelper.getInDb(
|
||||||
|
conversation_id,
|
||||||
|
last_message_id + 1,
|
||||||
|
lastMessageInDb
|
||||||
|
);
|
||||||
|
|
||||||
|
//Check for errors
|
||||||
|
if(newMessages == null){
|
||||||
|
|
||||||
|
//Callback : an error occurred.
|
||||||
|
Log.e(TAG, "Couldn't get the list of new messages from local database !");
|
||||||
|
|
||||||
|
mActivity.runOnUiThread(() -> listener.onLoadError());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
//Use the callback to send the messages to the UI thread
|
||||||
|
mActivity.runOnUiThread(() ->
|
||||||
|
listener.onAddMessages(lastMessageInDb, newMessages));
|
||||||
|
|
||||||
|
//Update the ID of the last message fetched
|
||||||
|
last_message_id = lastMessageInDb;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//Check if there isn't any message
|
||||||
|
if(last_message_id == 0){
|
||||||
|
mActivity.runOnUiThread(() -> listener.onNoMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make the thread quit safely (does not interrupt currently running operation)
|
* Make the thread quit safely (does not interrupt currently running operation)
|
||||||
*/
|
*/
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package org.communiquons.android.comunic.client.ui.fragments;
|
package org.communiquons.android.comunic.client.ui.fragments;
|
||||||
|
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.content.DialogInterface;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
@ -29,7 +28,6 @@ import org.communiquons.android.comunic.client.data.arrays.ConversationMessagesL
|
|||||||
import org.communiquons.android.comunic.client.data.helpers.CallsHelper;
|
import org.communiquons.android.comunic.client.data.helpers.CallsHelper;
|
||||||
import org.communiquons.android.comunic.client.data.helpers.ConversationMessagesHelper;
|
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.ConversationsListHelper;
|
||||||
import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper;
|
|
||||||
import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper;
|
import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper;
|
||||||
import org.communiquons.android.comunic.client.data.models.ConversationMessage;
|
import org.communiquons.android.comunic.client.data.models.ConversationMessage;
|
||||||
import org.communiquons.android.comunic.client.data.models.ConversationInfo;
|
import org.communiquons.android.comunic.client.data.models.ConversationInfo;
|
||||||
@ -40,10 +38,10 @@ import org.communiquons.android.comunic.client.data.utils.AccountUtils;
|
|||||||
import org.communiquons.android.comunic.client.ui.activities.MainActivity;
|
import org.communiquons.android.comunic.client.ui.activities.MainActivity;
|
||||||
import org.communiquons.android.comunic.client.ui.adapters.ConversationMessageAdapter;
|
import org.communiquons.android.comunic.client.ui.adapters.ConversationMessageAdapter;
|
||||||
import org.communiquons.android.comunic.client.ui.asynctasks.DeleteConversationMessageTask;
|
import org.communiquons.android.comunic.client.ui.asynctasks.DeleteConversationMessageTask;
|
||||||
import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask;
|
|
||||||
import org.communiquons.android.comunic.client.ui.asynctasks.SendConversationMessageTask;
|
import org.communiquons.android.comunic.client.ui.asynctasks.SendConversationMessageTask;
|
||||||
import org.communiquons.android.comunic.client.ui.asynctasks.UpdateConversationMessageContentTask;
|
import org.communiquons.android.comunic.client.ui.asynctasks.UpdateConversationMessageContentTask;
|
||||||
import org.communiquons.android.comunic.client.ui.listeners.OnConversationMessageActionsListener;
|
import org.communiquons.android.comunic.client.ui.listeners.OnConversationMessageActionsListener;
|
||||||
|
import org.communiquons.android.comunic.client.ui.listeners.OnMessagesChangeListener;
|
||||||
import org.communiquons.android.comunic.client.ui.listeners.OnOpenCallListener;
|
import org.communiquons.android.comunic.client.ui.listeners.OnOpenCallListener;
|
||||||
import org.communiquons.android.comunic.client.ui.listeners.OnScrollChangeDetectListener;
|
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.BitmapUtils;
|
||||||
@ -68,7 +66,7 @@ import static org.communiquons.android.comunic.client.ui.Constants.IntentRequest
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public class ConversationFragment extends Fragment
|
public class ConversationFragment extends Fragment
|
||||||
implements ConversationRefreshRunnable.onMessagesChangeListener,
|
implements OnMessagesChangeListener,
|
||||||
OnScrollChangeDetectListener, OnConversationMessageActionsListener,
|
OnScrollChangeDetectListener, OnConversationMessageActionsListener,
|
||||||
PopupMenu.OnMenuItemClickListener {
|
PopupMenu.OnMenuItemClickListener {
|
||||||
|
|
||||||
@ -387,19 +385,15 @@ public class ConversationFragment extends Fragment
|
|||||||
@Override
|
@Override
|
||||||
public void onNoMessage() {
|
public void onNoMessage() {
|
||||||
|
|
||||||
//Hide main progress bar
|
|
||||||
display_main_progress_bar(false);
|
|
||||||
|
|
||||||
//Display no message notice
|
//Display no message notice
|
||||||
display_not_msg_notice(true);
|
display_not_msg_notice(true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAddMessage(int lastID, @NonNull ArrayList<ConversationMessage> newMessages) {
|
public void onAddMessages(int lastID, @NonNull ArrayList<ConversationMessage> newMessages) {
|
||||||
|
|
||||||
//Remove main progress bar and no message notice
|
//Remove main progress bar and no message notice
|
||||||
display_main_progress_bar(false);
|
|
||||||
display_not_msg_notice(false);
|
display_not_msg_notice(false);
|
||||||
|
|
||||||
//Add the messages to the the main list of messages
|
//Add the messages to the the main list of messages
|
||||||
@ -426,6 +420,11 @@ public class ConversationFragment extends Fragment
|
|||||||
Toast.LENGTH_SHORT).show();
|
Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onGotMessageFromServer() {
|
||||||
|
display_main_progress_bar(false);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make sure we have got the information about all the users of the conversation
|
* Make sure we have got the information about all the users of the conversation
|
||||||
*/
|
*/
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
package org.communiquons.android.comunic.client.ui.listeners;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import org.communiquons.android.comunic.client.data.models.ConversationMessage;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OnMessagesChangeListener
|
||||||
|
*
|
||||||
|
* This interface is used to perform callback actions on the UI Thread to add messages
|
||||||
|
* to a list for example
|
||||||
|
*
|
||||||
|
* This method also changes in the conversations
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
*/
|
||||||
|
public interface OnMessagesChangeListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add new messages to a previous list of messages
|
||||||
|
*
|
||||||
|
* @param lastID The ID of the latest message downloaded from server
|
||||||
|
* @param messages The new messages
|
||||||
|
*/
|
||||||
|
void onAddMessages(int lastID, @NonNull ArrayList<ConversationMessage> messages);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called when there is not any message in the conversation
|
||||||
|
*
|
||||||
|
* Warning ! This method may be called several time
|
||||||
|
*/
|
||||||
|
void onNoMessage();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called when an error occur on a request on the database and / or on the
|
||||||
|
* remote server
|
||||||
|
*/
|
||||||
|
void onLoadError();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method get called once we successfully retrieved new messages from server
|
||||||
|
*/
|
||||||
|
void onGotMessageFromServer();
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user