From 900a91f13f5452cbd41e020692fd91689b507711 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 3 Oct 2022 11:31:27 +0200 Subject: [PATCH] Last fix with pagination and remote calls --- .../viewmodel/mastodon/NotificationsVM.java | 12 +++++++++ .../viewmodel/mastodon/TimelinesVM.java | 27 ++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/NotificationsVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/NotificationsVM.java index fcdd9e57..71991091 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/NotificationsVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/NotificationsVM.java @@ -23,6 +23,7 @@ import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -154,6 +155,17 @@ public class NotificationsVM extends AndroidViewModel { notifications = statusCacheDAO.getNotifications(timelineParams.excludeType, timelineParams.instance, timelineParams.userId, timelineParams.maxId, timelineParams.minId, timelineParams.sinceId); if (notifications != null) { if (notifications.notifications != null && notifications.notifications.size() > 0) { + if (notificationList != null) { + List notPresentNotifications = new ArrayList<>(); + for (Notification notification : notifications.notifications) { + if (!notificationList.contains(notification)) { + notification.cached = true; + notPresentNotifications.add(notification); + } + } + //Only not already present statuses are added + notifications.notifications = notPresentNotifications; + } TimelineHelper.filterNotification(getApplication().getApplicationContext(), notifications.notifications, true); addFetchMoreNotifications(notifications.notifications, notificationList, timelineParams); notifications.pagination = new Pagination(); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java index 5990c723..eab6b9a2 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java @@ -386,7 +386,6 @@ public class TimelinesVM extends AndroidViewModel { } public LiveData getTimeline(List timelineStatuses, TimelineParams timelineParams) { - statusesMutableLiveData = new MutableLiveData<>(); MastodonTimelinesService mastodonTimelinesService = init(timelineParams.instance); new Thread(() -> { @@ -418,6 +417,7 @@ public class TimelinesVM extends AndroidViewModel { statuses.statuses = TimelineHelper.filterStatus(getApplication().getApplicationContext(), statusList, timelineParams.type); statuses.pagination = MastodonHelper.getPagination(timelineResponse.headers()); if (statusList != null && statusList.size() > 0) { + addFetchMore(statusList, timelineStatuses, timelineParams); for (Status status : statuses.statuses) { StatusCache statusCacheDAO = new StatusCache(getApplication().getApplicationContext()); @@ -456,7 +456,18 @@ public class TimelinesVM extends AndroidViewModel { Statuses statuses = null; try { statuses = statusCacheDAO.geStatuses(timelineParams.slug, timelineParams.instance, timelineParams.userId, timelineParams.maxId, timelineParams.minId, timelineParams.sinceId); - if (statuses != null) { + if (statuses != null && statuses.statuses != null && statuses.statuses.size() > 0) { + if (timelineStatuses != null) { + List notPresentStatuses = new ArrayList<>(); + for (Status status : statuses.statuses) { + if (!timelineStatuses.contains(status)) { + status.cached = true; + notPresentStatuses.add(status); + } + } + //Only not already present statuses are added + statuses.statuses = notPresentStatuses; + } TimelineHelper.filterStatus(getApplication().getApplicationContext(), statuses.statuses, timelineParams.type, true); if (statuses.statuses != null && statuses.statuses.size() > 0) { addFetchMore(statuses.statuses, timelineStatuses, timelineParams); @@ -564,8 +575,16 @@ public class TimelinesVM extends AndroidViewModel { conversations = statusCacheDAO.getConversations(timelineParams.instance, timelineParams.userId, timelineParams.maxId, timelineParams.minId, timelineParams.sinceId); if (conversations != null) { if (conversations.conversations != null && conversations.conversations.size() > 0) { - for (Conversation conversation : conversations.conversations) { - conversation.cached = true; + if (timelineConversations != null) { + List notPresentConversations = new ArrayList<>(); + for (Conversation conversation : conversations.conversations) { + if (!timelineConversations.contains(conversation)) { + conversation.cached = true; + timelineConversations.add(conversation); + } + } + //Only not already present statuses are added + conversations.conversations = notPresentConversations; } addFetchMoreConversation(conversations.conversations, timelineConversations, timelineParams); conversations.pagination = new Pagination();