From 99c20f3b569e6eaeb36167442bbe9ccadc79a357 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Mon, 30 Nov 2015 19:17:31 -0800 Subject: [PATCH] fix(badge): Badge respects option in prefs Fixes #516 --- spec/stores/unread-badge-store-spec.coffee | 12 ++++-- src/flux/stores/unread-badge-store.coffee | 49 ++++++++-------------- 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/spec/stores/unread-badge-store-spec.coffee b/spec/stores/unread-badge-store-spec.coffee index 35be11c13..cd8b385d5 100644 --- a/spec/stores/unread-badge-store-spec.coffee +++ b/spec/stores/unread-badge-store-spec.coffee @@ -5,11 +5,15 @@ describe "UnreadBadgeStore", -> describe "_setBadgeForCount", -> it "should set the badge correctly", -> spyOn(UnreadBadgeStore, '_setBadge') - UnreadBadgeStore._setBadgeForCount(0) + UnreadBadgeStore._count = 0 + UnreadBadgeStore._setBadgeForCount() expect(UnreadBadgeStore._setBadge).toHaveBeenCalledWith("") - UnreadBadgeStore._setBadgeForCount(1) + UnreadBadgeStore._count = 1 + UnreadBadgeStore._setBadgeForCount() expect(UnreadBadgeStore._setBadge).toHaveBeenCalledWith("1") - UnreadBadgeStore._setBadgeForCount(100) + UnreadBadgeStore._count = 100 + UnreadBadgeStore._setBadgeForCount() expect(UnreadBadgeStore._setBadge).toHaveBeenCalledWith("100") - UnreadBadgeStore._setBadgeForCount(1000) + UnreadBadgeStore._count = 1000 + UnreadBadgeStore._setBadgeForCount() expect(UnreadBadgeStore._setBadge).toHaveBeenCalledWith("999+") diff --git a/src/flux/stores/unread-badge-store.coffee b/src/flux/stores/unread-badge-store.coffee index 715af4ff0..36cb06f10 100644 --- a/src/flux/stores/unread-badge-store.coffee +++ b/src/flux/stores/unread-badge-store.coffee @@ -2,19 +2,18 @@ Reflux = require 'reflux' _ = require 'underscore' NylasStore = require 'nylas-store' CategoryStore = require './category-store' -DatabaseStore = require './database-store' ThreadCountsStore = require './thread-counts-store' class UnreadBadgeStore extends NylasStore constructor: -> - @listenTo CategoryStore, @_onCategoriesChanged - @listenTo ThreadCountsStore, @_onCountsChanged - @_category = CategoryStore.getStandardCategory('inbox') + @_count = 0 - NylasEnv.config.observe 'core.showUnreadBadge', (val) => + @listenTo CategoryStore, @_updateCount + @listenTo ThreadCountsStore, @_updateCount + NylasEnv.config.observe 'core.notifications.unreadBadge', (val) => if val is true - @_setBadgeForCount(@_count) + @_setBadgeForCount() else @_setBadge("") @@ -24,43 +23,31 @@ class UnreadBadgeStore extends NylasStore count: -> @_count - _onCategoriesChanged: => - cat = CategoryStore.getStandardCategory('inbox') - if not cat - @_count = 0 - @_category = null - @_setBadgeForCount() - @trigger() - else if not @_category or cat.id isnt @_category.id - @_category = cat - @_updateCount() - - _onCountsChanged: => - @_updateCount() - _updateCount: => - return unless NylasEnv.isMainWindow() - return unless @_category + category = CategoryStore.getStandardCategory('inbox') + if category + count = ThreadCountsStore.unreadCountForCategoryId(category.id) ? 0 + else + count = 0 - count = ThreadCountsStore.unreadCountForCategoryId(@_category.id) ? 0 return if @_count is count @_count = count - @_setBadgeForCount(count) + @_setBadgeForCount() @trigger() - _setBadgeForCount: (count) => - if count > 999 + _setBadgeForCount: => + return unless NylasEnv.config.get('core.notifications.unreadBadge') + return unless NylasEnv.isMainWindow() or NylasEnv.inSpecMode() + + if @_count > 999 @_setBadge("999+") - else if count > 0 - @_setBadge("#{count}") + else if @_count > 0 + @_setBadge("#{@_count}") else @_setBadge("") _setBadge: (val) => - # NOTE: Do not underestimate how long this can take. It's a synchronous - # remote call and can take ~50+msec. - return if NylasEnv.config.get('core.showUnreadBadge') is false require('electron').ipcRenderer.send('set-badge-value', val) module.exports = new UnreadBadgeStore()