From da5ff2320d82bb0713689690cb97444d94418c78 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 10 Nov 2022 16:02:56 +0100 Subject: [PATCH] Fix messages/notifications not in the right order --- .../android/activities/CacheActivity.java | 45 +++++++++++-------- .../client/entities/app/StatusCache.java | 6 +-- .../app/fedilab/android/helper/Helper.java | 9 ++++ .../android/helper/NotificationsHelper.java | 6 +-- .../helper/ZoomOutPageTransformer.java | 44 ------------------ .../fragment/admin/FragmentAdminAccount.java | 4 +- .../fragment/admin/FragmentAdminReport.java | 8 ++-- .../fragment/media/FragmentMediaProfile.java | 4 +- .../FragmentMastodonConversation.java | 11 ++--- .../FragmentMastodonNotification.java | 10 ++--- .../timeline/FragmentMastodonTimeline.java | 10 ++--- 11 files changed, 65 insertions(+), 92 deletions(-) delete mode 100644 app/src/main/java/app/fedilab/android/helper/ZoomOutPageTransformer.java diff --git a/app/src/main/java/app/fedilab/android/activities/CacheActivity.java b/app/src/main/java/app/fedilab/android/activities/CacheActivity.java index b2bc8c7ad..23d2b8ea6 100644 --- a/app/src/main/java/app/fedilab/android/activities/CacheActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/CacheActivity.java @@ -70,28 +70,35 @@ public class CacheActivity extends BaseActivity { new Thread(() -> { - List accounts = new Account(CacheActivity.this).getPushNotificationAccounts(); + List accounts; cacheAccounts = new ArrayList<>(); - for (BaseAccount baseAccount : accounts) { - CacheAccount cacheAccount = new CacheAccount(); - cacheAccount.account = baseAccount; - try { - cacheAccount.home_cache_count = new StatusCache(CacheActivity.this).countHome(baseAccount); - } catch (DBException e) { - e.printStackTrace(); + try { + accounts = new Account(CacheActivity.this).getAll(); + for (BaseAccount baseAccount : accounts) { + CacheAccount cacheAccount = new CacheAccount(); + cacheAccount.account = baseAccount; + try { + cacheAccount.home_cache_count = new StatusCache(CacheActivity.this).countHome(baseAccount); + } catch (DBException e) { + e.printStackTrace(); + } + try { + cacheAccount.other_cache_count = new StatusCache(CacheActivity.this).countOther(baseAccount); + } catch (DBException e) { + e.printStackTrace(); + } + try { + cacheAccount.draft_count = new StatusDraft(CacheActivity.this).count(baseAccount); + } catch (DBException e) { + e.printStackTrace(); + } + cacheAccounts.add(cacheAccount); } - try { - cacheAccount.other_cache_count = new StatusCache(CacheActivity.this).countOther(baseAccount); - } catch (DBException e) { - e.printStackTrace(); - } - try { - cacheAccount.draft_count = new StatusDraft(CacheActivity.this).count(baseAccount); - } catch (DBException e) { - e.printStackTrace(); - } - cacheAccounts.add(cacheAccount); + } catch (DBException e) { + e.printStackTrace(); } + + Handler mainHandler = new Handler(Looper.getMainLooper()); Runnable myRunnable = () -> { cacheAdapter = new CacheAdapter(cacheAccounts); diff --git a/app/src/main/java/app/fedilab/android/client/entities/app/StatusCache.java b/app/src/main/java/app/fedilab/android/client/entities/app/StatusCache.java index b8d92dbf3..31f9ed6c7 100644 --- a/app/src/main/java/app/fedilab/android/client/entities/app/StatusCache.java +++ b/app/src/main/java/app/fedilab/android/client/entities/app/StatusCache.java @@ -759,7 +759,7 @@ public class StatusCache { Pagination pagination = new Pagination(); if (notificationList != null && notificationList.size() > 0) { //Status list is inverted, it happens for min_id due to ASC ordering - if (notificationList.get(0).id.compareTo(notificationList.get(notificationList.size() - 1).id) < 0) { + if (Helper.compareTo(notificationList.get(0).id, notificationList.get(notificationList.size() - 1).id) < 0) { Collections.reverse(notificationList); notifications.notifications = notificationList; } @@ -783,7 +783,7 @@ public class StatusCache { Pagination pagination = new Pagination(); if (conversationList != null && conversationList.size() > 0) { //Status list is inverted, it happens for min_id due to ASC ordering - if (conversationList.get(0).id.compareTo(conversationList.get(conversationList.size() - 1).id) < 0) { + if (Helper.compareTo(conversationList.get(0).id, conversationList.get(conversationList.size() - 1).id) < 0) { Collections.reverse(conversationList); conversations.conversations = conversationList; } @@ -806,7 +806,7 @@ public class StatusCache { Pagination pagination = new Pagination(); if (statusList != null && statusList.size() > 0) { //Status list is inverted, it happens for min_id due to ASC ordering - if (statusList.get(0).id.compareTo(statusList.get(statusList.size() - 1).id) < 0) { + if (Helper.compareTo(statusList.get(0).id, statusList.get(statusList.size() - 1).id) < 0) { Collections.reverse(statusList); statuses.statuses = statusList; } diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index e6742125b..79d43d292 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -1882,6 +1882,15 @@ public class Helper { } } + public static int compareTo(String value1, String value2) { + try { + long val1 = Long.parseLong(value1); + long val2 = Long.parseLong(value2); + return Long.compare(val1, val2); + } catch (Exception e) { + return value1.compareTo(value2); + } + } //Enum that described actions to replace inside a toot content public enum PatternType { diff --git a/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java b/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java index 08b0be4ea..8b4f16016 100644 --- a/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java @@ -161,7 +161,7 @@ public class NotificationsHelper { final List notifications = new ArrayList<>(); int pos = 0; for (Notification notif : notificationsReceived) { - if (max_id == null || notif.id.compareTo(max_id) > 0) { + if (max_id == null || Helper.compareTo(notif.id, max_id) > 0) { notifications.add(pos, notif); pos++; } @@ -338,7 +338,7 @@ public class NotificationsHelper { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { String lastNotif = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, null); - if (lastNotif == null || notification.id.compareTo(lastNotif) > 0) { + if (lastNotif == null || Helper.compareTo(notification.id, lastNotif) > 0) { SharedPreferences.Editor editor = prefs.edit(); since_ids.put(account.user_id + "@" + account.instance, lastNotif); editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id); @@ -353,7 +353,7 @@ public class NotificationsHelper { @Override public void onResourceReady(@NonNull Bitmap resource, Transition transition) { String lastNotif = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, null); - if (lastNotif == null || notification.id.compareTo(lastNotif) > 0) { + if (lastNotif == null || Helper.compareTo(notification.id, lastNotif) > 0) { SharedPreferences.Editor editor = prefs.edit(); editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id); editor.apply(); diff --git a/app/src/main/java/app/fedilab/android/helper/ZoomOutPageTransformer.java b/app/src/main/java/app/fedilab/android/helper/ZoomOutPageTransformer.java deleted file mode 100644 index b61b6cc9f..000000000 --- a/app/src/main/java/app/fedilab/android/helper/ZoomOutPageTransformer.java +++ /dev/null @@ -1,44 +0,0 @@ -package app.fedilab.android.helper; - -import android.view.View; - -import androidx.viewpager2.widget.ViewPager2; - -public class ZoomOutPageTransformer implements ViewPager2.PageTransformer { - private static final float MIN_SCALE = 0.85f; - private static final float MIN_ALPHA = 0.5f; - - public void transformPage(View view, float position) { - int pageWidth = view.getWidth(); - int pageHeight = view.getHeight(); - - if (position < -1) { // [-Infinity,-1) - // This page is way off-screen to the left. - view.setAlpha(0f); - - } else if (position <= 1) { // [-1,1] - // Modify the default slide transition to shrink the page as well - float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position)); - float vertMargin = pageHeight * (1 - scaleFactor) / 2; - float horzMargin = pageWidth * (1 - scaleFactor) / 2; - if (position < 0) { - view.setTranslationX(horzMargin - vertMargin / 2); - } else { - view.setTranslationX(-horzMargin + vertMargin / 2); - } - - // Scale the page down (between MIN_SCALE and 1) - view.setScaleX(scaleFactor); - view.setScaleY(scaleFactor); - - // Fade the page relative to its size. - view.setAlpha(MIN_ALPHA + - (scaleFactor - MIN_SCALE) / - (1 - MIN_SCALE) * (1 - MIN_ALPHA)); - - } else { // (1,+Infinity] - // This page is way off-screen to the right. - view.setAlpha(0f); - } - } -} \ No newline at end of file diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/admin/FragmentAdminAccount.java b/app/src/main/java/app/fedilab/android/ui/fragment/admin/FragmentAdminAccount.java index 8c8b6a378..3eecc6103 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/admin/FragmentAdminAccount.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/admin/FragmentAdminAccount.java @@ -140,7 +140,7 @@ public class FragmentAdminAccount extends Fragment { binding.recyclerView.setLayoutManager(mLayoutManager); binding.recyclerView.setAdapter(adminAccountAdapter); //Fetch the relationship - if (max_id == null || (adminAccounts.pagination.max_id != null && adminAccounts.pagination.max_id.compareTo(max_id) < 0)) { + if (max_id == null || (adminAccounts.pagination.max_id != null && Helper.compareTo(adminAccounts.pagination.max_id, max_id) < 0)) { max_id = adminAccounts.pagination.max_id; } binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @@ -190,7 +190,7 @@ public class FragmentAdminAccount extends Fragment { } int position = this.adminAccounts.size(); this.adminAccounts.addAll(adminAccounts.adminAccounts); - if (max_id == null || (adminAccounts.pagination.max_id != null && adminAccounts.pagination.max_id.compareTo(max_id) < 0)) { + if (max_id == null || (adminAccounts.pagination.max_id != null && Helper.compareTo(adminAccounts.pagination.max_id, max_id) < 0)) { max_id = adminAccounts.pagination.max_id; } adminAccountAdapter.notifyItemRangeInserted(startId, adminAccounts.adminAccounts.size()); diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/admin/FragmentAdminReport.java b/app/src/main/java/app/fedilab/android/ui/fragment/admin/FragmentAdminReport.java index cfbad282d..2481f8c5d 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/admin/FragmentAdminReport.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/admin/FragmentAdminReport.java @@ -131,10 +131,10 @@ public class FragmentAdminReport extends Fragment { this.adminReports.addAll(adminReports.adminReports); - if (max_id == null || (adminReports.pagination.max_id != null && adminReports.pagination.max_id.compareTo(max_id) < 0)) { + if (max_id == null || (adminReports.pagination.max_id != null && Helper.compareTo(adminReports.pagination.max_id, max_id) < 0)) { max_id = adminReports.pagination.max_id; } - if (min_id == null || (adminReports.pagination.max_id != null && adminReports.pagination.min_id.compareTo(min_id) > 0)) { + if (min_id == null || (adminReports.pagination.max_id != null && Helper.compareTo(adminReports.pagination.min_id, min_id) > 0)) { min_id = adminReports.pagination.min_id; } @@ -190,10 +190,10 @@ public class FragmentAdminReport extends Fragment { int startId = adminReports.size(); adminReports.addAll(admReports.adminReports); statusAdapter.notifyItemRangeInserted(startId, admReports.adminReports.size()); - if (max_id == null || (admReports.pagination.max_id != null && admReports.pagination.max_id.compareTo(max_id) < 0)) { + if (max_id == null || (admReports.pagination.max_id != null && Helper.compareTo(admReports.pagination.max_id, max_id) < 0)) { max_id = admReports.pagination.max_id; } - if (min_id == null || (admReports.pagination.min_id != null && admReports.pagination.min_id.compareTo(min_id) > 0)) { + if (min_id == null || (admReports.pagination.min_id != null && Helper.compareTo(admReports.pagination.min_id, min_id) > 0)) { min_id = admReports.pagination.min_id; } } diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/media/FragmentMediaProfile.java b/app/src/main/java/app/fedilab/android/ui/fragment/media/FragmentMediaProfile.java index 840624124..732592bfc 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/media/FragmentMediaProfile.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/media/FragmentMediaProfile.java @@ -107,7 +107,7 @@ public class FragmentMediaProfile extends Fragment { flagLoading = statuses.pagination.max_id == null; binding.recyclerView.setVisibility(View.VISIBLE); - if (max_id == null || (statuses.pagination.max_id != null && statuses.pagination.max_id.compareTo(max_id) < 0)) { + if (max_id == null || (statuses.pagination.max_id != null && Helper.compareTo(statuses.pagination.max_id, max_id) < 0)) { max_id = statuses.pagination.max_id; } GridLayoutManager gvLayout = new GridLayoutManager(requireActivity(), 3); @@ -180,7 +180,7 @@ public class FragmentMediaProfile extends Fragment { imageAdapter.notifyItemRangeInserted(this.mediaStatuses.size() - added, this.mediaStatuses.size()); if (fetched_statuses.pagination.max_id == null) { flagLoading = true; - } else if (max_id == null || fetched_statuses.pagination.max_id.compareTo(max_id) < 0) { + } else if (max_id == null || Helper.compareTo(fetched_statuses.pagination.max_id, max_id) < 0) { max_id = fetched_statuses.pagination.max_id; } } else { diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java index b7abf5582..4eede2b01 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java @@ -40,6 +40,7 @@ import app.fedilab.android.client.entities.app.StatusCache; import app.fedilab.android.client.entities.app.Timeline; import app.fedilab.android.databinding.FragmentPaginationBinding; import app.fedilab.android.exception.DBException; +import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.MastodonHelper; import app.fedilab.android.helper.ThemeHelper; import app.fedilab.android.ui.drawer.ConversationAdapter; @@ -276,10 +277,10 @@ public class FragmentMastodonConversation extends Fragment implements Conversati } conversationList.addAll(conversations.conversations); - if (max_id == null || (conversations.pagination.max_id != null && conversations.pagination.max_id.compareTo(max_id) < 0)) { + if (max_id == null || (conversations.pagination.max_id != null && Helper.compareTo(conversations.pagination.max_id, max_id) < 0)) { max_id = conversations.pagination.max_id; } - if (min_id == null || (conversations.pagination.min_id != null && conversations.pagination.min_id.compareTo(min_id) > 0)) { + if (min_id == null || (conversations.pagination.min_id != null && Helper.compareTo(conversations.pagination.min_id, min_id) > 0)) { min_id = conversations.pagination.min_id; } @@ -384,10 +385,10 @@ public class FragmentMastodonConversation extends Fragment implements Conversati if (!fetchingMissing) { if (fetched_conversations.pagination.max_id == null) { flagLoading = true; - } else if (max_id == null || fetched_conversations.pagination.max_id.compareTo(max_id) < 0) { + } else if (max_id == null || Helper.compareTo(fetched_conversations.pagination.max_id, max_id) < 0) { max_id = fetched_conversations.pagination.max_id; } - if (min_id == null || (fetched_conversations.pagination.min_id != null && fetched_conversations.pagination.min_id.compareTo(min_id) > 0)) { + if (min_id == null || (fetched_conversations.pagination.min_id != null && Helper.compareTo(fetched_conversations.pagination.min_id, min_id) > 0)) { min_id = fetched_conversations.pagination.min_id; } } @@ -435,7 +436,7 @@ public class FragmentMastodonConversation extends Fragment implements Conversati for (Conversation conversationsAlreadyPresent : conversationList) { //We compare the date of each status and we only add status having a date greater than the another, it is inserted at this position //Pinned messages are ignored because their date can be older - if (conversationReceived.id.compareTo(conversationsAlreadyPresent.id) > 0) { + if (Helper.compareTo(conversationReceived.id, conversationsAlreadyPresent.id) > 0) { if (!conversationList.contains(conversationReceived)) { conversationList.add(position, conversationReceived); conversationAdapter.notifyItemInserted(position); diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java index 04697e963..3b64b88ee 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java @@ -269,10 +269,10 @@ public class FragmentMastodonNotification extends Fragment implements Notificati } notificationList.addAll(notifications.notifications); - if (max_id == null || (notifications.pagination.max_id != null && notifications.pagination.max_id.compareTo(max_id) < 0)) { + if (max_id == null || (notifications.pagination.max_id != null && Helper.compareTo(notifications.pagination.max_id, max_id) < 0)) { max_id = notifications.pagination.max_id; } - if (min_id == null || (notifications.pagination.min_id != null && notifications.pagination.min_id.compareTo(min_id) > 0)) { + if (min_id == null || (notifications.pagination.min_id != null && Helper.compareTo(notifications.pagination.min_id, min_id) > 0)) { min_id = notifications.pagination.min_id; } @@ -565,10 +565,10 @@ public class FragmentMastodonNotification extends Fragment implements Notificati if (!fetchingMissing) { if (fetched_notifications.pagination.max_id == null) { flagLoading = true; - } else if (max_id == null || fetched_notifications.pagination.max_id.compareTo(max_id) < 0) { + } else if (max_id == null || Helper.compareTo(fetched_notifications.pagination.max_id, max_id) < 0) { max_id = fetched_notifications.pagination.max_id; } - if (min_id == null || (fetched_notifications.pagination.min_id != null && fetched_notifications.pagination.min_id.compareTo(min_id) > 0)) { + if (min_id == null || (fetched_notifications.pagination.min_id != null && Helper.compareTo(fetched_notifications.pagination.min_id, min_id) > 0)) { min_id = fetched_notifications.pagination.min_id; } } @@ -596,7 +596,7 @@ public class FragmentMastodonNotification extends Fragment implements Notificati for (Notification notificationsAlreadyPresent : notificationList) { //We compare the date of each status and we only add status having a date greater than the another, it is inserted at this position //Pinned messages are ignored because their date can be older - if (notificationReceived.id.compareTo(notificationsAlreadyPresent.id) > 0) { + if (Helper.compareTo(notificationReceived.id, notificationsAlreadyPresent.id) > 0) { if (!notificationList.contains(notificationReceived)) { notificationList.add(position, notificationReceived); notificationAdapter.notifyItemInserted(position); diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java index 8c354c9b3..74c3c2dcc 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java @@ -393,10 +393,10 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. if (!fetchingMissing) { if (fetched_statuses.pagination.max_id == null) { flagLoading = true; - } else if (max_id == null || fetched_statuses.pagination.max_id.compareTo(max_id) < 0) { + } else if (max_id == null || Helper.compareTo(fetched_statuses.pagination.max_id, max_id) < 0) { max_id = fetched_statuses.pagination.max_id; } - if (min_id == null || (fetched_statuses.pagination.min_id != null && fetched_statuses.pagination.min_id.compareTo(min_id) > 0)) { + if (min_id == null || (fetched_statuses.pagination.min_id != null && Helper.compareTo(fetched_statuses.pagination.min_id, min_id) > 0)) { min_id = fetched_statuses.pagination.min_id; } } @@ -474,10 +474,10 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. timelineStatuses.add(statusReport); } timelineStatuses.addAll(statuses.statuses); - if (max_id == null || (statuses.pagination.max_id != null && statuses.pagination.max_id.compareTo(max_id) < 0)) { + if (max_id == null || (statuses.pagination.max_id != null && Helper.compareTo(statuses.pagination.max_id, max_id) < 0)) { max_id = statuses.pagination.max_id; } - if (min_id == null || (statuses.pagination.min_id != null && statuses.pagination.min_id.compareTo(min_id) > 0)) { + if (min_id == null || (statuses.pagination.min_id != null && Helper.compareTo(statuses.pagination.min_id, min_id) > 0)) { min_id = statuses.pagination.min_id; } statusAdapter = new StatusAdapter(timelineStatuses, timelineType, minified, canBeFederated); @@ -548,7 +548,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. for (Status statusAlreadyPresent : timelineStatuses) { //We compare the id of each status and we only add status having an id greater than the another, it is inserted at this position //Pinned messages are ignored because their date can be older - if (statusReceived.id.compareTo(statusAlreadyPresent.id) > 0 && !statusAlreadyPresent.pinned) { + if (Helper.compareTo(statusReceived.id, statusAlreadyPresent.id) > 0 && !statusAlreadyPresent.pinned) { //We add the status to a list of id - thus we know it is already in the timeline if (!timelineStatuses.contains(statusReceived)) { timelineStatuses.add(position, statusReceived);