mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 22:09:30 +00:00
Can display older conversation messages
This commit is contained in:
parent
71a58ed337
commit
c672891e3e
@ -92,6 +92,38 @@ class ConversationMessagesDbHelper {
|
|||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the oldest message known of a conversation
|
||||||
|
*
|
||||||
|
* @param convID Target conversation ID
|
||||||
|
* @return The ID of the latest known message / -1 in case of failure
|
||||||
|
*/
|
||||||
|
int getOldestMessageID(int convID) {
|
||||||
|
|
||||||
|
SQLiteDatabase db = dbHelper.getReadableDatabase();
|
||||||
|
|
||||||
|
//Prepare the query over the database
|
||||||
|
String[] columns = {ConversationsMessagesSchema.COLUMN_NAME_MESSAGE_ID};
|
||||||
|
String selection = ConversationsMessagesSchema.COLUMN_NAME_CONVERSATION_ID + " = ?";
|
||||||
|
String[] selectionArgs = {""+convID};
|
||||||
|
String orderBy = ConversationsMessagesSchema.COLUMN_NAME_MESSAGE_ID;
|
||||||
|
|
||||||
|
//Perform the request
|
||||||
|
Cursor response = db.query(TABLE_NAME, columns, selection, selectionArgs, null,
|
||||||
|
null, orderBy, "1");
|
||||||
|
|
||||||
|
//Process response
|
||||||
|
int messageID = 0;
|
||||||
|
if(response.getCount() != 0){
|
||||||
|
response.moveToFirst();
|
||||||
|
messageID = response.getInt(response.getColumnIndexOrThrow(
|
||||||
|
ConversationsMessagesSchema.COLUMN_NAME_MESSAGE_ID));
|
||||||
|
}
|
||||||
|
response.close();
|
||||||
|
|
||||||
|
return messageID;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert a list of messages into the database
|
* Insert a list of messages into the database
|
||||||
*
|
*
|
||||||
|
@ -138,6 +138,39 @@ public class ConversationMessagesHelper {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request the request of older messages from the database / API
|
||||||
|
*
|
||||||
|
* @param convID Target conversationID
|
||||||
|
* @param oldestMessageID The ID of the oldest known message
|
||||||
|
* @return The list of messages / null in case of failure
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public ArrayList<ConversationMessage> getOlderMessages(int convID, int oldestMessageID){
|
||||||
|
|
||||||
|
//Check what is the oldest message stored in the database
|
||||||
|
int oldestMessageDownloaded = mDbHelper.getOldestMessageID(convID);
|
||||||
|
|
||||||
|
//Check if no message has been already download
|
||||||
|
if(oldestMessageDownloaded == 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
//Download older messages from the server
|
||||||
|
ArrayList<ConversationMessage> messages =
|
||||||
|
downloadOlder(convID, oldestMessageDownloaded, 10);
|
||||||
|
|
||||||
|
//Check for errors
|
||||||
|
if(messages == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
//Save the new messages
|
||||||
|
if(!mDbHelper.insertMultiple(messages))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
//Get the messages
|
||||||
|
return mDbHelper.getInterval(convID, 0, oldestMessageID - 1);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the ID of the last message of the conversation from the database
|
* Get the ID of the last message of the conversation from the database
|
||||||
*
|
*
|
||||||
@ -200,6 +233,49 @@ public class ConversationMessagesHelper {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get older messages for a conversation
|
||||||
|
*
|
||||||
|
* @param conversationID The ID of the target conversation
|
||||||
|
* @param oldestMessageID The ID of the oldest known message
|
||||||
|
* @param limit The limit for the download
|
||||||
|
* @return The list of message / null in case of failure
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
private ArrayList<ConversationMessage> downloadOlder(int conversationID, int oldestMessageID,
|
||||||
|
int limit){
|
||||||
|
|
||||||
|
ArrayList<ConversationMessage> list = new ArrayList<>();
|
||||||
|
|
||||||
|
//Prepare a request over the server
|
||||||
|
APIRequest req = new APIRequest(mContext, "conversations/get_older_messages");
|
||||||
|
req.addInt("conversationID", conversationID);
|
||||||
|
req.addInt("oldest_message_id", oldestMessageID);
|
||||||
|
req.addInt("limit", limit);
|
||||||
|
|
||||||
|
//Perform the request
|
||||||
|
try {
|
||||||
|
|
||||||
|
APIResponse response = new APIRequestHelper().exec(req);
|
||||||
|
|
||||||
|
//Check for errors
|
||||||
|
if(response.getResponse_code() != 200)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
//Process the list of messages
|
||||||
|
JSONArray messages = response.getJSONArray();
|
||||||
|
|
||||||
|
for(int i = 0; i < messages.length(); i++)
|
||||||
|
list.add(getMessageObject(conversationID, messages.getJSONObject(i)));
|
||||||
|
|
||||||
|
} catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a JSON object into a conversation message element
|
* Convert a JSON object into a conversation message element
|
||||||
*
|
*
|
||||||
|
@ -22,7 +22,8 @@ public class PreferencesUtils {
|
|||||||
* @return The preference value (if found) or the default value
|
* @return The preference value (if found) or the default value
|
||||||
*/
|
*/
|
||||||
public static boolean getBoolean(Context context, String key, boolean def){
|
public static boolean getBoolean(Context context, String key, boolean def){
|
||||||
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(
|
||||||
|
context.getApplicationContext());
|
||||||
return sharedPrefs.getBoolean(key, def);
|
return sharedPrefs.getBoolean(key, def);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ import android.app.Fragment;
|
|||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@ -17,10 +16,8 @@ import android.view.KeyEvent;
|
|||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.view.inputmethod.EditorInfo;
|
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.ImageButton;
|
import android.widget.ImageButton;
|
||||||
import android.widget.ListView;
|
|
||||||
import android.widget.ProgressBar;
|
import android.widget.ProgressBar;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
@ -38,7 +35,9 @@ import org.communiquons.android.comunic.client.data.utils.AccountUtils;
|
|||||||
import org.communiquons.android.comunic.client.data.utils.FilesUtils;
|
import org.communiquons.android.comunic.client.data.utils.FilesUtils;
|
||||||
import org.communiquons.android.comunic.client.ui.activities.MainActivity;
|
import org.communiquons.android.comunic.client.ui.activities.MainActivity;
|
||||||
import org.communiquons.android.comunic.client.ui.adapters.ConversationMessageAdapter;
|
import org.communiquons.android.comunic.client.ui.adapters.ConversationMessageAdapter;
|
||||||
|
import org.communiquons.android.comunic.client.ui.listeners.OnScrollChangeDetectListener;
|
||||||
import org.communiquons.android.comunic.client.ui.utils.BitmapUtils;
|
import org.communiquons.android.comunic.client.ui.utils.BitmapUtils;
|
||||||
|
import org.communiquons.android.comunic.client.ui.views.ScrollListView;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@ -57,7 +56,8 @@ import static android.app.Activity.RESULT_OK;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public class ConversationFragment extends Fragment
|
public class ConversationFragment extends Fragment
|
||||||
implements ConversationRefreshRunnable.onMessagesChangeListener {
|
implements ConversationRefreshRunnable.onMessagesChangeListener,
|
||||||
|
OnScrollChangeDetectListener {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pick image request number
|
* Pick image request number
|
||||||
@ -115,9 +115,9 @@ public class ConversationFragment extends Fragment
|
|||||||
private TextView no_msg_notice;
|
private TextView no_msg_notice;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converstion message listView
|
* Conversation message listView
|
||||||
*/
|
*/
|
||||||
private ListView convMessListView;
|
private ScrollListView convMessListView;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conversation messages helper
|
* Conversation messages helper
|
||||||
@ -164,6 +164,11 @@ public class ConversationFragment extends Fragment
|
|||||||
*/
|
*/
|
||||||
private GetUsersHelper getUsersHelper;
|
private GetUsersHelper getUsersHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Async task used to fetch older messages
|
||||||
|
*/
|
||||||
|
private AsyncTask mGetOlderMessagesTask;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@ -280,6 +285,10 @@ public class ConversationFragment extends Fragment
|
|||||||
|
|
||||||
//Hide new message sending wheel
|
//Hide new message sending wheel
|
||||||
new_message_progress_bar.setVisibility(View.GONE);
|
new_message_progress_bar.setVisibility(View.GONE);
|
||||||
|
|
||||||
|
|
||||||
|
//Set a listener to detect when the user reaches the top of the conversation
|
||||||
|
convMessListView.setOnScrollChangeDetectListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -607,4 +616,65 @@ public class ConversationFragment extends Fragment
|
|||||||
private void display_not_msg_notice(boolean visible){
|
private void display_not_msg_notice(boolean visible){
|
||||||
no_msg_notice.setVisibility(visible ? View.VISIBLE : View.GONE);
|
no_msg_notice.setVisibility(visible ? View.VISIBLE : View.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called when the user reach the top of the conversation
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onReachTop() {
|
||||||
|
|
||||||
|
//Check if we have got other messages to get
|
||||||
|
if(messagesList == null)
|
||||||
|
return;
|
||||||
|
if(messagesList.size() == 0)
|
||||||
|
return;
|
||||||
|
final int firstMessageID = messagesList.get(0).getId();
|
||||||
|
|
||||||
|
//Check if another task is running
|
||||||
|
if(mGetOlderMessagesTask != null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
//Create and execute the task
|
||||||
|
mGetOlderMessagesTask = new AsyncTask<Void, Void, ArrayList<ConversationMessage>>(){
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ArrayList<ConversationMessage> doInBackground(Void... params) {
|
||||||
|
return convMessHelper.getOlderMessages(conversation_id, firstMessageID);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(ArrayList<ConversationMessage> conversationMessages) {
|
||||||
|
if(getActivity() == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
onGotOlderMessages(conversationMessages);
|
||||||
|
|
||||||
|
}
|
||||||
|
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Actions to do once we downloaded older messages from the server
|
||||||
|
*
|
||||||
|
* @param list The list of messages that was downloaded
|
||||||
|
*/
|
||||||
|
private void onGotOlderMessages(@Nullable ArrayList<ConversationMessage> list){
|
||||||
|
|
||||||
|
//Remove link over task
|
||||||
|
mGetOlderMessagesTask = null;
|
||||||
|
|
||||||
|
//Check if the list is null (in case of error)
|
||||||
|
if(list == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(list.size() == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
//Add the messages to the list
|
||||||
|
messagesList.addAll(0, list);
|
||||||
|
|
||||||
|
//Notify adapter
|
||||||
|
convMessAdapter.notifyDataSetChanged();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user