-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.axaml.cs
94 lines (77 loc) · 2.54 KB
/
App.axaml.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
using System;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using System.Threading;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using iTimeSlot.Models;
using iTimeSlot.Shared;
using iTimeSlot.ViewModels;
using iTimeSlot.Views;
namespace iTimeSlot;
public partial class App : Application
{
public override void Initialize()
{
EnsureOneInstance();
AvaloniaXamlLoader.Load(this);
}
private static Mutex _mutex = null;
const string appName = "iTimeSlot";
private void EnsureOneInstance()
{
string mutexId = string.Format("Global\\{0}", appName);
bool createdNew;
_mutex = new Mutex(true, mutexId, out createdNew);
if (!createdNew)
{
Console.WriteLine("The app is already running, either in the foreground or the tray. \n" +
"Exiting the application...");
Environment.Exit(0);
}
}
public override void OnFrameworkInitializationCompleted()
{
LoadTimeSlots();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
//bind App.axaml to ApplicationViewModel
this.DataContext = new ApplicationViewModel();
desktop.MainWindow = new MainWindow
{
//DataContext = new MainWindowViewModel(),
};
}
base.OnFrameworkInitializationCompleted();
}
//Load time_slots.json or default config to shared instance
private void LoadTimeSlots()
{
string fileName = Global.ConfigPath;
Debug.WriteLine($"LoadTimeSlots from {fileName}");
if (File.Exists(fileName))
{
try
{
string jsonString = File.ReadAllText(fileName);
Console.WriteLine(jsonString);
var settings = JsonSerializer.Deserialize(jsonString,new JsonContext().Settings);
Global.LoaddedSetting = settings;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
var settings = Global.EnsureDefaultConfigFile();
Global.LoaddedSetting = settings;
}
}
else
{
Console.WriteLine("setting.json not found, use default time slots");
var settings = Global.EnsureDefaultConfigFile();
Global.LoaddedSetting = settings;
}
}
}