bloxstrap/Bloxstrap/UI/ViewModels/About/SupportersViewModel.cs
pizzaboxer b3a1b1c55e
Dynamically resize supporter grid columns
+ fix bootstrapper bug from yesterday
2024-10-26 23:03:47 +01:00

69 lines
2.0 KiB
C#

using System.Windows;
namespace Bloxstrap.UI.ViewModels.About
{
public class SupportersViewModel : NotifyPropertyChangedViewModel
{
public SupporterData? SupporterData { get; private set; }
public GenericTriState LoadedState { get; set; } = GenericTriState.Unknown;
public string LoadError { get; set; } = "";
public int Columns { get; set; } = 3;
public SizeChangedEventHandler? WindowResizeEvent;
public SupportersViewModel()
{
WindowResizeEvent += OnWindowResize;
// this will cause momentary freezes only when ran under the debugger
LoadSupporterData();
}
private void OnWindowResize(object sender, SizeChangedEventArgs e)
{
if (!e.WidthChanged)
return;
int newCols = (int)Math.Floor(e.NewSize.Width / 200);
if (Columns == newCols)
return;
Columns = newCols;
OnPropertyChanged(nameof(Columns));
}
public async void LoadSupporterData()
{
const string LOG_IDENT = "AboutViewModel::LoadSupporterData";
try
{
SupporterData = await Http.GetJson<SupporterData>("https://raw.githubusercontent.com/bloxstraplabs/config/main/supporters.json");
}
catch (Exception ex)
{
App.Logger.WriteLine(LOG_IDENT, "Could not load supporter data");
App.Logger.WriteException(LOG_IDENT, ex);
LoadedState = GenericTriState.Failed;
LoadError = ex.Message;
OnPropertyChanged(nameof(LoadError));
}
if (SupporterData is not null)
{
LoadedState = GenericTriState.Successful;
OnPropertyChanged(nameof(SupporterData));
}
OnPropertyChanged(nameof(LoadedState));
}
}
}