Improved the way conversations fragment get the list of conversations

This commit is contained in:
Pierre HUBERT 2019-03-01 18:10:07 +01:00
parent 8f87b05fcc
commit af5eb4fce2
9 changed files with 178 additions and 223 deletions

View File

@ -210,7 +210,7 @@ public class CallsHelper extends BaseHelper {
//Get call name
String name = new ConversationsListHelper(getContext())
.getConversationName(call.getConversationID());
.getConversationDisplayName(call.getConversationID());
if(name == null)
return null;

View File

@ -7,7 +7,7 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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;
@ -46,7 +46,7 @@ public class ConversationsListDbHelper {
* @param list The new list of conversation
* @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
delete_all();
@ -55,7 +55,7 @@ public class ConversationsListDbHelper {
//Process the list of conversation
boolean success = true;
for(ConversationsInfo info : list){
for(ConversationInfo info : list){
if(!insert(db, info))
success = false;
}
@ -72,7 +72,7 @@ public class ConversationsListDbHelper {
* @return Information about the conversation (if available locally) or null in case of failure
*/
@Nullable
ConversationsInfo getInfos(int convID){
ConversationInfo getInfo(int convID){
SQLiteDatabase db = databaseHelper.getReadableDatabase();
//Prepare database request
@ -92,7 +92,7 @@ public class ConversationsListDbHelper {
//Perform database request
Cursor c = db.query(table, columns, selection, selectionArgs, null, null, null);
ConversationsInfo infos = null;
ConversationInfo infos = null;
//Check for result
if(c.getCount() != 0){
@ -144,7 +144,7 @@ public class ConversationsListDbHelper {
* @param info Informations about the conversation to insert
* @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);
@ -158,7 +158,7 @@ public class ConversationsListDbHelper {
* @param info Information about a conversation
* @return The values of the conservation
*/
private ContentValues getContentValues(ConversationsInfo info){
private ContentValues getContentValues(ConversationInfo info){
ContentValues values = new ContentValues();
values.put(ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID, info.getID());
@ -187,9 +187,9 @@ public class ConversationsListDbHelper {
* @param c The cursor
* @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
infos.setID(c.getInt(c.getColumnIndexOrThrow(

View File

@ -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.utils.AccountUtils;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Objects;
/**
* Operations on the conversation list (helper)
@ -27,7 +28,10 @@ import java.util.ArrayList;
public class ConversationsListHelper {
private String TAG = "ConversationsList";
/**
* Debug tag
*/
private static final String TAG = ConversationsListHelper.class.getSimpleName();
/**
* The context of the application
@ -71,10 +75,10 @@ public class ConversationsListHelper {
* @return The list of conversations
*/
@Nullable
public ArrayList<ConversationsInfo> get(){
public ArrayList<ConversationInfo> getOnline(){
//Download a new list of conversations
ArrayList<ConversationsInfo> list = download();
ArrayList<ConversationInfo> list = download();
if(list != null){
//Save the list
@ -89,24 +93,24 @@ public class ConversationsListHelper {
* Get information about a conversation
*
* @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
* @return Information about the conversation, or false in case of failure
*/
@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
if((infos = convDBHelper.getInfos(convID)) != null)
return infos;
if((info = convDBHelper.getInfo(convID)) != null)
return info;
//Check if we are not allowed to fetch informations online
//Check if we are not allowed to fetch information online
if(!allowDownload)
return null;
//Get informations about the conversation online
//Get information about the conversation online
return downloadSingle(convID);
}
@ -159,57 +163,92 @@ public class ConversationsListHelper {
* @param info Information about a 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
if(info.hasName())
return info.getName();
ArrayList<ConversationInfo> list = new ArrayList<>();
list.add(info);
//Get the list of members of the conversation
ArrayList<Integer> members = info.getMembers();
if(!getConversationsDisplayName(list))
return "";
//Get the ID of the three first members
ArrayList<Integer> membersToGet = new ArrayList<>();
int num = 0;
for(int ID : members){
membersToGet.add(ID);
return info.getDisplayName();
}
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
ArrayMap<Integer, UserInfo> users =
new GetUsersHelper(mContext, dbHelper).getMultiple(membersToGet);
ArrayList<Integer> usersToFetch = new ArrayList<>();
if(users == null)
return ""; //No name by default
for(ConversationInfo c : list){
String name = "";
if(c.hasName())
c.setDisplayName(c.getName());
int count = 0;
for(Integer id : users.keySet()){
else {
//Do not display current user name
if(id == AccountUtils.getID(mContext))
continue;
int count = 0;
for(int userID : c.getMembers()){
if(users.get(id) != null){
if(userID == AccountUtils.getID(mContext))
continue;
if(count > 0)
name += ", ";
if(!usersToFetch.contains(userID))
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
*/
@Nullable
public String getConversationName(int convID){
public String getConversationDisplayName(int convID){
ConversationsInfo info = getInfosSingle(convID, true);
ConversationInfo info = getInfoSingle(convID, true);
if(info == 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){
//Turn the list of members into a string
String members_str = "";
StringBuilder members_str = new StringBuilder();
for(int id : members){
members_str += id + ",";
members_str.append(id).append(",");
}
//Make an API request
APIRequest params = new APIRequest(mContext, "conversations/create");
params.addString("name", name.equals("") ? "false" : name);
params.addString("follow", follow ? "true" : "false");
params.addString("users", members_str);
params.addString("users", members_str.toString());
//Perform the request
try {
@ -352,9 +391,9 @@ public class ConversationsListHelper {
* @return The list of conversations
*/
@Nullable
private ArrayList<ConversationsInfo> download(){
private ArrayList<ConversationInfo> download(){
ArrayList<ConversationsInfo> list = new ArrayList<>();
ArrayList<ConversationInfo> list = new ArrayList<>();
try {
@ -390,7 +429,7 @@ public class ConversationsListHelper {
* @return Informations about the conversation in case of success / null else
*/
@Nullable
private ConversationsInfo downloadSingle(int convID){
private ConversationInfo downloadSingle(int convID){
//Perform an API request
APIRequest params = new APIRequest(mContext,
@ -419,9 +458,9 @@ public class ConversationsListHelper {
* @return Conversation object or null in case of failure
*/
@Nullable
private ConversationsInfo parseConversationJSON(@NonNull JSONObject obj){
private ConversationInfo parseConversationJSON(@NonNull JSONObject obj){
ConversationsInfo info = new ConversationsInfo();
ConversationInfo info = new ConversationInfo();
try {
//Get information about the conversation

View File

@ -14,7 +14,7 @@ import java.util.ArrayList;
* Created by pierre on 12/9/17.
*/
public class ConversationsInfo {
public class ConversationInfo {
/**
* Values of the conversation

View File

@ -11,7 +11,7 @@ import android.widget.ArrayAdapter;
import android.widget.TextView;
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.ui.utils.UiUtils;
@ -26,7 +26,7 @@ import java.util.ArrayList;
* Created by pierre on 12/10/17.
*/
public class ConversationsListAdapter extends ArrayAdapter<ConversationsInfo> {
public class ConversationsListAdapter extends ArrayAdapter<ConversationInfo> {
/**
* Class constructor
@ -34,7 +34,7 @@ public class ConversationsListAdapter extends ArrayAdapter<ConversationsInfo> {
* @param context The context of the application
* @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);
}
@ -58,7 +58,7 @@ public class ConversationsListAdapter extends ArrayAdapter<ConversationsInfo> {
}
//Get information about the conversation
ConversationsInfo infos = getItem(position);
ConversationInfo infos = getItem(position);
//Set the name of the conversation
TextView conversationName = convertView

View File

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

View File

@ -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.GetUsersHelper;
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.UserInfo;
import org.communiquons.android.comunic.client.data.runnables.ConversationRefreshRunnable;
@ -90,7 +90,7 @@ public class ConversationFragment extends Fragment
/**
* Information about the conversation
*/
private ConversationsInfo conversationInfo = null;
private ConversationInfo conversationInfo = null;
/**
* The last available message id
@ -368,18 +368,18 @@ public class ConversationFragment extends Fragment
if(conversationInfo == null){
//Query information about the conversation
new AsyncTask<Void, Void, ConversationsInfo>(){
new AsyncTask<Void, Void, ConversationInfo>(){
@Override
protected ConversationsInfo doInBackground(Void... params) {
ConversationsInfo infos = convListHelper.getInfosSingle(conversation_id, true);
protected ConversationInfo doInBackground(Void... params) {
ConversationInfo infos = convListHelper.getInfoSingle(conversation_id, true);
if(infos != null)
infos.setDisplayName(convListHelper.getDisplayName(infos));
infos.setDisplayName(convListHelper.getConversationDisplayName(infos));
return infos;
}
@Override
protected void onPostExecute(ConversationsInfo conversationsInfo) {
onGotConversationInfo(conversationsInfo);
protected void onPostExecute(ConversationInfo conversationInfo) {
onGotConversationInfo(conversationInfo);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@ -509,7 +509,7 @@ public class ConversationFragment extends Fragment
*
* @param info Information about the conversation
*/
private void onGotConversationInfo(ConversationsInfo info){
private void onGotConversationInfo(ConversationInfo info){
//Check for errors
if(info == null){

View File

@ -1,14 +1,11 @@
package org.communiquons.android.comunic.client.ui.fragments;
import android.app.AlertDialog;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.ArrayMap;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
@ -21,15 +18,14 @@ import android.widget.ProgressBar;
import android.widget.TextView;
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.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.GetUsersHelper;
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.ui.activities.MainActivity;
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.updateConversationListener;
@ -44,17 +40,17 @@ import java.util.ArrayList;
* 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
*/
private String TAG = "ConversationsListFrag";
private static final String TAG = ConversationsListFragment.class.getSimpleName();
/**
* The list of conversations
*/
private ArrayList<ConversationsInfo> convList;
private ArrayList<ConversationInfo> convList;
/**
* User information helper
@ -94,7 +90,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
/**
* Loading progress bar
*/
private ProgressBar progressBar;
private ProgressBar mProgressBar;
@Nullable
@Override
@ -109,7 +105,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
//Database helper
DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity());
//Instantiate the user informations helper
//Instantiate the user information helper
userHelper = new GetUsersHelper(getActivity(), dbHelper);
//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);
//Get progress bar wheel
progressBar = view.findViewById(R.id.fragment_conversationslist_progressbar);
mProgressBar = view.findViewById(R.id.fragment_conversationslist_progressbar);
//Get 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
view.findViewById(R.id.fragment_conversationslist_create)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateConversationListener.createConversation();
}
});
.setOnClickListener(v -> updateConversationListener.createConversation());
}
@Override
@ -162,120 +153,13 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
private void refresh_conversations_list(){
//Display loading wheel
progressBar.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
//Get the list of conversations
new AsyncTask<Void, Void, ArrayList<ConversationsInfo>>(){
@Override
protected ArrayList<ConversationsInfo> doInBackground(Void... params) {
//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);
}
}
GetConversationsListTask getConversationsListTask = new GetConversationsListTask(getActivity());
getConversationsListTask.setOnPostExecuteListener(this::display_conversations_list);
getConversationsListTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
getTasksManager().addTask(getConversationsListTask);
}
/**
@ -283,7 +167,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
*
* @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
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) {
//Get the clicked conversation
ConversationsInfo conv = convList.get(position);
ConversationInfo conv = convList.get(position);
//Open the specified conversation
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
*/
private void display_progress_bar(boolean show){
progressBar.setVisibility(show ? View.VISIBLE : View.GONE);
mProgressBar.setVisibility(show ? View.VISIBLE : View.GONE);
}
/**

View File

@ -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.DatabaseHelper;
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.utils.AccountUtils;
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);
//Get informations about the conversation
new AsyncTask<Integer, Void, ConversationsInfo>(){
new AsyncTask<Integer, Void, ConversationInfo>(){
@Override
protected ConversationsInfo doInBackground(Integer... params) {
return convListHelper.getInfosSingle(params[0], true);
protected ConversationInfo doInBackground(Integer... params) {
return convListHelper.getInfoSingle(params[0], true);
}
@Override
protected void onPostExecute(ConversationsInfo conversationsInfo) {
onGotConversationInfos(conversationsInfo);
protected void onPostExecute(ConversationInfo conversationInfo) {
onGotConversationInfos(conversationInfo);
}
}.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
*/
private void onGotConversationInfos(@Nullable ConversationsInfo infos){
private void onGotConversationInfos(@Nullable ConversationInfo infos){
//Check if the activity has been destroyed
if(getActivity() == null)