Get the list of conversation from the API

This commit is contained in:
Pierre 2017-12-09 14:02:29 +01:00
parent 4e90fd9793
commit 456a1f830b
8 changed files with 430 additions and 10 deletions

View File

@ -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<Integer> 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<Integer> 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<Integer> getMembers() {
return members;
}
}

View File

@ -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<ConversationsInfo> download(){
ArrayList<ConversationsInfo> 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;
}
}

View File

@ -97,7 +97,7 @@ public class FriendsAdapter extends ArrayAdapter<FriendUser> {
//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<FriendUser> {
//Hide the view
v.setVisibility(View.GONE);
mFLfragment.acceptRequest(position);
mFLfragment.showPopupRequestResponse(position);
}
});

View File

@ -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 !");

View File

@ -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<ConversationsInfo> 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<Void, Void, Void>(){
@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);
}
}

View File

@ -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<Friend, Void, Void>(){
@Override
protected Void doInBackground(Friend... params) {
flist.respondRequest(params[0], true);
flist.respondRequest(params[0], accept);
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, targetFriend);

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- List of the conversations -->
<ListView
android:id="@+id/fragment_conversationslist_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:numColumns="auto_fit"/>
<!-- Create new conversations button -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/fragment_conversationslist_create_button"
android:enabled="false" /><!-- Not available yet -->
</LinearLayout>

View File

@ -28,4 +28,9 @@
<string name="action_friends_accept_request">Accept</string>
<string name="user_image_description">User Image Account</string>
<string name="navigation_bottom_conversations_item">Conversations</string>
<string name="fragment_conversationslist_create_button">Create a new conversation</string>
<string name="action_friends_respond_request">Respond request</string>
<string name="action_friends_deny_request">Deny</string>
<string name="popup_respond_friendship_request_title">Respond to the request</string>
<string name="popup_respond_friendship_request_message">Do you want to accept or deny this friendship request ?</string>
</resources>