Files
mapo-core/MP.Prog/Pages/Archive.razor.cs
T
Samuele Locatelli 66a2fd0923 refresh con pager
2021-09-13 08:46:58 +02:00

403 lines
11 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MP.FileData.DatabaseModels;
using MP.Prog.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MP.Prog.Pages
{
public partial class Archive : ComponentBase, IDisposable
{
#region Private Fields
private FileModel currRecord = null;
private List<FileModel> ListRecords;
private List<MacchinaModel> MacList;
private List<FileModel> SearchRecords;
private List<TagModel> TagList;
#endregion Private Fields
#region Protected Fields
protected string _SearchTag;
protected string defTag = "";
#endregion Protected Fields
#region Private Properties
private int _currPage { get; set; } = 1;
private int _numRecord { get; set; } = 10;
private int currPage
{
get => _currPage;
set
{
if (_currPage != value)
{
_currPage = value;
var pUpd = Task.Run(async () => await ReloadData());
pUpd.Wait();
}
}
}
private bool isLoading { get; set; } = false;
private int numRecord
{
get => _numRecord;
set
{
if (_numRecord != value)
{
_numRecord = value;
var pUpd = Task.Run(async () => await ReloadData());
pUpd.Wait();
}
}
}
private bool OnlyActive
{
get
{
bool answ = false;
if (AppMService.File_Filter != null)
{
answ = AppMService.File_Filter.OnlyActive;
}
return answ;
}
set
{
if (!AppMService.File_Filter.OnlyActive.Equals(value))
{
AppMService.File_Filter.OnlyActive = value;
var pUpd = Task.Run(async () => await ReloadData());
pUpd.Wait();
}
}
}
private bool OnlyMod
{
get
{
bool answ = false;
if (AppMService.File_Filter != null)
{
answ = AppMService.File_Filter.OnlyMod;
}
return answ;
}
set
{
if (!AppMService.File_Filter.OnlyMod.Equals(value))
{
AppMService.File_Filter.OnlyMod = value;
var pUpd = Task.Run(async () => await ReloadData());
pUpd.Wait();
}
}
}
private string SelCodArt
{
get
{
string answ = "";
if (AppMService.File_Filter != null)
{
answ = AppMService.File_Filter.CodArticolo;
}
return answ;
}
set
{
if (!AppMService.File_Filter.CodArticolo.Equals(value))
{
AppMService.File_Filter.CodArticolo = value;
var pUpd = Task.Run(async () => await ReloadData());
pUpd.Wait();
}
}
}
private string SelIdxMacc
{
get
{
string answ = "";
if (AppMService.File_Filter != null)
{
answ = AppMService.File_Filter.IdxMacchina;
}
return answ;
}
set
{
if (!AppMService.File_Filter.IdxMacchina.Equals(value))
{
AppMService.File_Filter.IdxMacchina = value;
var pUpd = Task.Run(async () => await ReloadData());
pUpd.Wait();
}
}
}
#endregion Private Properties
#region Protected Properties
[Inject]
protected MessageService AppMService { get; set; }
[Inject]
protected FileArchDataService DataService { get; set; }
[Inject]
protected IJSRuntime JSRuntime { get; set; }
[Inject]
protected NavigationManager NavManager { get; set; }
protected string SearchTag
{
get
{
return _SearchTag;
}
set
{
isLoading = true;
_SearchTag = value;
// se son > 3 char... debounce...
if (string.IsNullOrEmpty(value))
{
_SearchTag = defTag;
}
if (value.Length >= defTag.Length)
{
var pUpd = Task.Run(async () =>
{
TagList = await DataService.TagGetFilt(SearchTag);
});
pUpd.Wait();
}
isLoading = false;
}
}
protected int totalCount
{
get
{
int answ = 0;
if (SearchRecords != null)
{
answ = SearchRecords.Count;
}
return answ;
}
}
#endregion Protected Properties
#region Private Methods
private string cssActive(bool active)
{
string answ = active ? "text-dark" : "text-secondary textStriked";
return answ;
}
private string cssStatusByCod(FileData.FileState currStatus)
{
string answ = "badge";
switch (currStatus)
{
case FileData.FileState.Changed:
answ += " badge-warning";
break;
case FileData.FileState.Deleted:
answ += " badge-danger";
break;
case FileData.FileState.Ok:
answ += " badge-success";
break;
case FileData.FileState.ND:
default:
answ += " badge-light";
break;
}
return answ;
}
private async Task ReloadData()
{
isLoading = true;
SearchRecords = await DataService.FileGetFilt(AppMService.File_Filter.IdxMacchina, AppMService.File_Filter.CodArticolo, AppMService.File_Filter.OnlyActive, AppMService.File_Filter.OnlyMod, AppMService.SearchVal);
ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
isLoading = false;
}
#endregion Private Methods
#region Protected Methods
protected void Edit(FileModel selRecord)
{
// rileggo dal DB il record corrente...
var pUpd = Task.Run(async () => currRecord = await DataService.FileGetByKey(selRecord.FileId));
pUpd.Wait();
}
protected void ForceCheck(int maxHour)
{
var pUpd = Task.Run(async () =>
{
isLoading = true;
currRecord = null;
SearchRecords = null;
ListRecords = null;
await DataService.updateAllArchive(maxHour);
AppMService.File_Filter = SelectData.Init(5, 10);
isLoading = false;
await ReloadAllData();
});
pUpd.Wait();
}
protected void ForceReload(int newNum)
{
numRecord = newNum;
}
protected void ForceReloadPage(int newNum)
{
currPage = newNum;
}
protected override async Task OnInitializedAsync()
{
DataService.ResetController();
SearchTag = defTag;
AppMService.ShowSearch = false;
AppMService.PageName = "Archivio File Programmi";
AppMService.PageIcon = "fas fa-folder pr-2";
AppMService.EA_SearchUpdated += OnSeachUpdated;
await ReloadAllData();
}
protected async Task ReloadAllData()
{
isLoading = true;
MacList = await DataService.MacchineGetAll();
SelIdxMacc = "0";
SearchTag = defTag;
TagList = await DataService.TagGetFilt(SearchTag);
isLoading = false;
await ReloadData();
}
protected void ResetData()
{
DataService.rollBackEdit(currRecord);
currRecord = null;
}
protected async Task ResetFilter()
{
currRecord = null;
SearchRecords = null;
ListRecords = null;
AppMService.File_Filter = SelectData.Init(5, 10);
SearchTag = defTag;
await ReloadAllData();
}
protected void ResetSearchArt()
{
SearchTag = defTag;
}
protected void searchArtUpd(string newCod)
{
SelCodArt = newCod;
}
protected void Select(FileModel selRecord)
{
// applico filtro da selezione
currRecord = selRecord;
}
protected async void TestLoading()
{
await InvokeAsync(() =>
{
isLoading = !isLoading;
//StateHasChanged();
//Thread.Sleep(500);
//Task.Delay(1);
//isLoading = false;
});
}
protected async Task UpdateData()
{
currRecord = null;
DataService.ResetController();
await ReloadData();
}
#endregion Protected Methods
#region Public Methods
public string checkSelect(int FileId)
{
string answ = "";
if (currRecord != null)
{
try
{
answ = (currRecord.FileId == FileId) ? "table-info" : "";
}
catch
{ }
}
return answ;
}
public void Dispose()
{
AppMService.EA_SearchUpdated -= OnSeachUpdated;
}
public async void OnSeachUpdated()
{
await InvokeAsync(() =>
{
Task task = UpdateData();
StateHasChanged();
});
}
#endregion Public Methods
}
}