OSDN Git Service

string.IsNullOrEmpty の nullable annotation あり版のメソッドを追加
[opentween/open-tween.git] / OpenTween / ToolStripAPIGauge.cs
index 2a3f02f..8143ebc 100644 (file)
@@ -19,6 +19,8 @@
 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
 // Boston, MA 02110-1301, USA.
 
+#nullable enable
+
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -38,10 +40,8 @@ namespace OpenTween
     public class ToolStripAPIGauge : ToolStripStatusLabel
     {
         public ToolStripAPIGauge()
-            : base()
         {
-            this.Text = "API ???/???";
-            this.ToolTipText = "API rest ???/???" + Environment.NewLine + "(reset after ??? minutes)";
+            UpdateText();
 
             this.DisplayStyle = ToolStripItemDisplayStyle.Text;
         }
@@ -53,7 +53,7 @@ namespace OpenTween
         [RefreshProperties(RefreshProperties.Repaint)]
         public int GaugeHeight
         {
-            get { return this._GaugeHeight; }
+            get => this._GaugeHeight;
             set
             {
                 this._GaugeHeight = value;
@@ -68,10 +68,10 @@ namespace OpenTween
         /// API 実行回数制限の値
         /// </summary>
         [Browsable(false)]
-        public ApiLimit ApiLimit
+        public ApiLimit? ApiLimit
         {
-            get { return this._ApiLimit; }
-            set
+            get => this._ApiLimit;
+            private set
             {
                 this._ApiLimit = value;
 
@@ -81,30 +81,68 @@ namespace OpenTween
                 this.Invalidate();
             }
         }
-        private ApiLimit _ApiLimit = null;
+        private ApiLimit? _ApiLimit = null;
+
+        /// <summary>
+        /// API エンドポイント名
+        /// </summary>
+        [Browsable(false)]
+        public string? ApiEndpoint
+        {
+            get => this._ApiEndpoint;
+            set
+            {
+                if (MyCommon.IsNullOrEmpty(value))
+                {
+                    // リセット
+                    this._ApiEndpoint = null;
+                    this.ApiLimit = null;
+                    return;
+                }
+
+                var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[value];
+
+                if (this._ApiEndpoint != value)
+                {
+                    // ApiEndpointが変更されているので更新する
+                    this._ApiEndpoint = value;
+                    this.ApiLimit = apiLimit;
+                }
+                else
+                {
+                    // ApiLimitが変更されていれば更新する
+                    if (this._ApiLimit == null ||
+                        !this._ApiLimit.Equals(apiLimit))
+                    {
+                        this.ApiLimit = apiLimit;
+                    }
+                }
+            }
+        }
+        private string? _ApiEndpoint = null;
 
         [Browsable(false)]
         [EditorBrowsable(EditorBrowsableState.Never)]
         public new string Text
         {
-            get { return base.Text; }
-            set { base.Text = value; }
+            get => base.Text;
+            set => base.Text = value;
         }
 
         [Browsable(false)]
         [EditorBrowsable(EditorBrowsableState.Never)]
         public new string ToolTipText
         {
-            get { return base.ToolTipText; }
-            set { base.ToolTipText = value; }
+            get => base.ToolTipText;
+            set => base.ToolTipText = value;
         }
 
         [DefaultValue(ToolStripItemDisplayStyle.Text)]
         [RefreshProperties(RefreshProperties.Repaint)]
         public new ToolStripItemDisplayStyle DisplayStyle
         {
-            get { return base.DisplayStyle; }
-            set { base.DisplayStyle = value; }
+            get => base.DisplayStyle;
+            set => base.DisplayStyle = value;
         }
 
         protected double remainMinutes = -1;
@@ -112,7 +150,7 @@ namespace OpenTween
         protected virtual void UpdateRemainMinutes()
         {
             if (this._ApiLimit != null)
-                this.remainMinutes = (this._ApiLimit.AccessLimitResetDate - DateTime.Now).TotalMinutes;
+                this.remainMinutes = (this._ApiLimit.AccessLimitResetDate - DateTimeUtc.Now).TotalMinutes;
             else
                 this.remainMinutes = -1;
         }
@@ -171,7 +209,7 @@ namespace OpenTween
             string maxCountText;
             string minuteText;
 
-            if (this._ApiLimit == null)
+            if (this._ApiLimit == null || this.remainMinutes < 0)
             {
                 remainCountText = "???";
                 maxCountText = "???";
@@ -184,14 +222,16 @@ namespace OpenTween
                 minuteText = Math.Ceiling(this.remainMinutes).ToString();
             }
 
+            var endpointText = MyCommon.IsNullOrEmpty(this._ApiEndpoint) ? "unknown" : this._ApiEndpoint;
+
             var textFormat = "API {0}/{1}";
             this.Text = string.Format(textFormat, remainCountText, maxCountText);
 
             var toolTipTextFormat =
-                "API rest {0}/{1}" + Environment.NewLine +
-                "(reset after {2} minutes)";
+                "API rest {0} {1}/{2}" + Environment.NewLine +
+                "(reset after {3} minutes)";
 
-            this.ToolTipText = String.Format(toolTipTextFormat, remainCountText, maxCountText, minuteText);
+            this.ToolTipText = string.Format(toolTipTextFormat, endpointText, remainCountText, maxCountText, minuteText);
         }
 
         #endregion