OSDN Git Service

using var を使用する
[opentween/open-tween.git] / OpenTween / ListAvailable.cs
index 199498c..6df7500 100644 (file)
@@ -32,29 +32,21 @@ 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;
+                this.SelectedList = (ListElement)this.ListsList.SelectedItem;
                 this.DialogResult = System.Windows.Forms.DialogResult.OK;
                 this.Close();
             }
@@ -62,7 +54,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();
         }
@@ -79,7 +71,16 @@ namespace OpenTween
 
                     this.UpdateListsListBox(lists);
                 }
-                catch (WebApiException) { }
+                catch (OperationCanceledException)
+                {
+                    this.DialogResult = DialogResult.Cancel;
+                    return;
+                }
+                catch (WebApiException)
+                {
+                    this.DialogResult = DialogResult.Abort;
+                    return;
+                }
             }
         }
 
@@ -130,24 +131,24 @@ namespace OpenTween
                     var lists = await this.FetchListsAsync();
                     this.UpdateListsListBox(lists);
                 }
-                catch (WebApiException) { }
+                catch (OperationCanceledException) { }
+                catch (WebApiException ex)
+                {
+                    MessageBox.Show("Failed to get lists. (" + ex.Message + ")");
+                }
             }
         }
 
         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 err = await dialog.WaitForAsync(this, task);
-                if (!string.IsNullOrEmpty(err))
-                {
-                    MessageBox.Show("Failed to get lists. (" + err + ")");
-                    throw new WebApiException(err);
-                }
-            }
+            var tw = ((TweenMain)this.Owner).TwitterInstance;
+            var task = tw.GetListsApi();
+            await dialog.WaitForAsync(this, task);
+
+            cancellationToken.ThrowIfCancellationRequested();
 
             return TabInformations.GetInstance().SubscribableLists;
         }