Skip to content

Commit

Permalink
Merge pull request #3766 from Expensify/yuwen-LHNPrefs
Browse files Browse the repository at this point in the history
Start accounting for notificationPreferences in the unread indicator for Chat Rooms
  • Loading branch information
jasperhuangg authored Jun 29, 2021
2 parents 25ce008 + fd74b95 commit 1bfa5d2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
10 changes: 9 additions & 1 deletion desktop/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,15 @@ const mainWindow = (() => {
// Listen to badge updater event emitted by the render process
// and update the app badge count (MacOS only)
ipcMain.on(ELECTRON_EVENTS.REQUEST_UPDATE_BADGE_COUNT, (event, totalCount) => {
app.setBadgeCount(totalCount);
if (totalCount === -1) {
// The electron docs say you should be able to update this and pass no parameters to set the badge
// to a single red dot, but in practice it resulted in an error "TypeError: Insufficient number of
// arguments." - Thus, setting to 1 instead.
// See: https://www.electronjs.org/docs/api/app#appsetbadgecountcount-linux-macos
app.setBadgeCount(1);
} else {
app.setBadgeCount(totalCount);
}
});

return browserWindow;
Expand Down
5 changes: 5 additions & 0 deletions src/CONST.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions src/libs/UnreadIndicatorUpdater/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import ONYXKEYS from '../../ONYXKEYS';
import CONST from '../../CONST';
import updateUnread from './updateUnread';

// Stash the unread action counts for each report
Expand All @@ -11,7 +12,14 @@ const unreadActionCounts = {};
* and Mac OS or iOS dock icon with an unread indicator.
*/
const throttledUpdatePageTitleAndUnreadCount = _.throttle(() => {
const totalCount = _.reduce(unreadActionCounts, (total, reportCount) => total + reportCount, 0);
// If all of our nonzero unread action counts show -1, update the unread count to be -1 as well so we don't show a
// number in the indicator badge.
if (_.every(unreadActionCounts, count => count < 1) && _.some(unreadActionCounts, count => count === -1)) {
updateUnread(-1);
return;
}

const totalCount = _.reduce(unreadActionCounts, (total, reportCount) => total + Math.max(reportCount, 0), 0);
updateUnread(totalCount);
}, 1000, {leading: false});

Expand All @@ -29,7 +37,17 @@ function listenForReportChanges() {
return;
}

unreadActionCounts[report.reportID] = report.unreadActionCount || 0;
if (report.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE) {
return;
}

// An unreadActionCount of -1 signifies that we should show a badge icon with no number
if (report.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.DAILY
&& report.unreadActionCount > 0) {
unreadActionCounts[report.reportID] = -1;
} else {
unreadActionCounts[report.reportID] = report.unreadActionCount || 0;
}
throttledUpdatePageTitleAndUnreadCount();
},
});
Expand Down
6 changes: 5 additions & 1 deletion src/libs/UnreadIndicatorUpdater/updateUnread/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {UrbanAirship} from 'urbanairship-react-native';
* @param {Number} totalCount
*/
function updateUnread(totalCount) {
UrbanAirship.setBadgeNumber(totalCount);
if (totalCount === -1) {
UrbanAirship.setBadgeNumber(1);
} else {
UrbanAirship.setBadgeNumber(totalCount);
}
}

export default updateUnread;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import CONFIG from '../../../CONFIG';
* @param {Number} totalCount
*/
function updateUnread(totalCount) {
const hasUnread = totalCount > 0;
const hasUnread = totalCount !== 0;
document.title = hasUnread ? `(NEW!) ${CONFIG.SITE_TITLE}` : CONFIG.SITE_TITLE;
document.getElementById('favicon').href = hasUnread ? CONFIG.FAVICON.UNREAD : CONFIG.FAVICON.DEFAULT;
}
Expand Down

0 comments on commit 1bfa5d2

Please sign in to comment.