OSDN Git Service

TweenMain.UpdateTimelineSpeedメソッドをHomeTabModelクラスに移動
authorKimura Youichi <kim.upsilon@bucyou.net>
Sat, 14 May 2016 19:00:54 +0000 (04:00 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Fri, 20 May 2016 14:06:01 +0000 (23:06 +0900)
OpenTween/Models/HomeTabModel.cs
OpenTween/Tween.cs

index fe44419..cfa607c 100644 (file)
 // Boston, MA 02110-1301, USA.
 
 using System;
+using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace OpenTween.Models
@@ -38,6 +40,12 @@ namespace OpenTween.Models
         public override MyCommon.TabUsageType TabType
             => MyCommon.TabUsageType.Home;
 
+        public int TweetsPerHour => this.tweetsPerHour;
+
+        // 流速計測用
+        private int tweetsPerHour = 0;
+        private ConcurrentDictionary<DateTime, int> tweetsTimestamps = new ConcurrentDictionary<DateTime, int>();
+
         public HomeTabModel() : this(MyCommon.DEFAULTTAB.RECENT)
         {
         }
@@ -45,5 +53,41 @@ namespace OpenTween.Models
         public HomeTabModel(string tabName) : base(tabName)
         {
         }
+
+        public override void AddPostQueue(PostClass post)
+        {
+            base.AddPostQueue(post);
+            this.UpdateTimelineSpeed(post.CreatedAt);
+        }
+
+        /// <summary>
+        /// タイムラインに追加された発言件数を反映し、タイムラインの流速を更新します
+        /// </summary>
+        private void UpdateTimelineSpeed(DateTime postCreatedAt)
+        {
+            var now = DateTime.Now;
+
+            // 1 時間以上前の時刻は追加しない
+            var oneHour = TimeSpan.FromHours(1);
+            if (now - postCreatedAt > oneHour)
+                return;
+
+            this.tweetsTimestamps.AddOrUpdate(postCreatedAt, 1, (k, v) => v + 1);
+
+            var removeKeys = new List<DateTime>();
+            var tweetsInWindow = 0;
+            foreach (var pair in this.tweetsTimestamps)
+            {
+                if (now - pair.Key > oneHour)
+                    removeKeys.Add(pair.Key);
+                else
+                    tweetsInWindow += pair.Value;
+            }
+            Interlocked.Exchange(ref this.tweetsPerHour, tweetsInWindow);
+
+            int _;
+            foreach (var key in removeKeys)
+                this.tweetsTimestamps.TryRemove(key, out _);
+        }
     }
 }
index 2ef7f42..b4a70dc 100644 (file)
@@ -177,8 +177,6 @@ namespace OpenTween
         //時速表示用
         private List<DateTime> _postTimestamps = new List<DateTime>();
         private List<DateTime> _favTimestamps = new List<DateTime>();
-        private ConcurrentDictionary<DateTime, int> _tlTimestamps = new ConcurrentDictionary<DateTime, int>();
-        private int _tlCount;
 
         // 以下DrawItem関連
         private SolidBrush _brsHighLight = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight));
@@ -2286,9 +2284,6 @@ namespace OpenTween
                     this._statuses.SetReadHomeTab();
 
                 var addCount = this._statuses.DistributePosts();
-
-                if (!this._initial)
-                    this.UpdateTimelineSpeed(addCount);
             });
 
             if (ct.IsCancellationRequested)
@@ -2299,32 +2294,6 @@ namespace OpenTween
             this.RefreshTimeline();
         }
 
-        /// <summary>
-        /// タイムラインに追加された発言件数を反映し、タイムラインの流速を更新します
-        /// </summary>
-        /// <param name="addCount">直前にタイムラインに追加した発言件数</param>
-        private void UpdateTimelineSpeed(int addCount)
-        {
-            var now = DateTime.Now;
-            this._tlTimestamps.AddOrUpdate(now, addCount, (k, v) => v + addCount);
-
-            var removeKeys = new List<DateTime>();
-            var oneHour = TimeSpan.FromHours(1);
-            var tlCount = 0;
-            foreach (var pair in this._tlTimestamps)
-            {
-                if (now - pair.Key > oneHour)
-                    removeKeys.Add(pair.Key);
-                else
-                    tlCount += pair.Value;
-            }
-            Interlocked.Exchange(ref this._tlCount, tlCount);
-
-            int _;
-            foreach (var key in removeKeys)
-                this._tlTimestamps.TryRemove(key, out _);
-        }
-
         private Task GetReplyAsync()
         {
             return this.GetReplyAsync(loadMore: false);
@@ -9482,7 +9451,9 @@ namespace OpenTween
             UnreadCounter = ur;
             UnreadAtCounter = urat;
 
-            slbl.AppendFormat(Properties.Resources.SetStatusLabelText1, tur, tal, ur, al, urat, _postTimestamps.Count, _favTimestamps.Count, _tlCount);
+            var homeTab = this._statuses.GetTabByType<HomeTabModel>();
+
+            slbl.AppendFormat(Properties.Resources.SetStatusLabelText1, tur, tal, ur, al, urat, _postTimestamps.Count, _favTimestamps.Count, homeTab.TweetsPerHour);
             if (this._cfgCommon.TimelinePeriod == 0)
             {
                 slbl.Append(Properties.Resources.SetStatusLabelText2);
@@ -12849,8 +12820,6 @@ namespace OpenTween
 
             int rsltAddCount = _statuses.DistributePosts();
 
-            this.UpdateTimelineSpeed(rsltAddCount);
-
             if (this._cfgCommon.UserstreamPeriod > 0) return;
 
             // userStreamsRefreshing が 0 (インクリメント後は1) であれば RefreshTimeline を実行