mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-27 15:59:29 +00:00
Conversation are cached in the local database
This commit is contained in:
parent
c63f9904a5
commit
d4f586631c
@ -68,7 +68,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
|
||||
ConversationsListSchema.COLUMN_NAME_CONVERSATION_NAME + TEXT_TYPE + COMMA_SEP +
|
||||
ConversationsListSchema.COLUMN_NAME_CONVERSATION_FOLLOWING + INTEGER_TYPE + COMMA_SEP +
|
||||
ConversationsListSchema.COLUMN_NAME_CONVERSATION_SAW_LAST_MESSAGES + INTEGER_TYPE + COMMA_SEP +
|
||||
ConversationsListSchema.COLUMN_NAME_CONVERSATION_MEMBERS + TEXT_TYPE + COMMA_SEP +
|
||||
ConversationsListSchema.COLUMN_NAME_CONVERSATION_MEMBERS + TEXT_TYPE +
|
||||
" )";
|
||||
|
||||
private static final String SQL_DELETE_CONVERSATIONS_LIST_TABLE = "DROP TABLE IF EXISTS " +
|
||||
|
@ -193,7 +193,40 @@ public class ConversationsInfo {
|
||||
return members;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of members as a string
|
||||
*
|
||||
* @return The list of members as a string
|
||||
*/
|
||||
public String getMembersString() {
|
||||
if(members == null)
|
||||
return "";
|
||||
|
||||
String result = "";
|
||||
|
||||
for(int member : members){
|
||||
result += member + ",";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the list of members from a string generated using {@link #getMembersString()}
|
||||
*
|
||||
* @param input The input string
|
||||
*/
|
||||
public void parseMembersString(String input){
|
||||
|
||||
members = new ArrayList<>();
|
||||
|
||||
String[] membersStr = input.split(",");
|
||||
|
||||
for(String member : membersStr){
|
||||
members.add(Integer.decode(member));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,120 @@
|
||||
package org.communiquons.android.comunic.client.data.conversations;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.communiquons.android.comunic.client.data.DatabaseContract.ConversationsListSchema;
|
||||
import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Conversations DB Helper
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
* Created by pierre on 12/13/17.
|
||||
*/
|
||||
|
||||
public class ConversationsListDbHelper {
|
||||
|
||||
/**
|
||||
* Pointer on the database
|
||||
*/
|
||||
private DatabaseHelper databaseHelper;
|
||||
|
||||
/**
|
||||
* Conversations list table name
|
||||
*/
|
||||
private static final String TABLE_NAME = ConversationsListSchema.TABLE_NAME;
|
||||
|
||||
/**
|
||||
* Create the conversation database helper
|
||||
*
|
||||
* @param databaseHelper Object pointing on database helper
|
||||
*/
|
||||
public ConversationsListDbHelper(@NonNull DatabaseHelper databaseHelper){
|
||||
this.databaseHelper = databaseHelper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update the list of conversations currently installed in the system with a new list
|
||||
*
|
||||
* @param list The new list of conversation
|
||||
* @return TRUE for a success / FALSE else
|
||||
*/
|
||||
boolean update_list(ArrayList<ConversationsInfo> list){
|
||||
|
||||
//Remove any old list of conversations
|
||||
delete_all();
|
||||
|
||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||
|
||||
//Process the list of conversation
|
||||
boolean success = true;
|
||||
for(ConversationsInfo info : list){
|
||||
if(!insert(db, info))
|
||||
success = false;
|
||||
}
|
||||
|
||||
db.close();
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all the list of conversations
|
||||
*/
|
||||
private void delete_all(){
|
||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||
|
||||
//Prepare the request
|
||||
db.delete(TABLE_NAME, null, null);
|
||||
|
||||
db.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new conversation in the database
|
||||
* @param db Pointer on active database
|
||||
* @param info Informations about the conversation to insert
|
||||
* @return TRUE for a success / False else
|
||||
*/
|
||||
private boolean insert(@NonNull SQLiteDatabase db, @NonNull ConversationsInfo info){
|
||||
|
||||
ContentValues values = getContentValues(info);
|
||||
|
||||
return -1 == db.insert(TABLE_NAME, null, values);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the contentvalues corresponding to a conversation
|
||||
*
|
||||
* @param info Information about a conversation
|
||||
* @return The values of the conservation
|
||||
*/
|
||||
private ContentValues getContentValues(ConversationsInfo info){
|
||||
ContentValues values = new ContentValues();
|
||||
|
||||
values.put(ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID, info.getID());
|
||||
values.put(ConversationsListSchema.COLUMN_NAME_CONVERSATION_ID_OWNER, info.getID_owner());
|
||||
values.put(ConversationsListSchema.COLUMN_NAME_CONVERSATION_LAST_ACTIVE,
|
||||
info.getLast_active());
|
||||
|
||||
String name = info.hasName() ? info.getName() : "null";
|
||||
values.put(ConversationsListSchema.COLUMN_NAME_CONVERSATION_NAME, name);
|
||||
|
||||
values.put(ConversationsListSchema.COLUMN_NAME_CONVERSATION_FOLLOWING,
|
||||
info.isFollowing() ? 1 : 0);
|
||||
|
||||
values.put(ConversationsListSchema.COLUMN_NAME_CONVERSATION_SAW_LAST_MESSAGES,
|
||||
info.hasSaw_last_message() ? 1 : 0);
|
||||
|
||||
values.put(ConversationsListSchema.COLUMN_NAME_CONVERSATION_MEMBERS,
|
||||
info.getMembersString());
|
||||
|
||||
return values;
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ import android.util.Log;
|
||||
import org.communiquons.android.comunic.client.api.APIRequest;
|
||||
import org.communiquons.android.comunic.client.api.APIRequestParameters;
|
||||
import org.communiquons.android.comunic.client.api.APIResponse;
|
||||
import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
@ -30,13 +31,40 @@ public class ConversationsListHelper {
|
||||
*/
|
||||
private Context mContext;
|
||||
|
||||
/**
|
||||
* Conversations list database helper
|
||||
*/
|
||||
private ConversationsListDbHelper convDBHelper;
|
||||
|
||||
/**
|
||||
* The constructor of the class
|
||||
*
|
||||
* @param context The context of execution of the application
|
||||
* @param dbHelper Database helper
|
||||
*/
|
||||
public ConversationsListHelper(Context context){
|
||||
public ConversationsListHelper(Context context, DatabaseHelper dbHelper){
|
||||
mContext = context;
|
||||
convDBHelper = new ConversationsListDbHelper(dbHelper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of conversation or null in case of failure
|
||||
*
|
||||
* @return The list of conversations
|
||||
*/
|
||||
@Nullable
|
||||
public ArrayList<ConversationsInfo> get(){
|
||||
|
||||
//Download a new list of conversations
|
||||
ArrayList<ConversationsInfo> list = download();
|
||||
|
||||
if(list != null){
|
||||
//Save the list
|
||||
convDBHelper.update_list(list);
|
||||
}
|
||||
|
||||
//Return the list
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -45,7 +73,7 @@ public class ConversationsListHelper {
|
||||
* @return The list of conversations
|
||||
*/
|
||||
@Nullable
|
||||
public ArrayList<ConversationsInfo> download(){
|
||||
private ArrayList<ConversationsInfo> download(){
|
||||
|
||||
ArrayList<ConversationsInfo> list = new ArrayList<>();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user