Can create conversations

This commit is contained in:
Pierre 2018-01-02 17:24:13 +01:00
parent 703142df5b
commit 9c06507d0c
4 changed files with 141 additions and 1 deletions

View File

@ -183,6 +183,43 @@ public class ConversationsListHelper {
return true; 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<Integer> 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 * Get online (download) the list of all the conversations
* *

View File

@ -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.GetUsersHelper;
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; 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.UsersInfo.UsersAsysncInfoAdapter;
import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper;
import java.util.ArrayList; import java.util.ArrayList;
@ -96,6 +97,16 @@ public class UpdateConversationFragment extends Fragment {
*/ */
private GetUsersHelper usersHelper; private GetUsersHelper usersHelper;
/**
* Conversations list helper
*/
private ConversationsListHelper convListHelper;
/**
* Conversation opener
*/
private ConversationsListHelper.openConversationListener convOpener;
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -105,6 +116,17 @@ public class UpdateConversationFragment extends Fragment {
//Get User helper //Get User helper
usersHelper = new GetUsersHelper(getActivity(), dbHelper); 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 @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 //Initialize the form
init_form(); init_form();
@ -261,6 +291,64 @@ public class UpdateConversationFragment extends Fragment {
membersAdapter.notifyDataSetChanged(); 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<Void, Void, Integer>(){
@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 * Update progressbar visibility
* *
@ -269,4 +357,16 @@ public class UpdateConversationFragment extends Fragment {
private void set_progressbar_visibility(boolean visible){ private void set_progressbar_visibility(boolean visible){
progressBar.setVisibility(visible ? View.VISIBLE : View.GONE); 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);
}
} }

View File

@ -46,7 +46,8 @@
android:id="@+id/fragment_update_conversation_follow" android:id="@+id/fragment_update_conversation_follow"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/fragment_update_conversation_follow"/> android:text="@string/fragment_update_conversation_follow"
android:checked="true" /> <!-- Checked by default -->
<!-- Create the conversation --> <!-- Create the conversation -->
<Button <Button

View File

@ -84,4 +84,6 @@
<string name="err_search_user">Could not search user !</string> <string name="err_search_user">Could not search user !</string>
<string name="err_add_member_double">This user is already a member of the conversation!</string> <string name="err_add_member_double">This user is already a member of the conversation!</string>
<string name="err_get_users_info">Could not get information about users !</string> <string name="err_get_users_info">Could not get information about users !</string>
<string name="err_conversation_need_members">Please add at least one member to the conversation !</string>
<string name="err_conversation_create">Could not create conversation !</string>
</resources> </resources>