Login, NFCWaiter

This commit is contained in:
2025-03-24 14:02:25 +01:00
parent d55dee91e9
commit ebe33abf69
11 changed files with 308 additions and 62 deletions
@@ -3,26 +3,34 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="WorkFlowCheck.MAUI.Pages.BaseStock.CheckPointsPage"
Title="Ellenőrzési pontok">
<RefreshView x:Name="refreshView" Command="{Binding RefreshCommand}" IsRefreshing="{Binding IsRefreshing}">
<CollectionView x:Name="CheckPointsList">
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame BorderColor="Gray" Padding="10" Margin="5" HasShadow="True" BackgroundColor="WhiteSmoke">
<StackLayout>
<Label Text="{Binding ShortName}" FontSize="16" FontAttributes="Bold" />
<Label Text="{Binding Code}" FontSize="14" />
<HorizontalStackLayout HorizontalOptions="End">
<Button Text="Edit" Margin="5,0,0,0"
<StackLayout>
<Button
x:Name="ButtonSync" Margin="5"
Text="Adatok szinkronizálása"
SemanticProperties.Hint="Adatok szinkronizálása"
Clicked="OnButtonSync"
HorizontalOptions="Fill" />
<RefreshView x:Name="refreshView" Command="{Binding RefreshCommand}" IsRefreshing="{Binding IsRefreshing}">
<CollectionView x:Name="CheckPointsList">
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame BorderColor="Gray" Padding="10" Margin="5" HasShadow="True" BackgroundColor="WhiteSmoke">
<StackLayout>
<Label Text="{Binding ShortName}" FontSize="16" FontAttributes="Bold" />
<Label Text="{Binding Code}" FontSize="14" />
<HorizontalStackLayout HorizontalOptions="End">
<Button Text="Edit" Margin="5,0,0,0"
Command="{Binding BindingContext.EditCommand, Source={x:Reference CheckPointsList}}"
CommandParameter="{Binding .}" />
<Button Text="Pair" Margin="5,0,0,0"
<Button Text="Pair" Margin="5,0,0,0"
Command="{Binding BindingContext.PairCommand, Source={x:Reference CheckPointsList}}"
CommandParameter="{Binding .}" />
</HorizontalStackLayout>
</StackLayout>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</RefreshView>
CommandParameter="{Binding .}" />
</HorizontalStackLayout>
</StackLayout>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</RefreshView>
</StackLayout>
</ContentPage>
@@ -59,7 +59,10 @@ public partial class CheckPointsPage : ContentPage
Shell.Current.GoToAsync("//BaseStockPage");
return true;
}
private void OnButtonSync(object sender, EventArgs e)
{
_syncService.SyncCheckPointsUp();
}
#region NFC
bool _eventsAlreadySubscribed = false;
CheckPointDTO _checkPointDTO = null;
@@ -75,7 +78,7 @@ public partial class CheckPointsPage : ContentPage
}
catch (Exception ex)
{
}
}
async Task EndListening()
@@ -1,5 +1,7 @@
using System.Diagnostics;
using System.Windows.Input;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.MAUI.Pages.NFC;
using WorkFlowCheck.MAUI.Services.Interfaces;
namespace WorkFlowCheck.MAUI.Pages.CheckList;
@@ -13,10 +15,10 @@ public partial class CheckListPage : ContentPage
public ICommand ContinueCommand { get; set; }
public CheckListPage()
{
InitializeComponent();
{
InitializeComponent();
_checkListService = MauiProgram.ServiceProvider.GetRequiredService<ICheckListService>();
_userService = MauiProgram.ServiceProvider.GetRequiredService<IUserService>();
_userService = MauiProgram.ServiceProvider.GetRequiredService<IUserService>();
}
protected override bool OnBackButtonPressed()
@@ -29,18 +31,23 @@ public partial class CheckListPage : ContentPage
base.OnAppearing();
NewCommand = new Command<CheckListTemplateHeaderDTO>(OnNewItem);
ContinueCommand = new Command<CheckListHeaderDTO>(OnContinueItem);
BindingContext = this;
await LoadCheckListTemplate();
await LoadCheckList();
}
private async void OnNewItem(CheckListTemplateHeaderDTO checkListTemplateHeaderDTO)
{
//await Navigation.PushAsync(new CheckPointEditPage(checkPointDTO));
}
private async void OnContinueItem(CheckListHeaderDTO checkListTemplateHeaderDTO)
{
//await Navigation.PushAsync(new CheckPointEditPage(checkPointDTO));
}
private async void OnContinueItem(CheckListHeaderDTO checkListHeaderDTO)
{
var taskCompletionSource = new TaskCompletionSource<string>();
await Navigation.PushAsync(new NFCWaiter(taskCompletionSource));
var result = await taskCompletionSource.Task;
}
private async Task LoadCheckListTemplate()
{
@@ -0,0 +1,12 @@
<?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.NFC.NFCWaiter"
Title="NFCWaiter">
<VerticalStackLayout>
<Label
Text="Várakozás az NFC beolvasásra ..."
VerticalOptions="Center"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ContentPage>
@@ -0,0 +1,117 @@
using Plugin.NFC;
using System.Collections.ObjectModel;
namespace WorkFlowCheck.MAUI.Pages.NFC;
public partial class NFCWaiter : ContentPage
{
private bool _eventsAlreadySubscribed = false;
private bool _onReceiveing = false;
private TaskCompletionSource<string> _taskCompletionSource;
public NFCWaiter(TaskCompletionSource<string> taskCompletionSource)
{
InitializeComponent();
_taskCompletionSource = taskCompletionSource;
}
protected override async void OnAppearing()
{
base.OnAppearing();
CrossNFC.Legacy = false;
if (CrossNFC.IsSupported)
{
if (!CrossNFC.Current.IsAvailable)
await ShowAlert("NFC is not available");
if (!CrossNFC.Current.IsEnabled)
await ShowAlert("NFC is disabled");
await AutoStartAsync().ConfigureAwait(false);
}
else
{
await ShowAlert("NFC is not available");
}
}
async Task AutoStartAsync()
{
// Some delay to prevent Java.Lang.IllegalStateException "Foreground dispatch can only be enabled when your activity is resumed" on Android
await Task.Delay(500);
await StartListeningIfNotiOS();
}
async Task StartListeningIfNotiOS()
{
await BeginListening();
}
async Task BeginListening()
{
try
{
MainThread.BeginInvokeOnMainThread(() =>
{
SubscribeEvents();
CrossNFC.Current.StartListening();
});
}
catch (Exception ex)
{
await ShowAlert(ex.Message);
}
}
async Task EndListening()
{
try
{
MainThread.BeginInvokeOnMainThread(() =>
{
UnsubscribeEvents();
CrossNFC.Current.StopPublishing();
CrossNFC.Current.StopListening();
});
}
catch (Exception ex)
{
await ShowAlert(ex.Message);
}
}
void SubscribeEvents()
{
if (_eventsAlreadySubscribed)
UnsubscribeEvents();
_eventsAlreadySubscribed = true;
CrossNFC.Current.OnMessageReceived += Current_OnMessageReceived_NFCData;
}
void UnsubscribeEvents()
{
CrossNFC.Current.OnMessageReceived -= Current_OnMessageReceived_NFCData;
_eventsAlreadySubscribed = false;
}
async void Current_OnMessageReceived_NFCData(ITagInfo tagInfo)
{
_onReceiveing = true;
if (tagInfo == null)
{
await ShowAlert("No tag found");
_onReceiveing = false;
return;
}
// Customized serial number
var identifier = tagInfo.Identifier;
var serialNumber = NFCUtils.ByteArrayToHexString(identifier, ":");
_taskCompletionSource.TrySetResult(serialNumber);
await EndListening();
await Navigation.PopAsync();
_onReceiveing = false;
}
Task ShowAlert(string message, string title = null) => DisplayAlert(string.IsNullOrWhiteSpace(title) ? "NFC olvasás" : title, message, "OK");
}
@@ -1,27 +1,69 @@
<?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"
xmlns:dx="http://schemas.devexpress.com/maui"
x:Class="WorkFlowCheck.MAUI.Pages.Security.LoginPage"
Title="Bejelentkezés"
>
<StackLayout Padding="20" VerticalOptions="Center">
<Entry x:Name="UsernameEntry" Placeholder="Felhasználói név" />
<Entry x:Name="PasswordEntry" Placeholder="PIN kód" IsPassword="True" Keyboard="Numeric"/>
<!--<dx:PasswordEdit
Text="{Binding Password, Mode=TwoWay}"
HasError="{Binding PasswordHasError}"
LabelText="Password"
HelpText="*Required"
Keyboard="Numeric"
ErrorText="The password should contain more than 5 characters,
have at least one uppercase and one lowercase letter, and one number."
PlaceholderText="Enter password"/>-->
<Button x:Name="LoginBtn" Text="Bejelentkezés" Clicked="OnLoginClicked" />
<ActivityIndicator x:Name="LoadingIndicator"
IsRunning="False"
IsVisible="False"
VerticalOptions="Center"
HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
BackgroundColor="#f0f0f0">
<ScrollView>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- Középen lévő kártya, ami mobilon is jól mutat, tableten pedig középen marad -->
<Frame Grid.Row="1"
BackgroundColor="White"
BorderColor="#cccccc"
CornerRadius="20"
Padding="30"
HasShadow="True"
HorizontalOptions="Center"
VerticalOptions="Center"
WidthRequest="{OnPlatform Android=350, iOS=350, WinUI=400, MacCatalyst=400}"
MaximumWidthRequest="500">
<VerticalStackLayout Spacing="20">
<Label Text="Bejelentkezés"
FontSize="24"
FontAttributes="Bold"
HorizontalOptions="Center"
TextColor="#333" />
<Entry x:Name="UsernameEntry"
Placeholder="Felhasználói név"
FontSize="18"
BackgroundColor="#f9f9f9"
HeightRequest="50"
Margin="0" />
<Entry x:Name="PasswordEntry"
Placeholder="PIN kód"
IsPassword="True"
Keyboard="Numeric"
FontSize="18"
BackgroundColor="#f9f9f9"
HeightRequest="50"
Margin="0" />
<Button x:Name="LoginBtn"
Text="Bejelentkezés"
FontSize="18"
HeightRequest="50"
CornerRadius="10"
BackgroundColor="#007aff"
TextColor="White"
Clicked="OnLoginClicked" />
<ActivityIndicator x:Name="LoadingIndicator"
IsRunning="False"
IsVisible="False"
HorizontalOptions="Center" />
</VerticalStackLayout>
</Frame>
</Grid>
</ScrollView>
</ContentPage>
@@ -5,10 +5,12 @@ namespace WorkFlowCheck.MAUI.Pages.Security;
public partial class LoginPage : ContentPage
{
private IUserService _userService;
private ISyncService _syncService;
public LoginPage()
{
InitializeComponent();
_userService = MauiProgram.ServiceProvider.GetRequiredService<IUserService>();
_syncService = MauiProgram.ServiceProvider.GetRequiredService<ISyncService>();
}
private async void OnLoginClicked(object sender, EventArgs e)
{
@@ -24,6 +26,7 @@ public partial class LoginPage : ContentPage
if (response != null && response.IsSuccess)
{
_userService.SetCurrentUser(response.Data);
await _syncService.SyncDatas();
var wFCUser = Newtonsoft.Json.JsonConvert.SerializeObject(response.Data);
await SecureStorage.Default.SetAsync("WFCUser", "wFCUser");