Async loading

This commit is contained in:
2020-02-27 18:16:23 +01:00
parent 224a5e07df
commit 82722e77d6
2 changed files with 74 additions and 24 deletions

View File

@@ -10,6 +10,7 @@ using EstusShots.Shared.Models;
using Gtk;
using Application = Gtk.Application;
using DateTime = System.DateTime;
using Task = System.Threading.Tasks.Task;
using UI = Gtk.Builder.ObjectAttribute;
namespace EstusShots.Gtk
@@ -19,13 +20,14 @@ namespace EstusShots.Gtk
private const string ApiUrl = "http://localhost:5000/api/";
private EstusShotsClient Client { get; }
private BindableListView<Season> SeasonsView { get; }
[UI] private readonly TreeView _seasonsView = null;
[UI] private readonly Button _loadButton = null;
[UI] private readonly Button _newSeasonButton = null;
[UI] private readonly Label _infoLabel = null;
private BindableListView<Season> SeasonsView { get; set; }
[UI] private readonly Overlay _seasonsOverlay = null;
[UI] private readonly Box _loadingSpinner = null;
public MainWindow() : this(new Builder("MainWindow.glade")) { }
@@ -44,7 +46,6 @@ namespace EstusShots.Gtk
};
SeasonsView = new BindableListView<Season>(seasonsColumns, nameof(Season.SeasonId) ,_seasonsView);
SeasonsView.OnSelectionChanged += SeasonsViewOnOnSelectionChanged;
Info("Application Started");
}
@@ -54,14 +55,9 @@ namespace EstusShots.Gtk
Info($"Season '{season.DisplayName}' selected");
}
private void NewSeasonButtonOnClicked(object sender, EventArgs e)
private async void NewSeasonButtonOnClicked(object sender, EventArgs e)
{
if (!SeasonsView.Items.Any())
{
Info("Cannot add Season (Not Loaded)");
return;
}
var nextNum = SeasonsView.Items.Max(x => x.Number) + 1 ;
var nextNum = SeasonsView.Items.Any() ? SeasonsView.Items.Max(x => x.Number) + 1 : 1 ;
var season = new Season
{
Game = "Test Game",
@@ -72,7 +68,7 @@ namespace EstusShots.Gtk
var client = new HttpClient();
try
{
var response = client.PostAsync(ApiUrl + "season", content).Result;
var response = await client.PostAsync(ApiUrl + "season", content);
if (!response.IsSuccessStatusCode)
{
@@ -80,8 +76,8 @@ namespace EstusShots.Gtk
return;
}
ReloadSeasons();
Info($"Created new Season");
await ReloadSeasons();
Info("Created new Season");
}
catch (Exception ex)
{
@@ -90,17 +86,20 @@ namespace EstusShots.Gtk
}
}
private void LoadButtonClicked(object sender, EventArgs a)
private async void LoadButtonClicked(object sender, EventArgs a)
{
ReloadSeasons();
Info("Loading Seasons...");
await ReloadSeasons();
Info("List Refreshed");
}
private void ReloadSeasons()
private async Task ReloadSeasons()
{
var seasons = Client.GetSeasons().Result;
LoadingMode(true);
var seasons = await Task.Factory.StartNew(() => Client.GetSeasons().Result);
SeasonsView.Items = seasons;
SeasonsView.DataBind();
LoadingMode(false);
}
private void Window_DeleteEvent(object sender, DeleteEventArgs a)
@@ -108,6 +107,17 @@ namespace EstusShots.Gtk
Application.Quit();
}
private void LoadingMode(bool active)
{
_loadButton.Sensitive = !active;
_newSeasonButton.Sensitive = !active;
_seasonsView.Sensitive = !active;
if (active)
_seasonsOverlay.AddOverlay(_loadingSpinner);
else
_seasonsOverlay.Remove(_loadingSpinner);
}
private void Info(string message)
{
_infoLabel.Text = message;

View File

@@ -20,19 +20,28 @@
<property name="margin_bottom">2</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkScrolledWindow">
<object class="GtkOverlay" id="_seasonsOverlay">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<property name="can_focus">False</property>
<child>
<object class="GtkTreeView" id="_seasonsView">
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="enable_grid_lines">horizontal</property>
<child internal-child="selection">
<object class="GtkTreeSelection"/>
<property name="shadow_type">in</property>
<child>
<object class="GtkTreeView" id="_seasonsView">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="enable_grid_lines">horizontal</property>
<child internal-child="selection">
<object class="GtkTreeSelection"/>
</child>
</object>
</child>
</object>
<packing>
<property name="index">-1</property>
</packing>
</child>
</object>
<packing>
@@ -123,4 +132,35 @@
</object>
</child>
</object>
<object class="GtkBox" id="_loadingSpinner">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="spacing">5</property>
<child>
<object class="GtkSpinner">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="active">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Loading....</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</interface>