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 5f2d079..ddf5865 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 @@ -183,6 +183,43 @@ public class ConversationsListHelper { return true; } + /** + * Create a new conversation + * + * @param name The name of the conversation + * @param follow True to make the user follow the conversation + * @param members The members of the conversation + * @return The ID of the created conversation / null in case of failure + */ + @Nullable + public Integer create(String name, boolean follow, ArrayList members){ + + //Turn the list of members into a string + String members_str = ""; + for(int id : members){ + members_str += id + ","; + } + + //Make an API request + APIRequestParameters params = new APIRequestParameters(mContext, "conversations/create"); + params.addParameter("name", name.equals("") ? "false" : name); + params.addParameter("follow", follow ? "true" : "false"); + params.addParameter("users", members_str); + + //Perform the request + try { + APIResponse response = new APIRequest().exec(params); + + //Get conversation ID + JSONObject obj = response.getJSONObject(); + return obj.getInt("conversationID"); + + } catch (Exception e){ + e.printStackTrace(); + return null; + } + } + /** * Get online (download) the list of all the conversations * 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 ca0f99b..f0e99b0 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,6 +24,7 @@ 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.ConversationsListHelper; import java.util.ArrayList; @@ -96,6 +97,16 @@ public class UpdateConversationFragment extends Fragment { */ private GetUsersHelper usersHelper; + /** + * Conversations list helper + */ + private ConversationsListHelper convListHelper; + + /** + * Conversation opener + */ + private ConversationsListHelper.openConversationListener convOpener; + @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -105,6 +116,17 @@ public class UpdateConversationFragment extends Fragment { //Get User helper usersHelper = new GetUsersHelper(getActivity(), dbHelper); + + //Get conversation list helper + convListHelper = new ConversationsListHelper(getActivity(), dbHelper); + + //Get conversation opener + try { + convOpener = (ConversationsListHelper.openConversationListener) getActivity(); + } catch (ClassCastException e){ + throw new RuntimeException(getActivity().getClass().getName() + " must implement the" + + " ConversationsListHelper.openConversationListener interface !"); + } } @Nullable @@ -133,6 +155,14 @@ public class UpdateConversationFragment extends Fragment { } }); + //Make submit button lives + submitButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + submit_form(); + } + }); + //Initialize the form init_form(); @@ -261,6 +291,64 @@ public class UpdateConversationFragment extends Fragment { membersAdapter.notifyDataSetChanged(); } + /** + * Submit creation form + */ + private void submit_form(){ + + //Check there is at least on member to the conversation + if(membersID.size() == 0){ + Toast.makeText(getActivity(), R.string.err_conversation_need_members, + Toast.LENGTH_SHORT).show(); + } + + //Get the values + final String name = ""+nameView.getText(); + final boolean following = followCheckbox.isChecked(); + + //Block the form + set_form_blocked(true); + set_progressbar_visibility(true); + + //Create the task in the background + new AsyncTask(){ + + @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); + } + + /** + * This method is called once the conversation has been created (or not) + * + * @param convID The ID of the target conversation + */ + private void creationCallback(@Nullable Integer convID){ + + //Check for errors + if(convID == null){ + Toast.makeText(getActivity(), R.string.err_conversation_create, + Toast.LENGTH_SHORT).show(); + + //Release form + set_form_blocked(false); + set_progressbar_visibility(false); + + return; + } + + //Open conversation + convOpener.openConversation(convID); + } + /** * Update progressbar visibility * @@ -269,4 +357,16 @@ public class UpdateConversationFragment extends Fragment { private void set_progressbar_visibility(boolean visible){ progressBar.setVisibility(visible ? View.VISIBLE : View.GONE); } + + /** + * Make the fields of the form read only or read and write + * + * @param blocked Specify whether the fields should be blocked or not + */ + private void set_form_blocked(boolean blocked){ + nameView.setEnabled(!blocked); + submitButton.setEnabled(!blocked); + addMember.setEnabled(!blocked); + followCheckbox.setEnabled(!blocked); + } } diff --git a/app/src/main/res/layout/fragment_update_conversation.xml b/app/src/main/res/layout/fragment_update_conversation.xml index def33a1..279b610 100644 --- a/app/src/main/res/layout/fragment_update_conversation.xml +++ b/app/src/main/res/layout/fragment_update_conversation.xml @@ -46,7 +46,8 @@ android:id="@+id/fragment_update_conversation_follow" android:layout_width="match_parent" android:layout_height="wrap_content" - android:text="@string/fragment_update_conversation_follow"/> + android:text="@string/fragment_update_conversation_follow" + android:checked="true" />