OSDN Git Service

メソッドに式本体を使用する (IDE0021, IDE0022, IDE0025, IDE0027)
[opentween/open-tween.git] / OpenTween / ListAvailable.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 // All rights reserved.
8 // 
9 // This file is part of OpenTween.
10 // 
11 // This program is free software; you can redistribute it and/or modify it
12 // under the terms of the GNU General Public License as published by the Free
13 // Software Foundation; either version 3 of the License, or (at your option)
14 // any later version.
15 // 
16 // This program is distributed in the hope that it will be useful, but
17 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 // for more details. 
20 // 
21 // You should have received a copy of the GNU General Public License along
22 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
23 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24 // Boston, MA 02110-1301, USA.
25
26 using System;
27 using System.Collections.Generic;
28 using System.ComponentModel;
29 using System.Data;
30 using System.Drawing;
31 using System.Linq;
32 using System.Text;
33 using System.Threading.Tasks;
34 using System.Windows.Forms;
35 using OpenTween.Models;
36
37 namespace OpenTween
38 {
39     public partial class ListAvailable : OTBaseForm
40     {
41         public ListElement SelectedList { get; private set; }
42
43         public ListAvailable()
44             => this.InitializeComponent();
45
46         private void OK_Button_Click(object sender, EventArgs e)
47         {
48             if (this.ListsList.SelectedIndex > -1) {
49                 this.SelectedList = (ListElement)this.ListsList.SelectedItem;
50                 this.DialogResult = System.Windows.Forms.DialogResult.OK;
51                 this.Close();
52             }
53         }
54
55         private void Cancel_Button_Click(object sender, EventArgs e)
56         {
57             this.SelectedList = null;
58             this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
59             this.Close();
60         }
61
62         private async void ListAvailable_Shown(object sender, EventArgs e)
63         {
64             using (ControlTransaction.Disabled(this))
65             {
66                 try
67                 {
68                     var lists = (IReadOnlyList<ListElement>)TabInformations.GetInstance().SubscribableLists;
69                     if (lists.Count == 0)
70                         lists = await this.FetchListsAsync();
71
72                     this.UpdateListsListBox(lists);
73                 }
74                 catch (OperationCanceledException)
75                 {
76                     this.DialogResult = DialogResult.Cancel;
77                     return;
78                 }
79                 catch (WebApiException)
80                 {
81                     this.DialogResult = DialogResult.Abort;
82                     return;
83                 }
84             }
85         }
86
87         private void ListsList_SelectedIndexChanged(object sender, EventArgs e)
88         {
89             ListElement lst;
90             if (this.ListsList.SelectedIndex > -1)
91             {
92                 lst = (ListElement)this.ListsList.SelectedItem;
93             }
94             else
95             {
96                 lst = null;
97             }
98             if (lst == null)
99             {
100                 this.UsernameLabel.Text = "";
101                 this.NameLabel.Text = "";
102                 this.StatusLabel.Text = "";
103                 this.MemberCountLabel.Text = "0";
104                 this.SubscriberCountLabel.Text = "0";
105                 this.DescriptionText.Text = "";
106             }
107             else
108             {
109                 this.UsernameLabel.Text = lst.Username + " / " + lst.Nickname;
110                 this.NameLabel.Text = lst.Name;
111                 if (lst.IsPublic)
112                 {
113                     this.StatusLabel.Text = "Public";
114                 }
115                 else
116                 {
117                     this.StatusLabel.Text = "Private";
118                 }
119                 this.MemberCountLabel.Text = lst.MemberCount.ToString("#,##0");
120                 this.SubscriberCountLabel.Text = lst.SubscriberCount.ToString("#,##0");
121                 this.DescriptionText.Text = lst.Description;
122             }
123         }
124
125         private async void RefreshButton_Click(object sender, EventArgs e)
126         {
127             using (ControlTransaction.Disabled(this))
128             {
129                 try
130                 {
131                     var lists = await this.FetchListsAsync();
132                     this.UpdateListsListBox(lists);
133                 }
134                 catch (OperationCanceledException) { }
135                 catch (WebApiException ex)
136                 {
137                     MessageBox.Show("Failed to get lists. (" + ex.Message + ")");
138                 }
139             }
140         }
141
142         private async Task<IReadOnlyList<ListElement>> FetchListsAsync()
143         {
144             using (var dialog = new WaitingDialog("Getting Lists..."))
145             {
146                 var cancellationToken = dialog.EnableCancellation();
147
148                 var tw = ((TweenMain)this.Owner).TwitterInstance;
149                 var task = tw.GetListsApi();
150                 await dialog.WaitForAsync(this, task);
151
152                 cancellationToken.ThrowIfCancellationRequested();
153             }
154
155             return TabInformations.GetInstance().SubscribableLists;
156         }
157
158         private void UpdateListsListBox(IEnumerable<ListElement> lists)
159         {
160             using (ControlTransaction.Update(this.ListsList))
161             {
162                 this.ListsList.Items.Clear();
163                 this.ListsList.Items.AddRange(lists.ToArray());
164                 if (this.ListsList.Items.Count > 0)
165                 {
166                     this.ListsList.SelectedIndex = 0;
167                 }
168                 else
169                 {
170                     this.UsernameLabel.Text = "";
171                     this.NameLabel.Text = "";
172                     this.StatusLabel.Text = "";
173                     this.MemberCountLabel.Text = "0";
174                     this.SubscriberCountLabel.Text = "0";
175                     this.DescriptionText.Text = "";
176                 }
177             }
178         }
179     }
180 }