Can update conversation

This commit is contained in:
Pierre 2018-01-05 13:46:37 +01:00
parent 4674821c60
commit 742cdae638
10 changed files with 341 additions and 36 deletions

View File

@ -289,6 +289,7 @@ public class MainActivity extends AppCompatActivity
//Set the arguments of the fragment //Set the arguments of the fragment
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putInt(UpdateConversationFragment.ARG_CONVERSATION_ID, convID);
//Create the fragment //Create the fragment
UpdateConversationFragment updateConversationFragment = new UpdateConversationFragment(); UpdateConversationFragment updateConversationFragment = new UpdateConversationFragment();

View File

@ -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();
}
} }

View File

@ -3,6 +3,8 @@ package org.communiquons.android.comunic.client.data.conversations;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import org.communiquons.android.comunic.client.data.utils.ArrayUtils;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
@ -203,13 +205,7 @@ public class ConversationsInfo {
if(members == null) if(members == null)
return ""; return "";
String result = ""; return ArrayUtils.int_array_to_string(members, ",");
for(int member : members){
result += member + ",";
}
return result;
} }
/** /**

View File

@ -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 * Delete all the list of conversations
*/ */

View File

@ -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.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.utils.ArrayUtils;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; 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<Integer> 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 * Get online (download) the list of all the conversations
* *

View File

@ -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<Integer> input, String separator){
String result = "";
for(int value : input){
result += value + separator;
}
return result;
}
}

View File

@ -324,10 +324,15 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
//Check which action was chosen //Check which action was chosen
switch (item.getItemId()) { switch (item.getItemId()) {
//To delete the conversation
case R.id.menu_fragment_conversationslist_item_delete: case R.id.menu_fragment_conversationslist_item_delete:
confirmDeleteConversation(convID); confirmDeleteConversation(convID);
return true; return true;
//To udpate the conversation
case R.id.menu_fragment_conversationslist_item_update:
updateConversationListener.updateConversation(convID);
return true;
} }
} }

View File

@ -24,10 +24,12 @@ import android.widget.Toast;
import org.communiquons.android.comunic.client.MainActivity; import org.communiquons.android.comunic.client.MainActivity;
import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.SearchUserActivity; 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.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.ConversationsInfo;
import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper; import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper;
import java.util.ArrayList; import java.util.ArrayList;
@ -46,11 +48,41 @@ public class UpdateConversationFragment extends Fragment {
*/ */
private static final String TAG = "UpdateConversationFragment"; private static final String TAG = "UpdateConversationFragment";
/**
* The conversation ID argument
*/
public static final String ARG_CONVERSATION_ID = "conversation_id";
/** /**
* Find user ID intent * Find user ID intent
*/ */
public static final int FIND_USER_ID_INTENT = 0; 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 * The name of the conversation
*/ */
@ -111,6 +143,21 @@ public class UpdateConversationFragment extends Fragment {
*/ */
private ConversationsListHelper.openConversationListener convOpener; 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 @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -171,17 +218,7 @@ public class UpdateConversationFragment extends Fragment {
init_form(); init_form();
//Set members list context menu //Set members list context menu
membersList.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { membersList.setOnCreateContextMenuListener(membersListContext);
@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);
}
});
} }
@ -192,9 +229,14 @@ public class UpdateConversationFragment extends Fragment {
super.onResume(); super.onResume();
//Update title and dock //Update title and dock
getActivity().setTitle(R.string.fragment_update_conversation_title_create);
((MainActivity) getActivity()).setSelectedNavigationItem( ((MainActivity) getActivity()).setSelectedNavigationItem(
R.id.main_bottom_navigation_conversations); 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(){ private void init_form(){
//Hide progress bar
set_progressbar_visibility(false);
//Initialize the list of members //Initialize the list of members
membersID = new ArrayList<>(); membersID = new ArrayList<>();
membersInfo = new ArrayMap<>(); membersInfo = new ArrayMap<>();
membersAdapter = new UsersAsysncInfoAdapter(getActivity(), membersID, membersInfo); membersAdapter = new UsersAsysncInfoAdapter(getActivity(), membersID, membersInfo);
membersList.setAdapter(membersAdapter); 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<Integer, Void, ConversationsInfo>(){
@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,8 +474,11 @@ public class UpdateConversationFragment extends Fragment {
set_form_blocked(true); set_form_blocked(true);
set_progressbar_visibility(true); set_progressbar_visibility(true);
//Create the task in the background //Create the conversation if required
new AsyncTask<Void, Void, Integer>(){ if(current_action == ACTION_CREATE_CONVERSATION) {
//Create the conversation in the background
new AsyncTask<Void, Void, Integer>() {
@Override @Override
protected Integer doInBackground(Void... params) { protected Integer doInBackground(Void... params) {
@ -364,12 +487,35 @@ public class UpdateConversationFragment extends Fragment {
@Override @Override
protected void onPostExecute(Integer integer) { protected void onPostExecute(Integer integer) {
if(getActivity() != null) if (getActivity() != null)
creationCallback(integer); creationCallback(integer);
} }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
//Update the conversation if required
if(current_action == ACTION_UPDATE_CONVERSATION){
new AsyncTask<Void, Void, Boolean>(){
@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);
}
}
/** /**
* This method is called once the conversation has been created (or not) * This method is called once the conversation has been created (or not)
* *
@ -393,6 +539,32 @@ public class UpdateConversationFragment extends Fragment {
convOpener.openConversation(convID); 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 * Update progressbar visibility
* *
@ -408,9 +580,12 @@ public class UpdateConversationFragment extends Fragment {
* @param blocked Specify whether the fields should be blocked or not * @param blocked Specify whether the fields should be blocked or not
*/ */
private void set_form_blocked(boolean blocked){ private void set_form_blocked(boolean blocked){
nameView.setEnabled(!blocked); nameView.setEnabled(!blocked && conversation_owner);
submitButton.setEnabled(!blocked); submitButton.setEnabled(!blocked);
addMember.setEnabled(!blocked); addMember.setEnabled(!blocked && conversation_owner);
followCheckbox.setEnabled(!blocked); followCheckbox.setEnabled(!blocked);
membersList.setOnCreateContextMenuListener(
!blocked && conversation_owner ? membersListContext : null);
} }
} }

View File

@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Update the conversation -->
<item
android:id="@+id/menu_fragment_conversationslist_item_update"
android:title="@string/action_conversation_update" />
<!-- Delete the conversation -->
<item <item
android:id="@+id/menu_fragment_conversationslist_item_delete" android:id="@+id/menu_fragment_conversationslist_item_delete"
android:title="@string/action_conversation_delete" /> android:title="@string/action_conversation_delete" />

View File

@ -68,6 +68,7 @@
<string name="fragment_conversation_title">Title</string> <string name="fragment_conversation_title">Title</string>
<string name="fragment_conversation_err_getconvinfos">Could not get information about the conversation !</string> <string name="fragment_conversation_err_getconvinfos">Could not get information about the conversation !</string>
<string name="action_conversation_delete">Delete</string> <string name="action_conversation_delete">Delete</string>
<string name="action_conversation_update">Update</string>
<string name="popup_deleteconversation_title">Remove conversation</string> <string name="popup_deleteconversation_title">Remove conversation</string>
<string name="popup_deleteconversation_messsage">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.</string> <string name="popup_deleteconversation_messsage">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.</string>
<string name="popup_deleteconversation_confirm">Delete</string> <string name="popup_deleteconversation_confirm">Delete</string>
@ -75,8 +76,10 @@
<string name="fragment_conversationslist_err_del_conversation">Could not delete conversation !</string> <string name="fragment_conversationslist_err_del_conversation">Could not delete conversation !</string>
<string name="fragment_conversation_no_msg">No message yet.</string> <string name="fragment_conversation_no_msg">No message yet.</string>
<string name="fragment_update_conversation_title_create">Create a conversation</string> <string name="fragment_update_conversation_title_create">Create a conversation</string>
<string name="fragment_update_conversation_title_update">Update a conversation</string>
<string name="fragment_update_conversation_name_placeholder">Conversation name</string> <string name="fragment_update_conversation_name_placeholder">Conversation name</string>
<string name="fragment_update_conversation_button_create">Create</string> <string name="fragment_update_conversation_button_create">Create</string>
<string name="fragment_update_conversation_button_update">Update</string>
<string name="fragment_update_conversation_follow">Follow the conversation</string> <string name="fragment_update_conversation_follow">Follow the conversation</string>
<string name="fragment_update_conversation_addmember">Add a member</string> <string name="fragment_update_conversation_addmember">Add a member</string>
<string name="activity_searchuser_title">Search user</string> <string name="activity_searchuser_title">Search user</string>
@ -87,4 +90,6 @@
<string name="err_conversation_need_members">Please add at least one member to the conversation !</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> <string name="err_conversation_create">Could not create conversation !</string>
<string name="fragment_update_conversation_members_menu_delete">Remove</string> <string name="fragment_update_conversation_members_menu_delete">Remove</string>
<string name="err_get_conversation_info">Could not get information about the conversation !</string>
<string name="err_conversation_update">Could not update conversation !</string>
</resources> </resources>