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

124 lines
3.8 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-28 03:17:03 +01:00
bool _creating;
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;
2020-05-28 03:17:03 +01:00
_creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/screens/create", StringComparison.InvariantCulture);
if(Id <= 0 &&
!_creating)
2020-05-26 04:36:42 +01:00
return;
_resolutions = await ResolutionsService.GetAsync();
2020-05-28 03:17:03 +01:00
_model = _creating ? new ScreenViewModel() : await Service.GetAsync(Id);
2020-05-26 04:36:42 +01:00
2020-05-28 03:17:03 +01:00
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/screens/edit/",
StringComparison.InvariantCulture);
2020-05-27 03:48:34 +01:00
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;
2020-05-28 03:17:03 +01:00
if(_creating)
{
NavigationManager.ToBaseRelativePath("admin/screens");
return;
}
_model = await Service.GetAsync(Id);
2020-05-27 03:48:34 +01:00
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;
2020-05-28 03:17:03 +01:00
if(_creating)
Id = await Service.CreateAsync(_model);
else
await Service.UpdateAsync(_model);
_editing = false;
_creating = false;
_model = await Service.GetAsync(Id);
2020-05-27 03:48:34 +01:00
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
}
}