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

View File

@@ -20,19 +20,28 @@
<property name="margin_bottom">2</property> <property name="margin_bottom">2</property>
<property name="orientation">vertical</property> <property name="orientation">vertical</property>
<child> <child>
<object class="GtkScrolledWindow"> <object class="GtkOverlay" id="_seasonsOverlay">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">False</property>
<property name="shadow_type">in</property>
<child> <child>
<object class="GtkTreeView" id="_seasonsView"> <object class="GtkScrolledWindow">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="enable_grid_lines">horizontal</property> <property name="shadow_type">in</property>
<child internal-child="selection"> <child>
<object class="GtkTreeSelection"/> <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> </child>
</object> </object>
<packing>
<property name="index">-1</property>
</packing>
</child> </child>
</object> </object>
<packing> <packing>
@@ -123,4 +132,35 @@
</object> </object>
</child> </child>
</object> </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> </interface>