From 423e5f3cd2e580fb36f3c41e181ecec5d7ad9e42 Mon Sep 17 00:00:00 2001 From: DaPi Date: Wed, 21 Aug 2024 00:11:59 +0200 Subject: [PATCH] Barcodes speichern --- Jugenddienst Stunden/AppShell.xaml | 2 +- Jugenddienst Stunden/Models/Auth.cs | 3 ++ Jugenddienst Stunden/Models/Hours.cs | 2 +- Jugenddienst Stunden/Models/Operator.cs | 30 +++++++++++++++++- Jugenddienst Stunden/Models/Stunde.cs | 27 +++++++++++----- Jugenddienst Stunden/Models/TokenData.cs | 23 ++++++++++++++ .../ViewModels/AboutViewModel.cs | 22 +++++++------ .../ViewModels/StundenViewModel.cs | 12 ++++--- Jugenddienst Stunden/Views/AboutPage.xaml | 10 +++--- Jugenddienst Stunden/Views/AboutPage.xaml.cs | 31 ++++++++++++++----- Jugenddienst Stunden/Views/StundenPage.xaml | 18 ++++++----- .../Views/StundenPage.xaml.cs | 11 ++++--- 12 files changed, 142 insertions(+), 49 deletions(-) create mode 100644 Jugenddienst Stunden/Models/TokenData.cs diff --git a/Jugenddienst Stunden/AppShell.xaml b/Jugenddienst Stunden/AppShell.xaml index 41e9097..147ca19 100644 --- a/Jugenddienst Stunden/AppShell.xaml +++ b/Jugenddienst Stunden/AppShell.xaml @@ -18,7 +18,7 @@ Icon="{OnPlatform 'icon_about.png', iOS='icon_about_ios.png', MacCatalyst='icon_about_ios.png'}" /> diff --git a/Jugenddienst Stunden/Models/Auth.cs b/Jugenddienst Stunden/Models/Auth.cs index 1ca2b9b..5cf2999 100644 --- a/Jugenddienst Stunden/Models/Auth.cs +++ b/Jugenddienst Stunden/Models/Auth.cs @@ -2,6 +2,7 @@ using System.Net.Http; using System.Text; using System.Threading.Tasks; +using ZXing.QrCode.Internal; namespace Jugenddienst_Stunden.Models { @@ -14,6 +15,8 @@ namespace Jugenddienst_Stunden.Models public static async Task GetApiDataWithAuthAsync(string url, string token) { using (HttpClient client = new HttpClient()) { try { + client.DefaultRequestHeaders.Add("Accept", "application/json"); + // Hinzufügen des Bearer-Tokens zum Authorization-Header client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); diff --git a/Jugenddienst Stunden/Models/Hours.cs b/Jugenddienst Stunden/Models/Hours.cs index d33d497..765100b 100644 --- a/Jugenddienst Stunden/Models/Hours.cs +++ b/Jugenddienst Stunden/Models/Hours.cs @@ -9,7 +9,7 @@ namespace Jugenddienst_Stunden.Models { public Dictionary nominal_week_api; //public List time_line; public string zeit_total; - public Dictionary zeit_total_daily; + public Dictionary? zeit_total_daily; //public List wochensumme; public string overtime_month; public string overtime; diff --git a/Jugenddienst Stunden/Models/Operator.cs b/Jugenddienst Stunden/Models/Operator.cs index 355deb3..29910aa 100644 --- a/Jugenddienst Stunden/Models/Operator.cs +++ b/Jugenddienst Stunden/Models/Operator.cs @@ -1,6 +1,10 @@  +using CommunityToolkit.Mvvm.ComponentModel; +using Newtonsoft.Json; +using System.Text; + namespace Jugenddienst_Stunden.Models { - public class Operator { + public class Operator : ObservableObject { public string id; public string name; public string surname; @@ -23,5 +27,29 @@ namespace Jugenddienst_Stunden.Models { public string year; //public List? timetable; + + + + public static async Task 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(data); + Preferences.Default.Set("name", OperatorVar.name); + Preferences.Default.Set("surname", OperatorVar.surname); + } + + } + return OperatorVar; + } } } diff --git a/Jugenddienst Stunden/Models/Stunde.cs b/Jugenddienst Stunden/Models/Stunde.cs index d114c47..87da5b6 100644 --- a/Jugenddienst Stunden/Models/Stunde.cs +++ b/Jugenddienst Stunden/Models/Stunde.cs @@ -2,35 +2,48 @@ using Newtonsoft.Json; using Microsoft.Maui.Networking; using Microsoft.Maui.Controls; +using System; +using System.Text; namespace Jugenddienst_Stunden.Models { internal class Stunde : ObservableObject { - private static readonly string BaseAddress = "http://hours.dauni.mine.nu:81"; - //private static readonly string apiKey = "MTQzfEFlMVRjQXdZMnI4RmpxZ0FSY3A0VEN2bVZYVXxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk="; //Christine - public static string apiKey = "MTU0fGpkQUNYTGkvcjMvVk4rNkMyK0dDQkJmMkFwVXxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk="; //Damian - private static readonly string requestUrl = $"{BaseAddress}/appapi?hours"; + //Katharina + public static string apiKey = Preferences.Default.Get("apiKey", "MTAyfEJZZnB1L3VwcnhoVms0dDlLZENPZWtUVy85b3xodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk="); + + //Damian + //public static string apiKey = Preferences.Default.Get("apiKey", "MTU0fGpkQUNYTGkvcjMvVk4rNkMyK0dDQkJmMkFwVXxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk="); + + //private static readonly string requestUrl = $"{BaseAddress}/appapi?hours"; public DateTime Date { get; set; } - public static async Task LoadData() { + + public static async Task LoadData() { + Hours hours = new Hours(); + 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 { - 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) { throw new Exception("Keine Daten erhalten"); } hours = JsonConvert.DeserializeObject(data); + //Preferences.Default.Set("name", hours.operator_api.name); + //Preferences.Default.Set("surname", hours.operator_api.surname); + } return hours; diff --git a/Jugenddienst Stunden/Models/TokenData.cs b/Jugenddienst Stunden/Models/TokenData.cs new file mode 100644 index 0000000..7854925 --- /dev/null +++ b/Jugenddienst Stunden/Models/TokenData.cs @@ -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; + } + } +} diff --git a/Jugenddienst Stunden/ViewModels/AboutViewModel.cs b/Jugenddienst Stunden/ViewModels/AboutViewModel.cs index 82bbb02..1e2e190 100644 --- a/Jugenddienst Stunden/ViewModels/AboutViewModel.cs +++ b/Jugenddienst Stunden/ViewModels/AboutViewModel.cs @@ -7,19 +7,21 @@ namespace Jugenddienst_Stunden.ViewModels { internal class AboutViewModel { public string Title => AppInfo.Name; 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 ICommand ShowMoreInfoCommand { get; } + + public string Message => "Scanne den QR-Code von deinem Benutzerprofil auf der Stundenseite."; - 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() { // try { @@ -44,7 +46,7 @@ namespace Jugenddienst_Stunden.ViewModels { // } //} - + } } diff --git a/Jugenddienst Stunden/ViewModels/StundenViewModel.cs b/Jugenddienst Stunden/ViewModels/StundenViewModel.cs index ecdf4c2..77bbb74 100644 --- a/Jugenddienst Stunden/ViewModels/StundenViewModel.cs +++ b/Jugenddienst Stunden/ViewModels/StundenViewModel.cs @@ -3,6 +3,7 @@ using CommunityToolkit.Mvvm.Input; using System.Windows.Input; using Microsoft.Maui.Controls; using Microsoft.Maui.Networking; +using ZXing.Net.Maui; namespace Jugenddienst_Stunden.ViewModels { @@ -33,6 +34,7 @@ namespace Jugenddienst_Stunden.ViewModels { public string OvertimeMonth { get => _hour.overtime_month; } + public string Title { get; set; } = Preferences.Default.Get("name", "") + " " + Preferences.Default.Get("surname", ""); public StundenViewModel() { _hour = new Models.Hours(); @@ -43,9 +45,10 @@ namespace Jugenddienst_Stunden.ViewModels { // await Models.Stunde.LoadData(); public async Task LoadData() { - _hour = await Models.Stunde.LoadData(); - Models.Hours Hours = new Models.Hours(); - RefreshProperties(); + _hour = await Models.Stunde.LoadData(); + Models.Hours Hours = new Models.Hours(); + //Title = _hour.operator_api.name + " " + _hour.operator_api.surname; + RefreshProperties(); } private void RefreshProperties() { @@ -55,8 +58,9 @@ namespace Jugenddienst_Stunden.ViewModels { OnPropertyChanged(nameof(ZeitCalculated)); OnPropertyChanged(nameof(ZeitDone)); OnPropertyChanged(nameof(Hours)); + OnPropertyChanged(nameof(Title)); } - + } } diff --git a/Jugenddienst Stunden/Views/AboutPage.xaml b/Jugenddienst Stunden/Views/AboutPage.xaml index 0f2a1cc..5e43fc1 100644 --- a/Jugenddienst Stunden/Views/AboutPage.xaml +++ b/Jugenddienst Stunden/Views/AboutPage.xaml @@ -19,17 +19,15 @@