SyncService + SyncPage added

This commit is contained in:
Iván Szabó
2025-02-06 14:46:31 +01:00
parent b5e5158f1e
commit b233347a7d
11 changed files with 159 additions and 25 deletions
+2
View File
@@ -6,11 +6,13 @@
xmlns:local="clr-namespace:WorkFlowCheck.MAUI"
xmlns:security="clr-namespace:WorkFlowCheck.MAUI.Pages.Security"
xmlns:nfc="clr-namespace:WorkFlowCheck.MAUI.Pages.NFC"
xmlns:system="clr-namespace:WorkFlowCheck.MAUI.Pages.System"
Shell.FlyoutBehavior="Disabled"
Title="WorkFlowCheck.MAUI">
<ShellContent Title="Home" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" />
<ShellContent Title="Login" ContentTemplate="{DataTemplate security:LoginPage}" Route="LoginPage" />
<ShellContent Title="NFC Test" ContentTemplate="{DataTemplate nfc:NFCTestPage}" Route="NFCTestPage" />
<ShellContent Title="Sync data" ContentTemplate="{DataTemplate system:SyncPage}" Route="SyncPage" />
</Shell>
+6
View File
@@ -31,6 +31,12 @@
SemanticProperties.Hint="Get user info"
Clicked="OnButtonGetUser"
HorizontalOptions="Fill" />
<Button
x:Name="ButtonSync"
Text="Sync data"
SemanticProperties.Hint="Sync data"
Clicked="OnButtonSync"
HorizontalOptions="Fill" />
<Button
x:Name="CounterBtn"
Text="Hahó II."
+5
View File
@@ -53,6 +53,11 @@ namespace WorkFlowCheck.MAUI
SecureStorage.Default.Remove("WFCUser");
Shell.Current.GoToAsync("//LoginPage");
}
private void OnButtonSync(object sender, EventArgs e)
{
Shell.Current.GoToAsync("//SyncPage");
}
}
}
@@ -2,11 +2,12 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="WorkFlowCheck.MAUI.Pages.Security.LoginPage"
Title="LoginPage">
Title="Bejelentkezés"
>
<StackLayout Padding="20" VerticalOptions="Center">
<Entry x:Name="UsernameEntry" Placeholder="Username" />
<Entry x:Name="PasswordEntry" Placeholder="Password" IsPassword="True" Keyboard="Numeric"/>
<Button x:Name="LoginBtn" Text="Log In" Clicked="OnLoginClicked" />
<Entry x:Name="UsernameEntry" Placeholder="Felhasználói név" />
<Entry x:Name="PasswordEntry" Placeholder="PIN kód" IsPassword="True" Keyboard="Numeric"/>
<Button x:Name="LoginBtn" Text="Bejelentkezés" Clicked="OnLoginClicked" />
<ActivityIndicator x:Name="LoadingIndicator"
IsRunning="False"
IsVisible="False"
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="WorkFlowCheck.MAUI.Pages.System.SyncPage"
Title="SyncPage">
<VerticalStackLayout>
<Label
Text="Welcome to .NET MAUI!"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Button x:Name="SyncBtn" Text="Adat szinkronizálás" Clicked="OnSyncClicked" />
<ActivityIndicator x:Name="LoadingIndicator"
IsRunning="False"
IsVisible="False"
VerticalOptions="Center"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ContentPage>
@@ -0,0 +1,38 @@
using WorkFlowCheck.MAUI.Services;
namespace WorkFlowCheck.MAUI.Pages.System;
public partial class SyncPage : ContentPage
{
private ISyncService _syncService;
public SyncPage()
{
InitializeComponent();
_syncService = MauiProgram.ServiceProvider.GetRequiredService<ISyncService>();
}
private async void OnSyncClicked(object sender, EventArgs e)
{
LoadingIndicator.IsRunning = true;
LoadingIndicator.IsVisible = true;
try
{
await _syncService.SyncDatas();
}
catch (Exception)
{
throw;
}
LoadingIndicator.IsVisible = false;
LoadingIndicator.IsRunning = false;
}
protected override bool OnBackButtonPressed()
{
Shell.Current.GoToAsync("//MainPage");
return true;
}
}
@@ -0,0 +1,31 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkFlowCheck.MAUI.DataLayer;
namespace WorkFlowCheck.MAUI.Services
{
public class BaseService
{
public readonly HttpClient _httpClient;
public readonly AppDbContext _dbContext;
public readonly IConfiguration _configuration;
public BaseService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext)
{
_httpClient = httpClient;
_dbContext = dbContext;
_configuration = configuration;
var apiBaseUrl = configuration["ApiBaseUrl"]; // API-cím elérése a konfigurációból
_httpClient.BaseAddress = new Uri(apiBaseUrl);
var apiKey = configuration["ApiKey"];
_httpClient.DefaultRequestHeaders.Add("X-Api-Key", apiKey);
}
}
}
@@ -8,6 +8,7 @@ namespace WorkFlowCheck.MAUI.Services
{
public interface ISyncService
{
Task SyncDatas();
}
}
+45 -8
View File
@@ -1,21 +1,58 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.MAUI.DataLayer;
namespace WorkFlowCheck.MAUI.Services
{
public class SyncService: ISyncService
public class SyncService : BaseService, ISyncService
{
private readonly HttpClient _httpClient;
private readonly AppDbContext _dbContext;
public SyncService(HttpClient httpClient, AppDbContext dbContext)
public SyncService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext) : base(httpClient, configuration, dbContext)
{
_httpClient = httpClient;
_dbContext = dbContext;
}
public async Task SyncDatas()
{
await SyncCheckPoints();
}
public async Task SyncCheckPoints()
{
try
{
// Az API végpont meghatározása
string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckPoints";
// HTTP GET kérés küldése
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<CheckPointListDTO>>(endpoint);
if (response != null)
{
if (response.IsSuccess)
{
var checkPointList = await _dbContext.CheckPoints.ToListAsync();
var missingItems = response.Data.CheckPointList.Where(f => !checkPointList.Any(s => s.Id == f.Id)).ToList();
foreach (var item in missingItems) {
}
}
}
}
catch (Exception ex)
{
// Hiba visszaadása
}
}
}
+3 -11
View File
@@ -10,23 +10,15 @@ using System.Threading.Tasks;
using WorkFlowCheck.Common.DTO;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using WorkFlowCheck.MAUI.DataLayer;
namespace WorkFlowCheck.MAUI.Services
{
public class UserService : IUserService
public class UserService : BaseService, IUserService
{
private readonly HttpClient _httpClient;
public UserDTO? CurrentUser { get; private set; }
public UserService(HttpClient httpClient, IConfiguration configuration)
public UserService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext) : base(httpClient, configuration, dbContext)
{
_httpClient = httpClient;
var apiBaseUrl = configuration["ApiBaseUrl"]; // API-cím elérése a konfigurációból
_httpClient.BaseAddress = new Uri(apiBaseUrl);
var apiKey = configuration["ApiKey"];
_httpClient.DefaultRequestHeaders.Add("X-Api-Key", apiKey);
}
public async Task<ApiResponseDTO<UserDTO>> GetUserAsync()
{
@@ -86,6 +86,9 @@
<MauiXaml Update="Pages\Security\LoginPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="Pages\System\SyncPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
</ItemGroup>
<ItemGroup>