forked from mirrors/Fedilab
Fix db calls
This commit is contained in:
parent
020d218df9
commit
59472e7f67
3 changed files with 87 additions and 187 deletions
|
@ -24,18 +24,13 @@ import com.google.gson.annotations.SerializedName;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import app.fedilab.android.activities.MainActivity;
|
import app.fedilab.android.activities.MainActivity;
|
||||||
import app.fedilab.android.client.entities.api.Conversation;
|
import app.fedilab.android.client.entities.api.Conversation;
|
||||||
import app.fedilab.android.client.entities.api.Conversations;
|
|
||||||
import app.fedilab.android.client.entities.api.Notification;
|
import app.fedilab.android.client.entities.api.Notification;
|
||||||
import app.fedilab.android.client.entities.api.Notifications;
|
|
||||||
import app.fedilab.android.client.entities.api.Pagination;
|
|
||||||
import app.fedilab.android.client.entities.api.Status;
|
import app.fedilab.android.client.entities.api.Status;
|
||||||
import app.fedilab.android.client.entities.api.Statuses;
|
|
||||||
import app.fedilab.android.exception.DBException;
|
import app.fedilab.android.exception.DBException;
|
||||||
import app.fedilab.android.helper.Helper;
|
import app.fedilab.android.helper.Helper;
|
||||||
import app.fedilab.android.helper.MastodonHelper;
|
import app.fedilab.android.helper.MastodonHelper;
|
||||||
|
@ -266,36 +261,21 @@ public class StatusCache {
|
||||||
if (db == null) {
|
if (db == null) {
|
||||||
throw new DBException("db is null. Wrong initialization.");
|
throw new DBException("db is null. Wrong initialization.");
|
||||||
}
|
}
|
||||||
Cursor mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_STATUS_CACHE
|
String query = "select count(*) from " + Sqlite.TABLE_STATUS_CACHE
|
||||||
+ " where " + Sqlite.COL_STATUS_ID + " = '" + statusCache.status_id + "'"
|
+ " where " + Sqlite.COL_STATUS_ID + " = '" + statusCache.status_id + "'"
|
||||||
+ " AND " + Sqlite.COL_INSTANCE + " = '" + statusCache.instance + "'"
|
+ " AND " + Sqlite.COL_INSTANCE + " = '" + statusCache.instance + "'"
|
||||||
+ " AND " + Sqlite.COL_USER_ID + "= '" + statusCache.user_id + "'", null);
|
+ " AND " + Sqlite.COL_USER_ID + "= '" + statusCache.user_id + "'";
|
||||||
|
if (statusCache.type != null) {
|
||||||
|
query += " AND " + Sqlite.COL_TYPE + " = '" + statusCache.type.getValue() + "'";
|
||||||
|
}
|
||||||
|
|
||||||
|
Cursor mCount = db.rawQuery(query, null);
|
||||||
mCount.moveToFirst();
|
mCount.moveToFirst();
|
||||||
int count = mCount.getInt(0);
|
int count = mCount.getInt(0);
|
||||||
mCount.close();
|
mCount.close();
|
||||||
return (count > 0);
|
return (count > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a status exists in db
|
|
||||||
*
|
|
||||||
* @param status Status {@link Status}
|
|
||||||
* @return boolean - StatusCache exists
|
|
||||||
* @throws DBException Exception
|
|
||||||
*/
|
|
||||||
public boolean statusExist(Status status) throws DBException {
|
|
||||||
if (db == null) {
|
|
||||||
throw new DBException("db is null. Wrong initialization.");
|
|
||||||
}
|
|
||||||
Cursor mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_STATUS_CACHE
|
|
||||||
+ " where " + Sqlite.COL_STATUS_ID + " = '" + status.id + "'"
|
|
||||||
+ " AND " + Sqlite.COL_INSTANCE + " = '" + MainActivity.currentInstance + "'"
|
|
||||||
+ " AND " + Sqlite.COL_USER_ID + "= '" + MainActivity.currentUserID + "'", null);
|
|
||||||
mCount.moveToFirst();
|
|
||||||
int count = mCount.getInt(0);
|
|
||||||
mCount.close();
|
|
||||||
return (count > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert a status in db
|
* Insert a status in db
|
||||||
|
@ -313,7 +293,9 @@ public class StatusCache {
|
||||||
values.put(Sqlite.COL_INSTANCE, statusCache.instance);
|
values.put(Sqlite.COL_INSTANCE, statusCache.instance);
|
||||||
values.put(Sqlite.COL_SLUG, slug);
|
values.put(Sqlite.COL_SLUG, slug);
|
||||||
values.put(Sqlite.COL_STATUS_ID, statusCache.status_id);
|
values.put(Sqlite.COL_STATUS_ID, statusCache.status_id);
|
||||||
values.put(Sqlite.COL_TYPE, statusCache.type.getValue());
|
if (type != null) {
|
||||||
|
values.put(Sqlite.COL_TYPE, statusCache.type.getValue());
|
||||||
|
}
|
||||||
if (statusCache.status != null) {
|
if (statusCache.status != null) {
|
||||||
values.put(Sqlite.COL_STATUS, mastodonStatusToStringStorage(statusCache.status));
|
values.put(Sqlite.COL_STATUS, mastodonStatusToStringStorage(statusCache.status));
|
||||||
}
|
}
|
||||||
|
@ -345,8 +327,6 @@ public class StatusCache {
|
||||||
throw new DBException("db is null. Wrong initialization.");
|
throw new DBException("db is null. Wrong initialization.");
|
||||||
}
|
}
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put(Sqlite.COL_USER_ID, statusCache.user_id);
|
|
||||||
values.put(Sqlite.COL_STATUS_ID, statusCache.status_id);
|
|
||||||
if (statusCache.status != null) {
|
if (statusCache.status != null) {
|
||||||
values.put(Sqlite.COL_STATUS, mastodonStatusToStringStorage(statusCache.status));
|
values.put(Sqlite.COL_STATUS, mastodonStatusToStringStorage(statusCache.status));
|
||||||
}
|
}
|
||||||
|
@ -360,8 +340,8 @@ public class StatusCache {
|
||||||
//Inserts token
|
//Inserts token
|
||||||
try {
|
try {
|
||||||
return db.update(Sqlite.TABLE_STATUS_CACHE,
|
return db.update(Sqlite.TABLE_STATUS_CACHE,
|
||||||
values, Sqlite.COL_STATUS_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =?",
|
values, Sqlite.COL_STATUS_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =? AND " + Sqlite.COL_TYPE + " =? AND " + Sqlite.COL_USER_ID + " =?",
|
||||||
new String[]{statusCache.status_id, statusCache.instance});
|
new String[]{statusCache.status_id, statusCache.instance, statusCache.type != null ? statusCache.type.getValue() : "", statusCache.user_id});
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -543,10 +523,10 @@ public class StatusCache {
|
||||||
* @param user_id String - us
|
* @param user_id String - us
|
||||||
* @param max_id String - status having max id
|
* @param max_id String - status having max id
|
||||||
* @param min_id String - status having min id
|
* @param min_id String - status having min id
|
||||||
* @return Statuses
|
* @return List<Notification>
|
||||||
* @throws DBException - throws a db exception
|
* @throws DBException - throws a db exception
|
||||||
*/
|
*/
|
||||||
public Notifications getNotifications(List<String> exclude_type, String instance, String user_id, String max_id, String min_id, String since_id) throws DBException {
|
public List<Notification> getNotifications(List<String> exclude_type, String instance, String user_id, String max_id, String min_id, String since_id) throws DBException {
|
||||||
if (db == null) {
|
if (db == null) {
|
||||||
throw new DBException("db is null. Wrong initialization.");
|
throw new DBException("db is null. Wrong initialization.");
|
||||||
}
|
}
|
||||||
|
@ -573,7 +553,7 @@ public class StatusCache {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Cursor c = db.query(Sqlite.TABLE_STATUS_CACHE, null, selection, null, Sqlite.COL_STATUS_ID, null, Sqlite.COL_STATUS_ID + order, limit);
|
Cursor c = db.query(Sqlite.TABLE_STATUS_CACHE, null, selection, null, Sqlite.COL_STATUS_ID, null, Sqlite.COL_STATUS_ID + order, limit);
|
||||||
return createNotificationReply(cursorToListOfNotifications(c));
|
return cursorToListOfNotifications(c);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
|
@ -588,10 +568,10 @@ public class StatusCache {
|
||||||
* @param user_id String - us
|
* @param user_id String - us
|
||||||
* @param max_id String - status having max id
|
* @param max_id String - status having max id
|
||||||
* @param min_id String - status having min id
|
* @param min_id String - status having min id
|
||||||
* @return Statuses
|
* @return List<Conversation>
|
||||||
* @throws DBException - throws a db exception
|
* @throws DBException - throws a db exception
|
||||||
*/
|
*/
|
||||||
public Conversations getConversations(String instance, String user_id, String max_id, String min_id, String since_id) throws DBException {
|
public List<Conversation> getConversations(String instance, String user_id, String max_id, String min_id, String since_id) throws DBException {
|
||||||
if (db == null) {
|
if (db == null) {
|
||||||
throw new DBException("db is null. Wrong initialization.");
|
throw new DBException("db is null. Wrong initialization.");
|
||||||
}
|
}
|
||||||
|
@ -607,10 +587,9 @@ public class StatusCache {
|
||||||
selection += "AND " + Sqlite.COL_STATUS_ID + " > '" + since_id + "' ";
|
selection += "AND " + Sqlite.COL_STATUS_ID + " > '" + since_id + "' ";
|
||||||
limit = null;
|
limit = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Cursor c = db.query(Sqlite.TABLE_STATUS_CACHE, null, selection, null, Sqlite.COL_STATUS_ID, null, Sqlite.COL_STATUS_ID + order, limit);
|
Cursor c = db.query(Sqlite.TABLE_STATUS_CACHE, null, selection, null, Sqlite.COL_STATUS_ID, null, Sqlite.COL_STATUS_ID + order, limit);
|
||||||
return createConversationReply(cursorToListOfConversations(c));
|
return cursorToListOfConversations(c);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
|
@ -625,10 +604,10 @@ public class StatusCache {
|
||||||
* @param user_id String - us
|
* @param user_id String - us
|
||||||
* @param max_id String - status having max id
|
* @param max_id String - status having max id
|
||||||
* @param min_id String - status having min id
|
* @param min_id String - status having min id
|
||||||
* @return Statuses
|
* @return List<Status>
|
||||||
* @throws DBException - throws a db exception
|
* @throws DBException - throws a db exception
|
||||||
*/
|
*/
|
||||||
public Statuses geStatuses(String slug, String instance, String user_id, String max_id, String min_id, String since_id) throws DBException {
|
public List<Status> geStatuses(String slug, String instance, String user_id, String max_id, String min_id, String since_id) throws DBException {
|
||||||
if (db == null) {
|
if (db == null) {
|
||||||
throw new DBException("db is null. Wrong initialization.");
|
throw new DBException("db is null. Wrong initialization.");
|
||||||
}
|
}
|
||||||
|
@ -646,7 +625,7 @@ public class StatusCache {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Cursor c = db.query(Sqlite.TABLE_STATUS_CACHE, null, selection, null, Sqlite.COL_STATUS_ID, null, Sqlite.COL_STATUS_ID + order, limit);
|
Cursor c = db.query(Sqlite.TABLE_STATUS_CACHE, null, selection, null, Sqlite.COL_STATUS_ID, null, Sqlite.COL_STATUS_ID + order, limit);
|
||||||
return createStatusReply(cursorToListOfStatuses(c));
|
return cursorToListOfStatuses(c);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
|
@ -769,75 +748,6 @@ public class StatusCache {
|
||||||
return conversationList;
|
return conversationList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a reply from db in the same way than API call
|
|
||||||
*
|
|
||||||
* @param notificationList List<Notification>
|
|
||||||
* @return Notifications (with pagination)
|
|
||||||
*/
|
|
||||||
private Notifications createNotificationReply(List<Notification> notificationList) {
|
|
||||||
Notifications notifications = new Notifications();
|
|
||||||
notifications.notifications = notificationList;
|
|
||||||
Pagination pagination = new Pagination();
|
|
||||||
if (notificationList != null && notificationList.size() > 0) {
|
|
||||||
//Status list is inverted, it happens for min_id due to ASC ordering
|
|
||||||
if (Helper.compareTo(notificationList.get(0).id, notificationList.get(notificationList.size() - 1).id) < 0) {
|
|
||||||
Collections.reverse(notificationList);
|
|
||||||
notifications.notifications = notificationList;
|
|
||||||
}
|
|
||||||
pagination.max_id = notificationList.get(0).id;
|
|
||||||
pagination.min_id = notificationList.get(notificationList.size() - 1).id;
|
|
||||||
}
|
|
||||||
notifications.pagination = pagination;
|
|
||||||
return notifications;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a reply from db in the same way than API call
|
|
||||||
*
|
|
||||||
* @param conversationList List<Conversation>
|
|
||||||
* @return Conversations (with pagination)
|
|
||||||
*/
|
|
||||||
private Conversations createConversationReply(List<Conversation> conversationList) {
|
|
||||||
Conversations conversations = new Conversations();
|
|
||||||
conversations.conversations = conversationList;
|
|
||||||
Pagination pagination = new Pagination();
|
|
||||||
if (conversationList != null && conversationList.size() > 0) {
|
|
||||||
//Status list is inverted, it happens for min_id due to ASC ordering
|
|
||||||
if (Helper.compareTo(conversationList.get(0).id, conversationList.get(conversationList.size() - 1).id) < 0) {
|
|
||||||
Collections.reverse(conversationList);
|
|
||||||
conversations.conversations = conversationList;
|
|
||||||
}
|
|
||||||
pagination.max_id = conversationList.get(0).id;
|
|
||||||
pagination.min_id = conversationList.get(conversationList.size() - 1).id;
|
|
||||||
}
|
|
||||||
conversations.pagination = pagination;
|
|
||||||
return conversations;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a reply from db in the same way than API call
|
|
||||||
*
|
|
||||||
* @param statusList List<Status>
|
|
||||||
* @return Statuses (with pagination)
|
|
||||||
*/
|
|
||||||
private Statuses createStatusReply(List<Status> statusList) {
|
|
||||||
Statuses statuses = new Statuses();
|
|
||||||
statuses.statuses = statusList;
|
|
||||||
Pagination pagination = new Pagination();
|
|
||||||
if (statusList != null && statusList.size() > 0) {
|
|
||||||
//Status list is inverted, it happens for min_id due to ASC ordering
|
|
||||||
if (Helper.compareTo(statusList.get(0).id, statusList.get(statusList.size() - 1).id) < 0) {
|
|
||||||
Collections.reverse(statusList);
|
|
||||||
statuses.statuses = statusList;
|
|
||||||
}
|
|
||||||
pagination.max_id = statusList.get(0).id;
|
|
||||||
pagination.min_id = statusList.get(statusList.size() - 1).id;
|
|
||||||
}
|
|
||||||
statuses.pagination = pagination;
|
|
||||||
return statuses;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read cursor and hydrate without closing it
|
* Read cursor and hydrate without closing it
|
||||||
|
|
|
@ -112,24 +112,24 @@ public class NotificationsVM extends AndroidViewModel {
|
||||||
if (notificationsResponse.isSuccessful()) {
|
if (notificationsResponse.isSuccessful()) {
|
||||||
List<Notification> notFiltered = notificationsResponse.body();
|
List<Notification> notFiltered = notificationsResponse.body();
|
||||||
notifications.notifications = TimelineHelper.filterNotification(getApplication().getApplicationContext(), notFiltered);
|
notifications.notifications = TimelineHelper.filterNotification(getApplication().getApplicationContext(), notFiltered);
|
||||||
addFetchMoreNotifications(notifications.notifications, notificationList, timelineParams);
|
|
||||||
notifications.pagination = MastodonHelper.getPagination(notificationsResponse.headers());
|
notifications.pagination = MastodonHelper.getPagination(notificationsResponse.headers());
|
||||||
|
|
||||||
if (notifications.notifications != null && notifications.notifications.size() > 0) {
|
if (notifications.notifications != null && notifications.notifications.size() > 0) {
|
||||||
addFetchMoreNotifications(notifications.notifications, notificationList, timelineParams);
|
addFetchMoreNotifications(notifications.notifications, notificationList, timelineParams);
|
||||||
for (Notification notification : notifications.notifications) {
|
if (notFiltered != null && notFiltered.size() > 0) {
|
||||||
StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext());
|
for (Notification notification : notFiltered) {
|
||||||
StatusCache statusCache = new StatusCache();
|
StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext());
|
||||||
statusCache.instance = timelineParams.instance;
|
StatusCache statusCache = new StatusCache();
|
||||||
statusCache.user_id = timelineParams.userId;
|
statusCache.instance = timelineParams.instance;
|
||||||
statusCache.notification = notification;
|
statusCache.user_id = timelineParams.userId;
|
||||||
statusCache.slug = notification.type;
|
statusCache.notification = notification;
|
||||||
statusCache.type = Timeline.TimeLineEnum.NOTIFICATION;
|
statusCache.slug = notification.type;
|
||||||
statusCache.status_id = notification.id;
|
statusCache.type = Timeline.TimeLineEnum.NOTIFICATION;
|
||||||
try {
|
statusCache.status_id = notification.id;
|
||||||
statusCacheDAO.insertOrUpdate(statusCache, notification.type);
|
try {
|
||||||
} catch (DBException e) {
|
statusCacheDAO.insertOrUpdate(statusCache, notification.type);
|
||||||
e.printStackTrace();
|
} catch (DBException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -146,41 +146,39 @@ public class NotificationsVM extends AndroidViewModel {
|
||||||
return notificationsMutableLiveData;
|
return notificationsMutableLiveData;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LiveData<Notifications> getNotificationCache(List<Notification> notificationList, TimelinesVM.TimelineParams timelineParams) {
|
public LiveData<Notifications> getNotificationCache(List<Notification> timelineNotification, TimelinesVM.TimelineParams timelineParams) {
|
||||||
notificationsMutableLiveData = new MutableLiveData<>();
|
notificationsMutableLiveData = new MutableLiveData<>();
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext());
|
StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext());
|
||||||
Notifications notifications = null;
|
Notifications notifications = new Notifications();
|
||||||
|
List<Notification> notificationsDb;
|
||||||
try {
|
try {
|
||||||
notifications = statusCacheDAO.getNotifications(timelineParams.excludeType, timelineParams.instance, timelineParams.userId, timelineParams.maxId, timelineParams.minId, timelineParams.sinceId);
|
notificationsDb = statusCacheDAO.getNotifications(timelineParams.excludeType, timelineParams.instance, timelineParams.userId, timelineParams.maxId, timelineParams.minId, timelineParams.sinceId);
|
||||||
if (notifications != null) {
|
if (notificationsDb != null && notificationsDb.size() > 0) {
|
||||||
if (notifications.notifications != null && notifications.notifications.size() > 0) {
|
if (timelineNotification != null) {
|
||||||
if (notificationList != null) {
|
List<Notification> notPresentNotifications = new ArrayList<>();
|
||||||
List<Notification> notPresentNotifications = new ArrayList<>();
|
for (Notification notification : notificationsDb) {
|
||||||
for (Notification notification : notifications.notifications) {
|
if (!timelineNotification.contains(notification)) {
|
||||||
if (!notificationList.contains(notification)) {
|
notification.cached = true;
|
||||||
notification.cached = true;
|
notPresentNotifications.add(notification);
|
||||||
notPresentNotifications.add(notification);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//Only not already present statuses are added
|
|
||||||
notifications.notifications = notPresentNotifications;
|
|
||||||
}
|
|
||||||
TimelineHelper.filterNotification(getApplication().getApplicationContext(), notifications.notifications);
|
|
||||||
if (notifications.notifications.size() > 0) {
|
|
||||||
addFetchMoreNotifications(notifications.notifications, notificationList, timelineParams);
|
|
||||||
notifications.pagination = new Pagination();
|
|
||||||
notifications.pagination.min_id = notifications.notifications.get(0).id;
|
|
||||||
notifications.pagination.max_id = notifications.notifications.get(notifications.notifications.size() - 1).id;
|
|
||||||
}
|
}
|
||||||
|
//Only not already present statuses are added
|
||||||
|
notificationsDb = notPresentNotifications;
|
||||||
|
}
|
||||||
|
notifications.notifications = TimelineHelper.filterNotification(getApplication().getApplicationContext(), notificationsDb);
|
||||||
|
if (notifications.notifications.size() > 0) {
|
||||||
|
addFetchMoreNotifications(notifications.notifications, timelineNotification, timelineParams);
|
||||||
|
notifications.pagination = new Pagination();
|
||||||
|
notifications.pagination.min_id = notifications.notifications.get(0).id;
|
||||||
|
notifications.pagination.max_id = notifications.notifications.get(notifications.notifications.size() - 1).id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (DBException e) {
|
} catch (DBException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
Handler mainHandler = new Handler(Looper.getMainLooper());
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||||
Notifications finalNotifications = notifications;
|
Runnable myRunnable = () -> notificationsMutableLiveData.setValue(notifications);
|
||||||
Runnable myRunnable = () -> notificationsMutableLiveData.setValue(finalNotifications);
|
|
||||||
mainHandler.post(myRunnable);
|
mainHandler.post(myRunnable);
|
||||||
}).start();
|
}).start();
|
||||||
return notificationsMutableLiveData;
|
return notificationsMutableLiveData;
|
||||||
|
|
|
@ -463,21 +463,21 @@ public class TimelinesVM extends AndroidViewModel {
|
||||||
StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext());
|
StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext());
|
||||||
Statuses statuses = new Statuses();
|
Statuses statuses = new Statuses();
|
||||||
try {
|
try {
|
||||||
Statuses statusesDb = statusCacheDAO.geStatuses(timelineParams.slug, timelineParams.instance, timelineParams.userId, timelineParams.maxId, timelineParams.minId, timelineParams.sinceId);
|
List<Status> statusesDb = statusCacheDAO.geStatuses(timelineParams.slug, timelineParams.instance, timelineParams.userId, timelineParams.maxId, timelineParams.minId, timelineParams.sinceId);
|
||||||
if (statusesDb != null && statusesDb.statuses != null && statusesDb.statuses.size() > 0) {
|
if (statusesDb != null && statusesDb.size() > 0) {
|
||||||
if (timelineStatuses != null) {
|
if (timelineStatuses != null) {
|
||||||
List<Status> notPresentStatuses = new ArrayList<>();
|
List<Status> notPresentStatuses = new ArrayList<>();
|
||||||
for (Status status : statusesDb.statuses) {
|
for (Status status : statusesDb) {
|
||||||
if (!timelineStatuses.contains(status)) {
|
if (!timelineStatuses.contains(status)) {
|
||||||
status.cached = true;
|
status.cached = true;
|
||||||
notPresentStatuses.add(status);
|
notPresentStatuses.add(status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Only not already present statuses are added
|
//Only not already present statuses are added
|
||||||
statusesDb.statuses = notPresentStatuses;
|
statusesDb = notPresentStatuses;
|
||||||
}
|
}
|
||||||
statuses.statuses = TimelineHelper.filterStatus(getApplication().getApplicationContext(), statusesDb.statuses, timelineParams.type);
|
statuses.statuses = TimelineHelper.filterStatus(getApplication().getApplicationContext(), statusesDb, timelineParams.type);
|
||||||
if (statuses.statuses != null && statuses.statuses.size() > 0) {
|
if (statuses.statuses.size() > 0) {
|
||||||
addFetchMore(statuses.statuses, timelineStatuses, timelineParams);
|
addFetchMore(statuses.statuses, timelineStatuses, timelineParams);
|
||||||
statuses.pagination = new Pagination();
|
statuses.pagination = new Pagination();
|
||||||
statuses.pagination.min_id = statuses.statuses.get(0).id;
|
statuses.pagination.min_id = statuses.statuses.get(0).id;
|
||||||
|
@ -528,21 +528,16 @@ public class TimelinesVM extends AndroidViewModel {
|
||||||
conversationListMutableLiveData = new MutableLiveData<>();
|
conversationListMutableLiveData = new MutableLiveData<>();
|
||||||
MastodonTimelinesService mastodonTimelinesService = init(timelineParams.instance);
|
MastodonTimelinesService mastodonTimelinesService = init(timelineParams.instance);
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
Conversations conversations = null;
|
Conversations conversations = new Conversations();
|
||||||
Call<List<Conversation>> conversationsCall = mastodonTimelinesService.getConversations(timelineParams.token, timelineParams.maxId, timelineParams.sinceId, timelineParams.minId, timelineParams.limit);
|
Call<List<Conversation>> conversationsCall = mastodonTimelinesService.getConversations(timelineParams.token, timelineParams.maxId, timelineParams.sinceId, timelineParams.minId, timelineParams.limit);
|
||||||
if (conversationsCall != null) {
|
if (conversationsCall != null) {
|
||||||
conversations = new Conversations();
|
|
||||||
try {
|
try {
|
||||||
Response<List<Conversation>> conversationsResponse = conversationsCall.execute();
|
Response<List<Conversation>> conversationsResponse = conversationsCall.execute();
|
||||||
if (conversationsResponse.isSuccessful()) {
|
if (conversationsResponse.isSuccessful()) {
|
||||||
List<Conversation> conversationList = conversationsResponse.body();
|
conversations.conversations = conversationsResponse.body();
|
||||||
conversations.conversations = conversationList;
|
|
||||||
conversations.pagination = MastodonHelper.getPagination(conversationsResponse.headers());
|
conversations.pagination = MastodonHelper.getPagination(conversationsResponse.headers());
|
||||||
|
if (conversations.conversations != null && conversations.conversations.size() > 0) {
|
||||||
if (conversationList != null && conversationList.size() > 0) {
|
addFetchMoreConversation(conversations.conversations, conversationsTimeline, timelineParams);
|
||||||
|
|
||||||
addFetchMoreConversation(conversationList, conversationsTimeline, timelineParams);
|
|
||||||
|
|
||||||
for (Conversation conversation : conversations.conversations) {
|
for (Conversation conversation : conversations.conversations) {
|
||||||
StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext());
|
StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext());
|
||||||
StatusCache statusCache = new StatusCache();
|
StatusCache statusCache = new StatusCache();
|
||||||
|
@ -564,8 +559,7 @@ public class TimelinesVM extends AndroidViewModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Handler mainHandler = new Handler(Looper.getMainLooper());
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||||
Conversations finalConversations = conversations;
|
Runnable myRunnable = () -> conversationListMutableLiveData.setValue(conversations);
|
||||||
Runnable myRunnable = () -> conversationListMutableLiveData.setValue(finalConversations);
|
|
||||||
mainHandler.post(myRunnable);
|
mainHandler.post(myRunnable);
|
||||||
}).start();
|
}).start();
|
||||||
|
|
||||||
|
@ -577,36 +571,34 @@ public class TimelinesVM extends AndroidViewModel {
|
||||||
conversationListMutableLiveData = new MutableLiveData<>();
|
conversationListMutableLiveData = new MutableLiveData<>();
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext());
|
StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext());
|
||||||
Conversations conversations = null;
|
Conversations conversations = new Conversations();
|
||||||
try {
|
try {
|
||||||
conversations = statusCacheDAO.getConversations(timelineParams.instance, timelineParams.userId, timelineParams.maxId, timelineParams.minId, timelineParams.sinceId);
|
List<Conversation> conversationsDb = statusCacheDAO.getConversations(timelineParams.instance, timelineParams.userId, timelineParams.maxId, timelineParams.minId, timelineParams.sinceId);
|
||||||
if (conversations != null) {
|
if (conversationsDb != null && conversationsDb.size() > 0) {
|
||||||
if (conversations.conversations != null && conversations.conversations.size() > 0) {
|
if (timelineConversations != null) {
|
||||||
if (timelineConversations != null) {
|
List<Conversation> notPresentConversations = new ArrayList<>();
|
||||||
List<Conversation> notPresentConversations = new ArrayList<>();
|
for (Conversation conversation : conversationsDb) {
|
||||||
for (Conversation conversation : conversations.conversations) {
|
if (!timelineConversations.contains(conversation)) {
|
||||||
if (!timelineConversations.contains(conversation)) {
|
conversation.cached = true;
|
||||||
conversation.cached = true;
|
timelineConversations.add(conversation);
|
||||||
timelineConversations.add(conversation);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//Only not already present statuses are added
|
|
||||||
conversations.conversations = notPresentConversations;
|
|
||||||
}
|
|
||||||
if (conversations.conversations.size() > 0) {
|
|
||||||
addFetchMoreConversation(conversations.conversations, timelineConversations, timelineParams);
|
|
||||||
conversations.pagination = new Pagination();
|
|
||||||
conversations.pagination.min_id = conversations.conversations.get(0).id;
|
|
||||||
conversations.pagination.max_id = conversations.conversations.get(conversations.conversations.size() - 1).id;
|
|
||||||
}
|
}
|
||||||
|
//Only not already present statuses are added
|
||||||
|
conversationsDb = notPresentConversations;
|
||||||
|
}
|
||||||
|
conversations.conversations = conversationsDb;
|
||||||
|
if (conversations.conversations.size() > 0) {
|
||||||
|
addFetchMoreConversation(conversations.conversations, timelineConversations, timelineParams);
|
||||||
|
conversations.pagination = new Pagination();
|
||||||
|
conversations.pagination.min_id = conversations.conversations.get(0).id;
|
||||||
|
conversations.pagination.max_id = conversations.conversations.get(conversations.conversations.size() - 1).id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (DBException e) {
|
} catch (DBException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
Handler mainHandler = new Handler(Looper.getMainLooper());
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||||
Conversations finalConversations = conversations;
|
Runnable myRunnable = () -> conversationListMutableLiveData.setValue(conversations);
|
||||||
Runnable myRunnable = () -> conversationListMutableLiveData.setValue(finalConversations);
|
|
||||||
mainHandler.post(myRunnable);
|
mainHandler.post(myRunnable);
|
||||||
}).start();
|
}).start();
|
||||||
return conversationListMutableLiveData;
|
return conversationListMutableLiveData;
|
||||||
|
|
Loading…
Reference in a new issue