Skip to content

Commit 01e9897

Browse files
committed
A little cleanup
1 parent c343c2b commit 01e9897

File tree

9 files changed

+116
-120
lines changed

9 files changed

+116
-120
lines changed

Source/VisualPairCoding/VisualPairCoding.AvaloniaUI/EnterNamesWindow.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<Separator/>
1818
<MenuItem Click="LoadSessionConfiguration" Header="_Load"/>
1919
<MenuItem Click="SaveSessionConfiguration" Header="_Save"/>
20-
<MenuItem Click="OnMenuItemClicked" Header="Recent" x:Name="recentMenuItem">
20+
<MenuItem Header="Recent" x:Name="recentMenuItem">
2121
<MenuItem Header="Replace" />
2222
</MenuItem>
2323
<Separator/>

Source/VisualPairCoding/VisualPairCoding.AvaloniaUI/EnterNamesWindow.axaml.cs

Lines changed: 51 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Avalonia.Interactivity;
33
using System;
44
using System.Collections.Generic;
5-
using System.IO;
65
using System.Threading.Tasks;
76
using VisualPairCoding.BL;
87
using VisualPairCoding.BL.AutoUpdates;
@@ -16,18 +15,21 @@ public partial class EnterNamesForm : Window
1615
public EnterNamesForm()
1716
{
1817
InitializeComponent();
19-
WindowStartupLocation = WindowStartupLocation.CenterScreen;
20-
Opened += OnActivated;
21-
Closed += OnClosed;
2218
}
2319

24-
public EnterNamesForm(bool autostart)
20+
public EnterNamesForm(bool autostart, string configPath)
2521
{
26-
_autostart = autostart;
2722
InitializeComponent();
23+
24+
_autostart = autostart;
2825
WindowStartupLocation = WindowStartupLocation.CenterScreen;
2926
Opened += OnActivated;
3027
Closed += OnClosed;
28+
29+
if (!string.IsNullOrEmpty(configPath))
30+
{
31+
LoadSessionIntoGui(configPath);
32+
}
3133
}
3234

3335
private MenuItem GetRecentMenuItem()
@@ -61,37 +63,31 @@ private async Task<bool> AutoUpdate()
6163

6264
private void OnClosed(object? sender, EventArgs e)
6365
{
64-
var appDataPath = SessionConfigurationFolderHandler.GetSessionFolderPath();
65-
if (!string.IsNullOrEmpty(appDataPath))
66-
{
67-
var participants = GetParticipants();
68-
string configName = string.Join("_", participants);
69-
var path = Path.Combine(appDataPath, configName + ".vpcsession");
70-
SaveSessionConfiguration(path, participants);
71-
}
66+
SessionConfigurationFolderHandler.SaveAsRecentSession(
67+
new SessionConfiguration(GetParticipants(), (int)minutesPerTurn.Value)
68+
);
7269
}
7370

7471
private void OnMenuItemClicked(object? sender, RoutedEventArgs e)
7572
{
7673
MenuItem clickedMenuItem = (MenuItem)e.Source!;
7774
string subMenuHeader = clickedMenuItem.Header.ToString()!;
78-
var configPath = Path.Combine(SessionConfigurationFolderHandler.GetSessionFolderPath(), subMenuHeader + ".vpcsession");
79-
LoadSessionIntoGui(configPath);
75+
76+
var sessionConfiguration = SessionConfigurationFolderHandler.LoadRecentSession(subMenuHeader);
77+
78+
LoadSessionIntoGui(sessionConfiguration);
8079
}
8180
private async void OnActivated(object? sender, EventArgs e)
8281
{
83-
MenuItem menuItem = GetRecentMenuItem();
84-
85-
86-
if (!SessionConfigurationFolderHandler.CheckIfConfigurationFolderExistsUnderAppDataFolder())
87-
{
88-
SessionConfigurationFolderHandler.CreateConfigurationFolderUnderAppDataFolder();
89-
}
82+
MenuItem recentSessionsMenuItem = GetRecentMenuItem();
9083

91-
var configs = SessionConfigurationFolderHandler.GetConfigurationFiles();
84+
var configs = SessionConfigurationFolderHandler.GetRecentSessionNames();
9285

93-
menuItem.Items = configs;
94-
menuItem.SelectedIndex = 0;
86+
recentSessionsMenuItem.Items = configs;
87+
if (configs.Length > 0) recentSessionsMenuItem.Click += OnMenuItemClicked;
88+
recentSessionsMenuItem.IsEnabled = configs.Length > 0;
89+
90+
recentSessionsMenuItem.SelectedIndex = 0;
9591

9692
//Only perform auto - updates if not in dev environment
9793
if (AutoUpdateDetector.isUpdateAvailable())
@@ -113,9 +109,8 @@ public void CloseWindow(object? sender, RoutedEventArgs args)
113109

114110
public async void StartForm(object? sender, RoutedEventArgs args)
115111
{
116-
117112
var participants = GetParticipants();
118-
var session = new PairCodingSession(participants.ToArray(), (int)minutesPerTurn.Value);
113+
var session = new PairCodingSession(participants, (int)minutesPerTurn.Value);
119114

120115
var validationMessage = session.Validate();
121116

@@ -151,7 +146,7 @@ public async void StartForm(object? sender, RoutedEventArgs args)
151146
Show();
152147
}
153148

154-
public void LoadSessionIntoGui(string fileName)
149+
private void LoadSessionIntoGui(string fileName)
155150
{
156151
try
157152
{
@@ -180,7 +175,7 @@ public void LoadSessionIntoGui(SessionConfiguration session)
180175
}
181176
}
182177

183-
for (var i = 1; i <= session.Participants.Count; i++)
178+
for (var i = 1; i <= session.Participants.Length; i++)
184179
{
185180
var control = this.FindControl<TextBox>("participant" + i);
186181

@@ -197,7 +192,6 @@ private void RandomizeParticipants(object? sender, RoutedEventArgs args)
197192
{
198193
var participants = GetParticipants();
199194

200-
201195
participant1.Text = "";
202196
participant2.Text = "";
203197
participant3.Text = "";
@@ -211,19 +205,18 @@ private void RandomizeParticipants(object? sender, RoutedEventArgs args)
211205

212206
Shuffle(participants);
213207

214-
if (participants.Count > 0) participant1.Text = participants[0];
215-
if (participants.Count > 1) participant2.Text = participants[1];
216-
if (participants.Count > 2) participant3.Text = participants[2];
217-
if (participants.Count > 3) participant4.Text = participants[3];
218-
if (participants.Count > 4) participant5.Text = participants[4];
219-
if (participants.Count > 5) participant6.Text = participants[5];
220-
if (participants.Count > 6) participant7.Text = participants[6];
221-
if (participants.Count > 7) participant8.Text = participants[7];
222-
if (participants.Count > 8) participant9.Text = participants[8];
223-
if (participants.Count > 9) participant10.Text = participants[9];
208+
if (participants.Length > 0) participant1.Text = participants[0];
209+
if (participants.Length > 1) participant2.Text = participants[1];
210+
if (participants.Length > 2) participant3.Text = participants[2];
211+
if (participants.Length > 3) participant4.Text = participants[3];
212+
if (participants.Length > 4) participant5.Text = participants[4];
213+
if (participants.Length > 5) participant6.Text = participants[5];
214+
if (participants.Length > 6) participant7.Text = participants[6];
215+
if (participants.Length > 7) participant8.Text = participants[7];
216+
if (participants.Length > 8) participant9.Text = participants[8];
217+
if (participants.Length > 9) participant10.Text = participants[9];
224218
}
225219

226-
227220
private static readonly Random random = new();
228221

229222
public static void Shuffle<T>(IList<T> list)
@@ -243,7 +236,7 @@ private static void AddParticipantIfAvailable(string name, List<string> particip
243236
participants.Add(name);
244237
}
245238

246-
private List<string> GetParticipants()
239+
private string[] GetParticipants()
247240
{
248241
var participants = new List<string>();
249242
AddParticipantIfAvailable(participant1.Text, participants);
@@ -257,7 +250,7 @@ private List<string> GetParticipants()
257250
AddParticipantIfAvailable(participant9.Text, participants);
258251
AddParticipantIfAvailable(participant10.Text, participants);
259252

260-
return participants;
253+
return participants.ToArray();
261254
}
262255

263256
public async void LoadSessionConfiguration(object? sender, RoutedEventArgs args)
@@ -266,18 +259,19 @@ public async void LoadSessionConfiguration(object? sender, RoutedEventArgs args)
266259
{
267260
Title = "Open VPC Session",
268261
Filters = new List<FileDialogFilter>
269-
{
270-
new FileDialogFilter
271262
{
272-
Name = "VPC Session",
273-
Extensions = new List<string> { "vpcsession" }
263+
new()
264+
{
265+
Name = "VPC Session",
266+
Extensions = new List<string> { "vpcsession" }
267+
},
268+
new()
269+
{
270+
Name = "All Files",
271+
Extensions = new List<string> { "*" }
272+
}
273+
274274
},
275-
new FileDialogFilter
276-
{
277-
Name = "All Files",
278-
Extensions = new List<string> { "*" }
279-
}
280-
},
281275
AllowMultiple = false
282276
};
283277

@@ -287,13 +281,11 @@ public async void LoadSessionConfiguration(object? sender, RoutedEventArgs args)
287281
{
288282
LoadSessionIntoGui(result[0]);
289283
}
290-
291284
}
292285
public async void SaveSessionConfiguration(object? sender, RoutedEventArgs args)
293286
{
294287
var participants = GetParticipants();
295288

296-
297289
SaveFileDialog saveFileDialog = new();
298290
saveFileDialog.Filters?.Add(new FileDialogFilter { Name = "VPC Session", Extensions = { "vpcsession" } });
299291
saveFileDialog.InitialFileName = string.Join("_", participants);
@@ -313,25 +305,19 @@ public async void SaveSessionConfiguration(object? sender, RoutedEventArgs args)
313305
var messageBoxStandardWindow = MessageBox.Avalonia.MessageBoxManager.GetMessageBoxStandardWindow("Error Saving Config", "Error saving configuration File: " + ex.Message, MessageBox.Avalonia.Enums.ButtonEnum.Ok, MessageBox.Avalonia.Enums.Icon.Error, WindowStartupLocation.CenterScreen);
314306
messageBoxStandardWindow?.Show();
315307
}
316-
317-
} else
308+
}
309+
else
318310
{
319311
var messageBoxStandardWindow = MessageBox.Avalonia.MessageBoxManager.GetMessageBoxStandardWindow("Error Saving Config", "Could not save Configuration File ", MessageBox.Avalonia.Enums.ButtonEnum.Ok, MessageBox.Avalonia.Enums.Icon.Error, WindowStartupLocation.CenterScreen);
320312
messageBoxStandardWindow?.Show();
321313

322314
}
323315
}
324316

325-
public void SaveSessionConfiguration(string path, List<string> participants)
326-
{
327-
SessionConfigurationFileHandler.Save(path, new SessionConfiguration(participants, (int)minutesPerTurn.Value));
328-
}
329-
330317
public void NewSessionClick(object sender, RoutedEventArgs args)
331318
{
332-
var session = new SessionConfiguration(new List<string>(), 7);
319+
var session = new SessionConfiguration(Array.Empty<string>(), 7);
333320
LoadSessionIntoGui(session);
334321
}
335-
336322
}
337323
}

Source/VisualPairCoding/VisualPairCoding.AvaloniaUI/NewturnForm.axaml.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Avalonia;
21
using Avalonia.Controls;
32
using Avalonia.Input;
43
using Avalonia.Interactivity;
@@ -13,12 +12,11 @@ public partial class NewTurnForm : Window
1312
public NewTurnForm()
1413
{
1514
InitializeComponent();
16-
Topmost = true;
1715
}
1816

19-
[System.Obsolete]
2017
public NewTurnForm(string message, bool explicitChange)
2118
{
19+
Topmost = true;
2220
_explicitChange = explicitChange;
2321
InitializeComponent();
2422

@@ -39,7 +37,5 @@ private void CloseForm(object sender, RoutedEventArgs args)
3937
{
4038
Close();
4139
}
42-
43-
4440
}
4541
}

Source/VisualPairCoding/VisualPairCoding.AvaloniaUI/Program.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
using Avalonia;
22
using Avalonia.Controls;
3-
using Avalonia.Controls.ApplicationLifetimes;
43
using System;
5-
using System.Threading;
6-
using System.Threading.Tasks;
74
using VisualPairCoding.BL.AutoUpdates;
8-
using VisualPairCoding.Infrastructure;
95

106
namespace VisualPairCoding.AvaloniaUI
117
{
@@ -18,14 +14,14 @@ internal class Program
1814
public static void Main(string[] args)
1915
{
2016
BuildAvaloniaApp().Start(AppMain, args);
21-
2217
}
2318

2419
static void AppMain(Application app, string[] args)
2520
{
2621
app.Initialize();
2722

28-
var enterNamesForm = new EnterNamesForm();
23+
bool autostart = false;
24+
string configPath = string.Empty;
2925

3026
if (IsAutoUpdateShouldBeExecutedExplicitlySet(args))
3127
{
@@ -34,12 +30,11 @@ static void AppMain(Application app, string[] args)
3430

3531
if (IsStartupWithSessionFile(args))
3632
{
37-
var configPath = args[0];
38-
39-
enterNamesForm = new EnterNamesForm(true);
40-
enterNamesForm.LoadSessionIntoGui(configPath);
33+
autostart = true;
34+
configPath = args[0];
4135
}
4236

37+
var enterNamesForm = new EnterNamesForm(autostart, configPath);
4338
app.Run(enterNamesForm);
4439
}
4540

Source/VisualPairCoding/VisualPairCoding.AvaloniaUI/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"VisualPairCoding.AvaloniaUI": {
44
"commandName": "Project",
5-
"commandLineArgs": "AutoUpdate"
5+
"commandLineArgs": ""
66
}
77
}
88
}

Source/VisualPairCoding/VisualPairCoding.AvaloniaUI/RunSessionForm.axaml.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,6 @@ public partial class RunSessionForm : Window
2222
public RunSessionForm()
2323
{
2424
InitializeComponent();
25-
_pairCodingSession = new PairCodingSession(Array.Empty<string>(), 1);
26-
timer = new DispatcherTimer
27-
{
28-
Interval = TimeSpan.FromSeconds(1),
29-
IsEnabled = true
30-
};
31-
timer.Tick += Timer_Tick;
32-
ExtendClientAreaToDecorationsHint = true;
33-
ExtendClientAreaChromeHints = Avalonia.Platform.ExtendClientAreaChromeHints.NoChrome;
34-
ExtendClientAreaTitleBarHeightHint = -1;
35-
PointerPressed += OnPointerPressed;
36-
PointerMoved += OnPointerMoved;
37-
PointerReleased += OnPointerReleased;
38-
PointerLeave += OnPointerLeave;
3925
}
4026

4127
public RunSessionForm(PairCodingSession pairCodingSession)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace VisualPairCoding.Infrastructure;
22

33
public record SessionConfiguration(
4-
List<string> Participants,
4+
string[] Participants,
55
int SessionLength
66
);

Source/VisualPairCoding/VisualPairCoding.Infrastructure/SessionConfigurationFileHandler.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
namespace VisualPairCoding.Infrastructure
44
{
5-
public class SessionConfigurationFileHandler
5+
public static class SessionConfigurationFileHandler
66
{
7+
public static string GetFilenameProposal(string[] participants)
8+
{
9+
string configName = string.Join("_", participants);
10+
return configName + ".vpcsession";
11+
}
12+
713
public static SessionConfiguration Load(string filePath)
814
{
915
var configFile = File.ReadAllText(filePath);

0 commit comments

Comments
 (0)