Barcodes speichern

This commit is contained in:
2024-08-21 00:11:59 +02:00
parent a61aa38d24
commit 423e5f3cd2
12 changed files with 142 additions and 49 deletions

View File

@@ -18,7 +18,7 @@
Icon="{OnPlatform 'icon_about.png', iOS='icon_about_ios.png', MacCatalyst='icon_about_ios.png'}" /> Icon="{OnPlatform 'icon_about.png', iOS='icon_about_ios.png', MacCatalyst='icon_about_ios.png'}" />
<ShellContent <ShellContent
Title="About" Title="Login"
ContentTemplate="{DataTemplate views:AboutPage}" ContentTemplate="{DataTemplate views:AboutPage}"
Icon="{OnPlatform 'icon_about.png', iOS='icon_about_ios.png', MacCatalyst='icon_about_ios.png'}" /> Icon="{OnPlatform 'icon_about.png', iOS='icon_about_ios.png', MacCatalyst='icon_about_ios.png'}" />
</TabBar> </TabBar>

View File

@@ -2,6 +2,7 @@
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using ZXing.QrCode.Internal;
namespace Jugenddienst_Stunden.Models namespace Jugenddienst_Stunden.Models
{ {
@@ -14,6 +15,8 @@ namespace Jugenddienst_Stunden.Models
public static async Task<string> GetApiDataWithAuthAsync(string url, string token) { public static async Task<string> GetApiDataWithAuthAsync(string url, string token) {
using (HttpClient client = new HttpClient()) { using (HttpClient client = new HttpClient()) {
try { try {
client.DefaultRequestHeaders.Add("Accept", "application/json");
// Hinzufügen des Bearer-Tokens zum Authorization-Header // Hinzufügen des Bearer-Tokens zum Authorization-Header
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

View File

@@ -9,7 +9,7 @@ namespace Jugenddienst_Stunden.Models {
public Dictionary<int,NominalWeek> nominal_week_api; public Dictionary<int,NominalWeek> nominal_week_api;
//public List<string> time_line; //public List<string> time_line;
public string zeit_total; public string zeit_total;
public Dictionary<int,decimal> zeit_total_daily; public Dictionary<int,decimal>? zeit_total_daily;
//public List<string> wochensumme; //public List<string> wochensumme;
public string overtime_month; public string overtime_month;
public string overtime; public string overtime;

View File

@@ -1,6 +1,10 @@
 
using CommunityToolkit.Mvvm.ComponentModel;
using Newtonsoft.Json;
using System.Text;
namespace Jugenddienst_Stunden.Models { namespace Jugenddienst_Stunden.Models {
public class Operator { public class Operator : ObservableObject {
public string id; public string id;
public string name; public string name;
public string surname; public string surname;
@@ -23,5 +27,29 @@ namespace Jugenddienst_Stunden.Models {
public string year; public string year;
//public List<int>? timetable; //public List<int>? timetable;
public static async Task<Operator> LoadData(string apiKey) {
//var apiKey = Preferences.Default.Get("apiKey", "");
Operator OperatorVar = new Operator();
//Operator operator = new Operator();
if (Connectivity.Current.NetworkAccess == NetworkAccess.None) {
await App.Current.MainPage.DisplayAlert("Keine Internetverbindung",
"Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.",
"OK");
//throw new Exception("Keine Internetverbindung");
} else {
var tokendata = new TokenData(apiKey);
string data = await Auth.GetApiDataWithAuthAsync(tokendata.url, tokendata.apiKey);
if (data != null) {
OperatorVar = Newtonsoft.Json.JsonConvert.DeserializeObject<Operator>(data);
Preferences.Default.Set("name", OperatorVar.name);
Preferences.Default.Set("surname", OperatorVar.surname);
}
}
return OperatorVar;
}
} }
} }

View File

@@ -2,35 +2,48 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using Microsoft.Maui.Networking; using Microsoft.Maui.Networking;
using Microsoft.Maui.Controls; using Microsoft.Maui.Controls;
using System;
using System.Text;
namespace Jugenddienst_Stunden.Models { namespace Jugenddienst_Stunden.Models {
internal class Stunde : ObservableObject { internal class Stunde : ObservableObject {
private static readonly string BaseAddress = "http://hours.dauni.mine.nu:81"; //Katharina
//private static readonly string apiKey = "MTQzfEFlMVRjQXdZMnI4RmpxZ0FSY3A0VEN2bVZYVXxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk="; //Christine public static string apiKey = Preferences.Default.Get("apiKey", "MTAyfEJZZnB1L3VwcnhoVms0dDlLZENPZWtUVy85b3xodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk=");
public static string apiKey = "MTU0fGpkQUNYTGkvcjMvVk4rNkMyK0dDQkJmMkFwVXxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk="; //Damian
private static readonly string requestUrl = $"{BaseAddress}/appapi?hours"; //Damian
//public static string apiKey = Preferences.Default.Get("apiKey", "MTU0fGpkQUNYTGkvcjMvVk4rNkMyK0dDQkJmMkFwVXxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk=");
//private static readonly string requestUrl = $"{BaseAddress}/appapi?hours";
public DateTime Date { get; set; } public DateTime Date { get; set; }
public static async Task<Hours> LoadData() {
public static async Task<Hours> LoadData() {
Hours hours = new Hours(); Hours hours = new Hours();
if (Connectivity.Current.NetworkAccess == NetworkAccess.None) { if (Connectivity.Current.NetworkAccess == NetworkAccess.None) {
await App.Current.MainPage.DisplayAlert("Keine Internetverbindung", await App.Current.MainPage.DisplayAlert("Keine Internetverbindung",
"Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.", "Bitte überprüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.",
"OK"); "OK");
//throw new Exception("Keine Internetverbindung");
} else { } else {
string data = await Auth.GetApiDataWithAuthAsync(requestUrl, apiKey); var tokendata = new TokenData(apiKey);
//string data = await Auth.GetApiDataWithAuthAsync(requestUrl, apiKey);
string data = await Auth.GetApiDataWithAuthAsync(tokendata.url + "?hours", tokendata.apiKey);
if (data == null) { if (data == null) {
throw new Exception("Keine Daten erhalten"); throw new Exception("Keine Daten erhalten");
} }
hours = JsonConvert.DeserializeObject<Hours>(data); hours = JsonConvert.DeserializeObject<Hours>(data);
//Preferences.Default.Set("name", hours.operator_api.name);
//Preferences.Default.Set("surname", hours.operator_api.surname);
} }
return hours; return hours;

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Jugenddienst_Stunden.Models {
class TokenData {
public string token { get; set; }
public string apiKey { get; set; }
public string url { get; set; }
public string operator_id { get; set; }
public TokenData(string apiKey) {
string dat = Encoding.UTF8.GetString(System.Convert.FromBase64String(apiKey));
this.token = dat.Split('|')[1]; ;
this.url = dat.Split('|')[2]; ;
this.operator_id = dat.Split('|')[0]; ;
this.apiKey = apiKey;
}
}
}

View File

@@ -7,19 +7,21 @@ namespace Jugenddienst_Stunden.ViewModels {
internal class AboutViewModel { internal class AboutViewModel {
public string Title => AppInfo.Name; public string Title => AppInfo.Name;
public string Version => AppInfo.VersionString; public string Version => AppInfo.VersionString;
public string MoreInfoUrl => "https://aka.ms/maui";
public string Message => "This app is written in XAML and C# with .NET MAUI."; public string Message => "Scanne den QR-Code von deinem Benutzerprofil auf der Stundenseite.";
public ICommand ShowMoreInfoCommand { get; }
public AboutViewModel() {
ShowMoreInfoCommand = new AsyncRelayCommand(ShowMoreInfo);
}
async Task ShowMoreInfo() =>
await Launcher.Default.OpenAsync(MoreInfoUrl);
//private void BarcodesDetected(object sender, ZXing.Net.Maui.BarcodeDetectionEventArgs e) {
// var first = e.Results?.FirstOrDefault();
// if (first is null) {
// return;
// }
// System.Windows.Application.Current.Dispatcher.DispatchAsync(async () => {
// await DisplayAlert("Scan-Ergebnis", $"Barcode: {first.Value}", "OK");
// });
//}
//private async void SetupBarcodeScanner() { //private async void SetupBarcodeScanner() {
// try { // try {
@@ -44,7 +46,7 @@ namespace Jugenddienst_Stunden.ViewModels {
// } // }
//} //}
} }
} }

View File

@@ -3,6 +3,7 @@ using CommunityToolkit.Mvvm.Input;
using System.Windows.Input; using System.Windows.Input;
using Microsoft.Maui.Controls; using Microsoft.Maui.Controls;
using Microsoft.Maui.Networking; using Microsoft.Maui.Networking;
using ZXing.Net.Maui;
namespace Jugenddienst_Stunden.ViewModels { namespace Jugenddienst_Stunden.ViewModels {
@@ -33,6 +34,7 @@ namespace Jugenddienst_Stunden.ViewModels {
public string OvertimeMonth { public string OvertimeMonth {
get => _hour.overtime_month; get => _hour.overtime_month;
} }
public string Title { get; set; } = Preferences.Default.Get("name", "") + " " + Preferences.Default.Get("surname", "");
public StundenViewModel() { public StundenViewModel() {
_hour = new Models.Hours(); _hour = new Models.Hours();
@@ -43,9 +45,10 @@ namespace Jugenddienst_Stunden.ViewModels {
// await Models.Stunde.LoadData(); // await Models.Stunde.LoadData();
public async Task LoadData() { public async Task LoadData() {
_hour = await Models.Stunde.LoadData(); _hour = await Models.Stunde.LoadData();
Models.Hours Hours = new Models.Hours(); Models.Hours Hours = new Models.Hours();
RefreshProperties(); //Title = _hour.operator_api.name + " " + _hour.operator_api.surname;
RefreshProperties();
} }
private void RefreshProperties() { private void RefreshProperties() {
@@ -55,8 +58,9 @@ namespace Jugenddienst_Stunden.ViewModels {
OnPropertyChanged(nameof(ZeitCalculated)); OnPropertyChanged(nameof(ZeitCalculated));
OnPropertyChanged(nameof(ZeitDone)); OnPropertyChanged(nameof(ZeitDone));
OnPropertyChanged(nameof(Hours)); OnPropertyChanged(nameof(Hours));
OnPropertyChanged(nameof(Title));
} }
} }
} }

View File

@@ -19,17 +19,15 @@
</HorizontalStackLayout> </HorizontalStackLayout>
<Label Text="{Binding Message}" /> <Label Text="{Binding Message}" />
<Button Text="Learn more..." Command="{Binding ShowMoreInfoCommand}" />
<!--<scanner:CameraView x:Name="barcodeScannerView"
OnDetectionFinished="BarcodesDetected"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand" />-->
<zxing:CameraBarcodeReaderView x:Name="barcodeScannerView" <zxing:CameraBarcodeReaderView x:Name="barcodeScannerView"
BarcodesDetected="BarcodesDetected" BarcodesDetected="BarcodesDetected"
HorizontalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand" /> VerticalOptions="FillAndExpand"
Margin="0,60,0,0"/>
</VerticalStackLayout> </VerticalStackLayout>

View File

@@ -1,3 +1,4 @@
using Jugenddienst_Stunden.Models;
using Microsoft.Maui.Controls; using Microsoft.Maui.Controls;
using System.Collections.Generic; using System.Collections.Generic;
using ZXing.Net.Maui; using ZXing.Net.Maui;
@@ -21,15 +22,31 @@ public partial class AboutPage : ContentPage {
private void BarcodesDetected(object sender, ZXing.Net.Maui.BarcodeDetectionEventArgs e) { private void BarcodesDetected(object sender, ZXing.Net.Maui.BarcodeDetectionEventArgs e) {
//var first = e.Results?.FirstOrDefault(); //var first = e.Results?.FirstOrDefault();
var currentTime = DateTime.Now; var currentTime = DateTime.Now;
if ((currentTime - _lastDetectionTime) > _detectionInterval) { if ((currentTime - _lastDetectionTime) > _detectionInterval) {
_lastDetectionTime = currentTime; _lastDetectionTime = currentTime;
foreach (var barcode in e.Results) { foreach (var barcode in e.Results) {
MainThread.InvokeOnMainThreadAsync(() => { if (Preferences.Default.Get("apiKey", "") != barcode.Value) {
DisplayAlert("Barcode erkannt", $"Barcode: {barcode.Format} - {barcode.Value}", "OK"); MainThread.InvokeOnMainThreadAsync(async() => {
Models.Stunde.apiKey = barcode.Value; //DisplayAlert("Barcode erkannt", $"Barcode: {barcode.Format} - {barcode.Value}", "OK");
}); Preferences.Default.Set("apiKey", barcode.Value);
Models.Stunde.apiKey = barcode.Value;
var op = await Models.Operator.LoadData(barcode.Value);
Preferences.Default.Set("name", op.name);
Preferences.Default.Set("surname", op.surname);
DisplayAlert("Neuer Barcode erkannt", op.name + " " + op.surname, "OK");
});
} else {
MainThread.InvokeOnMainThreadAsync(() => {
DisplayAlert("Barcode bereits vorhanden",
Preferences.Default.Get("name", "") + " " + Preferences.Default.Get("surname", ""),
"OK");
});
}
} }
} }
@@ -50,12 +67,12 @@ public partial class AboutPage : ContentPage {
base.OnDisappearing(); base.OnDisappearing();
barcodeScannerView.CameraLocation = CameraLocation.Front; barcodeScannerView.CameraLocation = CameraLocation.Front;
barcodeScannerView.IsDetecting = false; barcodeScannerView.IsDetecting = false;
} }
protected override void OnAppearing() { protected override void OnAppearing() {
base.OnAppearing(); base.OnAppearing();
barcodeScannerView.IsDetecting = true; barcodeScannerView.IsDetecting = true;
barcodeScannerView.CameraLocation = CameraLocation.Rear; barcodeScannerView.CameraLocation = CameraLocation.Rear;
} }

View File

@@ -3,10 +3,12 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:Jugenddienst_Stunden.ViewModels" xmlns:models="clr-namespace:Jugenddienst_Stunden.ViewModels"
x:Class="Jugenddienst_Stunden.Views.StundenPage" x:Class="Jugenddienst_Stunden.Views.StundenPage"
Title="StundenPage"> Title="{Binding Title}">
<ContentPage.BindingContext> <ContentPage.BindingContext>
<models:StundenViewModel /> <models:StundenViewModel />
</ContentPage.BindingContext> </ContentPage.BindingContext>
<VerticalStackLayout Spacing="10" Margin="15"> <VerticalStackLayout Spacing="10" Margin="15">
<Label Text="{Binding Message}" /> <Label Text="{Binding Message}" />
@@ -18,13 +20,13 @@
<Label Text="Berechnet:" Grid.Row="2" /> <Label Text="Berechnet:" Grid.Row="2" />
<Label Text="Überstunden Monat:" Grid.Row="3" Margin="0,0,15,0" /> <Label Text="Überstunden Monat:" Grid.Row="3" Margin="0,0,15,0" />
<Label Text="Überstunden Jahr:" Grid.Row="4" /> <Label Text="Überstunden Jahr:" Grid.Row="4" />
<Label BackgroundColor="AliceBlue" Grid.Row="0" Grid.Column="1" HorizontalTextAlignment="End" Padding="0,0,5,0" Text="{Binding Nominal}" /> <Label Grid.Row="0" Grid.Column="1" HorizontalTextAlignment="End" Padding="0,0,5,0" Text="{Binding Nominal}" />
<Label BackgroundColor="AliceBlue" Grid.Row="1" Grid.Column="1" HorizontalTextAlignment="End" Padding="0,0,5,0" Text="{Binding ZeitDone}" /> <Label Grid.Row="1" Grid.Column="1" HorizontalTextAlignment="End" Padding="0,0,5,0" Text="{Binding ZeitDone}" />
<Label BackgroundColor="AliceBlue" Grid.Row="2" Grid.Column="1" HorizontalTextAlignment="End" Padding="0,0,5,0" Text="{Binding ZeitCalculated}" /> <Label Grid.Row="2" Grid.Column="1" HorizontalTextAlignment="End" Padding="0,0,5,0" Text="{Binding ZeitCalculated}" />
<Label BackgroundColor="AliceBlue" Grid.Row="3" Grid.Column="1" HorizontalTextAlignment="End" Padding="0,0,5,0" Text="{Binding OvertimeMonth}" /> <Label Grid.Row="3" Grid.Column="1" HorizontalTextAlignment="End" Padding="0,0,5,0" Text="{Binding OvertimeMonth}" />
<Label BackgroundColor="AliceBlue" Grid.Row="4" Grid.Column="1" HorizontalTextAlignment="End" Padding="0,0,5,0" Text="{Binding Overtime}" /> <Label Grid.Row="4" Grid.Column="1" HorizontalTextAlignment="End" Padding="0,0,5,0" Text="{Binding Overtime}" />
</Grid> </Grid>
</VerticalStackLayout> </VerticalStackLayout>
</ContentPage> </ContentPage>

View File

@@ -1,9 +1,12 @@
namespace Jugenddienst_Stunden.Views; namespace Jugenddienst_Stunden.Views;
public partial class StundenPage : ContentPage public partial class StundenPage : ContentPage {
{ public StundenPage() {
public StundenPage()
{
InitializeComponent(); InitializeComponent();
} }
protected override void OnAppearing() {
base.OnAppearing();
Title = Preferences.Default.Get("name", "") + " " + Preferences.Default.Get("surname", "");
}
} }