Errorhandling Token problems

This commit is contained in:
2024-09-11 14:29:21 +02:00
parent 6cf55aa258
commit 4f91c63400
4 changed files with 113 additions and 62 deletions

View File

@@ -28,7 +28,7 @@ namespace Jugenddienst_Stunden.Models {
//public List<int>? timetable; //public List<int>? timetable;
public event EventHandler<string> AlertEvent;
public static async Task<Operator> LoadData(string apiKey) { public static async Task<Operator> LoadData(string apiKey) {
//var apiKey = Preferences.Default.Get("apiKey", ""); //var apiKey = Preferences.Default.Get("apiKey", "");
@@ -41,12 +41,25 @@ namespace Jugenddienst_Stunden.Models {
//throw new Exception("Keine Internetverbindung"); //throw new Exception("Keine Internetverbindung");
} else { } else {
var tokendata = new TokenData(apiKey); var tokendata = new TokenData(apiKey);
//try {
string data = await Auth.GetApiDataWithAuthAsync(tokendata.url, tokendata.apiKey); string data = await Auth.GetApiDataWithAuthAsync(tokendata.url, tokendata.apiKey);
if (data == "\"Lalala\"") {
throw new Exception("Problem mit Token");
}
if (data != null) { if (data != null) {
OperatorVar = Newtonsoft.Json.JsonConvert.DeserializeObject<Operator>(data); OperatorVar = Newtonsoft.Json.JsonConvert.DeserializeObject<Operator>(data);
Preferences.Default.Set("name", OperatorVar.name); Preferences.Default.Set("name", OperatorVar.name);
Preferences.Default.Set("surname", OperatorVar.surname); Preferences.Default.Set("surname", OperatorVar.surname);
} }
//} catch (Exception e) {
// //AlertEvent?.Invoke(this, e.Message);
// await App.Current.MainPage.DisplayAlert("Fehler",
// e.Message,
// "OK");
//}
} }
return OperatorVar; return OperatorVar;

View File

@@ -41,12 +41,18 @@ namespace Jugenddienst_Stunden.Models
//apiKey = "MTQzfEFlMVRjQXdZMnI4RmpxZ0FSY3A0VEN2bVZYVXxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk="; //apiKey = "MTQzfEFlMVRjQXdZMnI4RmpxZ0FSY3A0VEN2bVZYVXxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk=";
//name = "Chri"; //name = "Chri";
//surname = "Fe"; //surname = "Fe";
apiKey = "MTI3fDEyYURVdHVZVWRaZk91eDlNcjZDUFlTdmdkNHxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk="; //apiKey = "MTI3fDEyYURVdHVZVWRaZk91eDlNcjZDUFlTdmdkNHxodHRwOi8vaG91cnMuZGF1bmkubWluZS5udTo4MS9hcHBhcGk=";
name = "Ju"; //name = "Ju";
surname = "Ze"; //surname = "Ze";
Preferences.Default.Set("apiKey", apiKey);
Preferences.Default.Set("name", name); //apiKey = "M3wvK09XQkZod1l4SXlVcGVWazdlUmYrTnBWaUl8aHR0cHM6Ly9ob3Vycy5waWRhLmluZm8vYXBwYXBp";
Preferences.Default.Set("surname", surname); //name = "Le";
//surname = "Ma";
//Preferences.Default.Set("apiKey", apiKey);
//Preferences.Default.Set("name", name);
//Preferences.Default.Set("surname", surname);
Hours hours = new Hours(); Hours hours = new Hours();
@@ -64,6 +70,9 @@ namespace Jugenddienst_Stunden.Models
if (data == null) { if (data == null) {
throw new Exception("Keine Daten erhalten"); throw new Exception("Keine Daten erhalten");
} }
if(data == "\"Lalala\"") {
throw new Exception("Problem mit Token");
}
hours = JsonConvert.DeserializeObject<Hours>(data); hours = JsonConvert.DeserializeObject<Hours>(data);
//Preferences.Default.Set("name", hours.operator_api.name); //Preferences.Default.Set("name", hours.operator_api.name);

View File

@@ -71,7 +71,9 @@ namespace Jugenddienst_Stunden.ViewModels {
GetDay = dateToday.Day; GetDay = dateToday.Day;
OnPropertyChanged(); OnPropertyChanged();
_ = LoadData(); // Use discard operator to explicitly ignore the returned Task _ = LoadData(); // Use discard operator to explicitly ignore the returned Task
RefreshProperties(); //RefreshProperties();
OnPropertyChanged(nameof(TimeDay));
OnPropertyChanged(nameof(ShowDay));
} }
} }
} }
@@ -144,15 +146,29 @@ namespace Jugenddienst_Stunden.ViewModels {
public async Task LoadData() { public async Task LoadData() {
try { try {
_hour = await Models.Stunde.LoadData(); _hour = await Models.Stunde.LoadData();
if (_hour.zeit_total_daily_api != null) {
TimeDay = _hour.zeit_total_daily_api.Where(static p => p.Day == GetDay).ToList() ?? new List<TimeDay> { new TimeDay { Day = GetDay, Hours = 0 } };
RefreshProperties();
}
} catch (Exception e) { } catch (Exception e) {
AlertEvent?.Invoke(this, e.Message); AlertEvent?.Invoke(this, e.Message);
} }
//Models.Hours Hours = new Models.Hours();
//Title = _hour.operator_api.name + " " + _hour.operator_api.surname;
if (_hour != null) { //if (_hour.zeit_total_daily_api != null) {
TimeDay = _hour.zeit_total_daily_api.Where(p => p.Day == GetDay).ToList(); // try {
RefreshProperties(); // TimeDay = _hour.zeit_total_daily_api.Where(static p => p.Day == GetDay).ToList() ?? new List<TimeDay> { new TimeDay { Day = GetDay, Hours = 0 } };
}
// RefreshProperties();
// } catch (Exception e) {
// AlertEvent?.Invoke(this, e.Message);
// }
//} else {
// await App.Current.MainPage.DisplayAlert("Fehler",
// "zeit_total_daily_api ist leer",
// "OK");
//}
} }
private void RefreshProperties() { private void RefreshProperties() {

View File

@@ -37,11 +37,13 @@
<DatePicker Grid.Column="0" MinimumDate="{Binding MinimumDate}" <DatePicker Grid.Column="0" MinimumDate="{Binding MinimumDate}"
MaximumDate="{Binding MaximumDate}" MaximumDate="{Binding MaximumDate}"
Date="{Binding DateToday}" Format="dddd, d. MMM. yyyy" /> Date="{Binding DateToday}" Format="dddd, d. MMM. yyyy" />
<Label Grid.Column="1" Text="{Binding TimeDay[0].Hours}" VerticalOptions="Center"></Label> <Border Grid.Column="1" Margin="15,0,0,0" Padding="15,0,0,0">
<Label Text="{Binding TimeDay[0].Hours}" VerticalOptions="Center"></Label>
</Border>
</Grid> </Grid>
<Grid ColumnDefinitions="Auto,Auto,Auto"> <Grid ColumnDefinitions="*,*,*">
<Picker x:Name="pick_gemeinde" Title="Gemeinde" ItemsSource="{Binding Options}" SelectedItem="{Binding SelectedOption, Mode=TwoWay}" Grid.Column="0" > <Picker x:Name="pick_gemeinde" Title="Gemeinde" ItemsSource="{Binding Options}" SelectedItem="{Binding SelectedOption, Mode=TwoWay}" Grid.Column="0" >
</Picker> </Picker>
<Picker x:Name="pick_projekt" Title="Projekt" ItemsSource="{Binding OptionsProjekt}" SelectedItem="{Binding SelectedOption, Mode=TwoWay}" Grid.Column="1" > <Picker x:Name="pick_projekt" Title="Projekt" ItemsSource="{Binding OptionsProjekt}" SelectedItem="{Binding SelectedOption, Mode=TwoWay}" Grid.Column="1" >
@@ -50,13 +52,24 @@
</Picker> </Picker>
</Grid> </Grid>
<Entry x:Name="entry" Placeholder="Tätigkeit" /> <Entry x:Name="entry" Placeholder="Tätigkeit" />
<Grid ColumnDefinitions="2*,Auto,2*,Auto">
<Label Text="Beginn" Grid.Column="0" Background="LightGreen" TextColor="Gray" VerticalTextAlignment="Center" HorizontalTextAlignment="End" Padding="0,0,10,0"></Label>
<TimePicker HorizontalOptions="Center" Grid.Column="1" Background="LightGreen" TextColor="Gray" />
<Label Text="Ende" Grid.Column="2" Background="OrangeRed" TextColor="White" VerticalTextAlignment="Center" HorizontalTextAlignment="End" Padding="0,0,10,0"></Label>
<TimePicker HorizontalOptions="Center" Grid.Column="3" Background="OrangeRed" TextColor="White" /> <FlexLayout Direction="Row" AlignItems="Start" Wrap="Wrap">
</Grid> <Border Background="LightGreen">
<HorizontalStackLayout>
<Label Text="Beginn" TextColor="Gray" VerticalTextAlignment="Center" HorizontalTextAlignment="End" Padding="0,0,10,0" Margin="5,0,0,13"></Label>
<TimePicker HorizontalOptions="Center" TextColor="Gray" Format="HH:mm" MinimumWidthRequest="80" />
</HorizontalStackLayout>
</Border>
<Border Background="OrangeRed">
<HorizontalStackLayout>
<Label Text="Ende" TextColor="White" VerticalTextAlignment="Center" HorizontalTextAlignment="End" Padding="0,0,10,0" Margin="5,0,0,13"></Label>
<TimePicker TextColor="White" Format="HH:mm" MinimumWidthRequest="80" />
</HorizontalStackLayout>
</Border>
</FlexLayout>
<Label Text="{Binding ShowDay}"></Label> <Label Text="{Binding ShowDay}"></Label>