From 0eed4d1f6edddab85d7e45c4a5762cabf1fc7d59 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 2 Mar 2019 10:10:07 +0100 Subject: [PATCH] Improved a few things --- .../helpers/ConversationMessagesDbHelper.java | 9 +- .../helpers/ConversationMessagesHelper.java | 34 ++--- .../ui/fragments/ConversationFragment.java | 125 +++++------------- 3 files changed, 51 insertions(+), 117 deletions(-) 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 19cb7ff..44b6851 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 @@ -1,6 +1,7 @@ package org.communiquons.android.comunic.client.data.helpers; import android.content.ContentValues; +import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.annotation.NonNull; @@ -23,7 +24,7 @@ class ConversationMessagesDbHelper { /** * Debug tag */ - private static final String TAG = "ConversationMessagesDbH"; + private static final String TAG = ConversationMessagesDbHelper.class.getSimpleName(); /** * Database helper object @@ -50,10 +51,10 @@ class ConversationMessagesDbHelper { /** * Class constructor * - * @param dbHelper Database helper + * @param context Application context */ - ConversationMessagesDbHelper(@NonNull DatabaseHelper dbHelper){ - this.dbHelper = dbHelper; + ConversationMessagesDbHelper(@NonNull Context context){ + this.dbHelper = DatabaseHelper.getInstance(context); } 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 51a9053..6159e03 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 @@ -23,42 +23,28 @@ import java.util.ArrayList; * Created by pierre on 12/16/17. */ -public class ConversationMessagesHelper { +public class ConversationMessagesHelper extends BaseHelper { /** * Debug tag */ - private static final String TAG = "ConversationMessagesHel"; + private static final String TAG = ConversationMessagesHelper.class.getSimpleName(); /** * Conversations messages database helper */ private ConversationMessagesDbHelper mDbHelper; - /** - * Context of execution of the application - */ - private Context mContext; - /** * Constructor of the helper * * @param context The context of the application */ public ConversationMessagesHelper(Context context){ - this(context.getApplicationContext(), DatabaseHelper.getInstance(context)); - } - - /** - * Public constructor of the helper - * - * @param context The context of execution of the application - * @param dbHelper Database helper associated with the context - */ - public ConversationMessagesHelper(Context context, DatabaseHelper dbHelper){ - mContext = context; - mDbHelper = new ConversationMessagesDbHelper(dbHelper); + super(context); + mDbHelper = new ConversationMessagesDbHelper(context); } + /** * Get the latest messages of a conversation @@ -98,7 +84,7 @@ public class ConversationMessagesHelper { public boolean sendMessage(NewConversationMessage message){ //Make an API request - APIFileRequest params = new APIFileRequest(mContext, + APIFileRequest params = new APIFileRequest(getContext(), "conversations/sendMessage"); params.addInt("conversationID", message.getConversationID()); params.addString("message", message.getMessage()); @@ -209,7 +195,7 @@ public class ConversationMessagesHelper { private ArrayList downloadNew(int conversationID, int last_message_id){ //Prepare a request on the API - APIRequest params = new APIRequest(mContext, + APIRequest params = new APIRequest(getContext(), "conversations/refresh_single"); params.addString("conversationID", ""+conversationID); params.addString("last_message_id", ""+last_message_id); @@ -254,7 +240,7 @@ public class ConversationMessagesHelper { ArrayList list = new ArrayList<>(); //Prepare a request over the server - APIRequest req = new APIRequest(mContext, "conversations/get_older_messages"); + APIRequest req = new APIRequest(getContext(), "conversations/get_older_messages"); req.addInt("conversationID", conversationID); req.addInt("oldest_message_id", oldestMessageID); req.addInt("limit", limit); @@ -291,7 +277,7 @@ public class ConversationMessagesHelper { public boolean updateMessage(ConversationMessage message){ //Perform the request over the API - APIRequest request = new APIRequest(mContext, "conversations/updateMessage"); + APIRequest request = new APIRequest(getContext(), "conversations/updateMessage"); request.addInt("messageID", message.getId()); if(message.hasContent()) @@ -317,7 +303,7 @@ public class ConversationMessagesHelper { public boolean deleteMessage(int messageID){ //Make a request on the server - APIRequest request = new APIRequest(mContext, "conversations/deleteMessage"); + APIRequest request = new APIRequest(getContext(), "conversations/deleteMessage"); request.addInt("messageID", messageID); try { 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 8eefb07..9935272 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 @@ -212,7 +212,7 @@ public class ConversationFragment extends Fragment DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity()); //Set conversation message helper - convMessHelper = new ConversationMessagesHelper(getActivity(), dbHelper); + convMessHelper = new ConversationMessagesHelper(getActivity()); //Set conversation list helper convListHelper = new ConversationsListHelper(getActivity(), dbHelper); @@ -270,19 +270,11 @@ public class ConversationFragment extends Fragment userID = AccountUtils.getID(getActivity()); //Initialize toolbar - mAppBar.addBackButton(new View.OnClickListener() { - @Override - public void onClick(View v) { - MainActivity.goBackward(getActivity()); - } - }); + mAppBar.addBackButton(v -> MainActivity.goBackward(Objects.requireNonNull(getActivity()))); - mAppBar.addButton(R.drawable.ic_settings, new View.OnClickListener() { - @Override - public void onClick(View v) { - ((MainActivity)getActivity()).updateConversation(conversation_id); - } - }); + mAppBar.addButton(R.drawable.ic_settings, + v -> ((MainActivity)Objects.requireNonNull(getActivity())) + .updateConversation(conversation_id)); //Create the adapter @@ -304,41 +296,25 @@ public class ConversationFragment extends Fragment new_message_progress_bar = view.findViewById(R.id.fragment_conversation_newmessage_loading); //Make send button lives - send_button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - send_message(); - } - }); + send_button.setOnClickListener(v -> send_message()); //Make message input act like send button on enter key press - new_message_content.setOnKeyListener(new View.OnKeyListener() { - @Override - public boolean onKey(View v, int keyCode, KeyEvent event) { - - if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) { - send_message(); - return false; - } - + new_message_content.setOnKeyListener((v, keyCode, event) -> { + if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) { + send_message(); return false; } + + + return false; }); //Make pick image button lives - pick_image_button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - pick_image(); - } - }); - pick_image_button.setOnLongClickListener(new View.OnLongClickListener() { - @Override - public boolean onLongClick(View v) { - confirm_remove_picked_image(); - return true; - } + pick_image_button.setOnClickListener(v -> pick_image()); + pick_image_button.setOnLongClickListener(v -> { + confirm_remove_picked_image(); + return true; }); //Hide new message sending wheel @@ -360,7 +336,8 @@ public class ConversationFragment extends Fragment new Thread(refreshRunnable).start(); //Update conversation title - setTitle(UiUtils.getString(getActivity(), R.string.fragment_conversation_title)); + setTitle(UiUtils.getString(Objects + .requireNonNull(getActivity()), R.string.fragment_conversation_title)); MainActivity.SetNavbarSelectedOption(getActivity(), R.id.action_conversations); @@ -533,13 +510,9 @@ public class ConversationFragment extends Fragment mHasCallButton = true; - mAppBar.addButton(R.drawable.ic_call, new View.OnClickListener() { - @Override - public void onClick(View v) { - ((OnOpenCallListener)Objects.requireNonNull(getActivity())) - .createCallForConversation(conversation_id); - } - }); + mAppBar.addButton(R.drawable.ic_call, + v -> ((OnOpenCallListener)Objects.requireNonNull(getActivity())) + .createCallForConversation(conversation_id)); } } @@ -563,13 +536,7 @@ public class ConversationFragment extends Fragment .setNegativeButton(R.string.conversation_message_remove_image_popup_cancel, null) - .setPositiveButton(R.string.conversation_message_remove_image_popup_confirm, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - remove_picked_image(); - } - }) + .setPositiveButton(R.string.conversation_message_remove_image_popup_confirm, (dialog, which) -> remove_picked_image()) .show(); } @@ -586,7 +553,8 @@ public class ConversationFragment extends Fragment try { //Get new message bitmap - new_message_bitmap = BitmapUtils.IntentResultToBitmap(getActivity(), data); + new_message_bitmap = BitmapUtils.IntentResultToBitmap( + Objects.requireNonNull(getActivity()), data); //Check for errors if(new_message_bitmap == null){ @@ -617,8 +585,8 @@ public class ConversationFragment extends Fragment //Reset image button pick_image_button.setImageBitmap(null); - pick_image_button.setImageDrawable(UiUtils.getDrawable(getActivity(), - android.R.drawable.ic_menu_gallery)); + pick_image_button.setImageDrawable(UiUtils.getDrawable( + Objects.requireNonNull(getActivity()), android.R.drawable.ic_menu_gallery)); } /** @@ -651,12 +619,7 @@ public class ConversationFragment extends Fragment //Send the message unsetSendMessageTask(); mSendConversationMessageTask = new SendConversationMessageTask(getActivity()); - mSendConversationMessageTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { - @Override - public void OnPostExecute(Boolean success) { - send_callback(success); - } - }); + mSendConversationMessageTask.setOnPostExecuteListener(this::send_callback); mSendConversationMessageTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, newMessage); } @@ -845,13 +808,7 @@ public class ConversationFragment extends Fragment .setMessage(R.string.dialog_delete_conversation_message_message) .setNegativeButton(R.string.dialog_delete_conversation_message_cancel, null) - .setPositiveButton(R.string.dialog_delete_conversation_message_confirm, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - deleteConversationMessage(pos); - } - }) + .setPositiveButton(R.string.dialog_delete_conversation_message_confirm, (dialog, which) -> deleteConversationMessage(pos)) .show(); } @@ -868,12 +825,9 @@ public class ConversationFragment extends Fragment .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(); + .setPositiveButton(R.string.dialog_edit_conversation_message_content_update, + (dialog, which) -> + update_conversation_message(pos, ""+input.getText())).show(); } @@ -888,12 +842,8 @@ public class ConversationFragment extends Fragment unsetPendingDeleteTasksCallback(); mDeleteMessageAsyncTask = new DeleteConversationMessageTask(getActivity()); - mDeleteMessageAsyncTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { - @Override - public void OnPostExecute(Boolean result) { - deleteConversationMessagesCallback(pos, result); - } - }); + mDeleteMessageAsyncTask.setOnPostExecuteListener( + result -> deleteConversationMessagesCallback(pos, result)); mDeleteMessageAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, @@ -950,12 +900,9 @@ public class ConversationFragment extends Fragment message.setContent(content); mUpdateConversationMessageContentTask = new UpdateConversationMessageContentTask(getActivity()); - mUpdateConversationMessageContentTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { - @Override - public void OnPostExecute(Boolean aBoolean) { - if(getActivity() != null) - updateConversationMessageCallback(aBoolean); - } + mUpdateConversationMessageContentTask.setOnPostExecuteListener(success -> { + if(getActivity() != null) + updateConversationMessageCallback(success); }); mUpdateConversationMessageContentTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, message); }