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()
39 InitializeComponent();
42 private readonly List<string> _tlEndpoints = new List<string>
44 "/statuses/home_timeline",
45 "/statuses/mentions_timeline",
47 "/statuses/user_timeline",
50 "/direct_messages/sent",
55 private void ApiInfoDialog_Shown(object sender, EventArgs e)
58 var group = this.ListViewApi.Groups[0];
59 foreach (var endpoint in _tlEndpoints)
61 var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[endpoint];
62 AddListViewItem(endpoint, apiLimit, group);
66 group = this.ListViewApi.Groups[1];
67 var apiStatuses = MyCommon.TwitterApiInfo.AccessLimit.Where(x => !_tlEndpoints.Contains(x.Key)).OrderBy(x => x.Key);
68 foreach (var (endpoint, apiLimit) in apiStatuses)
70 AddListViewItem(endpoint, apiLimit, group);
73 MyCommon.TwitterApiInfo.AccessLimitUpdated += this.TwitterApiStatus_AccessLimitUpdated;
76 private void AddListViewItem(string endpoint, ApiLimit apiLimit, ListViewGroup group)
81 apiLimit.AccessLimitRemain + "/" + apiLimit.AccessLimitCount,
82 apiLimit.AccessLimitResetDate.ToLocalTimeString(),
84 var item = new ListViewItem(subitems)
89 this.ListViewApi.Items.Add(item);
92 private void UpdateEndpointLimit(string endpoint)
94 var item = this.ListViewApi.Items.Cast<ListViewItem>().FirstOrDefault(x => x.SubItems[0].Text == endpoint);
97 var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[endpoint];
98 item.SubItems[1].Text = apiLimit.AccessLimitRemain + "/" + apiLimit.AccessLimitCount;
99 item.SubItems[2].Text = apiLimit.AccessLimitResetDate.ToLocalTimeString();
103 private async void TwitterApiStatus_AccessLimitUpdated(object sender, EventArgs e)
107 if (this.InvokeRequired && !this.IsDisposed)
109 await this.InvokeAsync(() => this.TwitterApiStatus_AccessLimitUpdated(sender, e));
113 var endpoint = (e as TwitterApiStatus.AccessLimitUpdatedEventArgs).EndpointName;
114 UpdateEndpointLimit(endpoint);
117 catch (ObjectDisposedException)
121 catch (InvalidOperationException)
127 private void ApiInfoDialog_FormClosing(object sender, FormClosingEventArgs e)
129 MyCommon.TwitterApiInfo.AccessLimitUpdated -= this.TwitterApiStatus_AccessLimitUpdated;
132 protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
134 base.ScaleControl(factor, specified);
135 ScaleChildControl(this.ListViewApi, factor);
140 public class BufferedListView : ListView
142 public BufferedListView()
144 DoubleBuffered = true;