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 new file mode 100644 index 0000000..fe71b9b --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsInfo.java @@ -0,0 +1,174 @@ +package org.communiquons.android.comunic.client.data.conversations; + +import java.util.ArrayList; + +/** + * Contains the information about a single conversation + * + * @author Pierre HUBERT + * Created by pierre on 12/9/17. + */ + +public class ConversationsInfo { + + /** + * Values of the conversation + */ + private int ID; + private int ID_owner; + private int last_active; + private String name; + private boolean following; + private boolean saw_last_message; + private ArrayList members; + + /** + * Set the ID of the conversation + * + * @param ID The ID of the conversation + */ + public void setID(int ID) { + this.ID = ID; + } + + /** + * Get the ID of the conversation + * + * @return The ID of the conversation + */ + public int getID() { + return ID; + } + + /** + * Set the ID of the owner of teh conversation + * + * @param ID_owner The ID of the owner of the conversation + */ + public void setID_owner(int ID_owner) { + this.ID_owner = ID_owner; + } + + /** + * Get the ID of the owner of the conversation + * + * @return The ID of the owner of the conversation + */ + public int getID_owner() { + return ID_owner; + } + + /** + * Set last activity of the conversation + * + * @param last_active The timestamp of the last activity of the conversation + */ + public void setLast_active(int last_active) { + this.last_active = last_active; + } + + /** + * Get the last activity of the conversation + * + * @return The last activity time of the conversation + */ + public int getLast_active() { + return last_active; + } + + /** + * Set the name of the conversation + * + * @param name The name of the conversation + */ + public void setName(String name) { + this.name = name; + } + + /** + * Get the name of the conversation + * + * @return The name of the conversation + */ + public String getName() { + return name; + } + + /** + * Specify whether the user is following the conversation or not + * + * Follows means get notifications, and get informed of the changes in the conversation + * + * @param following True if the user is following the conversation + */ + public void setFollowing(boolean following) { + this.following = following; + } + + /** + * Check whether the user is following or not the conversation + * + * @return True if user is following the conversation + */ + public boolean isFollowing() { + return following; + } + + /** + * Specify if the current user has seen the last message of the conversation + * + * @param saw_last_message True if the user has seen the last message of the conversation + */ + public void setSaw_last_message(boolean saw_last_message) { + this.saw_last_message = saw_last_message; + } + + /** + * Check if the user has saw the last message of the conversation + * + * @return True if the user has seen the last message of the conversation + */ + public boolean hasSaw_last_message() { + return saw_last_message; + } + + /** + * Set the list of members of the conversation + * + * @param members The IDs of the members of the conversation + */ + public void setMembers(ArrayList members) { + this.members = members; + } + + /** + * Add a member to the list of members + * + * @param id The ID of the member to add + */ + public void addMember(Integer id){ + if(members == null) + members = new ArrayList<>(); + + members.add(id); + } + + /** + * Get the number of members of the conversation + * + * @return The number of members of the conversation + */ + public int countMembers(){ + return members == null ? 0 : members.size(); + } + + /** + * Get the list of members of the conversation + * + * @return The list of members of the conversation + * + */ + public ArrayList getMembers() { + return members; + } +} 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 new file mode 100644 index 0000000..6f4d5c6 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java @@ -0,0 +1,111 @@ +package org.communiquons.android.comunic.client.data.conversations; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.Log; + +import org.communiquons.android.comunic.client.api.APIRequest; +import org.communiquons.android.comunic.client.api.APIRequestParameters; +import org.communiquons.android.comunic.client.api.APIResponse; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; + +/** + * Operations on the conversation list (helper) + * + * @author Pierre HUBERT + * Created by pierre on 12/9/17. + */ + +public class ConversationsListHelper { + + private String TAG = "ConversationsList"; + + /** + * The context of the application + */ + private Context mContext; + + /** + * The constructor of the class + * + * @param context The context of execution of the application + */ + public ConversationsListHelper(Context context){ + mContext = context; + } + + /** + * Get online (download) the list of all the conversations + * + * @return The list of conversations + */ + public ArrayList download(){ + + ArrayList list = new ArrayList<>(); + + try { + + //Prepare the request on the server + APIRequestParameters params = new APIRequestParameters(mContext, "conversations/getList"); + APIResponse response = new APIRequest().exec(params); + + //Check if an error occurred + JSONArray friends = response.getJSONArray(); + + if(friends == null){ + Log.e(TAG, "Couldn't retrieve friends list !"); + return null; + } + + for(int i = 0; i < friends.length(); i++){ + //Add the conversation to the list + list.add(parseConversationJSON(friends.getJSONObject(i))); + } + + } catch (Exception e){ + e.printStackTrace(); + return null; + } + + return list; + } + + /** + * Parse a JSONObject into a conversation info element + * + * @param obj Information about the conversation + * @return Conversation object or null in case of failure + */ + @Nullable + private ConversationsInfo parseConversationJSON(@NonNull JSONObject obj){ + + ConversationsInfo info = new ConversationsInfo(); + + try { + //Get information about the conversation + info.setID(obj.getInt("ID")); + info.setID_owner(obj.getInt("ID_owner")); + info.setLast_active(obj.getInt("last_active")); + info.setName(obj.getString("name")); + info.setFollowing(obj.getInt("following") == 1); + info.setSaw_last_message(obj.getInt("saw_last_message") == 1); + + //Get the list of the members + JSONArray members = obj.getJSONArray("members"); + for(int i = 0; i < members.length(); i++){ + info.addMember(members.getInt(i)); + } + + } catch(JSONException e){ + e.printStackTrace(); + return null; + } + return info; + } + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java index 5b497a9..c765a24 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java @@ -97,7 +97,7 @@ public class FriendsAdapter extends ArrayAdapter { //Update the button action.setVisibility(View.VISIBLE); - action.setText(R.string.action_friends_accept_request); + action.setText(R.string.action_friends_respond_request); //Make the button lives action.setOnClickListener(new View.OnClickListener() { @@ -107,7 +107,7 @@ public class FriendsAdapter extends ArrayAdapter { //Hide the view v.setVisibility(View.GONE); - mFLfragment.acceptRequest(position); + mFLfragment.showPopupRequestResponse(position); } }); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsList.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsList.java index 51f1a51..524250d 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsList.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsList.java @@ -73,8 +73,13 @@ public class FriendsList { new APIRequest().exec(reqParams); //Update the friend in the local database - friend.setAccepted(accept); - fdbHelper.update_friend(friend); + if(accept) { + friend.setAccepted(accept); + fdbHelper.update_friend(friend); + } + else { + fdbHelper.delete_friend(friend); + } } catch(Exception e){ Log.e(TAG, "Couldn't respond to friendship request !"); 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 dbdd460..fa9547b 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 @@ -1,6 +1,20 @@ package org.communiquons.android.comunic.client.fragments; import android.app.Fragment; +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Toast; + +import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.conversations.ConversationsInfo; +import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper; + +import java.util.ArrayList; /** * Conversation list fragment @@ -13,5 +27,56 @@ import android.app.Fragment; public class ConversationsListFragment extends Fragment { + /** + * Debug tag + */ + private String TAG = "ConversationsListFrag"; + /** + * The list of conversations + */ + private ArrayList convList; + + /** + * The conversation list helper + */ + private ConversationsListHelper conversationsListHelper; + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_conversationslist, container, false); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + //Create the conversation list helper + conversationsListHelper = new ConversationsListHelper(getActivity()); + + //Get the list of conversations + new AsyncTask(){ + @Override + protected Void doInBackground(Void... params) { + + //Get the list of conversations + convList = conversationsListHelper.download(); + + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + + for(ConversationsInfo conv : convList){ + Log.v(TAG, "Conversation "+conv.getID()+": " + conv.getName() + " / " + + conv.countMembers() + " members / Owner: " + conv.getID_owner()); + } + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + + + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java index 84e108c..e2efce1 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java @@ -246,23 +246,61 @@ public class FriendsListFragment extends Fragment { } /** - * Accept friendship request + * Show a popup to offer the user to respond to a friendship request * - * @param pos The position of the friend accepting the request + * @param pos The position of the friend in the list */ - public void acceptRequest(int pos){ + public void showPopupRequestResponse(final int pos){ + + new AlertDialog.Builder(getActivity()) + + .setTitle(R.string.popup_respond_friendship_request_title) + .setMessage(R.string.popup_respond_friendship_request_message) + + .setNegativeButton(R.string.action_friends_deny_request, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + respondRequest(pos, false); + } + }) + + .setPositiveButton(R.string.action_friends_accept_request, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + respondRequest(pos, true); + } + }) + + .show(); + + } + + /** + * Respond to a friendship request + * + * @param pos The position of the friend respond the request + * @param accept Specify wether the user accepted the request or not + */ + private void respondRequest(int pos, final boolean accept){ //Get the Friend object Friend targetFriend = friendsList.get(pos).getFriend(); - //Mark the friend as accepted - targetFriend.setAccepted(true); + if(accept) + //Mark the friend as accepted + targetFriend.setAccepted(true); + else + //Remove the friend from the list + friendsList.remove(pos); + + //Inform the adapter the list has changed + fAdapter.notifyDataSetChanged(); //Accept the request on a separate thread new AsyncTask(){ @Override protected Void doInBackground(Friend... params) { - flist.respondRequest(params[0], true); + flist.respondRequest(params[0], accept); return null; } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, targetFriend); diff --git a/app/src/main/res/layout/fragment_conversationslist.xml b/app/src/main/res/layout/fragment_conversationslist.xml new file mode 100644 index 0000000..8370081 --- /dev/null +++ b/app/src/main/res/layout/fragment_conversationslist.xml @@ -0,0 +1,22 @@ + + + + + + + + +