diff --git a/SambatWidget.UI/App.xaml.cs b/SambatWidget.UI/App.xaml.cs index 6d1b881..ff50d85 100644 --- a/SambatWidget.UI/App.xaml.cs +++ b/SambatWidget.UI/App.xaml.cs @@ -16,7 +16,6 @@ protected override void OnStartup(StartupEventArgs e) { Setting = AppHelpers.LoadAppSettings(); ThemeManager.ApplyDefaultTheme(); - EventParser.ParseEventsJson("./Resources/events_minified.json"); base.OnStartup(e); } protected override void OnExit(ExitEventArgs e) diff --git a/SambatWidget.UI/Helpers/AppHelpers.cs b/SambatWidget.UI/Helpers/AppHelpers.cs index e5cd33a..7c71557 100644 --- a/SambatWidget.UI/Helpers/AppHelpers.cs +++ b/SambatWidget.UI/Helpers/AppHelpers.cs @@ -3,6 +3,7 @@ using System.Windows; using SambatWidget.UI.Models; using System.Text.Json; +using SambatWidget.Core; namespace SambatWidget.UI.Helpers { @@ -11,6 +12,16 @@ public static class AppHelpers private const string REGISTRY_PATH = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; private const string APP_NAME = "SambatWidget"; private const string SETTING_FILE = "sambatwidget.json"; + private const string DEFAULT_EVENT_FILE_PATH = "./Resources/events_minified.json"; + private const string EVENT_FILE = "events_minified.json"; + private static string GetAppDirectoryFilePath(string fileName) + { + if (!string.IsNullOrWhiteSpace(fileName)) + { + return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), APP_NAME, fileName); + } + return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), APP_NAME); + } public static void DisableAutoStartAtStartup() { try @@ -56,13 +67,30 @@ public static bool CopyToClipboard(string text) } public static SettingModel LoadAppSettings() { - if (!File.Exists(SETTING_FILE)) - { - File.WriteAllText(SETTING_FILE, JsonSerializer.Serialize(new SettingModel())); - } try { - string json = File.ReadAllText(SETTING_FILE); + string settingFilePath = GetAppDirectoryFilePath(SETTING_FILE); + string eventFilePath = GetAppDirectoryFilePath(EVENT_FILE); + string appDir = GetAppDirectoryFilePath(null); + if (!Directory.Exists(appDir)) + { + Directory.CreateDirectory(appDir); + } + if (!File.Exists(settingFilePath)) + { + File.WriteAllText(settingFilePath, JsonSerializer.Serialize(new SettingModel())); + } + bool hasEvent = File.Exists(eventFilePath); + if (!hasEvent && File.Exists(DEFAULT_EVENT_FILE_PATH)) + { + File.Copy(DEFAULT_EVENT_FILE_PATH, GetAppDirectoryFilePath(EVENT_FILE), true); + hasEvent = true; + } + if (hasEvent) + { + EventParser.ParseEventsJson(GetAppDirectoryFilePath(EVENT_FILE)); + } + string json = File.ReadAllText(settingFilePath); return JsonSerializer.Deserialize(json); } catch @@ -74,7 +102,7 @@ public static SettingModel SaveAppSettings(SettingModel settings) { try { - File.WriteAllText(SETTING_FILE, JsonSerializer.Serialize(settings)); + File.WriteAllText(GetAppDirectoryFilePath(SETTING_FILE), JsonSerializer.Serialize(settings)); } catch { } return settings; @@ -84,7 +112,8 @@ private static void CoreAutoStartAtStartup(bool enable) RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_PATH, true); if (enable) { - string startPath = System.Reflection.Assembly.GetExecutingAssembly().Location; + string dirPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); + string startPath = Path.Combine(dirPath, $"{APP_NAME}.exe"); key.SetValue(APP_NAME, startPath); } else diff --git a/SambatWidget.UI/SettingWindow.xaml b/SambatWidget.UI/SettingWindow.xaml index c810451..75bac5d 100644 --- a/SambatWidget.UI/SettingWindow.xaml +++ b/SambatWidget.UI/SettingWindow.xaml @@ -7,7 +7,7 @@ xmlns:local="clr-namespace:SambatWidget.UI" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:SambatWidget.UI.ViewModels" - Title="Settings v1.1.0" + Title="Settings v1.1.1" Width="400" Height="450" Background="{DynamicResource BackgroundColor}"