Üzenetküldés frontenden

This commit is contained in:
2025-04-07 17:05:40 +02:00
parent a3be387ee1
commit b02fb58f2e
14 changed files with 338 additions and 70 deletions
+74 -2
View File
@@ -1,5 +1,8 @@
using Microsoft.EntityFrameworkCore;
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Threading;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.MAUI.DataLayer;
using WorkFlowCheck.MAUI.Services;
@@ -10,22 +13,29 @@ namespace WorkFlowCheck.MAUI
public partial class App : Application
{
private readonly AppDbContext _dbContext;
private readonly IUserService _userService;
private readonly ISyncService _syncService;
private CancellationTokenSource _cancellationTokenSource;
public App(IServiceProvider serviceProvider)
{
InitializeComponent();
_dbContext = serviceProvider.GetRequiredService<AppDbContext>();
_userService = serviceProvider.GetRequiredService<IUserService>();
_syncService = serviceProvider.GetRequiredService<ISyncService>();
_dbContext.Database.EnsureDeleted(); // Adatbázis törlése
_dbContext.Database.EnsureCreated(); // Új adatbázis létrehozása
StartBackgroundTask();
MainPage = new AppShell();
//MainPage = new NavigationPage(new MainPage(serviceProvider.GetRequiredService<IUserService>()));
}
protected async override void OnResume()
{
StartBackgroundTask();
base.OnResume();
var isLogged = await SecureStorage.Default.GetAsync("WFCUser");
if (isLogged == null)
@@ -35,6 +45,7 @@ namespace WorkFlowCheck.MAUI
}
protected override void OnSleep()
{
_cancellationTokenSource?.Cancel();
SecureStorage.Default.Remove("WFCUser");
base.OnSleep();
}
@@ -44,6 +55,67 @@ namespace WorkFlowCheck.MAUI
SecureStorage.Default.Remove("WFCUser");
await Shell.Current.GoToAsync("//LoginPage");
}
private void StartBackgroundTask()
{
_cancellationTokenSource = new CancellationTokenSource();
Task.Run(() => BackgroundTask(_cancellationTokenSource.Token));
}
private async Task BackgroundTask(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
try
{
var userDTO = _userService.GetCurrentUser();
if (userDTO != null && userDTO.Id > 0)
{
if (userDTO.RoleDTO?.Count > 0)
{
foreach (var roleDTO in userDTO.RoleDTO)
{
if (roleDTO.CanEnableBlocked)
{
var deviceMessageDTOList = await _syncService.DeviceMessageReadUnreaded(_userService.DeviceId.ToUpper());
if (deviceMessageDTOList != null && deviceMessageDTOList.Count > 0)
{
await ShowSnackbar(deviceMessageDTOList[0].Message);
break;
}
}
}
}
}
}
catch (Exception ex)
{
}
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
}
}
private async Task ShowSnackbar(string message)
{
var visualOptions = new SnackbarOptions
{
ActionButtonTextColor = Colors.Yellow,
CornerRadius = new CornerRadius(5),
Font = Microsoft.Maui.Font.SystemFontOfSize(16),
BackgroundColor = Microsoft.Maui.Graphics.Color.Parse("Red"),
TextColor = Colors.Yellow
};
var snackbar = Snackbar.Make(message,
duration: TimeSpan.FromSeconds(10),
action: () => Console.WriteLine("Snackbar dismissed"),
actionButtonText: "OK",
visualOptions: visualOptions
);
await snackbar.Show();
}
private async Task<ApiResponseDTO<UserDTO>> CheckSecurity()
{
var isLogged = await SecureStorage.Default.GetAsync("WFCUser");