From 01918049b4876dc3e4aebe79c2e2fa2be93024a7 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Thu, 25 May 2017 11:28:31 -0700 Subject: [PATCH] Keep last seen indicator around for five seconds Helps calm the user experience a little more, makes it easier to understand what's happening when messages are coming in quickly. FREEBIE --- js/views/conversation_view.js | 15 ++++++++++++--- js/views/last_seen_indicator_view.js | 15 +++++++++++++++ test/views/last_seen_indicator_view_test.js | 7 +++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/js/views/conversation_view.js b/js/views/conversation_view.js index 693e27d9e..1e1ca8627 100644 --- a/js/views/conversation_view.js +++ b/js/views/conversation_view.js @@ -251,8 +251,11 @@ } }, - removeLastSeenIndicator: function() { - if (this.lastSeenIndicator) { + removeLastSeenIndicator: function(options) { + options = options || {}; + _.defaults(options, {force: false}); + + if (this.lastSeenIndicator && (options.force || this.lastSeenIndicator.isOldEnough())) { this.lastSeenIndicator.remove(); this.lastSeenIndicator = null; } @@ -275,7 +278,7 @@ options = options || {}; _.defaults(options, {scroll: true}); - this.removeLastSeenIndicator(); + this.removeLastSeenIndicator({force: true}); var oldestUnread = this.model.messageCollection.find(function(model) { return model.get('unread'); @@ -341,6 +344,12 @@ this.model.messageCollection.add(message, {merge: true}); message.setToExpire(); + // if the last seen indicator is old enough, it will go away. + // if it isn't, we want to make sure it's up to date + if (this.lastSeenIndicator) { + this.lastSeenIndicator.increment(1); + } + if (!this.isHidden() && !window.isFocused()) { this.updateLastSeenIndicator({scroll: false}); } diff --git a/js/views/last_seen_indicator_view.js b/js/views/last_seen_indicator_view.js index 08ed82945..bef573a4b 100644 --- a/js/views/last_seen_indicator_view.js +++ b/js/views/last_seen_indicator_view.js @@ -5,12 +5,27 @@ 'use strict'; window.Whisper = window.Whisper || {}; + var FIVE_SECONDS = 5 * 1000; + Whisper.LastSeenIndicatorView = Whisper.View.extend({ className: 'last-seen-indicator-view', templateName: 'last-seen-indicator-view', initialize: function(options) { options = options || {}; this.count = options.count || 0; + this.start = Date.now(); + }, + + isOldEnough: function() { + var now = Date.now(); + if (now - this.start > FIVE_SECONDS) { + return true; + } + }, + + increment: function(count) { + this.count += count; + this.render(); }, render_attributes: function() { diff --git a/test/views/last_seen_indicator_view_test.js b/test/views/last_seen_indicator_view_test.js index 8ae5d9f3c..179715a73 100644 --- a/test/views/last_seen_indicator_view_test.js +++ b/test/views/last_seen_indicator_view_test.js @@ -9,4 +9,11 @@ describe('LastSeenIndicatorView', function() { assert.equal(view.count, 10); }); + it('increments count', function() { + var view = new Whisper.LastSeenIndicatorView({count: 4}); + assert.equal(view.count, 4); + view.increment(3); + assert.equal(view.count, 7); + }); + });