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