Improved client handling and episode models

This commit is contained in:
2020-02-27 23:11:36 +01:00
parent 82722e77d6
commit 2d2f8b6b76
22 changed files with 540 additions and 257 deletions

View File

@@ -0,0 +1,168 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GLib;
using Gtk;
using DateTime = GLib.DateTime;
namespace EstusShots.Gtk.Controls
{
public class SelectionChangedEventArgs : EventArgs
{
public SelectionChangedEventArgs(object selection)
{
Selection = selection;
}
public object Selection { get; }
}
public delegate void SelectionChangedEventHandler(object o, SelectionChangedEventArgs args);
public class BindableListControl<T>
{
/// <summary>
/// Initialize a new BindableListView with an existing TreeView Widget
/// </summary>
/// <param name="columns">The columns of the grid</param>
/// <param name="keyField">Unique key field in the item sources type</param>
/// <param name="treeView">An instance of an existing TreeView Widget</param>
public BindableListControl(List<DataColumn> columns, string keyField, TreeView treeView = null)
{
TreeView = treeView ?? new TreeView();
Columns = columns;
KeyField = keyField;
InitTreeViewColumns();
InitListStore();
TreeView.Model = ListStore;
Items = new List<T>();
TreeView.Selection.Changed += TreeView_SelectionChanged;
}
/// <summary> The GTK ListStore that is managed by this <see cref="BindableListControl{T}" />. </summary>
public ListStore ListStore { get; internal set; }
/// <summary> The GTK TreeView control that is managed by this <see cref="BindableListControl{T}" />. </summary>
public TreeView TreeView { get; }
/// <summary> Property of the element type that is used as a unique identifier for accessing elements. </summary>
public string KeyField { get; }
/// <summary> The collection of all elements, that should be shown in the list view. </summary>
public List<T> Items { get; set; }
/// <summary> The currently selected item in the view. </summary>
public T SelectedItem { get; set; }
/// <summary> All columns that are displayed in the list. </summary>
public List<DataColumn> Columns { get; }
/// <summary>
/// Event will be invoked when the selected item in the <see cref="TreeView" /> has changed.
/// </summary>
public event SelectionChangedEventHandler OnSelectionChanged;
/// <summary>
/// Set elements from the <see cref="Items" /> property in the <see cref="ListStore" />.
/// </summary>
/// <exception cref="TypeLoadException"></exception>
public void DataBind()
{
ListStore.Clear();
Items.ForEach(BindItem);
}
private void BindItem(T item)
{
var row = new List<object>();
foreach (var column in Columns)
{
var prop = item.GetType().GetProperty(column.PropertyName);
if (prop == null)
throw new TypeLoadException(
$"Property '{column.PropertyName}' does not exist on Type '{item.GetType()}'");
var val = prop.GetValue(item);
if (column.Format != null) val = column.Format(val);
row.Add(val.ToString());
}
// The key value must be the first value in the row
var key = item.GetType().GetProperty(KeyField)?.GetValue(item);
row.Insert(0, key);
try
{
ListStore.AppendValues(row.ToArray());
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private void TreeView_SelectionChanged(object sender, EventArgs e)
{
if (!(sender is TreeSelection selection)) return;
selection.GetSelected(out var model, out var iter);
var key = model.GetValue(iter, 0);
var item = Items.FirstOrDefault(x =>
{
var prop = x.GetType().GetProperty(KeyField);
var value = prop?.GetValue(x);
return value != null && value.Equals(key);
});
if (item == null)
{
Console.WriteLine($"No item for key '{key}' found in data store");
return;
}
SelectedItem = item;
OnSelectionChanged?.Invoke(this, new SelectionChangedEventArgs(SelectedItem));
}
private void InitTreeViewColumns()
{
foreach (var dataColumn in Columns)
{
// Offset by one, because the first column in the data store is fixed to the key value of the row
var index = Columns.IndexOf(dataColumn) + 1;
var column = new TreeViewColumn(
dataColumn.Title,
new CellRendererText(),
"text", index)
{
Resizable = true,
Reorderable = true
};
TreeView.AppendColumn(column);
}
}
private void InitListStore()
{
var types = Columns
.Select(x =>
{
var propType = typeof(T).GetProperty(x.PropertyName)?.PropertyType;
var gType = (GType) propType;
if (gType.ToString() == "GtkSharpValue") gType = MapType(propType);
return gType;
});
var data = new DateTime();
// The first column in the data store is always the key field.
var columns = new List<GType> {(GType) typeof(T).GetProperty(KeyField)?.PropertyType};
columns.AddRange(types);
ListStore = new ListStore(columns.ToArray());
}
private static GType MapType(Type type)
{
return type.Name switch
{
_ => GType.String
};
}
}
}

View File

@@ -1,135 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Gtk;
namespace EstusShots.Gtk.Controls
{
public class SelectionChangedEventArgs : EventArgs
{
public object Selection { get; }
public SelectionChangedEventArgs(object selection)
{
Selection = selection;
}
}
public delegate void SelectionChangedEventHandler(object o, SelectionChangedEventArgs args);
public class BindableListView<T>
{
public ListStore ListStore { get; internal set; }
public TreeView TreeView { get; }
public string KeyField { get; }
public IEnumerable<T> Items { get; set; }
public T SelectedItem { get; set; }
public List<DataColumn> Columns { get; }
public event SelectionChangedEventHandler OnSelectionChanged;
/// <summary>
/// Initialize a new BindableListView with an existing TreeView Widget
/// </summary>
/// <param name="columns">The columns of the grid</param>
/// <param name="keyField">Unique key field in the item sources type</param>
/// <param name="treeView">An instance of an existing TreeView Widget</param>
public BindableListView(List<DataColumn> columns, string keyField, TreeView treeView = null)
{
TreeView = treeView ?? new TreeView();
Columns = columns;
KeyField = keyField;
InitTreeViewColumns();
InitListStore();
TreeView.Model = ListStore;
Items = new List<T>();
TreeView.Selection.Changed += TreeView_SelectionChanged;
}
private void TreeView_SelectionChanged(object sender, EventArgs e)
{
if (!(sender is TreeSelection selection)) return;
selection.GetSelected(out var model, out var iter);
var key = model.GetValue(iter, 0);
var item = Items.FirstOrDefault(x =>
{
var prop = x.GetType().GetProperty(KeyField);
var value = prop?.GetValue(x);
return value != null && value.Equals(key);
});
if (item == null)
{
Console.WriteLine($"No item for key '{key}' found in data store");
return;
}
SelectedItem = item;
OnSelectionChanged?.Invoke(this, new SelectionChangedEventArgs(SelectedItem));
}
public void DataBind()
{
ListStore.Clear();
foreach (var item in Items)
{
var row = new List<object>();
foreach (var column in Columns)
{
var prop = item.GetType().GetProperty(column.PropertyName);
if (prop == null)
throw new TypeLoadException(
$"Property '{column.PropertyName}' does not exist on Type '{item.GetType()}'");
row.Add(prop.GetValue(item));
}
// The key value must be the first value in the row
var key = item.GetType().GetProperty(KeyField)?.GetValue(item);
row.Insert(0, key);
try
{
ListStore.AppendValues(row.ToArray());
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
private void InitTreeViewColumns()
{
foreach (var dataColumn in Columns)
{
// Offset by one, because the first column in the data store is fixed to the key value of the row
var index = Columns.IndexOf(dataColumn) + 1;
var column = new TreeViewColumn(
dataColumn.Title,
new CellRendererText(),
"text", index);
TreeView.AppendColumn(column);
}
}
private void InitListStore()
{
var types = Columns
.Select(x => typeof(T).GetProperty(x.PropertyName)?.PropertyType);
// The first column in the data store is always the key field.
var columns = new List<Type> {typeof(T).GetProperty(KeyField)?.PropertyType};
columns.AddRange(types);
ListStore = new ListStore(columns.ToArray());
}
}
}

View File

@@ -1,16 +1,32 @@
using System;
namespace EstusShots.Gtk.Controls
{
public class DataColumn
{
public string PropertyName { get; set; }
public string Title { get; set; }
public DataColumn() { }
public DataColumn()
{
}
public DataColumn(string propertyName)
{
PropertyName = propertyName;
}
/// <summary>
/// The name of the property in the data source, that should be show nin the view
/// </summary>
public string PropertyName { get; }
/// <summary>
/// The column header.
/// </summary>
public string Title { get; set; }
/// <summary>
/// Applies the given transformation on each item in the column.
/// This changes only the display of the value.
/// </summary>
public Func<object, string> Format { get; set; }
}
}

View File

@@ -0,0 +1,26 @@
using System;
namespace EstusShots.Gtk.Controls
{
internal class LoadingMode : IDisposable
{
private MainWindow Window { get; set; }
public LoadingMode(MainWindow window)
{
Window = window;
Window.LoadButton.Sensitive = false;
Window.NewSeasonButton.Sensitive = false;
Window.SeasonsView.Sensitive = false;
Window.SeasonsOverlay.AddOverlay(Window.LoadingSpinner);
}
public void Dispose()
{
Window.LoadButton.Sensitive = true;
Window.NewSeasonButton.Sensitive = true;
Window.SeasonsView.Sensitive = true;
Window.SeasonsOverlay.Remove(Window.LoadingSpinner);
}
}
}

View File

@@ -21,8 +21,4 @@
<ProjectReference Include="..\EstusShots.Shared\EstusShots.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="DataStores" />
</ItemGroup>
</Project>

View File

@@ -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;
}
}
}

View File

@@ -2,6 +2,37 @@
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.18"/>
<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>
<object class="GtkWindow" id="MainWindow">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Estus Shots</property>
@@ -20,7 +51,7 @@
<property name="margin_bottom">2</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkOverlay" id="_seasonsOverlay">
<object class="GtkOverlay" id="SeasonsOverlay">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
@@ -29,7 +60,7 @@
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkTreeView" id="_seasonsView">
<object class="GtkTreeView" id="SeasonsView">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="enable_grid_lines">horizontal</property>
@@ -56,7 +87,7 @@
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="_loadButton">
<object class="GtkButton" id="LoadButton">
<property name="label" translatable="yes">Reload</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
@@ -69,7 +100,7 @@
</packing>
</child>
<child>
<object class="GtkButton" id="_newSeasonButton">
<object class="GtkButton" id="NewSeasonButton">
<property name="label" translatable="yes">New Season</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
@@ -132,35 +163,4 @@
</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>