Skip to content

Commit

Permalink
Merge pull request #30133 from allroundexperts/fix-28536
Browse files Browse the repository at this point in the history
fix: notification count for empty reports
  • Loading branch information
Gonals authored Dec 5, 2023
2 parents 132f006 + bbe1a98 commit a7af90e
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions src/libs/UnreadIndicatorUpdater/index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,29 @@
import {InteractionManager} from 'react-native';
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import Navigation, {navigationRef} from '@navigation/Navigation';
import ONYXKEYS from '@src/ONYXKEYS';
import updateUnread from './updateUnread/index';

let previousUnreadCount = 0;
let allReports = [];

const triggerUnreadUpdate = () => {
const currentReportID = navigationRef.isReady() ? Navigation.getTopmostReportId() : '';

// We want to keep notification count consistent with what can be accessed from the LHN list
const unreadReports = _.filter(allReports, (report) => ReportUtils.isUnread(report) && ReportUtils.shouldReportBeInOptionList(report, currentReportID));
updateUnread(_.size(unreadReports));
};

Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (reportsFromOnyx) => {
if (!reportsFromOnyx) {
return;
}

/**
* We need to wait until after interactions have finished to update the unread count because otherwise
* the unread count will be updated while the interactions/animations are in progress and we don't want
* to put more work on the main thread.
*
* For eg. On web we are manipulating DOM and it makes it a better candidate to wait until after interactions
* have finished.
*
* More info: https://reactnative.dev/docs/interactionmanager
*/
InteractionManager.runAfterInteractions(() => {
const unreadReports = _.filter(reportsFromOnyx, (report) => ReportUtils.isUnread(report) && report.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN);
const unreadReportsCount = _.size(unreadReports);
if (previousUnreadCount !== unreadReportsCount) {
previousUnreadCount = unreadReportsCount;
updateUnread(unreadReportsCount);
}
});
allReports = reportsFromOnyx;
triggerUnreadUpdate();
},
});

navigationRef.addListener('state', () => {
triggerUnreadUpdate();
});

0 comments on commit a7af90e

Please sign in to comment.