-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
112 lines (94 loc) · 3.71 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;
namespace GuildWars2APIWpfApp
{
public partial class App : Application
{
private LoadingWindow? loadingScreen;
private bool isMainWindowOpen = false;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Create and show the loading screen
loadingScreen = new LoadingWindow();
loadingScreen.OkButtonClicked += LoadingScreen_OkButtonClicked; // Subscribe to the event
loadingScreen.Show();
}
private async void LoadingScreen_OkButtonClicked(object? sender, EventArgs e)
{
if (loadingScreen != null)
{
// Obtain the API key from the loading screen
string apiKey = loadingScreen.ApiKey;
// Perform initialization tasks asynchronously
bool apiKeyValidated = await InitializeApp(apiKey);
if (apiKeyValidated)
{
// Create an instance of the MainWindow with the apiKey parameter
MainWindow mainWindow = new(apiKey);
mainWindow.Closed += MainWindow_Closed; // Handle the Closed event
// Show the main window asynchronously
mainWindow.Show();
// Keep the loading screen open until the main window is fully loaded
mainWindow.ContentRendered += (s, _) =>
{
// Close the loading screen when the main window is fully loaded
loadingScreen?.Close();
};
}
else
{
// Exit the application if the API key is not validated
Shutdown();
}
}
}
private void MainWindow_Closed(object? sender, EventArgs e)
{
// Set the flag to indicate that the main window is closed
isMainWindowOpen = false;
}
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
// Shutdown the application
Application.Current.Shutdown();
}
private static async Task<bool> InitializeApp(string apiKey)
{
try
{
const string url = "https://api.guildwars2.com/v2/tokeninfo";
HttpClient httpClient = new();
bool apiKeyValidated = false;
// Add API key to request headers
httpClient.DefaultRequestHeaders.Clear();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
// Send GET request to the /v2/tokeninfo endpoint
HttpResponseMessage response = await httpClient.GetAsync(url);
// Check if the response is successful
if (response.IsSuccessStatusCode)
{
Debug.WriteLine("API Verify Success");
// If the API key is valid, return true
return true;
}
else
{
Debug.WriteLine("API Verify Failure");
// If the API key is invalid, return false
return false;
}
}
catch (Exception ex)
{
// Handle any exceptions
MessageBox.Show($"An error occurred during initialization: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
}
}
}