Skip to content

Commit

Permalink
๐Ÿ’พ ๐ŸŽ‡ Feat, Style(Step 1): All-round optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynesshely committed Jan 9, 2025
1 parent 28b009c commit 1cf6af0
Show file tree
Hide file tree
Showing 60 changed files with 1,235 additions and 1,101 deletions.
14 changes: 2 additions & 12 deletions KitX Dashboard/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,25 +172,15 @@ private static void InitializeLiveCharts()

public override void OnFrameworkInitializationCompleted()
{
var location = $"{nameof(App)}.{nameof(OnFrameworkInitializationCompleted)}";
const string location = $"{nameof(App)}.{nameof(OnFrameworkInitializationCompleted)}";

if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow { DataContext = new MainWindowViewModel() };
}

if (ConfigManager.Instance.AppConfig.App.ShowAnnouncementWhenStart)
new Thread(async () =>
{
try
{
await AnouncementManager.CheckNewAnnouncements();
}
catch (Exception ex)
{
Log.Error(ex, $"In {location}: {ex.Message}");
}
}).Start();
new Thread(async () => await AnnouncementManager.CheckNewAnnouncements()).Start();

base.OnFrameworkInitializationCompleted();
}
Expand Down
105 changes: 59 additions & 46 deletions KitX Dashboard/AppFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,27 @@ namespace KitX.Dashboard;

public static class AppFramework
{
private readonly static Queue<Action> actionsInInitialization = [];
private static readonly Queue<Action> actionsInInitialization = [];

public static void ProcessStartupArguments()
{
Parser.Default.ParseArguments<StartupOptions>(Environment.GetCommandLineArgs())
Parser
.Default.ParseArguments<StartupOptions>(Environment.GetCommandLineArgs())
.WithParsed(opt =>
{
ConstantTable.IsSingleProcessStartMode = !opt.DisableSingleProcessCheck;
ConstantTable.EnabledConfigFileHotReload = !opt.DisableConfigHotReload;
ConstantTable.SkipNetworkSystemOnStartup = opt.DisableNetworkSystemOnStartup;

TasksManager.RunTask(() =>
{
if (opt.PluginPath is not null)
ImportPlugin(opt.PluginPath);
}, $"{nameof(ImportPlugin)}", catchException: true);
TasksManager.RunTask(
() =>
{
if (opt.PluginPath is not null)
ImportPlugin(opt.PluginPath);
},
$"{nameof(ImportPlugin)}",
catchException: true
);
});
}

Expand All @@ -55,9 +60,12 @@ public static void RunFramework()
{
var waitCount = 0;

while (Process.GetProcesses().Count(x => x.ProcessName.StartsWith("KitX.Dashboard")) >= 2)
while (
Process.GetProcesses().Count(x => x.ProcessName.StartsWith("KitX.Dashboard")) >= 2
)
{
if (waitCount > 10) Environment.Exit(ExitCodes.WaitRestartingLockFileTooLong);
if (waitCount > 10)
Environment.Exit(ExitCodes.WaitRestartingLockFileTooLong);

++waitCount;

Expand All @@ -74,11 +82,13 @@ public static void RunFramework()
ProcessStartupArguments();

if (ConstantTable.IsSingleProcessStartMode)
Process.GetProcesses().WhenCount(
count => count >= 2,
item => item.ProcessName.StartsWith("KitX.Dashboard"),
_ => Environment.Exit(ExitCodes.MultiProcessesStarted)
);
Process
.GetProcesses()
.WhenCount(
count => count >= 2,
item => item.ProcessName.StartsWith("KitX.Dashboard"),
_ => Environment.Exit(ExitCodes.MultiProcessesStarted)
);

LoadResource();

Expand All @@ -97,11 +107,7 @@ public static void RunFramework()
rollingInterval: RollingInterval.Hour,
fileSizeLimitBytes: config.Log.LogFileSingleMaxSize,
buffered: true,
flushToDiskInterval: new(
0,
0,
config.Log.LogFileFlushInterval
),
flushToDiskInterval: new(0, 0, config.Log.LogFileFlushInterval),
restrictedToMinimumLevel: config.Log.LogLevel,
rollOnFileSizeLimit: true,
retainedFileCountLimit: config.Log.LogFileMaxCount
Expand Down Expand Up @@ -142,20 +148,21 @@ public static void RunFramework()

#region Initialize WebManager

Instances.SignalTasksManager!.SignalRun(nameof(SignalsNames.MainWindowInitSignal), () =>
{
new Thread(async () =>
Instances.SignalTasksManager!.SignalRun(
nameof(SignalsNames.MainWindowInitSignal),
() =>
{
Thread.Sleep(
Convert.ToInt32(config.Web.DelayStartSeconds * 1000)
);
new Thread(async () =>
{
Thread.Sleep(Convert.ToInt32(config.Web.DelayStartSeconds * 1000));

if (ConstantTable.SkipNetworkSystemOnStartup)
Instances.WebManager = new();
else
Instances.WebManager = await new WebManager().RunAsync(new());
}).Start();
});
if (ConstantTable.SkipNetworkSystemOnStartup)
Instances.WebManager = new();
else
Instances.WebManager = await new WebManager().RunAsync(new());
}).Start();
}
);

#endregion

Expand All @@ -167,13 +174,16 @@ public static void RunFramework()

#region Initialize persistent windows

Instances.SignalTasksManager.SignalRun(nameof(SignalsNames.MainWindowInitSignal), () =>
{
Dispatcher.UIThread.Post(() =>
Instances.SignalTasksManager.SignalRun(
nameof(SignalsNames.MainWindowInitSignal),
() =>
{
ViewInstances.PluginsLaunchWindow = new();
});
});
Dispatcher.UIThread.Post(() =>
{
ViewInstances.PluginsLaunchWindow = new();
});
}
);

#endregion

Expand All @@ -182,13 +192,14 @@ public static void RunFramework()

private static void InitDataBase()
{
var location = $"{nameof(AppFramework)}.{nameof(InitDataBase)}";
const string location = $"{nameof(AppFramework)}.{nameof(InitDataBase)}";

try
{
var dir = ConstantTable.DataPath.GetFullPath();

if (!Directory.Exists(dir)) _ = Directory.CreateDirectory(dir);
if (!Directory.Exists(dir))
_ = Directory.CreateDirectory(dir);

var dbfile = ConstantTable.ActivitiesDataBaseFilePath.GetFullPath();

Expand All @@ -206,7 +217,7 @@ private static void InitDataBase()

private static async void LoadResource()
{
var location = $"{nameof(AppFramework)}.{nameof(LoadResource)}";
const string location = $"{nameof(AppFramework)}.{nameof(LoadResource)}";

try
{
Expand All @@ -220,11 +231,12 @@ private static async void LoadResource()
}
}

public static void AfterInitailization(Action action) => actionsInInitialization.Enqueue(action);
public static void AfterInitailization(Action action) =>
actionsInInitialization.Enqueue(action);

private static void ImportPlugin(string kxpPath)
{
var location = $"{nameof(AppFramework)}.{nameof(ImportPlugin)}";
const string location = $"{nameof(AppFramework)}.{nameof(ImportPlugin)}";

try
{
Expand All @@ -247,11 +259,10 @@ private static void ImportPlugin(string kxpPath)

public static void EnsureExit()
{

if (Design.IsDesignMode)
return;

var location = $"{nameof(AppFramework)}.{nameof(EnsureExit)}";
const string location = $"{nameof(AppFramework)}.{nameof(EnsureExit)}";

ConstantTable.EnsureExiting = true;

Expand Down Expand Up @@ -281,7 +292,8 @@ public static void EnsureExit()

var path = Process.GetCurrentProcess().MainModule?.FileName;

if (path is not null) Process.Start(path);
if (path is not null)
Process.Start(path);
}

Thread.Sleep(ConfigManager.Instance.AppConfig.App.LastBreakAfterExit);
Expand All @@ -294,7 +306,8 @@ public static void EnsureExit()
}
}).Start();

while (ConstantTable.EnsureExiting) ;
while (ConstantTable.EnsureExiting)
;

Environment.Exit(0);
}
Expand Down
3 changes: 2 additions & 1 deletion KitX Dashboard/Configuration/ConfigFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public class ConfigFetcher
{
public static AppConfig AppConfig => ConfigManager.Instance.AppConfig;

public static AnnouncementConfig AnnouncementConfig => ConfigManager.Instance.AnnouncementConfig;
public static AnnouncementConfig AnnouncementConfig =>
ConfigManager.Instance.AnnouncementConfig;

public static MarketConfig MarketConfig => ConfigManager.Instance.MarketConfig;

Expand Down
2 changes: 1 addition & 1 deletion KitX Dashboard/Converters/Base64Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Base64ToIconConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
var location = $"{nameof(Base64ToIconConverter)}.{nameof(Convert)}";
const string location = $"{nameof(Base64ToIconConverter)}.{nameof(Convert)}";

try
{
Expand Down
11 changes: 7 additions & 4 deletions KitX Dashboard/Converters/WindowAttributesConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ internal class WindowAttributesConverter
{
internal static Distances PositionCameCenter(Distances location, Screen? screen, Resolution win)
{
if (location.Left == -1)
location.Left = ((screen?.WorkingArea.Width ?? 2560) - (int)win.Width!) / 2;
if (screen is null)
return location;

if (location.Top == -1)
location.Top = (screen?.WorkingArea.Height ?? 1440 - (int)win.Height!) / 2;
if (location.Left - -1.0 < 0.1)
location.Left = (double)((screen.WorkingArea.Width - win.Width!) / 2.0);

if (location.Top - -1.0 < 0.1)
location.Top = (double)((screen.WorkingArea.Height - win.Height!) / 2.0);

return location;
}
Expand Down
36 changes: 15 additions & 21 deletions KitX Dashboard/Generators/GreetingTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,31 @@ internal static string GetKey()
var time = DateTime.Now.Hour;

if (time >= 6 && time < 12)
key = key.Replace("%Step%", "Morning").Replace(
"%Index%",
GenerateRandomIndex(Step.Morning).ToString()
);
key = key.Replace("%Step%", "Morning")
.Replace("%Index%", GenerateRandomIndex(Step.Morning).ToString());
else if (time >= 12 && time < 14)
key = key.Replace("%Step%", "Noon").Replace(
"%Index%",
GenerateRandomIndex(Step.Noon).ToString()
);
key = key.Replace("%Step%", "Noon")
.Replace("%Index%", GenerateRandomIndex(Step.Noon).ToString());
else if (time >= 14 && time < 18)
key = key.Replace("%Step%", "AfterNoon").Replace(
"%Index%",
GenerateRandomIndex(Step.AfterNoon).ToString()
);
key = key.Replace("%Step%", "AfterNoon")
.Replace("%Index%", GenerateRandomIndex(Step.AfterNoon).ToString());
else if (time >= 18 && time < 24)
key = key.Replace("%Step%", "Evening").Replace(
"%Index%",
GenerateRandomIndex(Step.Evening).ToString()
);
key = key.Replace("%Step%", "Evening")
.Replace("%Index%", GenerateRandomIndex(Step.Evening).ToString());
else
key = key.Replace("%Step%", "Night").Replace(
"%Index%",
GenerateRandomIndex(Step.Night).ToString()
);
key = key.Replace("%Step%", "Night")
.Replace("%Index%", GenerateRandomIndex(Step.Night).ToString());

return key;
}

internal enum Step
{
Morning, Noon, AfterNoon, Evening, Night
Morning,
Noon,
AfterNoon,
Evening,
Night,
}

internal static int GenerateRandomIndex(Step step)
Expand Down
12 changes: 6 additions & 6 deletions KitX Dashboard/Instances.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static class Instances

internal static void Initialize()
{
var location = $"{nameof(Instances)}.{nameof(Initialize)}";
const string location = $"{nameof(Instances)}.{nameof(Initialize)}";

TasksManager.RunTask(() =>
{
Expand All @@ -32,11 +32,11 @@ internal static void Initialize()
catchException: true
);

TasksManager.RunTask(
() => KeyHookManager = new KeyHookManager().Hook(),
location.Append("." + nameof(KeyHookManager)),
catchException: true
);
//TasksManager.RunTask(
// () => KeyHookManager = new KeyHookManager().Hook(),
// location.Append("." + nameof(KeyHookManager)),
// catchException: true
//);

TasksManager.RunTask(
() => SecurityManager = SecurityManager.Instance,
Expand Down
Loading

0 comments on commit 1cf6af0

Please sign in to comment.