-
Notifications
You must be signed in to change notification settings - Fork 5
Refactor: Improve architecture, performance, and class organization #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,21 @@ | ||
| using Toolkit_Launcher.Models; | ||
| using Toolkit_Launcher.Services; | ||
| namespace Toolkit_Launcher; | ||
|
|
||
| public partial class FrmMain : Form | ||
| private readonly SettingsService _settingsService; | ||
| private readonly ImageCacheService _imageCacheService; | ||
| { | ||
| public bool Leaver = true; | ||
| private static string[] _args = { }; | ||
| private static MyEnums.TargetType _targetType; | ||
|
|
||
| public FrmMain(string[] args) | ||
| public FrmMain(string[] args, SettingsService settingsService, ImageCacheService imageCacheService) | ||
| { | ||
| if (args == null) throw new ArgumentNullException(nameof(args)); | ||
| InitializeComponent(); | ||
| _settingsService = settingsService ?? throw new ArgumentNullException(nameof(settingsService)); | ||
| _imageCacheService = imageCacheService ?? throw new ArgumentNullException(nameof(imageCacheService)); | ||
| #if DEBUG | ||
| args = new[] | ||
| { | ||
|
|
@@ -27,58 +33,22 @@ public FrmMain(string[] args) | |
|
|
||
| private void FrmMain_Load(object sender, EventArgs e) | ||
| { | ||
| PublicClass.ImageList = imageList1; | ||
| InitializeList(); | ||
| // PublicClass.ImageList = imageList1; // Removed: ImageCacheService now handles images | ||
| // InitializeList(); // Removed: SettingsService handles loading | ||
| AddItems(); | ||
| AddSetting(); | ||
| AddOption(); | ||
| contextMenuStrip1.Show(Cursor.Position); | ||
| } | ||
|
|
||
| private static void InitializeList() | ||
| { | ||
| if (!File.Exists(PublicClass.PATH)) return; | ||
| LoadFile(PublicClass.PATH); | ||
| } | ||
|
|
||
| private static void LoadFile(string fileName) | ||
| { | ||
| try | ||
| { | ||
| PublicClass.ListInfo = LoadJson<ListInfo>(fileName); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| MyMessage.ShowError(ex.Message); | ||
| } | ||
| finally | ||
| { | ||
| PublicClass.TempListInfo = PublicClass.ListInfo; | ||
| } | ||
| } | ||
|
|
||
| private static T LoadJson<T>(string fileName) | ||
| { | ||
| if (fileName.IsEmpty()) return default!; | ||
|
|
||
| var objectOut = default(T); | ||
|
|
||
| try | ||
| { | ||
| objectOut = JsonConvert.DeserializeObject<T>(File.ReadAllText(fileName)); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| MyMessage.ShowError(ex.Message); | ||
| } | ||
|
|
||
| return objectOut!; | ||
| } | ||
| // Removed InitializeList, LoadFile, LoadJson as SettingsService handles this | ||
|
|
||
| private void AddItems() | ||
| { | ||
| if (PublicClass.ListInfo == null!) return; | ||
| AddAll(contextMenuStrip1, PublicClass.ListInfo); | ||
| if (_settingsService.CurrentSettings == null!) return; // Use SettingsService | ||
| contextMenuStrip1.SuspendLayout(); | ||
| AddAll(contextMenuStrip1, _settingsService.CurrentSettings); // Use SettingsService | ||
| contextMenuStrip1.ResumeLayout(false); | ||
| } | ||
|
|
||
| private void AddSetting() | ||
|
|
@@ -92,7 +62,7 @@ private void AddSetting() | |
| { | ||
| Leaver = false; | ||
| contextMenuStrip1.Hide(); | ||
| var frmSetting = new FrmSetting(); | ||
| var frmSetting = new FrmSetting(_settingsService, _imageCacheService); | ||
| frmSetting.ShowDialog(); | ||
| frmSetting.Dispose(); | ||
| Application.Exit(); | ||
|
|
@@ -146,13 +116,16 @@ private void AddAll(ToolStrip menu, ListInfo listInfo) | |
| default: | ||
| { | ||
| if (string.IsNullOrWhiteSpace(group.Value.FilePath)) continue; | ||
| var image = group.Value.IconPath.GetImage(); | ||
| if (image == null!) | ||
| var image = _imageCacheService.GetImage(group.Value.IconPath); // Use ImageCacheService | ||
| if (image == null || image == _imageCacheService.GetImage(MyEnums.GroupType.Null)) // Check against default null image | ||
| { | ||
| image = group.Type.GetImage(); | ||
| menu.Items.Add(group.Type.ToString(), image); | ||
| return; | ||
| image = _imageCacheService.GetImage(group.Type); // Use ImageCacheService | ||
| // menu.Items.Add(group.Type.ToString(), image); // This line seems to be an early exit, might need review | ||
| // return; // This line seems to be an early exit, might need review | ||
|
Comment on lines
+123
to
+124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These lines for adding a menu item and returning were commented out: // menu.Items.Add(group.Type.ToString(), image);
// return; Was the early |
||
| } | ||
| // Ensure image is not null before creating ToolStripMenuItem, or handle it | ||
| if (image == null) image = _imageCacheService.GetImage(MyEnums.GroupType.Null); | ||
|
|
||
|
|
||
| var result = new ToolStripMenuItem(group.Identify.Name, image); | ||
| result.Click += (_, _) => | ||
|
|
@@ -197,11 +170,13 @@ private List<ToolStripItem> AddAll(GroupInfo groupInfo) | |
| default: | ||
| { | ||
| if (string.IsNullOrWhiteSpace(group.Value.FilePath)) continue; | ||
| var image = group.Value.IconPath.GetImage(); | ||
| if (image == null!) | ||
| var image = _imageCacheService.GetImage(group.Value.IconPath); // Use ImageCacheService | ||
| if (image == null || image == _imageCacheService.GetImage(MyEnums.GroupType.Null)) // Check against default null image | ||
| { | ||
| image = group.Type.GetImage(); | ||
| image = _imageCacheService.GetImage(group.Type); // Use ImageCacheService | ||
| } | ||
| // Ensure image is not null before creating ToolStripMenuItem, or handle it | ||
| if (image == null) image = _imageCacheService.GetImage(MyEnums.GroupType.Null); | ||
|
|
||
| var item = new ToolStripMenuItem(group.Identify.Name, image); | ||
| item.Click += (_, _) => | ||
|
|
@@ -226,4 +201,4 @@ private void FrmMain_Deactivate(object sender, EventArgs e) | |
| if (!Leaver) return; | ||
| Application.Exit(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the null-forgiving operator (
!) necessary here for_settingsService.CurrentSettings? TheSettingsServiceconstructor initializesCurrentSettingsby callingLoadSettingsFromFile(), which returns anew ListInfo()if the file doesn't exist or if an error occurs during loading. This suggestsCurrentSettingsshould always be non-null afterSettingsServiceinstantiation.If
CurrentSettingscan indeed be null under some circumstance not immediately apparent, could we add a comment explaining that scenario or handle the null case more explicitly rather than suppressing a potential warning?