Improved conversation messages sending system

This commit is contained in:
Pierre HUBERT 2018-09-01 09:41:33 +02:00
parent a78594772a
commit 06ce085636
4 changed files with 100 additions and 21 deletions

View File

@ -10,6 +10,7 @@ import org.communiquons.android.comunic.client.data.models.APIPostFile;
import org.communiquons.android.comunic.client.data.models.APIRequest; import org.communiquons.android.comunic.client.data.models.APIRequest;
import org.communiquons.android.comunic.client.data.models.APIResponse; import org.communiquons.android.comunic.client.data.models.APIResponse;
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.NewConversationMessage;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -92,31 +93,29 @@ public class ConversationMessagesHelper {
/** /**
* Send a new message to the conversation * Send a new message to the conversation
* *
* @param convID Target conversation ID * @param message Information about the message to send
* @param message The message to send
* @param image Image to include with the request, as bitmap (can be null)
* @return true in case of success / false else * @return true in case of success / false else
*/ */
public boolean sendMessage(int convID, String message, @Nullable Bitmap image){ public boolean sendMessage(NewConversationMessage message){
//Make an API request //Make an API request
APIFileRequest params = new APIFileRequest(mContext, APIFileRequest params = new APIFileRequest(mContext,
"conversations/sendMessage"); "conversations/sendMessage");
params.addString("conversationID", ""+convID); params.addInt("conversationID", message.getConversationID());
params.addString("message", message); params.addString("message", message.getMessage());
//Include image (if any) //Include image (if any)
if(image != null) { if(message.hasImage()) {
APIPostFile file = new APIPostFile(); APIPostFile file = new APIPostFile();
file.setFieldName("image"); file.setFieldName("image");
file.setFileName("conversationImage.png"); file.setFileName("conversationImage.png");
file.setBitmap(image); file.setBitmap(message.getImage());
params.addFile(file); params.addFile(file);
} }
try { try {
if(image != null){ if(message.hasImage()){
//Perform a POST request //Perform a POST request
new APIRequestHelper().execPostFile(params); new APIRequestHelper().execPostFile(params);
} }

View File

@ -0,0 +1,44 @@
package org.communiquons.android.comunic.client.data.models;
import android.graphics.Bitmap;
/**
* New conversation message information
*
* @author Pierre HUBERT
*/
public class NewConversationMessage {
//Private fields
private int conversationID;
private String message;
private Bitmap image;
public int getConversationID() {
return conversationID;
}
public void setConversationID(int conversationID) {
this.conversationID = conversationID;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Bitmap getImage() {
return image;
}
public boolean hasImage(){
return image != null;
}
public void setImage(Bitmap image) {
this.image = image;
}
}

View File

@ -0,0 +1,23 @@
package org.communiquons.android.comunic.client.ui.asynctasks;
import android.content.Context;
import org.communiquons.android.comunic.client.data.helpers.ConversationMessagesHelper;
import org.communiquons.android.comunic.client.data.models.NewConversationMessage;
/**
* Send conversation message task
*
* @author Pierre HUBERT
*/
public class SendConversationMessageTask extends SafeAsyncTask<NewConversationMessage, Void, Boolean> {
public SendConversationMessageTask(Context context) {
super(context);
}
@Override
protected Boolean doInBackground(NewConversationMessage... newConversationMessages) {
return new ConversationMessagesHelper(getContext()).sendMessage(newConversationMessages[0]);
}
}

View File

@ -26,6 +26,7 @@ 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.arrays.ConversationMessagesList; import org.communiquons.android.comunic.client.data.arrays.ConversationMessagesList;
import org.communiquons.android.comunic.client.data.models.NewConversationMessage;
import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask; import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask;
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;
@ -39,6 +40,7 @@ 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.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.OnScrollChangeDetectListener; import org.communiquons.android.comunic.client.ui.listeners.OnScrollChangeDetectListener;
@ -194,6 +196,7 @@ public class ConversationFragment extends Fragment
/** /**
* Safe AsyncTasks * Safe AsyncTasks
*/ */
private SendConversationMessageTask mSendConversationMessageTask;
private DeleteConversationMessageTask mDeleteMessageAsyncTask; private DeleteConversationMessageTask mDeleteMessageAsyncTask;
private UpdateConversationMessageContentTask mUpdateConversationMessageContentTask; private UpdateConversationMessageContentTask mUpdateConversationMessageContentTask;
@ -397,6 +400,7 @@ public class ConversationFragment extends Fragment
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
unsetSendMessageTask();
unsetPendingDeleteTasksCallback(); unsetPendingDeleteTasksCallback();
unsetPendingUpdatedTasksCallbacks(); unsetPendingUpdatedTasksCallbacks();
} }
@ -601,7 +605,7 @@ public class ConversationFragment extends Fragment
private void send_message(){ private void send_message(){
//Check message length //Check message length
if(new_message_content.length() < 3 && new_message_bitmap == null){ if(new_message_content.length() < 2 && new_message_bitmap == null){
Toast.makeText(getActivity(), R.string.conversation_message_err_too_short, Toast.makeText(getActivity(), R.string.conversation_message_err_too_short,
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
return; return;
@ -612,22 +616,31 @@ public class ConversationFragment extends Fragment
new_message_progress_bar.setVisibility(View.VISIBLE); new_message_progress_bar.setVisibility(View.VISIBLE);
//Get the message content //Get the message content
final String message_content = new_message_content.getText()+""; String message_content = new_message_content.getText()+"";
if(message_content.length() < 3 && new_message_bitmap == null)
message_content += " ";
NewConversationMessage newMessage = new NewConversationMessage();
newMessage.setConversationID(conversation_id);
newMessage.setMessage(message_content);
newMessage.setImage(new_message_bitmap);
//Send the message //Send the message
new AsyncTask<Void, Void, Boolean>(){ unsetSendMessageTask();
mSendConversationMessageTask = new SendConversationMessageTask(getActivity());
mSendConversationMessageTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<Boolean>() {
@Override @Override
protected Boolean doInBackground(Void... params) { public void OnPostExecute(Boolean success) {
return convMessHelper.sendMessage(conversation_id,
message_content, new_message_bitmap);
}
@Override
protected void onPostExecute(Boolean success) {
send_callback(success); send_callback(success);
} }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); });
mSendConversationMessageTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, newMessage);
}
private void unsetSendMessageTask(){
if(mSendConversationMessageTask != null)
mSendConversationMessageTask.setOnPostExecuteListener(null);
} }
/** /**