Skip to content

Commit d3dfccb

Browse files
authored
fix: use SelectedIndex for Platform/AppType ComboBox (avoids all type conversion) (#39)
- Config.Platform/AppType back to int (1=Windows, 2=Linux; 1=ClientApp, 2=UpgradeApp) - Add PlatformIndex/AppTypeIndex computed properties mapping int ↔ index - XAML: SelectedIndex binding instead of SelectedItem - No reference equality or type conversion issues
1 parent 1517a8a commit d3dfccb

5 files changed

Lines changed: 28 additions & 32 deletions

File tree

src/Models/SimulateConfigModel.cs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,16 @@ namespace GeneralUpdate.Tools.Models;
55
public record PlatformItem(int Value, string DisplayName) { public override string ToString() => DisplayName; }
66
public record AppTypeItem(int Value, string DisplayName) { public override string ToString() => DisplayName; }
77

8-
/// <summary>
9-
/// Configuration for the simulate-update module.
10-
/// </summary>
118
public partial class SimulateConfigModel : ObservableObject
129
{
13-
/// <summary>User-provided old-version app directory to test against.</summary>
1410
[ObservableProperty] private string _appDirectory = string.Empty;
15-
16-
/// <summary>Path to a patch .zip generated by the Patch module.</summary>
1711
[ObservableProperty] private string _patchFilePath = string.Empty;
18-
19-
/// <summary>The current version of the app being tested.</summary>
2012
[ObservableProperty] private string _currentVersion = "1.0.0.0";
21-
22-
/// <summary>The target version the patch upgrades to.</summary>
2313
[ObservableProperty] private string _targetVersion = "2.0.0.0";
24-
25-
/// <summary>Platform selector.</summary>
26-
[ObservableProperty] private PlatformItem _platform = new(1, "Windows");
27-
28-
/// <summary>AppType selector.</summary>
29-
[ObservableProperty] private AppTypeItem _appType = new(1, "ClientApp");
30-
31-
/// <summary>Application secret key for the update API.</summary>
14+
[ObservableProperty] private int _platform = 1;
15+
[ObservableProperty] private int _appType = 1;
3216
[ObservableProperty] private string _appSecretKey = "dfeb5833-975e-4afb-88f1-6278ee9aeff6";
33-
34-
/// <summary>Product identifier.</summary>
3517
[ObservableProperty] private string _productId = "2d974e2a-31e6-4887-9bb1-b4689e98c77a";
36-
37-
/// <summary>Directory where client.cs / upgrade.cs and server are generated.</summary>
3818
[ObservableProperty] private string _outputDirectory = string.Empty;
39-
40-
/// <summary>Server port assigned at runtime (set by SimulationService).</summary>
4119
public int ServerPort { get; set; } = 5000;
4220
}

src/Services/ReportGeneratorService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public async Task<string> GenerateAsync(
2626
sb.AppendLine("|-------|-------|");
2727
sb.AppendLine($"| Patch | {EscapeMd(config.PatchFilePath)} |");
2828
sb.AppendLine($"| App Directory | {EscapeMd(config.AppDirectory)} |");
29-
sb.AppendLine($"| Platform | {config.Platform.DisplayName} |");
30-
sb.AppendLine($"| AppType | {config.AppType.DisplayName} |");
29+
sb.AppendLine($"| Platform | {config.Platform} |");
30+
sb.AppendLine($"| AppType | {config.AppType} |");
3131
sb.AppendLine($"| Version | {config.CurrentVersion}{config.TargetVersion} |");
3232
sb.AppendLine($"| Server Port | {config.ServerPort} |");
3333
sb.AppendLine($"| Simulation Time | {DateTime.Now:yyyy-MM-dd HH:mm:ss} |");

src/Services/SimulationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task<SimulationResult> RunAsync(
5050

5151
var hash = ComputeQuickHash(patchDest);
5252
LocalUpdateServerFiles.Register(patchName, patchDest);
53-
_server.Updates.Add((config.CurrentVersion, config.TargetVersion, hash, patchDest, config.AppType.Value));
53+
_server.Updates.Add((config.CurrentVersion, config.TargetVersion, hash, patchDest, config.AppType));
5454

5555
await _server.StartAsync(config.ServerPort);
5656
Log($" Server running on {_server.BaseUrl}", progress);

src/ViewModels/SimulateViewModel.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.ObjectModel;
33
using System.IO;
4+
using System.Linq;
45
using System.Threading.Tasks;
56
using CommunityToolkit.Mvvm.ComponentModel;
67
using CommunityToolkit.Mvvm.Input;
@@ -36,8 +37,26 @@ public partial class SimulateViewModel : ViewModelBase
3637
public SimulateViewModel()
3738
{
3839
_status = _loc["Patch.Ready"];
39-
Config.Platform = Platforms[0];
40-
Config.AppType = AppTypes[0];
40+
}
41+
42+
/// <summary>
43+
/// Maps Config.Platform (int) to Platforms collection index.
44+
/// 1 (Windows) → 0, 2 (Linux) → 1.
45+
/// </summary>
46+
public int PlatformIndex
47+
{
48+
get => Config.Platform == 2 ? 1 : 0;
49+
set => Config.Platform = value == 1 ? 2 : 1;
50+
}
51+
52+
/// <summary>
53+
/// Maps Config.AppType (int) to AppTypes collection index.
54+
/// 1 (ClientApp) → 0, 2 (UpgradeApp) → 1.
55+
/// </summary>
56+
public int AppTypeIndex
57+
{
58+
get => Config.AppType == 2 ? 1 : 0;
59+
set => Config.AppType = value == 1 ? 2 : 1;
4160
}
4261

4362
async Task<string?> PickFolder(string title)
@@ -87,7 +106,6 @@ async Task StartSimulation()
87106
foreach (var note in result.Notes)
88107
L($" Note: {note}");
89108

90-
// Generate report
91109
var reportPath = await _report.GenerateAsync(Config, result, Config.OutputDirectory);
92110
L($"Report: {reportPath}");
93111
}

src/Views/SimulateView.axaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
<Grid ColumnDefinitions="Auto,*,Auto,*">
3939
<TextBlock Grid.Column="0" Text="Platform" VerticalAlignment="Center"/>
4040
<ComboBox Grid.Column="1" ItemsSource="{Binding Platforms}"
41-
SelectedItem="{Binding Config.Platform}" Margin="8,0,16,0"/>
41+
SelectedIndex="{Binding PlatformIndex}" Margin="8,0,16,0"/>
4242
<TextBlock Grid.Column="2" Text="AppType" VerticalAlignment="Center"/>
4343
<ComboBox Grid.Column="3" ItemsSource="{Binding AppTypes}"
44-
SelectedItem="{Binding Config.AppType}" Margin="8,0"/>
44+
SelectedIndex="{Binding AppTypeIndex}" Margin="8,0"/>
4545
</Grid>
4646
<Grid ColumnDefinitions="Auto,*,Auto,*">
4747
<TextBlock Grid.Column="0" Text="AppSecret" VerticalAlignment="Center"/>

0 commit comments

Comments
 (0)