using System.Drawing;
using System.Linq;
using System.Text;
+using System.Threading.Tasks;
using System.Windows.Forms;
+using OpenTween.Models;
namespace OpenTween
{
- public partial class ListAvailable : Form
+ public partial class ListAvailable : OTBaseForm
{
- private ListElement _selectedList = null;
- public ListElement SelectedList
- {
- get
- {
- return _selectedList;
- }
- }
+ public ListElement SelectedList { get; private set; }
+
+ public ListAvailable()
+ => 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();
}
private void Cancel_Button_Click(object sender, EventArgs e)
{
- _selectedList = null;
+ this.SelectedList = null;
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.Close();
}
- private void Cancel_Button_Shown(object sender, EventArgs e)
+ private async void ListAvailable_Shown(object sender, EventArgs e)
{
- if (TabInformations.GetInstance().SubscribableLists.Count == 0) this.RefreshLists();
- this.ListsList.Items.AddRange(TabInformations.GetInstance().SubscribableLists.ToArray());
- if (this.ListsList.Items.Count > 0)
+ using (ControlTransaction.Disabled(this))
{
- 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 = "";
+ try
+ {
+ var lists = (IReadOnlyList<ListElement>)TabInformations.GetInstance().SubscribableLists;
+ if (lists.Count == 0)
+ lists = await this.FetchListsAsync();
+
+ this.UpdateListsListBox(lists);
+ }
+ catch (OperationCanceledException)
+ {
+ this.DialogResult = DialogResult.Cancel;
+ return;
+ }
+ catch (WebApiException)
+ {
+ this.DialogResult = DialogResult.Abort;
+ return;
+ }
}
}
}
}
- private void RefreshButton_Click(object sender, EventArgs e)
+ private async void RefreshButton_Click(object sender, EventArgs e)
{
- this.RefreshLists();
- this.ListsList.Items.Clear();
- this.ListsList.Items.AddRange(TabInformations.GetInstance().SubscribableLists.ToArray());
- if (this.ListsList.Items.Count > 0)
+ using (ControlTransaction.Disabled(this))
{
- this.ListsList.SelectedIndex = 0;
+ try
+ {
+ var lists = await this.FetchListsAsync();
+ this.UpdateListsListBox(lists);
+ }
+ catch (OperationCanceledException) { }
+ catch (WebApiException ex)
+ {
+ MessageBox.Show("Failed to get lists. (" + ex.Message + ")");
+ }
}
}
- private void RefreshLists()
+ private async Task<IReadOnlyList<ListElement>> FetchListsAsync()
{
- using (FormInfo dlg = new FormInfo(this, "Getting Lists...", RefreshLists_DoWork))
- {
- dlg.ShowDialog();
- if (!String.IsNullOrEmpty(dlg.Result as String))
- {
- MessageBox.Show("Failed to get lists. (" + (String)dlg.Result + ")");
- return;
- }
- }
+ 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);
+
+ cancellationToken.ThrowIfCancellationRequested();
+
+ return TabInformations.GetInstance().SubscribableLists;
}
- private void RefreshLists_DoWork(object sender, DoWorkEventArgs e)
+ private void UpdateListsListBox(IEnumerable<ListElement> lists)
{
- try
- {
- e.Result = ((TweenMain)this.Owner).TwitterInstance.GetListsApi();
- }
- catch (InvalidCastException ex)
+ using (ControlTransaction.Update(this.ListsList))
{
- return;
+ this.ListsList.Items.Clear();
+ this.ListsList.Items.AddRange(lists.ToArray());
+ if (this.ListsList.Items.Count > 0)
+ {
+ 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 = "";
+ }
}
}
}