Improved client handling and episode models
This commit is contained in:
@@ -1,35 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using EstusShots.Client;
|
||||
using EstusShots.Gtk.Controls;
|
||||
using EstusShots.Shared.Models;
|
||||
using EstusShots.Shared.Dto;
|
||||
using Gtk;
|
||||
using Application = Gtk.Application;
|
||||
using DateTime = System.DateTime;
|
||||
using Task = System.Threading.Tasks.Task;
|
||||
using UI = Gtk.Builder.ObjectAttribute;
|
||||
|
||||
namespace EstusShots.Gtk
|
||||
{
|
||||
class MainWindow : Window
|
||||
internal class MainWindow : Window
|
||||
{
|
||||
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;
|
||||
[UI] private readonly Overlay _seasonsOverlay = null;
|
||||
[UI] private readonly Box _loadingSpinner = null;
|
||||
[UI] public readonly Button LoadButton = null;
|
||||
[UI] public readonly Box LoadingSpinner = null;
|
||||
[UI] public readonly Button NewSeasonButton = null;
|
||||
[UI] public readonly Overlay SeasonsOverlay = null;
|
||||
[UI] public readonly TreeView SeasonsView = null;
|
||||
|
||||
public MainWindow() : this(new Builder("MainWindow.glade")) { }
|
||||
public MainWindow() : this(new Builder("MainWindow.glade"))
|
||||
{
|
||||
}
|
||||
|
||||
private MainWindow(Builder builder) : base(builder.GetObject("MainWindow").Handle)
|
||||
{
|
||||
@@ -37,18 +30,30 @@ namespace EstusShots.Gtk
|
||||
Client = new EstusShotsClient("http://localhost:5000/api/");
|
||||
|
||||
DeleteEvent += Window_DeleteEvent;
|
||||
_loadButton.Clicked += LoadButtonClicked;
|
||||
_newSeasonButton.Clicked += NewSeasonButtonOnClicked;
|
||||
LoadButton.Clicked += LoadButtonClicked;
|
||||
NewSeasonButton.Clicked += NewSeasonButtonOnClicked;
|
||||
|
||||
var seasonsColumns = new List<DataColumn>
|
||||
{
|
||||
new DataColumn(nameof(Season.DisplayName)){Title = "Name"}
|
||||
new DataColumn(nameof(Season.DisplayName)) {Title = "Name"},
|
||||
new DataColumn(nameof(Season.Description)) {Title = "Description"},
|
||||
new DataColumn(nameof(Season.Start))
|
||||
{
|
||||
Title = "Start",
|
||||
Format = date => (date as DateTime?)?.ToString("dd.MM.yyyy hh:mm")
|
||||
}
|
||||
};
|
||||
SeasonsView = new BindableListView<Season>(seasonsColumns, nameof(Season.SeasonId) ,_seasonsView);
|
||||
SeasonsView.OnSelectionChanged += SeasonsViewOnOnSelectionChanged;
|
||||
SeasonsControl = new BindableListControl<Season>(seasonsColumns, nameof(Season.SeasonId), SeasonsView);
|
||||
SeasonsControl.OnSelectionChanged += SeasonsViewOnOnSelectionChanged;
|
||||
Info("Application Started");
|
||||
|
||||
// No need to wait for the loading to finnish
|
||||
var _ = ReloadSeasons();
|
||||
}
|
||||
|
||||
private EstusShotsClient Client { get; }
|
||||
private BindableListControl<Season> SeasonsControl { get; }
|
||||
|
||||
private void SeasonsViewOnOnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (!(e.Selection is Season season)) return;
|
||||
@@ -57,70 +62,56 @@ namespace EstusShots.Gtk
|
||||
|
||||
private async void NewSeasonButtonOnClicked(object sender, EventArgs e)
|
||||
{
|
||||
var nextNum = SeasonsView.Items.Any() ? SeasonsView.Items.Max(x => x.Number) + 1 : 1 ;
|
||||
using var _ = new LoadingMode(this);
|
||||
var season = new Season
|
||||
{
|
||||
Game = "Test Game",
|
||||
Number = nextNum,
|
||||
Start = DateTime.Now
|
||||
};
|
||||
var content = new StringContent(JsonSerializer.Serialize(season), Encoding.UTF8, "application/json");
|
||||
var client = new HttpClient();
|
||||
try
|
||||
{
|
||||
var response = await client.PostAsync(ApiUrl + "season", content);
|
||||
Number = SeasonsControl.Items.Any() ? SeasonsControl.Items.Max(x => x.Number) + 1 : 1,
|
||||
Start = DateTime.Now,
|
||||
Description = "This is a demo description!"
|
||||
};
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_infoLabel.Text = $"Error while creating Season: {response.ReasonPhrase}";
|
||||
return;
|
||||
}
|
||||
|
||||
await ReloadSeasons();
|
||||
Info("Created new Season");
|
||||
}
|
||||
catch (Exception ex)
|
||||
var (res, _) = await Client.CreateSeason(season);
|
||||
if (!res.Success)
|
||||
{
|
||||
_infoLabel.Text = $"Exception Occured: {ex.Message}";
|
||||
Console.WriteLine(ex.Message);
|
||||
_infoLabel.Text = $"Error while creating Season: {res.ShortMessage}";
|
||||
return;
|
||||
}
|
||||
|
||||
await ReloadSeasons();
|
||||
Info("Created new Season");
|
||||
}
|
||||
|
||||
|
||||
private async void LoadButtonClicked(object sender, EventArgs a)
|
||||
{
|
||||
using var _ = new LoadingMode(this);
|
||||
Info("Loading Seasons...");
|
||||
await ReloadSeasons();
|
||||
Info("List Refreshed");
|
||||
}
|
||||
|
||||
private async Task ReloadSeasons()
|
||||
{
|
||||
LoadingMode(true);
|
||||
var seasons = await Task.Factory.StartNew(() => Client.GetSeasons().Result);
|
||||
SeasonsView.Items = seasons;
|
||||
SeasonsView.DataBind();
|
||||
LoadingMode(false);
|
||||
var (res, seasons) = await Task.Factory.StartNew(() => Client.GetSeasons().Result);
|
||||
if (!res.Success)
|
||||
{
|
||||
_infoLabel.Text = $"Refresh Failed: {res.ShortMessage}";
|
||||
return;
|
||||
}
|
||||
|
||||
SeasonsControl.Items = seasons;
|
||||
SeasonsControl.DataBind();
|
||||
Info("List Refreshed");
|
||||
}
|
||||
|
||||
|
||||
private void Window_DeleteEvent(object sender, DeleteEventArgs a)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user