fix(badge): Badge respects option in prefs

Fixes #516
This commit is contained in:
Ben Gotow 2015-11-30 19:17:31 -08:00
parent a413c69f3b
commit bd9181f62e
2 changed files with 26 additions and 35 deletions

View file

@ -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+")

View file

@ -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()