First message sent from the Android application

This commit is contained in:
Pierre 2017-12-23 12:12:58 +01:00
parent 7f3a6eca4d
commit b45c7d1fd9
5 changed files with 132 additions and 1 deletions

View File

@ -78,6 +78,30 @@ public class ConversationMessagesHelper {
return true; 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 * Fetch messages from the database
* *

View File

@ -9,6 +9,8 @@ import android.util.ArrayMap;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast; 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.ConversationMessagesHelper;
import org.communiquons.android.comunic.client.data.conversations.ConversationRefreshRunnable; import org.communiquons.android.comunic.client.data.conversations.ConversationRefreshRunnable;
import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
@ -87,6 +88,16 @@ public class ConversationFragment extends Fragment
*/ */
private ConversationMessageAdapter convMessAdapter; private ConversationMessageAdapter convMessAdapter;
/**
* Conversation new message content
*/
private EditText new_message_content;
/**
* Conversation new message send button
*/
private ImageButton send_button;
/** /**
* Get user helper * Get user helper
*/ */
@ -138,6 +149,16 @@ public class ConversationFragment extends Fragment
//Apply adapter //Apply adapter
convMessListView.setAdapter(convMessAdapter); 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 @Override
@ -226,4 +247,60 @@ public class ConversationFragment extends Fragment
//Inform about dataset update //Inform about dataset update
convMessAdapter.notifyDataSetChanged(); 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<Void, Void, Boolean>(){
@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);
}
} }

View File

@ -3,6 +3,7 @@
android:orientation="vertical" android:layout_width="match_parent" android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<!-- Messages -->
<ListView <ListView
android:id="@+id/fragment_conversation_messageslist" android:id="@+id/fragment_conversation_messageslist"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -13,4 +14,26 @@
android:transcriptMode="alwaysScroll" android:transcriptMode="alwaysScroll"
/> />
<!-- Send messages form -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/conversation_footer_bg">
<EditText
android:id="@+id/fragment_conversation_newmessage_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="@string/fragment_conversation_new_message_placeholder"/>
<ImageButton
android:id="@+id/fragment_conversation_newmessage_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_send"
android:contentDescription="@string/conversation_message_send"/>
</LinearLayout>
</LinearLayout> </LinearLayout>

View File

@ -14,4 +14,7 @@
<color name="conversation_user_messages_textColor">#FFFFFF</color> <color name="conversation_user_messages_textColor">#FFFFFF</color>
<color name="conversation_otheruser_messages_background">#D8D8D8</color> <color name="conversation_otheruser_messages_background">#D8D8D8</color>
<color name="conversation_otheruser_messages_textColor">#000000</color> <color name="conversation_otheruser_messages_textColor">#000000</color>
<!-- Conversations footer -->
<color name="conversation_footer_bg">#8c9eff</color>
</resources> </resources>

View File

@ -53,4 +53,8 @@
<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> <string name="fragment_conversation_err_load_message">Could not load messages!</string>
<string name="fragment_conversation_message_image">Conversation message image</string> <string name="fragment_conversation_message_image">Conversation message image</string>
<string name="conversation_message_send">Send</string>
<string name="conversation_message_err_too_short">The length of the message is too short !</string>
<string name="conversation_message_err_send">The message could not be sent! Please check your message and try again…</string>
<string name="fragment_conversation_new_message_placeholder">New message…</string>
</resources> </resources>