Skip to content

Commit

Permalink
patch: app settings file path changes; startup fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Samir Dahal committed Apr 3, 2024
1 parent beee74f commit c1a91f2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
1 change: 0 additions & 1 deletion SambatWidget.UI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 36 additions & 7 deletions SambatWidget.UI/Helpers/AppHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Windows;
using SambatWidget.UI.Models;
using System.Text.Json;
using SambatWidget.Core;

namespace SambatWidget.UI.Helpers
{
Expand All @@ -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
Expand Down Expand Up @@ -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<SettingModel>(json);
}
catch
Expand All @@ -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;
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion SambatWidget.UI/SettingWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit c1a91f2

Please sign in to comment.