Use cached conversation list to display quickly an old version of it

This commit is contained in:
Pierre HUBERT 2019-03-01 18:42:36 +01:00
parent 038f2664f1
commit 87f5164d9b
4 changed files with 98 additions and 44 deletions

View File

@ -18,7 +18,25 @@ import java.util.ArrayList;
* Created by pierre on 12/13/17. * Created by pierre on 12/13/17.
*/ */
public class ConversationsListDbHelper { class ConversationsListDbHelper {
/**
* Debug tag
*/
private static final String TAG = ConversationsListDbHelper.class.getSimpleName();
/**
* The list of all the columns of the database
*/
private static final String[] mAllColumns = {
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,
};
/** /**
* Pointer on the database * Pointer on the database
@ -35,11 +53,40 @@ public class ConversationsListDbHelper {
* *
* @param databaseHelper Object pointing on database helper * @param databaseHelper Object pointing on database helper
*/ */
public ConversationsListDbHelper(@NonNull DatabaseHelper databaseHelper){ ConversationsListDbHelper(@NonNull DatabaseHelper databaseHelper){
this.databaseHelper = databaseHelper; this.databaseHelper = databaseHelper;
} }
/**
* Get the list of conversation stored in the database
*
* @return The list of conversations
*/
ArrayList<ConversationInfo> getList(){
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor c = db.query(TABLE_NAME, mAllColumns, null, null, null,
null,
ConversationsListSchema.COLUMN_NAME_CONVERSATION_LAST_ACTIVE + " DESC");
ArrayList<ConversationInfo> list = new ArrayList<>();
if(c.getCount() < 1)
return list;
c.moveToFirst();
while(!c.isAfterLast()){
list.add(getConvObj(c));
c.moveToNext();
}
c.close();
return list;
}
/** /**
* Update the list of conversations currently installed in the system with a new list * Update the list of conversations currently installed in the system with a new list
* *
@ -60,7 +107,6 @@ public class ConversationsListDbHelper {
success = false; success = false;
} }
//db.close();
return success; return success;
} }
@ -77,35 +123,25 @@ public class ConversationsListDbHelper {
//Prepare database request //Prepare database request
String table = ConversationsListSchema.TABLE_NAME; 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 selection = ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID + " = ?";
String[] selectionArgs = {""+convID}; String[] selectionArgs = {""+convID};
//Perform database request //Perform database request
Cursor c = db.query(table, columns, selection, selectionArgs, null, null, null); Cursor c = db.query(table, mAllColumns, selection, selectionArgs, null, null, null);
ConversationInfo infos = null; ConversationInfo info = null;
//Check for result //Check for result
if(c.getCount() != 0){ if(c.getCount() != 0){
c.moveToFirst(); c.moveToFirst();
//Parse result //Parse result
infos = getConvObj(c); info = getConvObj(c);
} }
c.close(); c.close();
//db.close();
return infos; return info;
} }
@ -135,7 +171,6 @@ public class ConversationsListDbHelper {
//Prepare the request //Prepare the request
db.delete(TABLE_NAME, null, null); db.delete(TABLE_NAME, null, null);
//db.close();
} }
/** /**

View File

@ -85,6 +85,15 @@ public class ConversationsListHelper extends BaseHelper {
return list; return list;
} }
/**
* Get the cached version of the list of conversation
*
* @return The cached list of conversations
*/
public ArrayList<ConversationInfo> getCachedList(){
return convDBHelper.getList();
}
/** /**
* Get information about a conversation * Get information about a conversation
* *

View File

@ -12,17 +12,24 @@ import java.util.ArrayList;
* *
* @author Pierre HUBERT * @author Pierre HUBERT
*/ */
public class GetConversationsListTask extends SafeAsyncTask<Void, Void, ArrayList<ConversationInfo>> { public class GetConversationsListTask extends SafeAsyncTask<Boolean, Void, ArrayList<ConversationInfo>> {
public GetConversationsListTask(Context context) { public GetConversationsListTask(Context context) {
super(context); super(context);
} }
@Override @Override
protected ArrayList<ConversationInfo> doInBackground(Void... voids) { protected ArrayList<ConversationInfo> doInBackground(Boolean... booleans) {
ConversationsListHelper conversationsListHelper = new ConversationsListHelper(getContext()); ConversationsListHelper conversationsListHelper = new ConversationsListHelper(getContext());
ArrayList<ConversationInfo> list = conversationsListHelper.getOnline(); boolean getOnline = booleans[0];
ArrayList<ConversationInfo> list;
if(getOnline)
list = conversationsListHelper.getOnline();
else
list = conversationsListHelper.getCachedList();
if(list == null || !conversationsListHelper.getConversationsDisplayName(list)) if(list == null || !conversationsListHelper.getConversationsDisplayName(list))
return null; return null;

View File

@ -30,6 +30,7 @@ import org.communiquons.android.comunic.client.ui.listeners.openConversationList
import org.communiquons.android.comunic.client.ui.listeners.updateConversationListener; import org.communiquons.android.comunic.client.ui.listeners.updateConversationListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects;
/** /**
* Conversation list fragment * Conversation list fragment
@ -92,6 +93,11 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
*/ */
private ProgressBar mProgressBar; private ProgressBar mProgressBar;
/**
* Specify whether we got the first version of the list of conversation
*/
private boolean mGotFirstList = false;
@Nullable @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
@ -106,7 +112,7 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity()); DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity());
//Instantiate the user information helper //Instantiate the user information helper
userHelper = new GetUsersHelper(getActivity(), dbHelper); userHelper = new GetUsersHelper(Objects.requireNonNull(getActivity()), dbHelper);
//Create the conversation list helper //Create the conversation list helper
conversationsListHelper = new ConversationsListHelper(getActivity(), dbHelper); conversationsListHelper = new ConversationsListHelper(getActivity(), dbHelper);
@ -122,7 +128,7 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
mNoConversationNotice.setVisibility(View.GONE); mNoConversationNotice.setVisibility(View.GONE);
//Refresh conversations list //Refresh conversations list
refresh_conversations_list(); refresh_conversations_list(false);
//Set the open and update conversation listener //Set the open and update conversation listener
try { try {
@ -143,14 +149,14 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
super.onResume(); super.onResume();
//Update activity title //Update activity title
getActivity().setTitle(R.string.fragment_conversationslist_title); Objects.requireNonNull(getActivity()).setTitle(R.string.fragment_conversationslist_title);
MainActivity.SetNavbarSelectedOption(getActivity(), R.id.action_conversations); MainActivity.SetNavbarSelectedOption(getActivity(), R.id.action_conversations);
} }
/** /**
* Refresh the list of conversations * Refresh the list of conversations
*/ */
private void refresh_conversations_list(){ private void refresh_conversations_list(boolean online){
//Display loading wheel //Display loading wheel
mProgressBar.setVisibility(View.VISIBLE); mProgressBar.setVisibility(View.VISIBLE);
@ -158,7 +164,7 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
//Get the list of conversations //Get the list of conversations
GetConversationsListTask getConversationsListTask = new GetConversationsListTask(getActivity()); GetConversationsListTask getConversationsListTask = new GetConversationsListTask(getActivity());
getConversationsListTask.setOnPostExecuteListener(this::display_conversations_list); getConversationsListTask.setOnPostExecuteListener(this::display_conversations_list);
getConversationsListTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); getConversationsListTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, online);
getTasksManager().addTask(getConversationsListTask); getTasksManager().addTask(getConversationsListTask);
} }
@ -167,13 +173,20 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
* *
* @param list The list to display * @param list The list to display
*/ */
public void display_conversations_list(ArrayList<ConversationInfo> list){ public void display_conversations_list(@Nullable ArrayList<ConversationInfo> list){
display_progress_bar(false);
//Check if we were fetching the local list of conversation
if(!mGotFirstList) {
mGotFirstList = true;
refresh_conversations_list(true);
}
//Check if we got a list //Check if we got a list
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;
} }
@ -189,17 +202,11 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
//Add click listener //Add click listener
conversationsListView.setOnItemClickListener(this); conversationsListView.setOnItemClickListener(this);
conversationsListView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { conversationsListView.setOnCreateContextMenuListener((menu, v, menuInfo) -> {
@Override MenuInflater inflater = Objects.requireNonNull(getActivity()).getMenuInflater();
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { inflater.inflate(R.menu.menu_fragment_conversationslist_item, menu);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_fragment_conversationslist_item, menu);
}
}); });
//Remove progress bar
display_progress_bar(false);
//Update the visibility of the no conversation notice //Update the visibility of the no conversation notice
updateNoConversationNotice(); updateNoConversationNotice();
} }
@ -272,12 +279,8 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
.setMessage(R.string.popup_deleteconversation_messsage) .setMessage(R.string.popup_deleteconversation_messsage)
.setNegativeButton(R.string.popup_deleteconversation_cancel, null) .setNegativeButton(R.string.popup_deleteconversation_cancel, null)
.setPositiveButton(R.string.popup_deleteconversation_confirm, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.popup_deleteconversation_confirm,
@Override (dialog, which) -> delete_conversation(convID))
public void onClick(DialogInterface dialog, int which) {
delete_conversation(convID);
}
})
.show(); .show();
@ -303,7 +306,7 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
if(getActivity() == null) if(getActivity() == null)
return; return;
refresh_conversations_list(); refresh_conversations_list(true);
//Display a toast if an error occurred //Display a toast if an error occurred
if(!result) if(!result)