1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 spx (@5px)
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
25 using System.Collections.Generic;
26 using System.ComponentModel;
31 using System.Threading.Tasks;
32 using System.Windows.Forms;
34 using OpenTween.Api.TwitterV2;
38 public partial class ApiInfoDialog : OTBaseForm
40 public ApiInfoDialog()
41 => this.InitializeComponent();
43 private readonly List<string> tlEndpoints = new()
45 GetTimelineRequest.EndpointName,
46 "/statuses/mentions_timeline",
48 "/statuses/user_timeline",
50 "/direct_messages/events/list",
52 "/direct_messages/sent",
57 private void ApiInfoDialog_Shown(object sender, EventArgs e)
60 var group = this.ListViewApi.Groups[0];
61 foreach (var endpoint in this.tlEndpoints)
63 var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[endpoint];
67 this.AddListViewItem(endpoint, apiLimit, group);
71 group = this.ListViewApi.Groups[1];
72 var apiStatuses = MyCommon.TwitterApiInfo.AccessLimit.Where(x => !this.tlEndpoints.Contains(x.Key)).OrderBy(x => x.Key);
73 foreach (var (endpoint, apiLimit) in apiStatuses)
75 this.AddListViewItem(endpoint, apiLimit, group);
78 MyCommon.TwitterApiInfo.AccessLimitUpdated += this.TwitterApiStatus_AccessLimitUpdated;
81 private void AddListViewItem(string endpoint, ApiLimit apiLimit, ListViewGroup group)
86 apiLimit.AccessLimitRemain + "/" + apiLimit.AccessLimitCount,
87 apiLimit.AccessLimitResetDate.ToLocalTimeString(),
89 var item = new ListViewItem(subitems)
94 this.ListViewApi.Items.Add(item);
97 private void UpdateEndpointLimit(string endpoint)
99 var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[endpoint];
100 if (apiLimit != null)
102 var item = this.ListViewApi.Items.Cast<ListViewItem>().Single(x => x.SubItems[0].Text == endpoint);
103 item.SubItems[1].Text = apiLimit.AccessLimitRemain + "/" + apiLimit.AccessLimitCount;
104 item.SubItems[2].Text = apiLimit.AccessLimitResetDate.ToLocalTimeString();
108 private async void TwitterApiStatus_AccessLimitUpdated(object sender, EventArgs e)
112 if (this.InvokeRequired && !this.IsDisposed)
114 await this.InvokeAsync(() => this.TwitterApiStatus_AccessLimitUpdated(sender, e));
118 var endpoint = ((TwitterApiStatus.AccessLimitUpdatedEventArgs)e).EndpointName;
119 if (endpoint != null)
120 this.UpdateEndpointLimit(endpoint);
123 catch (ObjectDisposedException)
127 catch (InvalidOperationException)
133 private void ApiInfoDialog_FormClosing(object sender, FormClosingEventArgs e)
134 => MyCommon.TwitterApiInfo.AccessLimitUpdated -= this.TwitterApiStatus_AccessLimitUpdated;
136 protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
138 base.ScaleControl(factor, specified);
139 ScaleChildControl(this.ListViewApi, factor);
144 public class BufferedListView : ListView
146 public BufferedListView()
147 => this.DoubleBuffered = true;