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
Bundle args = new Bundle();
args.putInt(UpdateConversationFragment.ARG_CONVERSATION_ID, convID);
//Create the fragment
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.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, ",");
}
/**

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
*/

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.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<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
*

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
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;
}
}

View File

@ -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<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,7 +474,10 @@ public class UpdateConversationFragment extends Fragment {
set_form_blocked(true);
set_progressbar_visibility(true);
//Create the task in the background
//Create the conversation if required
if(current_action == ACTION_CREATE_CONVERSATION) {
//Create the conversation in the background
new AsyncTask<Void, Void, Integer>() {
@Override
@ -370,6 +493,29 @@ public class UpdateConversationFragment extends Fragment {
}.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)
*
@ -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);
}
}

View File

@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<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
android:id="@+id/menu_fragment_conversationslist_item_delete"
android:title="@string/action_conversation_delete" />

View File

@ -68,6 +68,7 @@
<string name="fragment_conversation_title">Title</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_update">Update</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_confirm">Delete</string>
@ -75,8 +76,10 @@
<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_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_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_addmember">Add a member</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_create">Could not create conversation !</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>