Display conversation names

This commit is contained in:
Pierre 2017-12-29 12:37:11 +01:00
parent 2af59a19ed
commit 491e75e814
13 changed files with 277 additions and 23 deletions

View File

@ -83,7 +83,7 @@ public class MainActivity extends AppCompatActivity
} }
//Initialize DatabaseHelper //Initialize DatabaseHelper
dbHelper = new DatabaseHelper(getApplicationContext()); dbHelper = DatabaseHelper.getInstance(this);
//If it is the first time the application is launched, start the user friends tab //If it is the first time the application is launched, start the user friends tab
if(savedInstanceState == null){ if(savedInstanceState == null){

View File

@ -95,10 +95,21 @@ public class DatabaseHelper extends SQLiteOpenHelper {
/** /**
* Public constructor * Get the current database helper instance
*
* @param context The context
* @return DatabaseHelper object
*/
public static synchronized DatabaseHelper getInstance(Context context){
return new DatabaseHelper(context.getApplicationContext());
}
/**
* Private constructor
*
* @param context The context where the database is used * @param context The context where the database is used
*/ */
public DatabaseHelper(Context context){ private DatabaseHelper(Context context){
super(context, DatabaseContract.DATABASE_NAME, null, DatabaseContract.DATABASE_VERSION); super(context, DatabaseContract.DATABASE_NAME, null, DatabaseContract.DATABASE_VERSION);
} }

View File

@ -88,7 +88,7 @@ class UsersInfosDbHelper {
c.close(); c.close();
//Close the database //Close the database
db.close(); //db.close();
return number_entries > 0; return number_entries > 0;
} }
@ -115,7 +115,7 @@ class UsersInfosDbHelper {
long newRowId = db.insert(UsersInfoSchema.TABLE_NAME, null, newValues); long newRowId = db.insert(UsersInfoSchema.TABLE_NAME, null, newValues);
//Close the database //Close the database
db.close(); //db.close();
return (int) newRowId; return (int) newRowId;
} }
@ -187,7 +187,7 @@ class UsersInfosDbHelper {
c.close(); c.close();
//Close the database //Close the database
db.close(); //db.close();
return result; return result;
} }
@ -211,7 +211,7 @@ class UsersInfosDbHelper {
int result = db.delete(UsersInfoSchema.TABLE_NAME, condition, conditionArgs); int result = db.delete(UsersInfoSchema.TABLE_NAME, condition, conditionArgs);
//Close database //Close database
db.close(); //db.close();
return result > 0; return result > 0;
} }
@ -243,7 +243,7 @@ class UsersInfosDbHelper {
//Perform the request //Perform the request
int result = db.update(UsersInfoSchema.TABLE_NAME, newValues, conditions, conditionArgs); int result = db.update(UsersInfoSchema.TABLE_NAME, newValues, conditions, conditionArgs);
db.close(); //db.close();
return result > 0; return result > 0;
} }

View File

@ -88,7 +88,7 @@ class ConversationMessagesDbHelper {
} }
response.close(); response.close();
db.close(); //db.close();
return message; return message;
} }
@ -114,7 +114,7 @@ class ConversationMessagesDbHelper {
} }
db.close(); //db.close();
return success; return success;
} }
@ -154,7 +154,7 @@ class ConversationMessagesDbHelper {
//Close objects //Close objects
cur.close(); cur.close();
db.close(); //db.close();
return list; return list;
} }

View File

@ -1,6 +1,7 @@
package org.communiquons.android.comunic.client.data.conversations; package org.communiquons.android.comunic.client.data.conversations;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log;
import java.util.ArrayList; import java.util.ArrayList;
@ -91,7 +92,7 @@ public class ConversationsInfo {
public void setName(@Nullable String name) { public void setName(@Nullable String name) {
//Check the validity of the name //Check the validity of the name
if(name == "false" || name == "null" || name == null) if(("false").equals(name) || ("null").equals(name) || name == null)
this.name = null; this.name = null;
else else
this.name = name; this.name = name;

View File

@ -1,8 +1,11 @@
package org.communiquons.android.comunic.client.data.conversations; package org.communiquons.android.comunic.client.data.conversations;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
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.DatabaseHelper; import org.communiquons.android.comunic.client.data.DatabaseHelper;
@ -58,11 +61,55 @@ public class ConversationsListDbHelper {
success = false; success = false;
} }
db.close(); //db.close();
return success; return success;
} }
/**
* Get information about a single conversation
*
* @param convID The conversation ID
* @return Information about the conversation (if available locally) or null in case of failure
*/
@Nullable
ConversationsInfo getInfos(int convID){
SQLiteDatabase db = databaseHelper.getReadableDatabase();
//Prepare database request
String table = ConversationsListSchema.TABLE_NAME;
String[] columns = {
ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID,
ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID_OWNER,
ConversationsListSchema.COLUMN_NAME_CONVERSATION_LAST_ACTIVE,
ConversationsListSchema.COLUMN_NAME_CONVERSATION_NAME,
ConversationsListSchema.COLUMN_NAME_CONVERSATION_FOLLOWING,
ConversationsListSchema.COLUMN_NAME_CONVERSATION_SAW_LAST_MESSAGES,
ConversationsListSchema.COLUMN_NAME_CONVERSATION_MEMBERS,
};
String selection = ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID + " = ?";
String[] selectionArgs = {""+convID};
//Perform database request
Cursor c = db.query(table, columns, selection, selectionArgs, null, null, null);
ConversationsInfo infos = null;
//Check for result
if(c.getCount() != 0){
c.moveToFirst();
//Parse result
infos = getConvObj(c);
}
c.close();
//db.close();
return infos;
}
/** /**
* Delete all the list of conversations * Delete all the list of conversations
*/ */
@ -72,7 +119,7 @@ public class ConversationsListDbHelper {
//Prepare the request //Prepare the request
db.delete(TABLE_NAME, null, null); db.delete(TABLE_NAME, null, null);
db.close(); //db.close();
} }
/** /**
@ -117,4 +164,34 @@ public class ConversationsListDbHelper {
return values; return values;
} }
/**
* Get the conversation object related to a current cursor position
*
* @param c The cursor
* @return The Generated conversation information
*/
private ConversationsInfo getConvObj(Cursor c){
ConversationsInfo infos = new ConversationsInfo();
//Get the values
infos.setID(c.getInt(c.getColumnIndexOrThrow(
ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID)));
infos.setID_owner(c.getInt(c.getColumnIndexOrThrow(
ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID_OWNER)));
infos.setLast_active(c.getInt(c.getColumnIndexOrThrow(
ConversationsListSchema.COLUMN_NAME_CONVERSATION_LAST_ACTIVE)));
infos.setName(c.getString(c.getColumnIndexOrThrow(
ConversationsListSchema.COLUMN_NAME_CONVERSATION_NAME)));
infos.setFollowing(c.getInt(c.getColumnIndexOrThrow(
ConversationsListSchema.COLUMN_NAME_CONVERSATION_FOLLOWING)) == 1);
infos.setSaw_last_message(c.getInt(c.getColumnIndexOrThrow(
ConversationsListSchema.COLUMN_NAME_CONVERSATION_SAW_LAST_MESSAGES)) == 1);
infos.parseMembersString(c.getString(c.getColumnIndexOrThrow(
ConversationsListSchema.COLUMN_NAME_CONVERSATION_MEMBERS)));
return infos;
}
} }

View File

@ -3,12 +3,15 @@ package org.communiquons.android.comunic.client.data.conversations;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull; 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.util.Log;
import org.communiquons.android.comunic.client.api.APIRequest; import org.communiquons.android.comunic.client.api.APIRequest;
import org.communiquons.android.comunic.client.api.APIRequestParameters; import org.communiquons.android.comunic.client.api.APIRequestParameters;
import org.communiquons.android.comunic.client.api.APIResponse; import org.communiquons.android.comunic.client.api.APIResponse;
import org.communiquons.android.comunic.client.data.DatabaseHelper; 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.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -36,6 +39,11 @@ public class ConversationsListHelper {
*/ */
private ConversationsListDbHelper convDBHelper; private ConversationsListDbHelper convDBHelper;
/**
* Database helper
*/
private DatabaseHelper dbHelper;
/** /**
* The constructor of the class * The constructor of the class
* *
@ -45,6 +53,7 @@ public class ConversationsListHelper {
public ConversationsListHelper(Context context, DatabaseHelper dbHelper){ public ConversationsListHelper(Context context, DatabaseHelper dbHelper){
mContext = context; mContext = context;
convDBHelper = new ConversationsListDbHelper(dbHelper); convDBHelper = new ConversationsListDbHelper(dbHelper);
this.dbHelper = dbHelper;
} }
/** /**
@ -67,6 +76,73 @@ public class ConversationsListHelper {
return list; return list;
} }
/**
* Get information about a conversation
*
* @param convID The conversation ID
* @param allowDownload In case the conversation was not found locally, allow informations 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){
ConversationsInfo infos;
//Try to fetch information from the local database
if((infos = convDBHelper.getInfos(convID)) != null)
return infos;
//Check if we are not allowed to fetch informations online
if(!allowDownload)
return null;
//Get informations about the conversation online
return downloadSingle(convID);
}
/**
* Get the display name of a conversation
*
* @param infos Informations about a conversation
* @return The name of the conversation
*/
public String getDisplayName(ConversationsInfo infos){
//Check if a specific name has been specified
if(infos.hasName())
return infos.getName();
//Get the list of members of the conversation
ArrayList<Integer> members = infos.getMembers();
//Get information about the users
ArrayMap<Integer, UserInfo> users =
new GetUsersHelper(mContext, dbHelper).getMultiple(members);
if(users == null)
return ""; //No name by default
String name = "";
int count = 0;
for(Integer id : users.keySet()){
if(users.get(id) != null){
if(count > 0)
name += ", ";
name += users.get(id).getFullName();
count++;
if(count > 3)
break;
}
}
return name;
}
/** /**
* Get online (download) the list of all the conversations * Get online (download) the list of all the conversations
* *
@ -104,6 +180,35 @@ public class ConversationsListHelper {
return list; return list;
} }
/**
* Download informations about a single conversation online
*
* @param convID The ID of the conversation to fetch
* @return Informations about the conversation in case of success / null else
*/
@Nullable
private ConversationsInfo downloadSingle(int convID){
//Perform an API request
APIRequestParameters params = new APIRequestParameters(mContext,
"conversations/getInfosOne");
params.addParameter("conversationID", ""+convID);
try {
APIResponse response = new APIRequest().exec(params);
JSONObject object = response.getJSONObject();
return parseConversationJSON(object);
} catch (Exception e){
e.printStackTrace();
return null;
}
}
/** /**
* Parse a JSONObject into a conversation info element * Parse a JSONObject into a conversation info element
* *

View File

@ -97,7 +97,7 @@ public class FriendsListDbHelper {
c.close(); c.close();
//Close the access to the database //Close the access to the database
db.close(); //db.close();
return friendsList; return friendsList;
} }
@ -123,7 +123,7 @@ public class FriendsListDbHelper {
} }
//Close the database //Close the database
db.close(); //db.close();
return success; return success;
} }
@ -167,7 +167,7 @@ public class FriendsListDbHelper {
int result = db.delete(table_name, whereClause, whereArgs); int result = db.delete(table_name, whereClause, whereArgs);
//Close the database //Close the database
db.close(); //db.close();
return result; return result;
} }
@ -190,7 +190,7 @@ public class FriendsListDbHelper {
int result = db.delete(table_name, whereClause, whereValues); int result = db.delete(table_name, whereClause, whereValues);
//Close access to the database //Close access to the database
db.close(); //db.close();
return result > 0; return result > 0;
} }
@ -216,7 +216,7 @@ public class FriendsListDbHelper {
int result = db.update(table, values, whereClause, whereArgs); int result = db.update(table, values, whereClause, whereArgs);
//Close access to the database //Close access to the database
db.close(); //db.close();
return result > 0; return result > 0;
} }

View File

@ -31,6 +31,8 @@ import org.communiquons.android.comunic.client.data.conversations.ConversationMe
import org.communiquons.android.comunic.client.data.conversations.ConversationMessageAdapter; import org.communiquons.android.comunic.client.data.conversations.ConversationMessageAdapter;
import org.communiquons.android.comunic.client.data.conversations.ConversationMessagesHelper; import org.communiquons.android.comunic.client.data.conversations.ConversationMessagesHelper;
import org.communiquons.android.comunic.client.data.conversations.ConversationRefreshRunnable; import org.communiquons.android.comunic.client.data.conversations.ConversationRefreshRunnable;
import org.communiquons.android.comunic.client.data.conversations.ConversationsInfo;
import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper;
import org.communiquons.android.comunic.client.data.utils.BitmapUtils; import org.communiquons.android.comunic.client.data.utils.BitmapUtils;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -71,6 +73,11 @@ public class ConversationFragment extends Fragment
*/ */
private int conversation_id; private int conversation_id;
/**
* Information about the conversation
*/
private ConversationsInfo conversationInfo = null;
/** /**
* The last available message id * The last available message id
*/ */
@ -101,6 +108,11 @@ public class ConversationFragment extends Fragment
*/ */
private ConversationMessagesHelper convMessHelper; private ConversationMessagesHelper convMessHelper;
/**
* Conversation list helper
*/
private ConversationsListHelper convListHelper;
/** /**
* Conversation messages adapter * Conversation messages adapter
*/ */
@ -141,11 +153,14 @@ public class ConversationFragment extends Fragment
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
//Database helper //Database helper
DatabaseHelper dbHelper = new DatabaseHelper(getActivity()); DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity());
//Set conversation message helper //Set conversation message helper
convMessHelper = new ConversationMessagesHelper(getActivity(), dbHelper); convMessHelper = new ConversationMessagesHelper(getActivity(), dbHelper);
//Set conversation list helper
convListHelper = new ConversationsListHelper(getActivity(), dbHelper);
//Get the conversation ID //Get the conversation ID
conversation_id = getArguments().getInt(ARG_CONVERSATION_ID); conversation_id = getArguments().getInt(ARG_CONVERSATION_ID);
@ -156,6 +171,9 @@ public class ConversationFragment extends Fragment
throw new RuntimeException(TAG + " requires a valid conversation ID when created !"); throw new RuntimeException(TAG + " requires a valid conversation ID when created !");
} }
//Get information about the conversation
} }
@Override @Override
@ -238,9 +256,34 @@ public class ConversationFragment extends Fragment
//Create and start the thread //Create and start the thread
new Thread(refreshRunnable).start(); new Thread(refreshRunnable).start();
//Update conversation title
getActivity().setTitle(R.string.fragement_conversation_title);
//Update the bottom navigation menu //Update the bottom navigation menu
((MainActivity) getActivity()) ((MainActivity) getActivity())
.setSelectedNavigationItem(R.id.main_bottom_navigation_conversations); .setSelectedNavigationItem(R.id.main_bottom_navigation_conversations);
//Check for conversation information
if(conversationInfo == null){
//Query information about the conversation
new AsyncTask<Void, Void, ConversationsInfo>(){
@Override
protected ConversationsInfo doInBackground(Void... params) {
ConversationsInfo infos = convListHelper.getInfosSingle(conversation_id, true);
if(infos != null)
infos.setDisplayName(convListHelper.getDisplayName(infos));
return infos;
}
@Override
protected void onPostExecute(ConversationsInfo conversationsInfo) {
onGotConversationInfos(conversationsInfo);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
else
onGotConversationInfos(conversationInfo);
} }
@Override @Override
@ -319,6 +362,21 @@ public class ConversationFragment extends Fragment
convMessAdapter.notifyDataSetChanged(); convMessAdapter.notifyDataSetChanged();
} }
/**
* What to do once we got conversation informations
*
* @param infos Informations about the conversation
*/
private void onGotConversationInfos(ConversationsInfo infos){
//Save conversation informations
conversationInfo = infos;
//Update the name of the conversation
getActivity().setTitle(infos.getDisplayName());
}
/** /**
* This method is called when the user request to add an image to a message * This method is called when the user request to add an image to a message
*/ */

View File

@ -91,7 +91,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
//Database helper //Database helper
DatabaseHelper dbHelper = new DatabaseHelper(getActivity()); DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity());
//Instantiate the user informations helper //Instantiate the user informations helper
userHelper = new GetUsersHelper(getActivity(), dbHelper); userHelper = new GetUsersHelper(getActivity(), dbHelper);
@ -240,6 +240,7 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O
if(list == null) { if(list == null) {
Toast.makeText(getActivity(), R.string.fragment_conversationslist_err_get_list, Toast.makeText(getActivity(), R.string.fragment_conversationslist_err_get_list,
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
display_progress_bar(false);
return; return;
} }

View File

@ -93,7 +93,7 @@ public class FriendsListFragment extends Fragment {
mContext = getActivity().getApplicationContext(); mContext = getActivity().getApplicationContext();
//Create database helper //Create database helper
mDbHelper = new DatabaseHelper(mContext); mDbHelper = DatabaseHelper.getInstance(mContext);
//Create friendlist operation object //Create friendlist operation object
flist = new FriendsList(mDbHelper, mContext); flist = new FriendsList(mDbHelper, mContext);

View File

@ -57,7 +57,7 @@ public class UserInfosFragment extends Fragment {
//Open DBHelper if required //Open DBHelper if required
if(dbHelper == null) if(dbHelper == null)
dbHelper = new DatabaseHelper(mContext); dbHelper = DatabaseHelper.getInstance(mContext);
//Get required views //Get required views
final ImageView imageView = view.findViewById(R.id.fragments_userinfos_account_image); final ImageView imageView = view.findViewById(R.id.fragments_userinfos_account_image);

View File

@ -65,4 +65,5 @@
<string name="fragment_friendslist_title">Friends</string> <string name="fragment_friendslist_title">Friends</string>
<string name="fragment_conversationslist_title">Conversations</string> <string name="fragment_conversationslist_title">Conversations</string>
<string name="fragment_userinfos_title">About me</string> <string name="fragment_userinfos_title">About me</string>
<string name="fragement_conversation_title">Title</string>
</resources> </resources>