22using Avalonia . Interactivity ;
33using System ;
44using System . Collections . Generic ;
5- using System . IO ;
65using System . Threading . Tasks ;
76using VisualPairCoding . BL ;
87using 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}
0 commit comments