CAn update the content of a conversation message.

This commit is contained in:
Pierre HUBERT 2018-08-29 14:27:08 +02:00
parent d3f95adb46
commit e533770b6d
11 changed files with 217 additions and 3 deletions

View File

@ -25,7 +25,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -190,6 +190,22 @@ class ConversationMessagesDbHelper {
return list;
}
/**
* Update a conversation message in the database
*
* @param message Information about the message to update
* @return TRUE in case of success / FALSE else
*/
boolean updateMessage(ConversationMessage message){
SQLiteDatabase db = dbHelper.getWritableDatabase();
String conditions = ConversationsMessagesSchema.COLUMN_NAME_MESSAGE_ID + " = ?";
String[] values = new String[]{message.getId()+""};
return db.update(TABLE_NAME, getContentValues(message), conditions, values) > 0;
}
/**
* Delete a message from the local database
*

View File

@ -284,6 +284,32 @@ public class ConversationMessagesHelper {
return list;
}
/**
* Update a conversation message
*
* @param message Information about the message to update
* @return TRUE for a success / FALSE else
*/
public boolean updateMessage(ConversationMessage message){
//Perform the request over the API
APIRequest request = new APIRequest(mContext, "conversations/updateMessage");
request.addInt("messageID", message.getId());
if(message.hasContent())
request.addString("content", message.getContent());
try {
APIResponse response = new APIRequestHelper().exec(request);
return response.getResponse_code() == 200 && mDbHelper.updateMessage(message);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* Delete a conversation message
*

View File

@ -131,6 +131,13 @@ public class ConversationMessage {
this.content = content;
}
/**
* @return TRUE if the content of the message has been specified / FALSE else
*/
public boolean hasContent(){
return this.content != null;
}
/**
* Get the content of the message
*

View File

@ -0,0 +1,24 @@
package org.communiquons.android.comunic.client.ui.asynctasks;
import android.content.Context;
import org.communiquons.android.comunic.client.data.asynctasks.SafeAsyncTask;
import org.communiquons.android.comunic.client.data.helpers.ConversationMessagesHelper;
import org.communiquons.android.comunic.client.data.models.ConversationMessage;
/**
* Update the content of a conversation message
*
* @author Pierre HUBERT
*/
public class UpdateConversationMessageContentTask extends SafeAsyncTask<ConversationMessage, Void, Boolean> {
public UpdateConversationMessageContentTask(Context context) {
super(context);
}
@Override
protected Boolean doInBackground(ConversationMessage... conversationMessages) {
return new ConversationMessagesHelper(getContext()).updateMessage(conversationMessages[0]);
}
}

View File

@ -39,6 +39,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.UpdateConversationMessageContentTask;
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.utils.BitmapUtils;
@ -48,6 +49,8 @@ import org.communiquons.android.comunic.client.ui.views.ScrollRecyclerView;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.zip.Inflater;
import static android.app.Activity.RESULT_OK;
@ -191,9 +194,10 @@ public class ConversationFragment extends Fragment
private int mMessageInContextMenu;
/**
* Safely delete message
* Safe AsyncTasks
*/
private DeleteConversationMessageTask mDeleteMessageAsyncTask;
private UpdateConversationMessageContentTask mUpdateConversationMessageContentTask;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
@ -396,6 +400,7 @@ public class ConversationFragment extends Fragment
super.onDestroy();
unsetPendingDeleteTasksCallback();
unsetPendingUpdatedTasksCallbacks();
}
@Override
@ -773,8 +778,10 @@ public class ConversationFragment extends Fragment
PopupMenu popup = new PopupMenu(getActivity(), v);
popup.inflate(R.menu.menu_conversation_message);
if(message.getUser_id() != userID)
if(message.getUser_id() != userID) {
popup.getMenu().findItem(R.id.action_delete).setEnabled(false);
popup.getMenu().findItem(R.id.action_update_content).setEnabled(false);
}
popup.setOnMenuItemClickListener(this);
@ -789,9 +796,15 @@ public class ConversationFragment extends Fragment
return true;
}
if(item.getItemId() == R.id.action_update_content){
onRequestConversationMessageUpdate(mMessageInContextMenu);
return true;
}
return false;
}
@Override
public void onConfirmDeleteConversationMessage(final int pos) {
new AlertDialog.Builder(getActivity())
@ -810,6 +823,29 @@ public class ConversationFragment extends Fragment
.show();
}
@Override
public void onRequestConversationMessageUpdate(final int pos) {
View view = LayoutInflater.from(getActivity()).inflate(
R.layout.dialog_edit_conversation_message, null);
final EditText input = view.findViewById(R.id.input);
input.setText(messagesList.get(pos).getContent());
new AlertDialog.Builder(getActivity())
.setTitle(R.string.dialog_edit_conversation_message_content_title)
.setView(view)
.setNegativeButton(R.string.dialog_edit_conversation_message_content_cancel, null)
.setPositiveButton(R.string.dialog_edit_conversation_message_content_update, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
update_conversation_message(pos, ""+input.getText());
}
}).show();
}
/**
* Delete conversation message at a specified position
*
@ -861,4 +897,57 @@ public class ConversationFragment extends Fragment
}
/**
* Perform the update of the content of a conversation message
*
* @param pos The position of the message to update
* @param content The new content of the message
*/
private void update_conversation_message(int pos, String content){
if(content.length() < 3){
Toast.makeText(getActivity(), R.string.err_invalid_conversation_message_content,
Toast.LENGTH_SHORT).show();
return;
}
unsetPendingUpdatedTasksCallbacks();
ConversationMessage message = messagesList.get(pos);
message.setContent(content);
mUpdateConversationMessageContentTask = new UpdateConversationMessageContentTask(getActivity());
mUpdateConversationMessageContentTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<Boolean>() {
@Override
public void OnPostExecute(Boolean aBoolean) {
if(getActivity() != null)
updateConversationMessageCallback(aBoolean);
}
});
mUpdateConversationMessageContentTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, message);
}
/**
* Unset any conversation message update pending tasks
*/
private void unsetPendingUpdatedTasksCallbacks(){
if(mUpdateConversationMessageContentTask != null)
mUpdateConversationMessageContentTask.setOnPostExecuteListener(null);
}
/**
* Update Conversation message callback
*
* @param result TRUE in case of result / FALSE else
*/
private void updateConversationMessageCallback(boolean result){
//Check for errors
Toast.makeText(getActivity(),
!result ? R.string.err_update_conversation_message_content :
R.string.success_update_conversation_message_content,
Toast.LENGTH_SHORT).show();
convMessAdapter.notifyDataSetChanged();
}
}

View File

@ -26,4 +26,11 @@ public interface OnConversationMessageActionsListener {
*/
void onConfirmDeleteConversationMessage(int pos);
/**
* Ask the user the new content for the conversation message
*
* @param pos The position of the message to update
*/
void onRequestConversationMessageUpdate(int pos);
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_edit_conversation_message" />
</android.support.design.widget.TextInputLayout>
</android.support.constraint.ConstraintLayout>

View File

@ -5,4 +5,8 @@
android:id="@+id/action_delete"
android:title="@string/action_delete" />
<item
android:id="@+id/action_update_content"
android:title="@string/action_update_content" />
</menu>

View File

@ -257,4 +257,12 @@
<string name="err_delete_conversation_message">Une erreur a survenue lors de la suppression du message !</string>
<string name="err_get_user_friends">Une erreur a survenu lors de la récupération de la liste des amis de l\'utilisateur !</string>
<string name="notice_user_has_no_friend">Ce utilisateur n\'a aucun ami pour le moment.</string>
<string name="action_update_content">Mettre à jour le contenu</string>
<string name="hint_edit_conversation_message">Contenu du message</string>
<string name="dialog_edit_conversation_message_content_title">Mise à jour du message</string>
<string name="dialog_edit_conversation_message_content_cancel">Annuler</string>
<string name="dialog_edit_conversation_message_content_update">Mettre à jour</string>
<string name="err_invalid_conversation_message_content">Le contenu du message est invalide !</string>
<string name="err_update_conversation_message_content">Une erreur a survenue lors de la mise à jour du contenu du message ! Veuillez réessayer…</string>
<string name="success_update_conversation_message_content">Le contenu du message a bien été mis à jour !</string>
</resources>

View File

@ -256,4 +256,12 @@
<string name="tab_friends">Friends</string>
<string name="err_get_user_friends">Could not get the friends of the user!</string>
<string name="notice_user_has_no_friend">This user has not any friend yet.</string>
<string name="action_update_content">Update content</string>
<string name="hint_edit_conversation_message">Message content</string>
<string name="dialog_edit_conversation_message_content_title">Update message</string>
<string name="dialog_edit_conversation_message_content_cancel">Cancel</string>
<string name="dialog_edit_conversation_message_content_update">Update</string>
<string name="err_invalid_conversation_message_content">The content of the conversation message is invalid!</string>
<string name="err_update_conversation_message_content">Could not update conversation message content!</string>
<string name="success_update_conversation_message_content">The content of the conversation message has been successfully updated!</string>
</resources>