mirror of
				https://github.com/pierre42100/ComunicAndroid
				synced 2025-11-04 11:34:06 +00:00 
			
		
		
		
	Use cached conversation list to display quickly an old version of it
This commit is contained in:
		@@ -18,7 +18,25 @@ import java.util.ArrayList;
 | 
			
		||||
 * 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
 | 
			
		||||
@@ -35,11 +53,40 @@ public class ConversationsListDbHelper {
 | 
			
		||||
     *
 | 
			
		||||
     * @param databaseHelper Object pointing on database helper
 | 
			
		||||
     */
 | 
			
		||||
    public ConversationsListDbHelper(@NonNull DatabaseHelper databaseHelper){
 | 
			
		||||
    ConversationsListDbHelper(@NonNull 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
 | 
			
		||||
     *
 | 
			
		||||
@@ -60,7 +107,6 @@ public class ConversationsListDbHelper {
 | 
			
		||||
                success = false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //db.close();
 | 
			
		||||
 | 
			
		||||
        return success;
 | 
			
		||||
    }
 | 
			
		||||
@@ -77,35 +123,25 @@ public class ConversationsListDbHelper {
 | 
			
		||||
 | 
			
		||||
        //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);
 | 
			
		||||
        Cursor c = db.query(table, mAllColumns, selection, selectionArgs, null, null, null);
 | 
			
		||||
 | 
			
		||||
        ConversationInfo infos = null;
 | 
			
		||||
        ConversationInfo info = null;
 | 
			
		||||
 | 
			
		||||
        //Check for result
 | 
			
		||||
        if(c.getCount() != 0){
 | 
			
		||||
            c.moveToFirst();
 | 
			
		||||
 | 
			
		||||
            //Parse result
 | 
			
		||||
            infos = getConvObj(c);
 | 
			
		||||
            info = getConvObj(c);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        c.close();
 | 
			
		||||
        //db.close();
 | 
			
		||||
 | 
			
		||||
        return infos;
 | 
			
		||||
        return info;
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -135,7 +171,6 @@ public class ConversationsListDbHelper {
 | 
			
		||||
        //Prepare the request
 | 
			
		||||
        db.delete(TABLE_NAME, null, null);
 | 
			
		||||
 | 
			
		||||
        //db.close();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -85,6 +85,15 @@ public class ConversationsListHelper extends BaseHelper {
 | 
			
		||||
        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
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -12,17 +12,24 @@ import java.util.ArrayList;
 | 
			
		||||
 *
 | 
			
		||||
 * @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) {
 | 
			
		||||
        super(context);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected ArrayList<ConversationInfo> doInBackground(Void... voids) {
 | 
			
		||||
    protected ArrayList<ConversationInfo> doInBackground(Boolean... booleans) {
 | 
			
		||||
        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))
 | 
			
		||||
            return null;
 | 
			
		||||
 
 | 
			
		||||
@@ -30,6 +30,7 @@ import org.communiquons.android.comunic.client.ui.listeners.openConversationList
 | 
			
		||||
import org.communiquons.android.comunic.client.ui.listeners.updateConversationListener;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Conversation list fragment
 | 
			
		||||
@@ -92,6 +93,11 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
 | 
			
		||||
     */
 | 
			
		||||
    private ProgressBar mProgressBar;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Specify whether we got the first version of the list of conversation
 | 
			
		||||
     */
 | 
			
		||||
    private boolean mGotFirstList = false;
 | 
			
		||||
 | 
			
		||||
    @Nullable
 | 
			
		||||
    @Override
 | 
			
		||||
    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());
 | 
			
		||||
 | 
			
		||||
        //Instantiate the user information helper
 | 
			
		||||
        userHelper = new GetUsersHelper(getActivity(), dbHelper);
 | 
			
		||||
        userHelper = new GetUsersHelper(Objects.requireNonNull(getActivity()), dbHelper);
 | 
			
		||||
 | 
			
		||||
        //Create the conversation list helper
 | 
			
		||||
        conversationsListHelper = new ConversationsListHelper(getActivity(), dbHelper);
 | 
			
		||||
@@ -122,7 +128,7 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
 | 
			
		||||
        mNoConversationNotice.setVisibility(View.GONE);
 | 
			
		||||
 | 
			
		||||
        //Refresh conversations list
 | 
			
		||||
        refresh_conversations_list();
 | 
			
		||||
        refresh_conversations_list(false);
 | 
			
		||||
 | 
			
		||||
        //Set the open and update conversation listener
 | 
			
		||||
        try {
 | 
			
		||||
@@ -143,14 +149,14 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
 | 
			
		||||
        super.onResume();
 | 
			
		||||
 | 
			
		||||
        //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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Refresh the list of conversations
 | 
			
		||||
     */
 | 
			
		||||
    private void refresh_conversations_list(){
 | 
			
		||||
    private void refresh_conversations_list(boolean online){
 | 
			
		||||
 | 
			
		||||
        //Display loading wheel
 | 
			
		||||
        mProgressBar.setVisibility(View.VISIBLE);
 | 
			
		||||
@@ -158,7 +164,7 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
 | 
			
		||||
        //Get the list of conversations
 | 
			
		||||
        GetConversationsListTask getConversationsListTask = new GetConversationsListTask(getActivity());
 | 
			
		||||
        getConversationsListTask.setOnPostExecuteListener(this::display_conversations_list);
 | 
			
		||||
        getConversationsListTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
 | 
			
		||||
        getConversationsListTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, online);
 | 
			
		||||
        getTasksManager().addTask(getConversationsListTask);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -167,13 +173,20 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
 | 
			
		||||
     *
 | 
			
		||||
     * @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
 | 
			
		||||
        if(list == null) {
 | 
			
		||||
            Toast.makeText(getActivity(), R.string.fragment_conversationslist_err_get_list,
 | 
			
		||||
                    Toast.LENGTH_LONG).show();
 | 
			
		||||
            display_progress_bar(false);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -189,17 +202,11 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
 | 
			
		||||
        //Add click listener
 | 
			
		||||
        conversationsListView.setOnItemClickListener(this);
 | 
			
		||||
 | 
			
		||||
        conversationsListView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
 | 
			
		||||
                MenuInflater inflater = getActivity().getMenuInflater();
 | 
			
		||||
                inflater.inflate(R.menu.menu_fragment_conversationslist_item, menu);
 | 
			
		||||
            }
 | 
			
		||||
        conversationsListView.setOnCreateContextMenuListener((menu, v, menuInfo) -> {
 | 
			
		||||
            MenuInflater inflater = Objects.requireNonNull(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
 | 
			
		||||
        updateNoConversationNotice();
 | 
			
		||||
    }
 | 
			
		||||
@@ -272,12 +279,8 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
 | 
			
		||||
                .setMessage(R.string.popup_deleteconversation_messsage)
 | 
			
		||||
                .setNegativeButton(R.string.popup_deleteconversation_cancel, null)
 | 
			
		||||
 | 
			
		||||
                .setPositiveButton(R.string.popup_deleteconversation_confirm, new DialogInterface.OnClickListener() {
 | 
			
		||||
                    @Override
 | 
			
		||||
                    public void onClick(DialogInterface dialog, int which) {
 | 
			
		||||
                        delete_conversation(convID);
 | 
			
		||||
                    }
 | 
			
		||||
                })
 | 
			
		||||
                .setPositiveButton(R.string.popup_deleteconversation_confirm,
 | 
			
		||||
                        (dialog, which) -> delete_conversation(convID))
 | 
			
		||||
 | 
			
		||||
                .show();
 | 
			
		||||
 | 
			
		||||
@@ -303,7 +306,7 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt
 | 
			
		||||
                if(getActivity() == null)
 | 
			
		||||
                    return;
 | 
			
		||||
 | 
			
		||||
                refresh_conversations_list();
 | 
			
		||||
                refresh_conversations_list(true);
 | 
			
		||||
 | 
			
		||||
                //Display a toast if an error occurred
 | 
			
		||||
                if(!result)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user