mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-27 07:49:28 +00:00
Improved the way conversations fragment get the list of conversations
This commit is contained in:
parent
8f87b05fcc
commit
af5eb4fce2
@ -210,7 +210,7 @@ public class CallsHelper extends BaseHelper {
|
|||||||
|
|
||||||
//Get call name
|
//Get call name
|
||||||
String name = new ConversationsListHelper(getContext())
|
String name = new ConversationsListHelper(getContext())
|
||||||
.getConversationName(call.getConversationID());
|
.getConversationDisplayName(call.getConversationID());
|
||||||
|
|
||||||
if(name == null)
|
if(name == null)
|
||||||
return null;
|
return null;
|
||||||
|
@ -7,7 +7,7 @@ import android.support.annotation.NonNull;
|
|||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
import org.communiquons.android.comunic.client.data.DatabaseContract.ConversationsListSchema;
|
import org.communiquons.android.comunic.client.data.DatabaseContract.ConversationsListSchema;
|
||||||
import org.communiquons.android.comunic.client.data.models.ConversationsInfo;
|
import org.communiquons.android.comunic.client.data.models.ConversationInfo;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@ -46,7 +46,7 @@ public class ConversationsListDbHelper {
|
|||||||
* @param list The new list of conversation
|
* @param list The new list of conversation
|
||||||
* @return TRUE for a success / FALSE else
|
* @return TRUE for a success / FALSE else
|
||||||
*/
|
*/
|
||||||
boolean update_list(ArrayList<ConversationsInfo> list){
|
boolean update_list(ArrayList<ConversationInfo> list){
|
||||||
|
|
||||||
//Remove any old list of conversations
|
//Remove any old list of conversations
|
||||||
delete_all();
|
delete_all();
|
||||||
@ -55,7 +55,7 @@ public class ConversationsListDbHelper {
|
|||||||
|
|
||||||
//Process the list of conversation
|
//Process the list of conversation
|
||||||
boolean success = true;
|
boolean success = true;
|
||||||
for(ConversationsInfo info : list){
|
for(ConversationInfo info : list){
|
||||||
if(!insert(db, info))
|
if(!insert(db, info))
|
||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
@ -72,7 +72,7 @@ public class ConversationsListDbHelper {
|
|||||||
* @return Information about the conversation (if available locally) or null in case of failure
|
* @return Information about the conversation (if available locally) or null in case of failure
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
ConversationsInfo getInfos(int convID){
|
ConversationInfo getInfo(int convID){
|
||||||
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||||
|
|
||||||
//Prepare database request
|
//Prepare database request
|
||||||
@ -92,7 +92,7 @@ public class ConversationsListDbHelper {
|
|||||||
//Perform database request
|
//Perform database request
|
||||||
Cursor c = db.query(table, columns, selection, selectionArgs, null, null, null);
|
Cursor c = db.query(table, columns, selection, selectionArgs, null, null, null);
|
||||||
|
|
||||||
ConversationsInfo infos = null;
|
ConversationInfo infos = null;
|
||||||
|
|
||||||
//Check for result
|
//Check for result
|
||||||
if(c.getCount() != 0){
|
if(c.getCount() != 0){
|
||||||
@ -144,7 +144,7 @@ public class ConversationsListDbHelper {
|
|||||||
* @param info Informations about the conversation to insert
|
* @param info Informations about the conversation to insert
|
||||||
* @return TRUE for a success / False else
|
* @return TRUE for a success / False else
|
||||||
*/
|
*/
|
||||||
private boolean insert(@NonNull SQLiteDatabase db, @NonNull ConversationsInfo info){
|
private boolean insert(@NonNull SQLiteDatabase db, @NonNull ConversationInfo info){
|
||||||
|
|
||||||
ContentValues values = getContentValues(info);
|
ContentValues values = getContentValues(info);
|
||||||
|
|
||||||
@ -158,7 +158,7 @@ public class ConversationsListDbHelper {
|
|||||||
* @param info Information about a conversation
|
* @param info Information about a conversation
|
||||||
* @return The values of the conservation
|
* @return The values of the conservation
|
||||||
*/
|
*/
|
||||||
private ContentValues getContentValues(ConversationsInfo info){
|
private ContentValues getContentValues(ConversationInfo info){
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
|
|
||||||
values.put(ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID, info.getID());
|
values.put(ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID, info.getID());
|
||||||
@ -187,9 +187,9 @@ public class ConversationsListDbHelper {
|
|||||||
* @param c The cursor
|
* @param c The cursor
|
||||||
* @return The Generated conversation information
|
* @return The Generated conversation information
|
||||||
*/
|
*/
|
||||||
private ConversationsInfo getConvObj(Cursor c){
|
private ConversationInfo getConvObj(Cursor c){
|
||||||
|
|
||||||
ConversationsInfo infos = new ConversationsInfo();
|
ConversationInfo infos = new ConversationInfo();
|
||||||
|
|
||||||
//Get the values
|
//Get the values
|
||||||
infos.setID(c.getInt(c.getColumnIndexOrThrow(
|
infos.setID(c.getInt(c.getColumnIndexOrThrow(
|
||||||
|
@ -10,13 +10,14 @@ import org.communiquons.android.comunic.client.data.models.APIRequest;
|
|||||||
import org.communiquons.android.comunic.client.data.models.APIResponse;
|
import org.communiquons.android.comunic.client.data.models.APIResponse;
|
||||||
import org.communiquons.android.comunic.client.data.utils.AccountUtils;
|
import org.communiquons.android.comunic.client.data.utils.AccountUtils;
|
||||||
import org.communiquons.android.comunic.client.data.models.UserInfo;
|
import org.communiquons.android.comunic.client.data.models.UserInfo;
|
||||||
import org.communiquons.android.comunic.client.data.models.ConversationsInfo;
|
import org.communiquons.android.comunic.client.data.models.ConversationInfo;
|
||||||
import org.communiquons.android.comunic.client.data.utils.ArrayUtils;
|
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;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Operations on the conversation list (helper)
|
* Operations on the conversation list (helper)
|
||||||
@ -27,7 +28,10 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
public class ConversationsListHelper {
|
public class ConversationsListHelper {
|
||||||
|
|
||||||
private String TAG = "ConversationsList";
|
/**
|
||||||
|
* Debug tag
|
||||||
|
*/
|
||||||
|
private static final String TAG = ConversationsListHelper.class.getSimpleName();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The context of the application
|
* The context of the application
|
||||||
@ -71,10 +75,10 @@ public class ConversationsListHelper {
|
|||||||
* @return The list of conversations
|
* @return The list of conversations
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public ArrayList<ConversationsInfo> get(){
|
public ArrayList<ConversationInfo> getOnline(){
|
||||||
|
|
||||||
//Download a new list of conversations
|
//Download a new list of conversations
|
||||||
ArrayList<ConversationsInfo> list = download();
|
ArrayList<ConversationInfo> list = download();
|
||||||
|
|
||||||
if(list != null){
|
if(list != null){
|
||||||
//Save the list
|
//Save the list
|
||||||
@ -89,24 +93,24 @@ public class ConversationsListHelper {
|
|||||||
* Get information about a conversation
|
* Get information about a conversation
|
||||||
*
|
*
|
||||||
* @param convID The conversation ID
|
* @param convID The conversation ID
|
||||||
* @param allowDownload In case the conversation was not found locally, allow informations about
|
* @param allowDownload In case the conversation was not found locally, allow information about
|
||||||
* the conversation to be fetched online
|
* the conversation to be fetched online
|
||||||
* @return Information about the conversation, or false in case of failure
|
* @return Information about the conversation, or false in case of failure
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public ConversationsInfo getInfosSingle(int convID, boolean allowDownload){
|
public ConversationInfo getInfoSingle(int convID, boolean allowDownload){
|
||||||
|
|
||||||
ConversationsInfo infos;
|
ConversationInfo info;
|
||||||
|
|
||||||
//Try to fetch information from the local database
|
//Try to fetch information from the local database
|
||||||
if((infos = convDBHelper.getInfos(convID)) != null)
|
if((info = convDBHelper.getInfo(convID)) != null)
|
||||||
return infos;
|
return info;
|
||||||
|
|
||||||
//Check if we are not allowed to fetch informations online
|
//Check if we are not allowed to fetch information online
|
||||||
if(!allowDownload)
|
if(!allowDownload)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
//Get informations about the conversation online
|
//Get information about the conversation online
|
||||||
return downloadSingle(convID);
|
return downloadSingle(convID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,57 +163,92 @@ public class ConversationsListHelper {
|
|||||||
* @param info Information about a conversation
|
* @param info Information about a conversation
|
||||||
* @return The name of the conversation
|
* @return The name of the conversation
|
||||||
*/
|
*/
|
||||||
public String getDisplayName(ConversationsInfo info){
|
@NonNull
|
||||||
|
public String getConversationDisplayName(ConversationInfo info){
|
||||||
|
|
||||||
//Check if a specific name has been specified
|
ArrayList<ConversationInfo> list = new ArrayList<>();
|
||||||
if(info.hasName())
|
list.add(info);
|
||||||
return info.getName();
|
|
||||||
|
|
||||||
//Get the list of members of the conversation
|
if(!getConversationsDisplayName(list))
|
||||||
ArrayList<Integer> members = info.getMembers();
|
return "";
|
||||||
|
|
||||||
//Get the ID of the three first members
|
return info.getDisplayName();
|
||||||
ArrayList<Integer> membersToGet = new ArrayList<>();
|
}
|
||||||
int num = 0;
|
|
||||||
for(int ID : members){
|
|
||||||
membersToGet.add(ID);
|
|
||||||
|
|
||||||
num++;
|
/**
|
||||||
|
* Get the name of a list of conversation
|
||||||
|
*
|
||||||
|
* @param list The list of conversations to process
|
||||||
|
* @return TRUE if we could get all conversations name / FALSE else
|
||||||
|
*/
|
||||||
|
public boolean getConversationsDisplayName(ArrayList<ConversationInfo> list){
|
||||||
|
|
||||||
if(num > 3)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Get information about the users
|
ArrayList<Integer> usersToFetch = new ArrayList<>();
|
||||||
ArrayMap<Integer, UserInfo> users =
|
|
||||||
new GetUsersHelper(mContext, dbHelper).getMultiple(membersToGet);
|
|
||||||
|
|
||||||
if(users == null)
|
for(ConversationInfo c : list){
|
||||||
return ""; //No name by default
|
|
||||||
|
|
||||||
String name = "";
|
if(c.hasName())
|
||||||
|
c.setDisplayName(c.getName());
|
||||||
|
|
||||||
int count = 0;
|
else {
|
||||||
for(Integer id : users.keySet()){
|
|
||||||
|
|
||||||
//Do not display current user name
|
int count = 0;
|
||||||
if(id == AccountUtils.getID(mContext))
|
for(int userID : c.getMembers()){
|
||||||
continue;
|
|
||||||
|
|
||||||
if(users.get(id) != null){
|
if(userID == AccountUtils.getID(mContext))
|
||||||
|
continue;
|
||||||
|
|
||||||
if(count > 0)
|
if(!usersToFetch.contains(userID))
|
||||||
name += ", ";
|
usersToFetch.add(userID);
|
||||||
|
|
||||||
name += users.get(id).getFullName();
|
count++;
|
||||||
count++;
|
|
||||||
|
if(count > 2)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if(count > 3)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return name;
|
//Check if there is not anything to do more
|
||||||
|
if(usersToFetch.size() == 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
ArrayMap<Integer, UserInfo> users = new GetUsersHelper(mContext).getMultiple(usersToFetch);
|
||||||
|
|
||||||
|
if(users == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
|
||||||
|
for(ConversationInfo c : list){
|
||||||
|
|
||||||
|
if(c.hasName())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
StringBuilder name = new StringBuilder();
|
||||||
|
|
||||||
|
for(int userID : c.getMembers()){
|
||||||
|
|
||||||
|
if(!users.containsKey(userID) || users.get(userID) == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if(count > 0)
|
||||||
|
name.append(", ");
|
||||||
|
|
||||||
|
name.append(Objects.requireNonNull(users.get(userID)).getFullName());
|
||||||
|
count++;
|
||||||
|
|
||||||
|
if(count == 2)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
c.setDisplayName(name.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -219,14 +258,14 @@ public class ConversationsListHelper {
|
|||||||
* @return The name of the conversation / null in case of failure
|
* @return The name of the conversation / null in case of failure
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public String getConversationName(int convID){
|
public String getConversationDisplayName(int convID){
|
||||||
|
|
||||||
ConversationsInfo info = getInfosSingle(convID, true);
|
ConversationInfo info = getInfoSingle(convID, true);
|
||||||
|
|
||||||
if(info == null)
|
if(info == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return getDisplayName(info);
|
return getConversationDisplayName(info);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,16 +303,16 @@ public class ConversationsListHelper {
|
|||||||
public Integer create(String name, boolean follow, ArrayList<Integer> members){
|
public Integer create(String name, boolean follow, ArrayList<Integer> members){
|
||||||
|
|
||||||
//Turn the list of members into a string
|
//Turn the list of members into a string
|
||||||
String members_str = "";
|
StringBuilder members_str = new StringBuilder();
|
||||||
for(int id : members){
|
for(int id : members){
|
||||||
members_str += id + ",";
|
members_str.append(id).append(",");
|
||||||
}
|
}
|
||||||
|
|
||||||
//Make an API request
|
//Make an API request
|
||||||
APIRequest params = new APIRequest(mContext, "conversations/create");
|
APIRequest params = new APIRequest(mContext, "conversations/create");
|
||||||
params.addString("name", name.equals("") ? "false" : name);
|
params.addString("name", name.equals("") ? "false" : name);
|
||||||
params.addString("follow", follow ? "true" : "false");
|
params.addString("follow", follow ? "true" : "false");
|
||||||
params.addString("users", members_str);
|
params.addString("users", members_str.toString());
|
||||||
|
|
||||||
//Perform the request
|
//Perform the request
|
||||||
try {
|
try {
|
||||||
@ -352,9 +391,9 @@ public class ConversationsListHelper {
|
|||||||
* @return The list of conversations
|
* @return The list of conversations
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private ArrayList<ConversationsInfo> download(){
|
private ArrayList<ConversationInfo> download(){
|
||||||
|
|
||||||
ArrayList<ConversationsInfo> list = new ArrayList<>();
|
ArrayList<ConversationInfo> list = new ArrayList<>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
@ -390,7 +429,7 @@ public class ConversationsListHelper {
|
|||||||
* @return Informations about the conversation in case of success / null else
|
* @return Informations about the conversation in case of success / null else
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private ConversationsInfo downloadSingle(int convID){
|
private ConversationInfo downloadSingle(int convID){
|
||||||
|
|
||||||
//Perform an API request
|
//Perform an API request
|
||||||
APIRequest params = new APIRequest(mContext,
|
APIRequest params = new APIRequest(mContext,
|
||||||
@ -419,9 +458,9 @@ public class ConversationsListHelper {
|
|||||||
* @return Conversation object or null in case of failure
|
* @return Conversation object or null in case of failure
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private ConversationsInfo parseConversationJSON(@NonNull JSONObject obj){
|
private ConversationInfo parseConversationJSON(@NonNull JSONObject obj){
|
||||||
|
|
||||||
ConversationsInfo info = new ConversationsInfo();
|
ConversationInfo info = new ConversationInfo();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//Get information about the conversation
|
//Get information about the conversation
|
||||||
|
@ -14,7 +14,7 @@ import java.util.ArrayList;
|
|||||||
* Created by pierre on 12/9/17.
|
* Created by pierre on 12/9/17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class ConversationsInfo {
|
public class ConversationInfo {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Values of the conversation
|
* Values of the conversation
|
@ -11,7 +11,7 @@ import android.widget.ArrayAdapter;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import org.communiquons.android.comunic.client.R;
|
import org.communiquons.android.comunic.client.R;
|
||||||
import org.communiquons.android.comunic.client.data.models.ConversationsInfo;
|
import org.communiquons.android.comunic.client.data.models.ConversationInfo;
|
||||||
import org.communiquons.android.comunic.client.data.utils.TimeUtils;
|
import org.communiquons.android.comunic.client.data.utils.TimeUtils;
|
||||||
import org.communiquons.android.comunic.client.ui.utils.UiUtils;
|
import org.communiquons.android.comunic.client.ui.utils.UiUtils;
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ import java.util.ArrayList;
|
|||||||
* Created by pierre on 12/10/17.
|
* Created by pierre on 12/10/17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class ConversationsListAdapter extends ArrayAdapter<ConversationsInfo> {
|
public class ConversationsListAdapter extends ArrayAdapter<ConversationInfo> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class constructor
|
* Class constructor
|
||||||
@ -34,7 +34,7 @@ public class ConversationsListAdapter extends ArrayAdapter<ConversationsInfo> {
|
|||||||
* @param context The context of the application
|
* @param context The context of the application
|
||||||
* @param list The list of conversations to display
|
* @param list The list of conversations to display
|
||||||
*/
|
*/
|
||||||
public ConversationsListAdapter(Context context, ArrayList<ConversationsInfo> list){
|
public ConversationsListAdapter(Context context, ArrayList<ConversationInfo> list){
|
||||||
super(context, 0, list);
|
super(context, 0, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +58,7 @@ public class ConversationsListAdapter extends ArrayAdapter<ConversationsInfo> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Get information about the conversation
|
//Get information about the conversation
|
||||||
ConversationsInfo infos = getItem(position);
|
ConversationInfo infos = getItem(position);
|
||||||
|
|
||||||
//Set the name of the conversation
|
//Set the name of the conversation
|
||||||
TextView conversationName = convertView
|
TextView conversationName = convertView
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
package org.communiquons.android.comunic.client.ui.asynctasks;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper;
|
||||||
|
import org.communiquons.android.comunic.client.data.models.ConversationInfo;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get conversations list task
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
*/
|
||||||
|
public class GetConversationsListTask extends SafeAsyncTask<Void, Void, ArrayList<ConversationInfo>> {
|
||||||
|
|
||||||
|
public GetConversationsListTask(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ArrayList<ConversationInfo> doInBackground(Void... voids) {
|
||||||
|
ConversationsListHelper conversationsListHelper = new ConversationsListHelper(getContext());
|
||||||
|
|
||||||
|
ArrayList<ConversationInfo> list = conversationsListHelper.getOnline();
|
||||||
|
|
||||||
|
if(list == null || !conversationsListHelper.getConversationsDisplayName(list))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
@ -32,7 +32,7 @@ import org.communiquons.android.comunic.client.data.helpers.ConversationsListHel
|
|||||||
import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper;
|
import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper;
|
||||||
import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper;
|
import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper;
|
||||||
import org.communiquons.android.comunic.client.data.models.ConversationMessage;
|
import org.communiquons.android.comunic.client.data.models.ConversationMessage;
|
||||||
import org.communiquons.android.comunic.client.data.models.ConversationsInfo;
|
import org.communiquons.android.comunic.client.data.models.ConversationInfo;
|
||||||
import org.communiquons.android.comunic.client.data.models.NewConversationMessage;
|
import org.communiquons.android.comunic.client.data.models.NewConversationMessage;
|
||||||
import org.communiquons.android.comunic.client.data.models.UserInfo;
|
import org.communiquons.android.comunic.client.data.models.UserInfo;
|
||||||
import org.communiquons.android.comunic.client.data.runnables.ConversationRefreshRunnable;
|
import org.communiquons.android.comunic.client.data.runnables.ConversationRefreshRunnable;
|
||||||
@ -90,7 +90,7 @@ public class ConversationFragment extends Fragment
|
|||||||
/**
|
/**
|
||||||
* Information about the conversation
|
* Information about the conversation
|
||||||
*/
|
*/
|
||||||
private ConversationsInfo conversationInfo = null;
|
private ConversationInfo conversationInfo = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The last available message id
|
* The last available message id
|
||||||
@ -368,18 +368,18 @@ public class ConversationFragment extends Fragment
|
|||||||
if(conversationInfo == null){
|
if(conversationInfo == null){
|
||||||
|
|
||||||
//Query information about the conversation
|
//Query information about the conversation
|
||||||
new AsyncTask<Void, Void, ConversationsInfo>(){
|
new AsyncTask<Void, Void, ConversationInfo>(){
|
||||||
@Override
|
@Override
|
||||||
protected ConversationsInfo doInBackground(Void... params) {
|
protected ConversationInfo doInBackground(Void... params) {
|
||||||
ConversationsInfo infos = convListHelper.getInfosSingle(conversation_id, true);
|
ConversationInfo infos = convListHelper.getInfoSingle(conversation_id, true);
|
||||||
if(infos != null)
|
if(infos != null)
|
||||||
infos.setDisplayName(convListHelper.getDisplayName(infos));
|
infos.setDisplayName(convListHelper.getConversationDisplayName(infos));
|
||||||
return infos;
|
return infos;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(ConversationsInfo conversationsInfo) {
|
protected void onPostExecute(ConversationInfo conversationInfo) {
|
||||||
onGotConversationInfo(conversationsInfo);
|
onGotConversationInfo(conversationInfo);
|
||||||
}
|
}
|
||||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
}
|
}
|
||||||
@ -509,7 +509,7 @@ public class ConversationFragment extends Fragment
|
|||||||
*
|
*
|
||||||
* @param info Information about the conversation
|
* @param info Information about the conversation
|
||||||
*/
|
*/
|
||||||
private void onGotConversationInfo(ConversationsInfo info){
|
private void onGotConversationInfo(ConversationInfo info){
|
||||||
|
|
||||||
//Check for errors
|
//Check for errors
|
||||||
if(info == null){
|
if(info == null){
|
||||||
|
@ -1,14 +1,11 @@
|
|||||||
package org.communiquons.android.comunic.client.ui.fragments;
|
package org.communiquons.android.comunic.client.ui.fragments;
|
||||||
|
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import android.support.v4.app.Fragment;
|
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.util.ArrayMap;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.view.ContextMenu;
|
import android.view.ContextMenu;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
@ -21,15 +18,14 @@ import android.widget.ProgressBar;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import org.communiquons.android.comunic.client.ui.activities.MainActivity;
|
|
||||||
import org.communiquons.android.comunic.client.R;
|
import org.communiquons.android.comunic.client.R;
|
||||||
import org.communiquons.android.comunic.client.data.utils.AccountUtils;
|
import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper;
|
||||||
import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper;
|
import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper;
|
||||||
import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper;
|
import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper;
|
||||||
import org.communiquons.android.comunic.client.data.models.UserInfo;
|
import org.communiquons.android.comunic.client.data.models.ConversationInfo;
|
||||||
import org.communiquons.android.comunic.client.data.models.ConversationsInfo;
|
import org.communiquons.android.comunic.client.ui.activities.MainActivity;
|
||||||
import org.communiquons.android.comunic.client.ui.adapters.ConversationsListAdapter;
|
import org.communiquons.android.comunic.client.ui.adapters.ConversationsListAdapter;
|
||||||
import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper;
|
import org.communiquons.android.comunic.client.ui.asynctasks.GetConversationsListTask;
|
||||||
import org.communiquons.android.comunic.client.ui.listeners.openConversationListener;
|
import org.communiquons.android.comunic.client.ui.listeners.openConversationListener;
|
||||||
import org.communiquons.android.comunic.client.ui.listeners.updateConversationListener;
|
import org.communiquons.android.comunic.client.ui.listeners.updateConversationListener;
|
||||||
|
|
||||||
@ -44,17 +40,17 @@ import java.util.ArrayList;
|
|||||||
* Created by pierre on 12/6/17.
|
* Created by pierre on 12/6/17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class ConversationsListFragment extends Fragment implements AdapterView.OnItemClickListener {
|
public class ConversationsListFragment extends AbstractFragment implements AdapterView.OnItemClickListener {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Debug tag
|
* Debug tag
|
||||||
*/
|
*/
|
||||||
private String TAG = "ConversationsListFrag";
|
private static final String TAG = ConversationsListFragment.class.getSimpleName();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of conversations
|
* The list of conversations
|
||||||
*/
|
*/
|
||||||
private ArrayList<ConversationsInfo> convList;
|
private ArrayList<ConversationInfo> convList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User information helper
|
* User information helper
|
||||||
@ -94,7 +90,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
|
|||||||
/**
|
/**
|
||||||
* Loading progress bar
|
* Loading progress bar
|
||||||
*/
|
*/
|
||||||
private ProgressBar progressBar;
|
private ProgressBar mProgressBar;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
@ -109,7 +105,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
|
|||||||
//Database helper
|
//Database helper
|
||||||
DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity());
|
DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity());
|
||||||
|
|
||||||
//Instantiate the user informations helper
|
//Instantiate the user information helper
|
||||||
userHelper = new GetUsersHelper(getActivity(), dbHelper);
|
userHelper = new GetUsersHelper(getActivity(), dbHelper);
|
||||||
|
|
||||||
//Create the conversation list helper
|
//Create the conversation list helper
|
||||||
@ -119,7 +115,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
|
|||||||
conversationsListView = view.findViewById(R.id.fragment_conversationslist_list);
|
conversationsListView = view.findViewById(R.id.fragment_conversationslist_list);
|
||||||
|
|
||||||
//Get progress bar wheel
|
//Get progress bar wheel
|
||||||
progressBar = view.findViewById(R.id.fragment_conversationslist_progressbar);
|
mProgressBar = view.findViewById(R.id.fragment_conversationslist_progressbar);
|
||||||
|
|
||||||
//Get no conversation notice
|
//Get no conversation notice
|
||||||
mNoConversationNotice = view.findViewById(R.id.no_conversation_notice);
|
mNoConversationNotice = view.findViewById(R.id.no_conversation_notice);
|
||||||
@ -139,12 +135,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
|
|||||||
|
|
||||||
//Set create conversation button listener
|
//Set create conversation button listener
|
||||||
view.findViewById(R.id.fragment_conversationslist_create)
|
view.findViewById(R.id.fragment_conversationslist_create)
|
||||||
.setOnClickListener(new View.OnClickListener() {
|
.setOnClickListener(v -> updateConversationListener.createConversation());
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
updateConversationListener.createConversation();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -162,120 +153,13 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
|
|||||||
private void refresh_conversations_list(){
|
private void refresh_conversations_list(){
|
||||||
|
|
||||||
//Display loading wheel
|
//Display loading wheel
|
||||||
progressBar.setVisibility(View.VISIBLE);
|
mProgressBar.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
//Get the list of conversations
|
//Get the list of conversations
|
||||||
new AsyncTask<Void, Void, ArrayList<ConversationsInfo>>(){
|
GetConversationsListTask getConversationsListTask = new GetConversationsListTask(getActivity());
|
||||||
@Override
|
getConversationsListTask.setOnPostExecuteListener(this::display_conversations_list);
|
||||||
protected ArrayList<ConversationsInfo> doInBackground(Void... params) {
|
getConversationsListTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
|
getTasksManager().addTask(getConversationsListTask);
|
||||||
//Get the list of conversations
|
|
||||||
ArrayList<ConversationsInfo> list = conversationsListHelper.get();
|
|
||||||
process_conversations_list(list);
|
|
||||||
return list;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPostExecute(ArrayList<ConversationsInfo> list) {
|
|
||||||
if(getActivity() != null)
|
|
||||||
display_conversations_list(list);
|
|
||||||
}
|
|
||||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Process the conversation list
|
|
||||||
*
|
|
||||||
* This method must be called on a separate thread
|
|
||||||
*
|
|
||||||
* @param list The list of conversations
|
|
||||||
*/
|
|
||||||
private void process_conversations_list(ArrayList<ConversationsInfo> list){
|
|
||||||
|
|
||||||
//Check if got the list
|
|
||||||
if(list == null){
|
|
||||||
return; //Nothing to be done
|
|
||||||
}
|
|
||||||
|
|
||||||
//Process the list of conversation
|
|
||||||
ArrayList<Integer> usersToGet = new ArrayList<>();
|
|
||||||
ArrayList<ConversationsInfo> convToUpdateName = new ArrayList<>();
|
|
||||||
for(ConversationsInfo conv : list){
|
|
||||||
|
|
||||||
//Set the displayed names of the conversation
|
|
||||||
if(conv.hasName()){
|
|
||||||
//Use the name of the conversation if available
|
|
||||||
conv.setDisplayName(conv.getName());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
|
|
||||||
//Add the first users of the conversations to the users for which we need info
|
|
||||||
for(int i = 0; i < 2; i++){
|
|
||||||
|
|
||||||
if(conv.getMembers().size() <= i)
|
|
||||||
break;
|
|
||||||
|
|
||||||
usersToGet.add(conv.getMembers().get(i));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
convToUpdateName.add(conv);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Check if we have user to get information about
|
|
||||||
if(usersToGet.size() > 0){
|
|
||||||
|
|
||||||
//Get information about the users
|
|
||||||
ArrayMap<Integer, UserInfo> usersInfo = userHelper.getMultiple(usersToGet);
|
|
||||||
|
|
||||||
//Check for errors
|
|
||||||
if(usersInfo == null){
|
|
||||||
Log.e(TAG, "Couldn't get information about some users !");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Process the conversation that have to be processed
|
|
||||||
for(ConversationsInfo conv : convToUpdateName){
|
|
||||||
|
|
||||||
//Get the name of the first members
|
|
||||||
String conversationName = "";
|
|
||||||
int count = 0;
|
|
||||||
for(int userID : conv.getMembers()){
|
|
||||||
|
|
||||||
//Do not display current user name
|
|
||||||
if(userID == AccountUtils.getID(getActivity()))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if(usersInfo.containsKey(userID)){
|
|
||||||
|
|
||||||
UserInfo userInfo = usersInfo.get(userID);
|
|
||||||
|
|
||||||
if(count > 0){
|
|
||||||
conversationName += ", ";
|
|
||||||
}
|
|
||||||
|
|
||||||
if(userInfo != null){
|
|
||||||
conversationName += userInfo.getFullName();
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if(count == 2)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(conv.getMembers().size() > 3)
|
|
||||||
conversationName += "...";
|
|
||||||
|
|
||||||
//Update the displayed name of the conversation
|
|
||||||
conv.setDisplayName(conversationName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -283,7 +167,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
|
|||||||
*
|
*
|
||||||
* @param list The list to display
|
* @param list The list to display
|
||||||
*/
|
*/
|
||||||
public void display_conversations_list(ArrayList<ConversationsInfo> list){
|
public void display_conversations_list(ArrayList<ConversationInfo> list){
|
||||||
|
|
||||||
//Check if we got a list
|
//Check if we got a list
|
||||||
if(list == null) {
|
if(list == null) {
|
||||||
@ -360,7 +244,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
|
|||||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||||
|
|
||||||
//Get the clicked conversation
|
//Get the clicked conversation
|
||||||
ConversationsInfo conv = convList.get(position);
|
ConversationInfo conv = convList.get(position);
|
||||||
|
|
||||||
//Open the specified conversation
|
//Open the specified conversation
|
||||||
openConvListener.openConversation(conv.getID());
|
openConvListener.openConversation(conv.getID());
|
||||||
@ -373,7 +257,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
|
|||||||
* @param show Set wether the progress bar should be shown or not
|
* @param show Set wether the progress bar should be shown or not
|
||||||
*/
|
*/
|
||||||
private void display_progress_bar(boolean show){
|
private void display_progress_bar(boolean show){
|
||||||
progressBar.setVisibility(show ? View.VISIBLE : View.GONE);
|
mProgressBar.setVisibility(show ? View.VISIBLE : View.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -26,7 +26,7 @@ import org.communiquons.android.comunic.client.R;
|
|||||||
import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper;
|
import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper;
|
||||||
import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper;
|
import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper;
|
||||||
import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper;
|
import org.communiquons.android.comunic.client.data.helpers.GetUsersHelper;
|
||||||
import org.communiquons.android.comunic.client.data.models.ConversationsInfo;
|
import org.communiquons.android.comunic.client.data.models.ConversationInfo;
|
||||||
import org.communiquons.android.comunic.client.data.models.UserInfo;
|
import org.communiquons.android.comunic.client.data.models.UserInfo;
|
||||||
import org.communiquons.android.comunic.client.data.utils.AccountUtils;
|
import org.communiquons.android.comunic.client.data.utils.AccountUtils;
|
||||||
import org.communiquons.android.comunic.client.ui.activities.MainActivity;
|
import org.communiquons.android.comunic.client.ui.activities.MainActivity;
|
||||||
@ -315,16 +315,16 @@ public class UpdateConversationFragment extends Fragment {
|
|||||||
submitButton.setText(R.string.fragment_update_conversation_button_update);
|
submitButton.setText(R.string.fragment_update_conversation_button_update);
|
||||||
|
|
||||||
//Get informations about the conversation
|
//Get informations about the conversation
|
||||||
new AsyncTask<Integer, Void, ConversationsInfo>(){
|
new AsyncTask<Integer, Void, ConversationInfo>(){
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ConversationsInfo doInBackground(Integer... params) {
|
protected ConversationInfo doInBackground(Integer... params) {
|
||||||
return convListHelper.getInfosSingle(params[0], true);
|
return convListHelper.getInfoSingle(params[0], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(ConversationsInfo conversationsInfo) {
|
protected void onPostExecute(ConversationInfo conversationInfo) {
|
||||||
onGotConversationInfos(conversationsInfo);
|
onGotConversationInfos(conversationInfo);
|
||||||
}
|
}
|
||||||
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, conversation_id);
|
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, conversation_id);
|
||||||
}
|
}
|
||||||
@ -336,7 +336,7 @@ public class UpdateConversationFragment extends Fragment {
|
|||||||
*
|
*
|
||||||
* @param infos Informations about a conversation, or null in case of failure
|
* @param infos Informations about a conversation, or null in case of failure
|
||||||
*/
|
*/
|
||||||
private void onGotConversationInfos(@Nullable ConversationsInfo infos){
|
private void onGotConversationInfos(@Nullable ConversationInfo infos){
|
||||||
|
|
||||||
//Check if the activity has been destroyed
|
//Check if the activity has been destroyed
|
||||||
if(getActivity() == null)
|
if(getActivity() == null)
|
||||||
|
Loading…
Reference in New Issue
Block a user