From 742cdae6380247368676ff08a9950b0505783c20 Mon Sep 17 00:00:00 2001 From: Pierre Date: Fri, 5 Jan 2018 13:46:37 +0100 Subject: [PATCH] Can update conversation --- .../android/comunic/client/MainActivity.java | 1 + .../client/data/Account/AccountUtils.java | 10 + .../data/conversations/ConversationsInfo.java | 10 +- .../ConversationsListDbHelper.java | 17 ++ .../ConversationsListHelper.java | 58 +++++ .../comunic/client/data/utils/ArrayUtils.java | 32 +++ .../fragments/ConversationsListFragment.java | 5 + .../fragments/UpdateConversationFragment.java | 233 +++++++++++++++--- .../menu_fragment_conversationslist_item.xml | 6 + app/src/main/res/values/strings.xml | 5 + 10 files changed, 341 insertions(+), 36 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/utils/ArrayUtils.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java index 4596252..4b09d3e 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java @@ -289,6 +289,7 @@ public class MainActivity extends AppCompatActivity //Set the arguments of the fragment Bundle args = new Bundle(); + args.putInt(UpdateConversationFragment.ARG_CONVERSATION_ID, convID); //Create the fragment UpdateConversationFragment updateConversationFragment = new UpdateConversationFragment(); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java index 4ed04f5..2607b75 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java @@ -136,4 +136,14 @@ public class AccountUtils { } } + /** + * Get the current user ID quickly + * + * @param context The context of execution of the application + * @return The ID of the current user or -1 in case of failure + */ + public static int getID(Context context){ + return new AccountUtils(context).get_current_user_id(); + } + } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsInfo.java index 16532b6..6a71a85 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsInfo.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsInfo.java @@ -3,6 +3,8 @@ package org.communiquons.android.comunic.client.data.conversations; import android.support.annotation.Nullable; import android.util.Log; +import org.communiquons.android.comunic.client.data.utils.ArrayUtils; + import java.util.ArrayList; /** @@ -203,13 +205,7 @@ public class ConversationsInfo { if(members == null) return ""; - String result = ""; - - for(int member : members){ - result += member + ","; - } - - return result; + return ArrayUtils.int_array_to_string(members, ","); } /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListDbHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListDbHelper.java index 29cf5ab..22bb0ed 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListDbHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListDbHelper.java @@ -110,6 +110,23 @@ public class ConversationsListDbHelper { } + /** + * Delete a single conversation from the database + * + * @param convID The ID of the conversation to delete + */ + void delete(int convID){ + + //Get writeable access to the database + SQLiteDatabase db = databaseHelper.getWritableDatabase(); + + //Perform a request on the database + String condition = ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID + " = ?"; + String[] values = {""+convID}; + db.delete(ConversationsListSchema.TABLE_NAME, condition, values); + + } + /** * Delete all the list of conversations */ diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java index ddf5865..4806149 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java @@ -13,6 +13,7 @@ import org.communiquons.android.comunic.client.data.Account.AccountUtils; import org.communiquons.android.comunic.client.data.DatabaseHelper; import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper; import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; +import org.communiquons.android.comunic.client.data.utils.ArrayUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -220,6 +221,63 @@ public class ConversationsListHelper { } } + /** + * Update a conversation + * + * @param convID The ID of the conversation to update + * @param following Specify wether the user would like to follow or not the conversation + * @return True for a success / False else + */ + public boolean update(int convID, boolean following){ + return update(convID, null, null, following); + } + + /** + * Update a conversation + * + * @param convID The ID of the conversation + * @param name The name of the conversation + * @param members The list of members of the conversation + * @param following True to follow the conversation / false else + * @return True for a success / false for a failure + */ + public boolean update(int convID, @Nullable String name, + @Nullable ArrayList members, boolean following){ + + //Prepare a request on the database + APIRequestParameters params = new APIRequestParameters(mContext, + "conversations/updateSettings"); + params.addParameter("conversationID", ""+convID); + + //Add the name (if any) + if(name != null) + params.addParameter("name", name.equals("") ? "false" : name); + + //Add the members (if any) + if(members != null) + params.addParameter("members", ArrayUtils.int_array_to_string(members, ",")); + + //Add following state + params.addParameter("following", following ? "true" : "false"); + + //Perform the request + try { + + //Try to perform the request + new APIRequest().exec(params); + + //Delete the conversation from the local database to force it to be refreshed + //on next load + convDBHelper.delete(convID); + + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + /** * Get online (download) the list of all the conversations * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/ArrayUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/ArrayUtils.java new file mode 100644 index 0000000..cad1eda --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/ArrayUtils.java @@ -0,0 +1,32 @@ +package org.communiquons.android.comunic.client.data.utils; + +import java.util.ArrayList; + +/** + * Array utilities functions + * + * @author Pierre HUBERT + * Created by pierre on 1/5/18. + */ + +public class ArrayUtils { + + /** + * Convert an ArrayList of Integer into a string + * + * @param input The input array + * @param separator The separator of the values of the array + * @return Generated string + */ + public static String int_array_to_string(ArrayList input, String separator){ + + String result = ""; + + for(int value : input){ + result += value + separator; + } + + return result; + + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java index 6cab6a6..f57b0d2 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java @@ -324,10 +324,15 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O //Check which action was chosen switch (item.getItemId()) { + //To delete the conversation case R.id.menu_fragment_conversationslist_item_delete: confirmDeleteConversation(convID); return true; + //To udpate the conversation + case R.id.menu_fragment_conversationslist_item_update: + updateConversationListener.updateConversation(convID); + return true; } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/UpdateConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/UpdateConversationFragment.java index fd194b9..5002119 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/UpdateConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/UpdateConversationFragment.java @@ -24,10 +24,12 @@ import android.widget.Toast; import org.communiquons.android.comunic.client.MainActivity; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.SearchUserActivity; +import org.communiquons.android.comunic.client.data.Account.AccountUtils; import org.communiquons.android.comunic.client.data.DatabaseHelper; import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper; import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; import org.communiquons.android.comunic.client.data.UsersInfo.UsersAsysncInfoAdapter; +import org.communiquons.android.comunic.client.data.conversations.ConversationsInfo; import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper; import java.util.ArrayList; @@ -46,11 +48,41 @@ public class UpdateConversationFragment extends Fragment { */ private static final String TAG = "UpdateConversationFragment"; + /** + * The conversation ID argument + */ + public static final String ARG_CONVERSATION_ID = "conversation_id"; + /** * Find user ID intent */ public static final int FIND_USER_ID_INTENT = 0; + /** + * Action : create a conversation + */ + private static final int ACTION_CREATE_CONVERSATION = 0; + + /** + * Action : update a conversation + */ + private static final int ACTION_UPDATE_CONVERSATION = 1; + + /** + * Current action of the fragment + */ + private int current_action = ACTION_CREATE_CONVERSATION; + + /** + * Target conversation ID + */ + private int conversation_id = 0; + + /** + * Specify whether the user is the owner of the conversation or not + */ + private boolean conversation_owner = true; + /** * The name of the conversation */ @@ -111,6 +143,21 @@ public class UpdateConversationFragment extends Fragment { */ private ConversationsListHelper.openConversationListener convOpener; + /** + * Conversation members list context menu + */ + View.OnCreateContextMenuListener membersListContext = new View.OnCreateContextMenuListener() { + @Override + public void onCreateContextMenu(ContextMenu menu, View v, + ContextMenu.ContextMenuInfo menuInfo) { + + //Create menu + MenuInflater menuInflater = getActivity().getMenuInflater(); + menuInflater.inflate(R.menu.menu_fragment_update_conversation_memberslist, menu); + + } + }; + @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -171,17 +218,7 @@ public class UpdateConversationFragment extends Fragment { init_form(); //Set members list context menu - membersList.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { - @Override - public void onCreateContextMenu(ContextMenu menu, View v, - ContextMenu.ContextMenuInfo menuInfo) { - - //Create menu - MenuInflater menuInflater = getActivity().getMenuInflater(); - menuInflater.inflate(R.menu.menu_fragment_update_conversation_memberslist, menu); - - } - }); + membersList.setOnCreateContextMenuListener(membersListContext); } @@ -192,9 +229,14 @@ public class UpdateConversationFragment extends Fragment { super.onResume(); //Update title and dock - getActivity().setTitle(R.string.fragment_update_conversation_title_create); ((MainActivity) getActivity()).setSelectedNavigationItem( R.id.main_bottom_navigation_conversations); + + //Set the adapted title + if(current_action == ACTION_CREATE_CONVERSATION) + getActivity().setTitle(R.string.fragment_update_conversation_title_create); + else + getActivity().setTitle(R.string.fragment_update_conversation_title_update); } /** @@ -229,14 +271,92 @@ public class UpdateConversationFragment extends Fragment { */ private void init_form(){ - //Hide progress bar - set_progressbar_visibility(false); - //Initialize the list of members membersID = new ArrayList<>(); membersInfo = new ArrayMap<>(); membersAdapter = new UsersAsysncInfoAdapter(getActivity(), membersID, membersInfo); membersList.setAdapter(membersAdapter); + + //Check if we have to create or to update a conversation + conversation_id = getArguments().getInt(ARG_CONVERSATION_ID, 0); + + //Check if we have to create a conversation + if(conversation_id == 0) { + + //Hide progress bar + set_progressbar_visibility(false); + + //Set action + current_action = ACTION_CREATE_CONVERSATION; + + //Update submit button text + submitButton.setText(R.string.fragment_update_conversation_button_create); + } + + //Check if we have to update a conversation + else { + + //Lock the form + set_form_blocked(true); + + //Set action + current_action = ACTION_UPDATE_CONVERSATION; + + //Update submit button text + submitButton.setText(R.string.fragment_update_conversation_button_update); + + //Get informations about the conversation + new AsyncTask(){ + + @Override + protected ConversationsInfo doInBackground(Integer... params) { + return convListHelper.getInfosSingle(params[0], true); + } + + @Override + protected void onPostExecute(ConversationsInfo conversationsInfo) { + onGotConversationInfos(conversationsInfo); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, conversation_id); + } + + } + + /** + * This method is called when we received information about a conversation + * + * @param infos Informations about a conversation, or null in case of failure + */ + private void onGotConversationInfos(@Nullable ConversationsInfo infos){ + + //Check if the activity has been destroyed + if(getActivity() == null) + return; + + //Check for errors + if(infos == null){ + Toast.makeText(getActivity(), R.string.err_get_conversation_info, + Toast.LENGTH_SHORT).show(); + return; + } + + //Check if the user is the owner of the conversation or not + conversation_owner = AccountUtils.getID(getActivity()) == infos.getID_owner(); + + //Update the values + nameView.setText(infos.getName()); + followCheckbox.setChecked(infos.isFollowing()); + membersID.addAll(infos.getMembers()); + + //Notify members adapter and refresh users informations + membersAdapter.notifyDataSetChanged(); + refresh_members_information(); + + //Remove progress bar + set_progressbar_visibility(false); + + //Unlock form fields + set_form_blocked(false); } /** @@ -354,20 +474,46 @@ public class UpdateConversationFragment extends Fragment { set_form_blocked(true); set_progressbar_visibility(true); - //Create the task in the background - new AsyncTask(){ + //Create the conversation if required + if(current_action == ACTION_CREATE_CONVERSATION) { - @Override - protected Integer doInBackground(Void... params) { - return convListHelper.create(name, following, membersID); - } + //Create the conversation in the background + new AsyncTask() { - @Override - protected void onPostExecute(Integer integer) { - if(getActivity() != null) - creationCallback(integer); - } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + @Override + protected Integer doInBackground(Void... params) { + return convListHelper.create(name, following, membersID); + } + + @Override + protected void onPostExecute(Integer integer) { + if (getActivity() != null) + creationCallback(integer); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + + //Update the conversation if required + if(current_action == ACTION_UPDATE_CONVERSATION){ + + new AsyncTask(){ + @Override + protected Boolean doInBackground(Void... params) { + + //Check if the current user is the owner of the conversation or not + if(conversation_owner) + return convListHelper.update(conversation_id, name, membersID, following); + else + return convListHelper.update(conversation_id, following); + } + + @Override + protected void onPostExecute(Boolean result) { + updateCallback(result); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + + } } /** @@ -393,6 +539,32 @@ public class UpdateConversationFragment extends Fragment { convOpener.openConversation(convID); } + /** + * This method is called once the update request on the server has been made + * + * @param result The result of the operation + */ + private void updateCallback(boolean result){ + + if(getActivity() == null) + return; + + //Handle errors + if(!result){ + Toast.makeText(getActivity(), R.string.err_conversation_update, + Toast.LENGTH_SHORT).show(); + + //Release form + set_form_blocked(false); + set_progressbar_visibility(false); + + return; + } + + //Open conversation + convOpener.openConversation(conversation_id); + } + /** * Update progressbar visibility * @@ -408,9 +580,12 @@ public class UpdateConversationFragment extends Fragment { * @param blocked Specify whether the fields should be blocked or not */ private void set_form_blocked(boolean blocked){ - nameView.setEnabled(!blocked); + nameView.setEnabled(!blocked && conversation_owner); submitButton.setEnabled(!blocked); - addMember.setEnabled(!blocked); + addMember.setEnabled(!blocked && conversation_owner); followCheckbox.setEnabled(!blocked); + + membersList.setOnCreateContextMenuListener( + !blocked && conversation_owner ? membersListContext : null); } } diff --git a/app/src/main/res/menu/menu_fragment_conversationslist_item.xml b/app/src/main/res/menu/menu_fragment_conversationslist_item.xml index f60ce17..d56e7b7 100644 --- a/app/src/main/res/menu/menu_fragment_conversationslist_item.xml +++ b/app/src/main/res/menu/menu_fragment_conversationslist_item.xml @@ -1,6 +1,12 @@ + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 68e8909..05b5d42 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -68,6 +68,7 @@ Title Could not get information about the conversation ! Delete + Update Remove conversation Are you sure do you want to remove this conversation ? All the messages you posted will be deleted. If you are the owner of the conversation, all the messages will be removed. Delete @@ -75,8 +76,10 @@ Could not delete conversation ! No message yet. Create a conversation + Update a conversation Conversation name Create + Update Follow the conversation Add a member Search user @@ -87,4 +90,6 @@ Please add at least one member to the conversation ! Could not create conversation ! Remove + Could not get information about the conversation ! + Could not update conversation !