This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
124 lines (106 loc) · 4.73 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
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using System;
//using Windows.Storage;
using ZoaInfoTool.Services;
using ZoaInfoTool.Services.Interfaces;
using ZoaInfoTool.ViewModels;
using ZoaInfoTool.Utils;
using Windows.Graphics;
namespace ZoaInfoTool;
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application
{
public IServiceProvider Services { get; }
public new static App Current => (App)Application.Current;
public static ISettingsService Settings { get; private set; }
private AppWindow MainAppWindowInterop { get; set; }
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
Services = ConfigureServices();
this.InitializeComponent();
}
private static IServiceProvider ConfigureServices()
{
var services = new ServiceCollection();
// Services & Repositories
services.AddHttpClient();
services.AddSingleton<IAtisService, ClowdDatisFetchService>();
services.AddSingleton<IRouteSummaryService, FlightAwareRouteService>();
services.AddSingleton<IChartService, FaaChartService>();
services.AddSingleton<IAirlineIcaoService, GithubAirlineIcaoService>();
services.AddSingleton<IAircraftIcaoService, GithubAircraftIcaoService>();
services.AddSingleton<ILoaRulesService, GithubLoaService>();
services.AddSingleton<IAliasRouteService, GithubAliasRouteService>();
services.AddSingleton<IAirportIcaoService, VatSpyDataService>();
services.AddSingleton<ISettingsService, SettingsService>();
// Viewmodels
services.AddTransient<DatisViewModel>();
services.AddTransient<RwRouteViewModel>();
services.AddTransient<SkyVectorViewModel>();
services.AddTransient<ChartViewModel>();
services.AddTransient<IcaoCodesViewModel>();
services.AddTransient<LoaViewModel>();
services.AddTransient<AliasRouteViewModel>();
services.AddTransient<ClockViewModel>();
return services.BuildServiceProvider();
}
/// <summary>
/// Invoked when the application is launched.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
// Get interop window and save
IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(m_window);
WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd);
MainAppWindowInterop = AppWindow.GetFromWindowId(windowId);
//Set icon
MainAppWindowInterop.SetIcon(@"Assets\Ico-256x256.ico");
// Check if saved settings exist for window size and resize if found. If not, use 800x600 default
Settings = App.Current.Services.GetRequiredService<ISettingsService>();
await Settings.LoadFromFileAsync();
SizeInt32 size = new(800, 600);
if (Settings.Values.TryGetValue("WindowWidth", out string width) && Settings.Values.TryGetValue("WindowHeight", out string height))
{
size = new SizeInt32(int.Parse(width), int.Parse(height));
}
MainAppWindowInterop.Resize(size);
// Check if saved settings exist for window position. If so, move window to saved position
if (Settings.Values.TryGetValue("WindowX", out string x) && Settings.Values.TryGetValue("WindowY", out string y))
{
MainAppWindowInterop.Move(new PointInt32(int.Parse(x), int.Parse(y)));
}
// Register handlers to save the window size and position for future startup whenever user changes
m_window.SizeChanged += SaveSize;
MainAppWindowInterop.Changed += SavePosition;
// Set title and start
m_window.Title = "ZOA Info";
m_window.Activate();
}
private void SaveSize(object _, WindowSizeChangedEventArgs args)
{
Settings.Values["WindowWidth"] = args.Size.Width.ToString();
Settings.Values["WindowHeight"] = args.Size.Height.ToString();
}
private void SavePosition(object sender, AppWindowChangedEventArgs args)
{
if (args.DidPositionChange)
{
Settings.Values["WindowX"] = MainAppWindowInterop.Position.X.ToString();
Settings.Values["WindowY"] = MainAppWindowInterop.Position.Y.ToString();
}
}
private Window m_window;
}