mirror of
				https://codeberg.org/tom79/Fedilab.git
				synced 2025-10-20 11:20:16 +03:00 
			
		
		
		
	Some fixes
This commit is contained in:
		
							parent
							
								
									866920933e
								
							
						
					
					
						commit
						3d3f0039a3
					
				
					 3 changed files with 78 additions and 88 deletions
				
			
		|  | @ -43,8 +43,10 @@ import app.fedilab.android.R; | ||||||
| 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.Notifications; | ||||||
| import app.fedilab.android.client.entities.api.Status; | import app.fedilab.android.client.entities.api.Status; | ||||||
|  | import app.fedilab.android.client.entities.app.StatusCache; | ||||||
| import app.fedilab.android.client.entities.app.Timeline; | import app.fedilab.android.client.entities.app.Timeline; | ||||||
| import app.fedilab.android.databinding.FragmentPaginationBinding; | import app.fedilab.android.databinding.FragmentPaginationBinding; | ||||||
|  | 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; | ||||||
| import app.fedilab.android.helper.ThemeHelper; | import app.fedilab.android.helper.ThemeHelper; | ||||||
|  | @ -111,6 +113,25 @@ public class FragmentMastodonNotification extends Fragment implements Notificati | ||||||
|         return found ? position : -1; |         return found ? position : -1; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Return the position of the notification in the ArrayList | ||||||
|  |      * | ||||||
|  |      * @param notification - Notification to fetch | ||||||
|  |      * @return position or -1 if not found | ||||||
|  |      */ | ||||||
|  |     private int getPosition(Notification notification) { | ||||||
|  |         int position = 0; | ||||||
|  |         boolean found = false; | ||||||
|  |         for (Notification _notification : notificationList) { | ||||||
|  |             if (_notification.status != null && _notification.id.compareTo(notification.id) == 0) { | ||||||
|  |                 found = true; | ||||||
|  |                 break; | ||||||
|  |             } | ||||||
|  |             position++; | ||||||
|  |         } | ||||||
|  |         return found ? position : -1; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     public View onCreateView(@NonNull LayoutInflater inflater, |     public View onCreateView(@NonNull LayoutInflater inflater, | ||||||
|                              ViewGroup container, Bundle savedInstanceState) { |                              ViewGroup container, Bundle savedInstanceState) { | ||||||
| 
 | 
 | ||||||
|  | @ -307,7 +328,6 @@ public class FragmentMastodonNotification extends Fragment implements Notificati | ||||||
|         } else { |         } else { | ||||||
|             timelineParams.maxId = max_id; |             timelineParams.maxId = max_id; | ||||||
|         } |         } | ||||||
|         timelineParams.userId = null; |  | ||||||
|         timelineParams.excludeType = getExcludeType(); |         timelineParams.excludeType = getExcludeType(); | ||||||
| 
 | 
 | ||||||
|         timelineParams.fetchingMissing = fetchingMissing; |         timelineParams.fetchingMissing = fetchingMissing; | ||||||
|  | @ -416,6 +436,7 @@ public class FragmentMastodonNotification extends Fragment implements Notificati | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * Update view and pagination when scrolling down |      * Update view and pagination when scrolling down | ||||||
|      * |      * | ||||||
|  | @ -429,10 +450,31 @@ public class FragmentMastodonNotification extends Fragment implements Notificati | ||||||
|         binding.loadingNextElements.setVisibility(View.GONE); |         binding.loadingNextElements.setVisibility(View.GONE); | ||||||
|         flagLoading = false; |         flagLoading = false; | ||||||
|         if (notificationList != null && fetched_notifications != null && fetched_notifications.notifications != null && fetched_notifications.notifications.size() > 0) { |         if (notificationList != null && fetched_notifications != null && fetched_notifications.notifications != null && fetched_notifications.notifications.size() > 0) { | ||||||
|  |             try { | ||||||
|  |                 if (notificationToUpdate != null) { | ||||||
|  |                     new Thread(() -> { | ||||||
|  |                         StatusCache statusCache = new StatusCache(); | ||||||
|  |                         statusCache.instance = BaseMainActivity.currentInstance; | ||||||
|  |                         statusCache.user_id = BaseMainActivity.currentUserID; | ||||||
|  |                         statusCache.notification = notificationToUpdate; | ||||||
|  |                         statusCache.status_id = notificationToUpdate.id; | ||||||
|  |                         try { | ||||||
|  |                             new StatusCache(requireActivity()).updateIfExists(statusCache); | ||||||
|  |                         } catch (DBException e) { | ||||||
|  |                             e.printStackTrace(); | ||||||
|  |                         } | ||||||
|  |                     }).start(); | ||||||
|  |                 } | ||||||
|  |             } catch (Exception ignored) { | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|             flagLoading = fetched_notifications.pagination.max_id == null; |             flagLoading = fetched_notifications.pagination.max_id == null; | ||||||
|             binding.noAction.setVisibility(View.GONE); |             binding.noAction.setVisibility(View.GONE); | ||||||
|             //Update the timeline with new statuses |             //Update the timeline with new statuses | ||||||
|             updateNotificationListWith(direction, fetched_notifications.notifications, fetchingMissing); |             updateNotificationListWith(fetched_notifications.notifications); | ||||||
|  |             if (direction == FragmentMastodonTimeline.DIRECTION.TOP && fetchingMissing) { | ||||||
|  |                 binding.recyclerView.scrollToPosition(getPosition(fetched_notifications.notifications.get(fetched_notifications.notifications.size() - 1)) + 1); | ||||||
|  |             } | ||||||
|             if (!fetchingMissing) { |             if (!fetchingMissing) { | ||||||
|                 if (fetched_notifications.pagination.max_id == null) { |                 if (fetched_notifications.pagination.max_id == null) { | ||||||
|                     flagLoading = true; |                     flagLoading = true; | ||||||
|  | @ -452,56 +494,10 @@ public class FragmentMastodonNotification extends Fragment implements Notificati | ||||||
|      * Update the timeline with received statuses |      * Update the timeline with received statuses | ||||||
|      * |      * | ||||||
|      * @param notificationsReceived - List<Notification> Notifications received |      * @param notificationsReceived - List<Notification> Notifications received | ||||||
|      * @param fetchingMissing       - boolean if the call concerns fetching messages (ie: refresh of from fetch more button) |  | ||||||
|      */ |      */ | ||||||
|     private void updateNotificationListWith(FragmentMastodonTimeline.DIRECTION direction, List<Notification> notificationsReceived, boolean fetchingMissing) { |     private void updateNotificationListWith(List<Notification> notificationsReceived) { | ||||||
|         int numberInserted = 0; |  | ||||||
|         int lastInsertedPosition = 0; |  | ||||||
|         int initialInsertedPosition = NOTIFICATION_PRESENT; |  | ||||||
|         if (notificationsReceived != null && notificationsReceived.size() > 0) { |         if (notificationsReceived != null && notificationsReceived.size() > 0) { | ||||||
|             int insertedPosition = NOTIFICATION_PRESENT; |  | ||||||
|             for (Notification notificationReceived : notificationsReceived) { |             for (Notification notificationReceived : notificationsReceived) { | ||||||
|                 insertedPosition = insertNotification(notificationReceived); |  | ||||||
|                 if (insertedPosition != NOTIFICATION_PRESENT && insertedPosition != NOTIFICATION__AT_THE_BOTTOM) { |  | ||||||
|                     numberInserted++; |  | ||||||
|                     if (initialInsertedPosition == NOTIFICATION_PRESENT) { |  | ||||||
|                         initialInsertedPosition = insertedPosition; |  | ||||||
|                     } |  | ||||||
|                     if (insertedPosition < initialInsertedPosition) { |  | ||||||
|                         initialInsertedPosition = lastInsertedPosition; |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|             lastInsertedPosition = initialInsertedPosition + numberInserted; |  | ||||||
|             //lastInsertedPosition contains the position of the last inserted status |  | ||||||
|             //If there were no overlap for top status |  | ||||||
|             if (fetchingMissing && insertedPosition != NOTIFICATION_PRESENT && insertedPosition != NOTIFICATION__AT_THE_BOTTOM && this.notificationList.size() > insertedPosition) { |  | ||||||
|                 Notification notificationFetchMore = new Notification(); |  | ||||||
|                 notificationFetchMore.isFetchMore = true; |  | ||||||
|                 notificationFetchMore.id = Helper.generateString(); |  | ||||||
|                 int insertAt; |  | ||||||
|                 if (direction == FragmentMastodonTimeline.DIRECTION.REFRESH) { |  | ||||||
|                     insertAt = lastInsertedPosition; |  | ||||||
|                 } else { |  | ||||||
|                     insertAt = initialInsertedPosition; |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 this.notificationList.add(insertAt, notificationFetchMore); |  | ||||||
|                 notificationAdapter.notifyItemInserted(insertAt); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     /** |  | ||||||
|      * Insert a status if not yet in the timeline |  | ||||||
|      * |  | ||||||
|      * @param notificationReceived - Notification coming from the api/db |  | ||||||
|      * @return int >= 0 |  STATUS_PRESENT = -1 | STATUS_AT_THE_BOTTOM = -2 |  | ||||||
|      */ |  | ||||||
|     private int insertNotification(Notification notificationReceived) { |  | ||||||
|         if (idOfAddedNotifications.contains(notificationReceived.id)) { |  | ||||||
|             return NOTIFICATION_PRESENT; |  | ||||||
|         } |  | ||||||
|                 int position = 0; |                 int position = 0; | ||||||
|                 //We loop through messages already in the timeline |                 //We loop through messages already in the timeline | ||||||
|                 if (this.notificationList != null) { |                 if (this.notificationList != null) { | ||||||
|  | @ -510,25 +506,22 @@ public class FragmentMastodonNotification extends Fragment implements Notificati | ||||||
|                         //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 |                         //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 |                         //Pinned messages are ignored because their date can be older | ||||||
|                         if (notificationReceived.id.compareTo(notificationsAlreadyPresent.id) > 0) { |                         if (notificationReceived.id.compareTo(notificationsAlreadyPresent.id) > 0) { | ||||||
|                     //We add the status to a list of id - thus we know it is already in the timeline |                             notificationList.add(position, notificationReceived); | ||||||
|                     idOfAddedNotifications.add(notificationReceived.id); |  | ||||||
|                     this.notificationList.add(position, notificationReceived); |  | ||||||
|                             notificationAdapter.notifyItemInserted(position); |                             notificationAdapter.notifyItemInserted(position); | ||||||
|                             break; |                             break; | ||||||
|                         } |                         } | ||||||
|                         position++; |                         position++; | ||||||
|                     } |                     } | ||||||
|                     //Statuses added at the bottom, we flag them by position = -2 for not dealing with them and fetch more |                     //Statuses added at the bottom, we flag them by position = -2 for not dealing with them and fetch more | ||||||
|             if (position == this.notificationList.size()) { |                     if (position == notificationList.size() && !notificationList.contains(notificationReceived)) { | ||||||
|                 //We add the status to a list of id - thus we know it is already in the timeline |                         notificationList.add(position, notificationReceived); | ||||||
|                 idOfAddedNotifications.add(notificationReceived.id); |  | ||||||
|                 this.notificationList.add(position, notificationReceived); |  | ||||||
|                         notificationAdapter.notifyItemInserted(position); |                         notificationAdapter.notifyItemInserted(position); | ||||||
|                 return NOTIFICATION__AT_THE_BOTTOM; |  | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|         return position; |  | ||||||
|             } |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|  |  | ||||||
|  | @ -391,20 +391,20 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. | ||||||
|         flagLoading = false; |         flagLoading = false; | ||||||
|         if (timelineStatuses != null && fetched_statuses != null && fetched_statuses.statuses != null && fetched_statuses.statuses.size() > 0) { |         if (timelineStatuses != null && fetched_statuses != null && fetched_statuses.statuses != null && fetched_statuses.statuses.size() > 0) { | ||||||
|             try { |             try { | ||||||
|  |                 if (statusToUpdate != null) { | ||||||
|                     new Thread(() -> { |                     new Thread(() -> { | ||||||
|                         StatusCache statusCache = new StatusCache(); |                         StatusCache statusCache = new StatusCache(); | ||||||
|                         statusCache.instance = BaseMainActivity.currentInstance; |                         statusCache.instance = BaseMainActivity.currentInstance; | ||||||
|                         statusCache.user_id = BaseMainActivity.currentUserID; |                         statusCache.user_id = BaseMainActivity.currentUserID; | ||||||
|                         statusCache.status = statusToUpdate; |                         statusCache.status = statusToUpdate; | ||||||
|                     if (statusToUpdate != null) { |  | ||||||
|                         statusCache.status_id = statusToUpdate.id; |                         statusCache.status_id = statusToUpdate.id; | ||||||
|                     } |  | ||||||
|                         try { |                         try { | ||||||
|                             new StatusCache(requireActivity()).updateIfExists(statusCache); |                             new StatusCache(requireActivity()).updateIfExists(statusCache); | ||||||
|                         } catch (DBException e) { |                         } catch (DBException e) { | ||||||
|                             e.printStackTrace(); |                             e.printStackTrace(); | ||||||
|                         } |                         } | ||||||
|                     }).start(); |                     }).start(); | ||||||
|  |                 } | ||||||
|             } catch (Exception ignored) { |             } catch (Exception ignored) { | ||||||
|             } |             } | ||||||
|             flagLoading = fetched_statuses.pagination.max_id == null; |             flagLoading = fetched_statuses.pagination.max_id == null; | ||||||
|  | @ -460,8 +460,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. | ||||||
|      * |      * | ||||||
|      * @param statusListReceived - List<Status> Statuses received |      * @param statusListReceived - List<Status> Statuses received | ||||||
|      */ |      */ | ||||||
|     private int updateStatusListWith(List<Status> statusListReceived) { |     private void updateStatusListWith(List<Status> statusListReceived) { | ||||||
|         int inserted = 0; |  | ||||||
|         if (statusListReceived != null && statusListReceived.size() > 0) { |         if (statusListReceived != null && statusListReceived.size() > 0) { | ||||||
|             for (Status statusReceived : statusListReceived) { |             for (Status statusReceived : statusListReceived) { | ||||||
|                 int position = 0; |                 int position = 0; | ||||||
|  | @ -477,7 +476,6 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. | ||||||
|                             if (!timelineStatuses.contains(statusReceived) && !statusReceived.pinned && timelineType != Timeline.TimeLineEnum.ACCOUNT_TIMELINE) { |                             if (!timelineStatuses.contains(statusReceived) && !statusReceived.pinned && timelineType != Timeline.TimeLineEnum.ACCOUNT_TIMELINE) { | ||||||
|                                 timelineStatuses.add(position, statusReceived); |                                 timelineStatuses.add(position, statusReceived); | ||||||
|                                 statusAdapter.notifyItemInserted(position); |                                 statusAdapter.notifyItemInserted(position); | ||||||
|                                 inserted++; |  | ||||||
|                             } |                             } | ||||||
|                             break; |                             break; | ||||||
|                         } |                         } | ||||||
|  | @ -492,7 +490,6 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         return inserted; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|  |  | ||||||
|  | @ -104,7 +104,7 @@ public class NotificationsVM extends AndroidViewModel { | ||||||
|         MastodonNotificationsService mastodonNotificationsService = init(timelineParams.instance); |         MastodonNotificationsService mastodonNotificationsService = init(timelineParams.instance); | ||||||
|         new Thread(() -> { |         new Thread(() -> { | ||||||
|             Notifications notifications = new Notifications(); |             Notifications notifications = new Notifications(); | ||||||
|             Call<List<Notification>> notificationsCall = mastodonNotificationsService.getNotifications(timelineParams.token, timelineParams.excludeType, timelineParams.userId, timelineParams.maxId, timelineParams.sinceId, timelineParams.minId, timelineParams.limit); |             Call<List<Notification>> notificationsCall = mastodonNotificationsService.getNotifications(timelineParams.token, timelineParams.excludeType, null, timelineParams.maxId, timelineParams.sinceId, timelineParams.minId, timelineParams.limit); | ||||||
|             if (notificationsCall != null) { |             if (notificationsCall != null) { | ||||||
|                 try { |                 try { | ||||||
|                     Response<List<Notification>> notificationsResponse = notificationsCall.execute(); |                     Response<List<Notification>> notificationsResponse = notificationsCall.execute(); | ||||||
|  | @ -125,7 +125,7 @@ public class NotificationsVM extends AndroidViewModel { | ||||||
|                                 statusCache.type = Timeline.TimeLineEnum.NOTIFICATION; |                                 statusCache.type = Timeline.TimeLineEnum.NOTIFICATION; | ||||||
|                                 statusCache.status_id = notification.id; |                                 statusCache.status_id = notification.id; | ||||||
|                                 try { |                                 try { | ||||||
|                                     statusCacheDAO.insertOrUpdate(statusCache, timelineParams.slug); |                                     statusCacheDAO.insertOrUpdate(statusCache, notification.type); | ||||||
|                                 } catch (DBException e) { |                                 } catch (DBException e) { | ||||||
|                                     e.printStackTrace(); |                                     e.printStackTrace(); | ||||||
|                                 } |                                 } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue