diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/ConversationMessagesHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/ConversationMessagesHelper.java index af6d1a6..65b4486 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/ConversationMessagesHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/ConversationMessagesHelper.java @@ -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.APIResponse; 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.JSONException; import org.json.JSONObject; @@ -92,31 +93,29 @@ public class ConversationMessagesHelper { /** * Send a new message to the conversation * - * @param convID Target conversation ID - * @param message The message to send - * @param image Image to include with the request, as bitmap (can be null) + * @param message Information about the message to send * @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 APIFileRequest params = new APIFileRequest(mContext, "conversations/sendMessage"); - params.addString("conversationID", ""+convID); - params.addString("message", message); + params.addInt("conversationID", message.getConversationID()); + params.addString("message", message.getMessage()); //Include image (if any) - if(image != null) { + if(message.hasImage()) { APIPostFile file = new APIPostFile(); file.setFieldName("image"); file.setFileName("conversationImage.png"); - file.setBitmap(image); + file.setBitmap(message.getImage()); params.addFile(file); } try { - if(image != null){ + if(message.hasImage()){ //Perform a POST request new APIRequestHelper().execPostFile(params); } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/NewConversationMessage.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/NewConversationMessage.java new file mode 100644 index 0000000..40a4f95 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/NewConversationMessage.java @@ -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; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/SendConversationMessageTask.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/SendConversationMessageTask.java new file mode 100644 index 0000000..7051499 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/SendConversationMessageTask.java @@ -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 { + + public SendConversationMessageTask(Context context) { + super(context); + } + + @Override + protected Boolean doInBackground(NewConversationMessage... newConversationMessages) { + return new ConversationMessagesHelper(getContext()).sendMessage(newConversationMessages[0]); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java index 533f808..cc7bf74 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java @@ -26,6 +26,7 @@ import android.widget.Toast; import org.communiquons.android.comunic.client.R; 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.data.helpers.ConversationMessagesHelper; 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.adapters.ConversationMessageAdapter; 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.listeners.OnConversationMessageActionsListener; import org.communiquons.android.comunic.client.ui.listeners.OnScrollChangeDetectListener; @@ -194,6 +196,7 @@ public class ConversationFragment extends Fragment /** * Safe AsyncTasks */ + private SendConversationMessageTask mSendConversationMessageTask; private DeleteConversationMessageTask mDeleteMessageAsyncTask; private UpdateConversationMessageContentTask mUpdateConversationMessageContentTask; @@ -397,6 +400,7 @@ public class ConversationFragment extends Fragment public void onDestroy() { super.onDestroy(); + unsetSendMessageTask(); unsetPendingDeleteTasksCallback(); unsetPendingUpdatedTasksCallbacks(); } @@ -601,7 +605,7 @@ public class ConversationFragment extends Fragment private void send_message(){ //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.LENGTH_SHORT).show(); return; @@ -612,22 +616,31 @@ public class ConversationFragment extends Fragment new_message_progress_bar.setVisibility(View.VISIBLE); //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 - new AsyncTask(){ - + unsetSendMessageTask(); + mSendConversationMessageTask = new SendConversationMessageTask(getActivity()); + mSendConversationMessageTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { @Override - protected Boolean doInBackground(Void... params) { - return convMessHelper.sendMessage(conversation_id, - message_content, new_message_bitmap); - } - - @Override - protected void onPostExecute(Boolean success) { + public void OnPostExecute(Boolean 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); } /**