OSDN Git Service

ブロックの括弧を独立した行に書く (SA1500, SA1501, SA1502)
[opentween/open-tween.git] / OpenTween / ListAvailable.cs
index 48b2715..0a88f05 100644 (file)
@@ -5,24 +5,26 @@
 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
 // All rights reserved.
-// 
+//
 // This file is part of OpenTween.
-// 
+//
 // This program is free software; you can redistribute it and/or modify it
 // under the terms of the GNU General Public License as published by the Free
 // Software Foundation; either version 3 of the License, or (at your option)
 // any later version.
-// 
+//
 // This program is distributed in the hope that it will be useful, but
 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
-// for more details. 
-// 
+// for more details.
+//
 // You should have received a copy of the GNU General Public License along
 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
 // 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.ComponentModel;
@@ -32,29 +34,22 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
+using OpenTween.Models;
 
 namespace OpenTween
 {
     public partial class ListAvailable : OTBaseForm
     {
-        private ListElement _selectedList = null;
-        public ListElement SelectedList
-        {
-            get
-            {
-                return _selectedList;
-            }
-        }
+        public ListElement? SelectedList { get; private set; }
 
         public ListAvailable()
-        {
-            InitializeComponent();
-        }
+            => this.InitializeComponent();
 
         private void OK_Button_Click(object sender, EventArgs e)
         {
-            if (this.ListsList.SelectedIndex > -1) {
-                _selectedList = (ListElement)this.ListsList.SelectedItem;
+            if (this.ListsList.SelectedIndex > -1)
+            {
+                this.SelectedList = (ListElement)this.ListsList.SelectedItem;
                 this.DialogResult = System.Windows.Forms.DialogResult.OK;
                 this.Close();
             }
@@ -62,7 +57,7 @@ namespace OpenTween
 
         private void Cancel_Button_Click(object sender, EventArgs e)
         {
-            _selectedList = null;
+            this.SelectedList = null;
             this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
             this.Close();
         }
@@ -71,29 +66,30 @@ namespace OpenTween
         {
             using (ControlTransaction.Disabled(this))
             {
-                if (TabInformations.GetInstance().SubscribableLists.Count == 0)
-                    await this.RefreshLists();
+                try
+                {
+                    var lists = (IReadOnlyList<ListElement>)TabInformations.GetInstance().SubscribableLists;
+                    if (lists.Count == 0)
+                        lists = await this.FetchListsAsync();
 
-                this.ListsList.Items.AddRange(TabInformations.GetInstance().SubscribableLists.ToArray());
-                if (this.ListsList.Items.Count > 0)
+                    this.UpdateListsListBox(lists);
+                }
+                catch (OperationCanceledException)
                 {
-                    this.ListsList.SelectedIndex = 0;
+                    this.DialogResult = DialogResult.Cancel;
+                    return;
                 }
-                else
+                catch (WebApiException)
                 {
-                    this.UsernameLabel.Text = "";
-                    this.NameLabel.Text = "";
-                    this.StatusLabel.Text = "";
-                    this.MemberCountLabel.Text = "0";
-                    this.SubscriberCountLabel.Text = "0";
-                    this.DescriptionText.Text = "";
+                    this.DialogResult = DialogResult.Abort;
+                    return;
                 }
             }
         }
 
         private void ListsList_SelectedIndexChanged(object sender, EventArgs e)
         {
-            ListElement lst;
+            ListElement? lst;
             if (this.ListsList.SelectedIndex > -1)
             {
                 lst = (ListElement)this.ListsList.SelectedItem;
@@ -133,29 +129,53 @@ namespace OpenTween
         {
             using (ControlTransaction.Disabled(this))
             {
-                await this.RefreshLists();
-
-                this.ListsList.Items.Clear();
-                this.ListsList.Items.AddRange(TabInformations.GetInstance().SubscribableLists.ToArray());
-                if (this.ListsList.Items.Count > 0)
+                try
+                {
+                    var lists = await this.FetchListsAsync();
+                    this.UpdateListsListBox(lists);
+                }
+                catch (OperationCanceledException)
                 {
-                    this.ListsList.SelectedIndex = 0;
+                }
+                catch (WebApiException ex)
+                {
+                    MessageBox.Show("Failed to get lists. (" + ex.Message + ")");
                 }
             }
         }
 
-        private async Task RefreshLists()
+        private async Task<IReadOnlyList<ListElement>> FetchListsAsync()
         {
-            using (var dialog = new WaitingDialog("Getting Lists..."))
-            {
-                var tw = ((TweenMain)this.Owner).TwitterInstance;
-                var task = Task.Run(() => tw.GetListsApi());
+            using var dialog = new WaitingDialog("Getting Lists...");
+            var cancellationToken = dialog.EnableCancellation();
+
+            var tw = ((TweenMain)this.Owner).TwitterInstance;
+            var task = tw.GetListsApi();
+            await dialog.WaitForAsync(this, task);
 
-                var err = await dialog.WaitForAsync(this, task);
-                if (!string.IsNullOrEmpty(err))
+            cancellationToken.ThrowIfCancellationRequested();
+
+            return TabInformations.GetInstance().SubscribableLists;
+        }
+
+        private void UpdateListsListBox(IEnumerable<ListElement> lists)
+        {
+            using (ControlTransaction.Update(this.ListsList))
+            {
+                this.ListsList.Items.Clear();
+                this.ListsList.Items.AddRange(lists.ToArray());
+                if (this.ListsList.Items.Count > 0)
                 {
-                    MessageBox.Show("Failed to get lists. (" + err + ")");
-                    return;
+                    this.ListsList.SelectedIndex = 0;
+                }
+                else
+                {
+                    this.UsernameLabel.Text = "";
+                    this.NameLabel.Text = "";
+                    this.StatusLabel.Text = "";
+                    this.MemberCountLabel.Text = "0";
+                    this.SubscriberCountLabel.Text = "0";
+                    this.DescriptionText.Text = "";
                 }
             }
         }