Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start accounting for notificationPreferences in the unread indicator for Chat Rooms #3766

Merged
merged 4 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
thienlnam marked this conversation as resolved.
Show resolved Hide resolved
} 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.
thienlnam marked this conversation as resolved.
Show resolved Hide resolved
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;
thienlnam marked this conversation as resolved.
Show resolved Hide resolved
document.title = hasUnread ? `(NEW!) ${CONFIG.SITE_TITLE}` : CONFIG.SITE_TITLE;
document.getElementById('favicon').href = hasUnread ? CONFIG.FAVICON.UNREAD : CONFIG.FAVICON.DEFAULT;
}
Expand Down