Skip to content

Commit c1f1e78

Browse files
committed
Added Recent session functionality
1 parent 329d35c commit c1f1e78

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

Source/VisualPairCoding/VisualPairCoding.AvaloniaUI/EnterNamesWindow.axaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<Separator/>
1818
<MenuItem Click="LoadSessionConfiguration" Header="_Load"/>
1919
<MenuItem Click="SaveSessionConfiguration" Header="_Save"/>
20+
<MenuItem Click="OnMenuItemClicked" Header="Recent" x:Name="recentMenuItem">
21+
<MenuItem Header="Replace" />
22+
</MenuItem>
2023
<Separator/>
2124
<MenuItem Click="CloseWindow" Header="_Exit"/>
2225
</MenuItem>

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Avalonia.Interactivity;
33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Threading.Tasks;
67
using VisualPairCoding.BL;
78
using VisualPairCoding.BL.AutoUpdates;
@@ -12,12 +13,12 @@ namespace VisualPairCoding.AvaloniaUI
1213
public partial class EnterNamesForm : Window
1314
{
1415
private bool _autostart = false;
15-
1616
public EnterNamesForm()
1717
{
1818
InitializeComponent();
1919
WindowStartupLocation = WindowStartupLocation.CenterScreen;
2020
Opened += OnActivated;
21+
Closed += OnClosed;
2122
}
2223

2324
public EnterNamesForm(bool autostart)
@@ -26,6 +27,12 @@ public EnterNamesForm(bool autostart)
2627
InitializeComponent();
2728
WindowStartupLocation = WindowStartupLocation.CenterScreen;
2829
Opened += OnActivated;
30+
Closed += OnClosed;
31+
}
32+
33+
private MenuItem GetRecentMenuItem()
34+
{
35+
return this.FindControl<MenuItem>("recentMenuItem");
2936
}
3037

3138
private async Task<bool> AutoUpdate()
@@ -52,8 +59,40 @@ private async Task<bool> AutoUpdate()
5259
return false;
5360
}
5461

62+
private void OnClosed(object? sender, EventArgs e)
63+
{
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+
}
72+
}
73+
74+
private void OnMenuItemClicked(object? sender, RoutedEventArgs e)
75+
{
76+
MenuItem clickedMenuItem = (MenuItem)e.Source!;
77+
string subMenuHeader = clickedMenuItem.Header.ToString()!;
78+
var configPath = Path.Combine(SessionConfigurationFolderHandler.GetSessionFolderPath(), subMenuHeader + ".vpcsession");
79+
LoadSessionIntoGui(configPath);
80+
}
5581
private async void OnActivated(object? sender, EventArgs e)
5682
{
83+
MenuItem menuItem = GetRecentMenuItem();
84+
85+
86+
if (!SessionConfigurationFolderHandler.CheckIfConfigurationFolderExistsUnderAppDataFolder())
87+
{
88+
SessionConfigurationFolderHandler.CreateConfigurationFolderUnderAppDataFolder();
89+
}
90+
91+
var configs = SessionConfigurationFolderHandler.GetConfigurationFiles();
92+
93+
menuItem.Items = configs;
94+
menuItem.SelectedIndex = 0;
95+
5796
//Only perform auto - updates if not in dev environment
5897
if (AutoUpdateDetector.isUpdateAvailable())
5998
{
@@ -283,6 +322,11 @@ public async void SaveSessionConfiguration(object? sender, RoutedEventArgs args)
283322
}
284323
}
285324

325+
public void SaveSessionConfiguration(string path, List<string> participants)
326+
{
327+
SessionConfigurationFileHandler.Save(path, new SessionConfiguration(participants, (int)minutesPerTurn.Value));
328+
}
329+
286330
public void NewSessionClick(object sender, RoutedEventArgs args)
287331
{
288332
var session = new SessionConfiguration(new List<string>(), 7);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using VisualPairCoding.Infrastructure;
2+
3+
public static class SessionConfigurationFolderHandler
4+
{
5+
public static string GetEnvironnementAppDataFolder()
6+
{
7+
string appDataFolderPath;
8+
9+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
10+
{
11+
appDataFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
12+
}
13+
else if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
14+
{
15+
appDataFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
16+
}
17+
else
18+
{
19+
appDataFolderPath = "";
20+
}
21+
22+
return appDataFolderPath!;
23+
}
24+
25+
public static bool CheckIfConfigurationFolderExistsUnderAppDataFolder()
26+
{
27+
var folderPath = GetSessionFolderPath();
28+
29+
if (!Directory.Exists(folderPath))
30+
{
31+
return false;
32+
}
33+
34+
return true;
35+
}
36+
37+
public static void CreateConfigurationFolderUnderAppDataFolder()
38+
{
39+
var folderPath = GetSessionFolderPath();
40+
Directory.CreateDirectory(folderPath);
41+
42+
return;
43+
}
44+
45+
public static string GetSessionFolderPath()
46+
{
47+
var appdataDir = GetEnvironnementAppDataFolder();
48+
var folderPath = Path.Combine(appdataDir, "VisualPairCoding");
49+
50+
return folderPath;
51+
}
52+
53+
public static List<string> GetConfigurationFiles()
54+
{
55+
var configDir = GetSessionFolderPath();
56+
List<string> configs = new();
57+
58+
string[] fileNames = Directory.GetFiles(configDir);
59+
60+
foreach (string fileName in fileNames)
61+
{
62+
configs.Add(Path.GetFileName(fileName.Replace(".vpcsession", "")));
63+
}
64+
65+
return configs;
66+
}
67+
}

0 commit comments

Comments
 (0)