From b45c7d1fd97f2d1c00195b9ac510dd267a788069 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 23 Dec 2017 12:12:58 +0100 Subject: [PATCH] First message sent from the Android application --- .../ConversationMessagesHelper.java | 24 ++++++ .../fragments/ConversationFragment.java | 79 ++++++++++++++++++- .../main/res/layout/fragment_conversation.xml | 23 ++++++ app/src/main/res/values/colors.xml | 3 + app/src/main/res/values/strings.xml | 4 + 5 files changed, 132 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessagesHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessagesHelper.java index 5b660ac..899b50c 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessagesHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationMessagesHelper.java @@ -78,6 +78,30 @@ public class ConversationMessagesHelper { return true; } + /** + * Send a new message to the conversation + * + * @param convID Target conversation ID + * @param message The message to send + * @return true in case of success / false else + */ + public boolean sendMessage(int convID, String message){ + + //Make an API request + APIRequestParameters params = new APIRequestParameters(mContext, + "conversations/sendMessage"); + params.addParameter("conversationID", ""+convID); + params.addParameter("message", message); + + try { + new APIRequest().exec(params); + return true; + } catch (Exception e){ + e.printStackTrace(); + return false; + } + } + /** * Fetch messages from the database * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java index 8b44037..a454b2c 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java @@ -9,6 +9,8 @@ import android.util.ArrayMap; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.EditText; +import android.widget.ImageButton; import android.widget.ListView; import android.widget.Toast; @@ -22,7 +24,6 @@ import org.communiquons.android.comunic.client.data.conversations.ConversationMe import org.communiquons.android.comunic.client.data.conversations.ConversationMessagesHelper; import org.communiquons.android.comunic.client.data.conversations.ConversationRefreshRunnable; -import java.lang.reflect.Array; import java.util.ArrayList; /** @@ -87,6 +88,16 @@ public class ConversationFragment extends Fragment */ private ConversationMessageAdapter convMessAdapter; + /** + * Conversation new message content + */ + private EditText new_message_content; + + /** + * Conversation new message send button + */ + private ImageButton send_button; + /** * Get user helper */ @@ -138,6 +149,16 @@ public class ConversationFragment extends Fragment //Apply adapter convMessListView.setAdapter(convMessAdapter); + //Get new messages input fields + new_message_content = view.findViewById(R.id.fragment_conversation_newmessage_content); + send_button = view.findViewById(R.id.fragment_conversation_newmessage_send); + + send_button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + send_message(); + } + }); } @Override @@ -226,4 +247,60 @@ public class ConversationFragment extends Fragment //Inform about dataset update convMessAdapter.notifyDataSetChanged(); } + + /** + * This method is called when the user click on the "send_message" button + */ + private void send_message(){ + + //Check message length + if(new_message_content.length() < 3){ + Toast.makeText(getActivity(), R.string.conversation_message_err_too_short, + Toast.LENGTH_SHORT).show(); + return; + } + + //Hide the send button + send_button.setVisibility(View.INVISIBLE); + + //Get the message content + final String message_content = new_message_content.getText()+""; + + //Send the message + new AsyncTask(){ + + @Override + protected Boolean doInBackground(Void... params) { + return convMessHelper.sendMessage(conversation_id, message_content); + } + + @Override + protected void onPostExecute(Boolean success) { + send_callback(success); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + + /** + * This method is called in response to a post message request + * + * @param success Specify wether the message was successfully posted or not + */ + private void send_callback(boolean success){ + + //Check for error + if(!success){ + Toast.makeText(getActivity(), R.string.conversation_message_err_send, + Toast.LENGTH_SHORT).show(); + } + + //Remove previous message in case of succes + if(success){ + new_message_content.setText(""); + } + + //Make the "send" button available again + send_button.setVisibility(View.VISIBLE); + + } } diff --git a/app/src/main/res/layout/fragment_conversation.xml b/app/src/main/res/layout/fragment_conversation.xml index 259bb99..c364774 100644 --- a/app/src/main/res/layout/fragment_conversation.xml +++ b/app/src/main/res/layout/fragment_conversation.xml @@ -3,6 +3,7 @@ android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index b1d7801..41a0a76 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -14,4 +14,7 @@ #FFFFFF #D8D8D8 #000000 + + + #8c9eff diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 15adaf2..d5f097c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -53,4 +53,8 @@ Couldn\'t get user information ! Could not load messages! Conversation message image + Send + The length of the message is too short ! + The message could not be sent! Please check your message and try again… + New message…