Get and save player
This commit is contained in:
@@ -100,7 +100,7 @@ namespace EstusShots.Gtk.Controls
|
||||
}
|
||||
}
|
||||
|
||||
private void TreeViewOnRowActivated(object o, RowActivatedArgs args)
|
||||
private void TreeViewOnRowActivated(object o, RowActivatedArgs args)
|
||||
{
|
||||
if (!(o is TreeView tree)) return;
|
||||
var selection = tree.Selection;
|
||||
@@ -128,19 +128,35 @@ namespace EstusShots.Gtk.Controls
|
||||
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);
|
||||
var valueIndex = Columns.IndexOf(dataColumn) + 1;
|
||||
var cell = GetRenderer(dataColumn);
|
||||
var attr = GetAttribute(dataColumn);
|
||||
dataColumn.PackStart(cell, true);
|
||||
dataColumn.AddAttribute(cell, attr, valueIndex);
|
||||
TreeView.AppendColumn(dataColumn);
|
||||
}
|
||||
}
|
||||
|
||||
private CellRenderer GetRenderer(DataColumn column)
|
||||
{
|
||||
var property = typeof(T).GetProperty(column.PropertyName);
|
||||
return property?.PropertyType.Name switch
|
||||
{
|
||||
nameof(Boolean) => new CellRendererToggle(),
|
||||
_ => new CellRendererText()
|
||||
};
|
||||
}
|
||||
|
||||
private string GetAttribute(DataColumn column)
|
||||
{
|
||||
var property = typeof(T).GetProperty(column.PropertyName);
|
||||
return property?.PropertyType.Name switch
|
||||
{
|
||||
nameof(Boolean) => "active",
|
||||
_ => "text"
|
||||
};
|
||||
}
|
||||
|
||||
private void InitListStore()
|
||||
{
|
||||
var types = Columns
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
using System;
|
||||
using Gtk;
|
||||
|
||||
namespace EstusShots.Gtk.Controls
|
||||
{
|
||||
public class DataColumn
|
||||
public class DataColumn : TreeViewColumn
|
||||
{
|
||||
public DataColumn()
|
||||
{
|
||||
Resizable = true;
|
||||
Reorderable = true;
|
||||
}
|
||||
|
||||
public DataColumn(string propertyName)
|
||||
{
|
||||
PropertyName = propertyName;
|
||||
Title = propertyName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -18,11 +22,6 @@ namespace EstusShots.Gtk.Controls
|
||||
/// </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.
|
||||
|
||||
@@ -42,6 +42,11 @@ namespace EstusShots.Gtk.Dialogs
|
||||
_builder.Autoconnect(this);
|
||||
|
||||
SavePlayerButton.Clicked += SavePlayerButtonOnClicked;
|
||||
CancelPlayerEditorButton.Clicked += (sender, args) =>
|
||||
{
|
||||
OnDialogClosed?.Invoke(this, new DialogClosedEventArgs(false, null));
|
||||
PlayerEditorDialog.Dispose();
|
||||
};
|
||||
|
||||
PlayerEditorDialog.TransientFor = parent;
|
||||
PlayerEditorDialog.Show();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using EstusShots.Gtk.Controls;
|
||||
using EstusShots.Gtk.Dialogs;
|
||||
using EstusShots.Shared.Dto;
|
||||
using EstusShots.Shared.Models.Parameters;
|
||||
@@ -15,14 +17,34 @@ namespace EstusShots.Gtk
|
||||
[UI] public readonly TreeView PlayersTreeView = null;
|
||||
[UI] public readonly Button NewPlayerButton = null;
|
||||
[UI] public readonly Box PlayerEditorContainer = null;
|
||||
|
||||
|
||||
private BindableListControl<Player> PlayersControl;
|
||||
|
||||
private void InitPlayersPage()
|
||||
{
|
||||
NewPlayerButton.Clicked += NewPlayerButtonOnClicked;
|
||||
|
||||
var columns = new List<DataColumn>
|
||||
{
|
||||
new DataColumn(nameof(Player.Name)),
|
||||
new DataColumn(nameof(Player.Alias)),
|
||||
new DataColumn(nameof(Player.HexId)) {Title = "Hex ID"},
|
||||
new DataColumn(nameof(Player.Anonymous)) {Title = "Is Anonymous?", FixedWidth = 30}
|
||||
};
|
||||
PlayersControl = new BindableListControl<Player>(columns, nameof(Player.PlayerId), PlayersTreeView);
|
||||
PlayersControl.OnSelectionChanged += PlayersControlOnOnSelectionChanged;
|
||||
|
||||
Task.Factory.StartNew(ReloadPlayers);
|
||||
}
|
||||
|
||||
// Events
|
||||
|
||||
private void PlayersControlOnOnSelectionChanged(object o, SelectionChangedEventArgs args)
|
||||
{
|
||||
if (!(args.Selection is Player player)) return;
|
||||
var dialog = new PlayerEditor(this, player);
|
||||
dialog.OnDialogClosed += PlayerEditorClosed;
|
||||
}
|
||||
|
||||
private void NewPlayerButtonOnClicked(object sender, EventArgs e)
|
||||
{
|
||||
@@ -33,7 +55,7 @@ namespace EstusShots.Gtk
|
||||
private async void PlayerEditorClosed(object o, DialogClosedEventArgs args)
|
||||
{
|
||||
if (!args.Ok || !(args.Model is Player player)) return;
|
||||
var res = await Task.Factory.StartNew(()
|
||||
var res = await Task.Factory.StartNew(()
|
||||
=> Client.Players.SavePlayer(new SavePlayerParameter(player)).Result);
|
||||
if (!res.OperationResult.Success)
|
||||
{
|
||||
@@ -42,12 +64,12 @@ namespace EstusShots.Gtk
|
||||
return;
|
||||
}
|
||||
|
||||
// ReloadPlayers();
|
||||
await ReloadPlayers();
|
||||
}
|
||||
|
||||
|
||||
// Private Methods
|
||||
|
||||
private async void ReloadPlayers()
|
||||
private async Task ReloadPlayers()
|
||||
{
|
||||
var res = await Task.Factory.StartNew(()
|
||||
=> Client.Players.GetPlayers(new GetPlayersParameter()).Result);
|
||||
@@ -58,10 +80,9 @@ namespace EstusShots.Gtk
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO
|
||||
// SeasonsControl.Items = res.Data.Seasons;
|
||||
// SeasonsControl.DataBind();
|
||||
// Info("Player list refreshed");
|
||||
PlayersControl.Items = res.Data.Players;
|
||||
PlayersControl.DataBind();
|
||||
Info("Player list refreshed");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user