From e533770b6da1ddd3cdfeeea9656af62fcd4057a7 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 29 Aug 2018 14:27:08 +0200 Subject: [PATCH] CAn update the content of a conversation message. --- .idea/misc.xml | 2 +- .../helpers/ConversationMessagesDbHelper.java | 16 ++++ .../helpers/ConversationMessagesHelper.java | 26 ++++++ .../data/models/ConversationMessage.java | 7 ++ .../UpdateConversationMessageContentTask.java | 24 +++++ .../ui/fragments/ConversationFragment.java | 93 ++++++++++++++++++- .../OnConversationMessageActionsListener.java | 7 ++ .../dialog_edit_conversation_message.xml | 25 +++++ .../res/menu/menu_conversation_message.xml | 4 + app/src/main/res/values-fr/strings.xml | 8 ++ app/src/main/res/values/strings.xml | 8 ++ 11 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/UpdateConversationMessageContentTask.java create mode 100644 app/src/main/res/layout/dialog_edit_conversation_message.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index c0f68ed..99202cc 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -25,7 +25,7 @@ - + diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/ConversationMessagesDbHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/ConversationMessagesDbHelper.java index cda5895..19cb7ff 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/ConversationMessagesDbHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/ConversationMessagesDbHelper.java @@ -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 * 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 eec5037..af6d1a6 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 @@ -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 * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/ConversationMessage.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/ConversationMessage.java index 225b2ca..d1b0897 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/models/ConversationMessage.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/ConversationMessage.java @@ -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 * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/UpdateConversationMessageContentTask.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/UpdateConversationMessageContentTask.java new file mode 100644 index 0000000..79d7e83 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/UpdateConversationMessageContentTask.java @@ -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 { + + public UpdateConversationMessageContentTask(Context context) { + super(context); + } + + @Override + protected Boolean doInBackground(ConversationMessage... conversationMessages) { + return new ConversationMessagesHelper(getContext()).updateMessage(conversationMessages[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 5e897e1..a6d0534 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 @@ -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() { + @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(); + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnConversationMessageActionsListener.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnConversationMessageActionsListener.java index 2240ac0..2e4da8d 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnConversationMessageActionsListener.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/listeners/OnConversationMessageActionsListener.java @@ -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); + } diff --git a/app/src/main/res/layout/dialog_edit_conversation_message.xml b/app/src/main/res/layout/dialog_edit_conversation_message.xml new file mode 100644 index 0000000..cb7e2b3 --- /dev/null +++ b/app/src/main/res/layout/dialog_edit_conversation_message.xml @@ -0,0 +1,25 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_conversation_message.xml b/app/src/main/res/menu/menu_conversation_message.xml index 7e4a21d..4fe555d 100644 --- a/app/src/main/res/menu/menu_conversation_message.xml +++ b/app/src/main/res/menu/menu_conversation_message.xml @@ -5,4 +5,8 @@ android:id="@+id/action_delete" android:title="@string/action_delete" /> + + \ No newline at end of file diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 45c8dfd..76efe00 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -257,4 +257,12 @@ Une erreur a survenue lors de la suppression du message ! Une erreur a survenu lors de la récupération de la liste des amis de l\'utilisateur ! Ce utilisateur n\'a aucun ami pour le moment. + Mettre à jour le contenu + Contenu du message + Mise à jour du message + Annuler + Mettre à jour + Le contenu du message est invalide ! + Une erreur a survenue lors de la mise à jour du contenu du message ! Veuillez réessayer… + Le contenu du message a bien été mis à jour ! \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 524ba54..3529ebb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -256,4 +256,12 @@ Friends Could not get the friends of the user! This user has not any friend yet. + Update content + Message content + Update message + Cancel + Update + The content of the conversation message is invalid! + Could not update conversation message content! + The content of the conversation message has been successfully updated!