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.
23 using System.Collections.Generic;
24 using System.ComponentModel;
29 using System.Threading.Tasks;
30 using System.Windows.Forms;
35 public partial class ApiInfoDialog : OTBaseForm
37 public ApiInfoDialog()
38 => this.InitializeComponent();
40 private readonly List<string> _tlEndpoints = new List<string>
42 "/statuses/home_timeline",
43 "/statuses/mentions_timeline",
45 "/statuses/user_timeline",
47 "/direct_messages/events/list",
49 "/direct_messages/sent",
54 private void ApiInfoDialog_Shown(object sender, EventArgs e)
57 var group = this.ListViewApi.Groups[0];
58 foreach (var endpoint in _tlEndpoints)
60 var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[endpoint];
61 AddListViewItem(endpoint, apiLimit, group);
65 group = this.ListViewApi.Groups[1];
66 var apiStatuses = MyCommon.TwitterApiInfo.AccessLimit.Where(x => !_tlEndpoints.Contains(x.Key)).OrderBy(x => x.Key);
67 foreach (var (endpoint, apiLimit) in apiStatuses)
69 AddListViewItem(endpoint, apiLimit, group);
72 MyCommon.TwitterApiInfo.AccessLimitUpdated += this.TwitterApiStatus_AccessLimitUpdated;
75 private void AddListViewItem(string endpoint, ApiLimit apiLimit, ListViewGroup group)
80 apiLimit.AccessLimitRemain + "/" + apiLimit.AccessLimitCount,
81 apiLimit.AccessLimitResetDate.ToLocalTimeString(),
83 var item = new ListViewItem(subitems)
88 this.ListViewApi.Items.Add(item);
91 private void UpdateEndpointLimit(string endpoint)
93 var item = this.ListViewApi.Items.Cast<ListViewItem>().FirstOrDefault(x => x.SubItems[0].Text == endpoint);
96 var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[endpoint];
97 item.SubItems[1].Text = apiLimit.AccessLimitRemain + "/" + apiLimit.AccessLimitCount;
98 item.SubItems[2].Text = apiLimit.AccessLimitResetDate.ToLocalTimeString();
102 private async void TwitterApiStatus_AccessLimitUpdated(object sender, EventArgs e)
106 if (this.InvokeRequired && !this.IsDisposed)
108 await this.InvokeAsync(() => this.TwitterApiStatus_AccessLimitUpdated(sender, e));
112 var endpoint = (e as TwitterApiStatus.AccessLimitUpdatedEventArgs).EndpointName;
113 UpdateEndpointLimit(endpoint);
116 catch (ObjectDisposedException)
120 catch (InvalidOperationException)
126 private void ApiInfoDialog_FormClosing(object sender, FormClosingEventArgs e)
127 => MyCommon.TwitterApiInfo.AccessLimitUpdated -= this.TwitterApiStatus_AccessLimitUpdated;
129 protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
131 base.ScaleControl(factor, specified);
132 ScaleChildControl(this.ListViewApi, factor);
137 public class BufferedListView : ListView
139 public BufferedListView()
140 => this.DoubleBuffered = true;