#915 comment - cache status in db
This commit is contained in:
parent
feff53ccb0
commit
6350841ccb
|
@ -193,7 +193,9 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
|
|||
return null;
|
||||
switch (action){
|
||||
case HOME:
|
||||
|
||||
apiResponse = api.getHomeTimeline(max_id);
|
||||
|
||||
break;
|
||||
case LOCAL:
|
||||
apiResponse = api.getPublicTimeline(true, max_id);
|
||||
|
|
|
@ -78,6 +78,7 @@ import fr.gouv.etalab.mastodon.fragments.DisplayNotificationsFragment;
|
|||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
|
||||
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||
import fr.gouv.etalab.mastodon.sqlite.TimelineCacheDAO;
|
||||
|
||||
import static fr.gouv.etalab.mastodon.client.API.StatusAction.REFRESHPOLL;
|
||||
|
||||
|
@ -977,36 +978,7 @@ public class API {
|
|||
String response = httpsConnection.get(getAbsoluteUrl("/timelines/home"), 60, params, prefKeyOauthTokenT);
|
||||
apiResponse.setSince_id(httpsConnection.getSince_id());
|
||||
apiResponse.setMax_id(httpsConnection.getMax_id());
|
||||
statuses = parseStatuses(context, new JSONArray(response));
|
||||
/*if( response != null) {
|
||||
Thread thread = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
List<Status> statuses;
|
||||
statuses = API.parseStatuses(context, new JSONArray(response));
|
||||
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
|
||||
List<Status> alreadyCached = new TimelineCacheDAO(context, db).getAllStatus(TimelineCacheDAO.HOME_TIMELINE);
|
||||
ArrayList<String> cachedId = new ArrayList<>();
|
||||
if(alreadyCached != null){
|
||||
for(Status status: alreadyCached){
|
||||
cachedId.add(status.getId());
|
||||
}
|
||||
}
|
||||
for(Status status: statuses){
|
||||
if(!cachedId.contains(status.getId())){
|
||||
new TimelineCacheDAO(context, db).insertStatus(TimelineCacheDAO.HOME_TIMELINE, status, prefKeyOauthTokenT);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
thread.start();
|
||||
}*/
|
||||
statuses = parseStatuses(context, new JSONArray(response), instance, true);
|
||||
} catch (HttpsConnection.HttpsConnectionException e) {
|
||||
setError(e.getStatusCode(), e);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
|
@ -3750,6 +3722,38 @@ public class API {
|
|||
return schedules;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parse json response for several status
|
||||
* @param jsonArray JSONArray
|
||||
* @return List<Status>
|
||||
*/
|
||||
private static List<Status> parseStatuses(Context context, JSONArray jsonArray, String instance, boolean cached){
|
||||
|
||||
List<Status> statuses = new ArrayList<>();
|
||||
try {
|
||||
int i = 0;
|
||||
while (i < jsonArray.length() ){
|
||||
|
||||
JSONObject resobj = jsonArray.getJSONObject(i);
|
||||
Status status = parseStatuses(context, resobj);
|
||||
if( cached) {
|
||||
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
Status alreadyCached = new TimelineCacheDAO(context, db).getSingle(instance, status.getId());
|
||||
if (alreadyCached == null) {
|
||||
new TimelineCacheDAO(context, db).insert(status.getId(), instance, resobj.toString());
|
||||
}
|
||||
}
|
||||
i++;
|
||||
statuses.add(status);
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return statuses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse json response for several status
|
||||
* @param jsonArray JSONArray
|
||||
|
|
|
@ -106,7 +106,40 @@ public class TimelineCacheDAO {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns one cached Statuses
|
||||
* @return stored Status List<Status>
|
||||
*/
|
||||
public Status getSingle(String instance, String statusId){
|
||||
try {
|
||||
Cursor c = db.query(Sqlite.TABLE_TIMELINE_CACHE, null, Sqlite.COL_INSTANCE + " = \"" + instance + "\" AND " + Sqlite.COL_STATUS_ID + " = " + statusId, null, null, null, null, "1");
|
||||
return cursorToSingleStatus(c);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* Method to hydrate one cached status from database
|
||||
* @param c Cursor
|
||||
* @return Status
|
||||
*/
|
||||
private Status cursorToSingleStatus(Cursor c){
|
||||
//No element found
|
||||
if (c.getCount() == 0)
|
||||
return null;
|
||||
c.moveToFirst();
|
||||
Status status = null;
|
||||
try {
|
||||
status = API.parseStatuses(context, new JSONObject(c.getString(c.getColumnIndex(Sqlite.COL_CACHE))));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//Close the cursor
|
||||
c.close();
|
||||
//Statuses list is returned
|
||||
return status;
|
||||
}
|
||||
|
||||
/***
|
||||
* Method to hydrate cached statuses from database
|
||||
|
|
Loading…
Reference in New Issue