Files
marechai/Marechai/Pages/Admin/Details/Screen.razor.cs

105 lines
3.1 KiB
C#
Raw Normal View History

2020-05-27 03:48:34 +01:00
using System;
2020-05-26 04:36:42 +01:00
using System.Collections.Generic;
using System.Threading.Tasks;
2020-05-27 03:48:34 +01:00
using Blazorise;
using Marechai.Shared;
2020-05-26 04:36:42 +01:00
using Marechai.ViewModels;
using Microsoft.AspNetCore.Components;
namespace Marechai.Pages.Admin.Details
{
public partial class Screen
{
2020-05-27 03:48:34 +01:00
bool _editing;
2020-05-26 04:36:42 +01:00
bool _loaded;
2020-05-27 03:48:34 +01:00
ScreenViewModel _model;
2020-05-26 04:36:42 +01:00
List<ResolutionViewModel> _resolutions;
2020-05-27 03:48:34 +01:00
bool _unknownColors;
bool _unknownHeight;
bool _unknownType;
bool _unknownWidth;
2020-05-26 04:36:42 +01:00
[Parameter]
public int Id { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if(_loaded)
return;
_loaded = true;
if(Id <= 0)
return;
_resolutions = await ResolutionsService.GetAsync();
_model = await Service.GetAsync(Id);
2020-05-27 03:48:34 +01:00
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/screens/edit/", StringComparison.InvariantCulture);
if(_editing)
SetCheckboxes();
StateHasChanged();
}
void SetCheckboxes()
{
_unknownWidth = !_model.Width.HasValue;
_unknownType = string.IsNullOrWhiteSpace(_model.Type);
_unknownHeight = !_model.Height.HasValue;
_unknownColors = !_model.Colors.HasValue;
}
void OnEditClicked()
{
_editing = true;
SetCheckboxes();
StateHasChanged();
}
async void OnCancelClicked()
{
_editing = false;
_model = await Service.GetAsync(Id);
SetCheckboxes();
StateHasChanged();
}
async void OnSaveClicked()
{
if(_unknownWidth)
_model.Width = null;
else if(_model.Width < 0)
return;
if(_unknownHeight)
_model.Height = null;
else if(_model.Height < 0)
return;
if(_unknownType)
_model.Type = null;
else if(string.IsNullOrWhiteSpace(_model.Type))
return;
if(_unknownColors)
_model.EffectiveColors = null;
else if(_model.EffectiveColors < 0)
return;
_editing = false;
await Service.UpdateAsync(_model);
_model = await Service.GetAsync(Id);
SetCheckboxes();
2020-05-26 04:36:42 +01:00
StateHasChanged();
}
2020-05-27 03:48:34 +01:00
2020-05-27 05:29:37 +01:00
void ValidateDoubleBiggerThanZero(ValidatorEventArgs e) => Validators.ValidateDouble(e, 1, 131072);
2020-05-27 03:48:34 +01:00
2020-05-27 05:29:37 +01:00
void ValidateLongBiggerThanZero(ValidatorEventArgs e) => Validators.ValidateLong(e, 2);
2020-05-27 03:48:34 +01:00
void ValidateType(ValidatorEventArgs e) =>
2020-05-27 05:29:37 +01:00
Validators.ValidateString(e, L["Screen type cannot be bigger than 256 characters."], 256);
2020-05-26 04:36:42 +01:00
}
}