22using Avalonia . Interactivity ;
33using Avalonia . Markup . Xaml ;
44using HedgeModManager . CodeCompiler ;
5+ using HedgeModManager . Foundation ;
56using HedgeModManager . UI . ViewModels ;
67using HedgeModManager . UI . ViewModels . Codes ;
78using System . Collections . ObjectModel ;
@@ -22,49 +23,55 @@ public Codes()
2223 AvaloniaXamlLoader . Load ( this ) ;
2324 }
2425
25- //public void LogCodes(int level, CodeCategoryViewModel codeCategory)
26- //{
27- // if (codeCategory != null)
28- // {
29- // Logger.Debug($"{new string(' ', level * 4)}{codeCategory.Name}/");
30- // foreach (var category in codeCategory.Cateories)
31- // LogCodes(level + 1, category);
32- // foreach (var code in codeCategory.Codes)
33- // Logger.Debug($"{new string(' ', level * 4)}{code.Code.Name}");
34- // }
35- //}
36-
37- //public ObservableCollection<CodeCategoryViewModel> MergeCategories(ObservableCollection<CodeCategoryViewModel> root)
38- //{
39- // var categoryGroups = root.GroupBy(x => x.Name);
40- // var categories = new ObservableCollection<CodeCategoryViewModel>();
41- //
42- // // Merge codes
43- // foreach (var group in categoryGroups)
44- // {
45- // var category = group.First();
46- // categories.Add(category);
47- //
48- // if (group.Count() == 1)
49- // continue;
50- //
51- // // Move Codes
52- // group.Skip(1)
53- // .SelectMany(x => x.Codes)
54- // .ToList()
55- // .ForEach(category.Codes.Add);
56- //
57- // // Move sub categories
58- // group.Skip(1)
59- // .SelectMany(x => x.Cateories)
60- // .ToList()
61- // .ForEach(category.Cateories.Add);
62- //
63- // //Merge sub categories
64- // category.Cateories = MergeCategories(category.Cateories);
65- // }
66- // return categories;
67- //}
26+ public CodeCategoryViewModel CreateCategory ( string path )
27+ {
28+ var split = path . Split ( '/' ) ;
29+ CodeCategoryViewModel ? currentCategory = null ;
30+ if ( ! ( split . Length == 1 && string . IsNullOrEmpty ( split [ 0 ] ) ) )
31+ {
32+ for ( int i = 0 ; i < split . Length ; ++ i )
33+ {
34+ if ( currentCategory == null )
35+ {
36+ currentCategory = Cateories . FirstOrDefault ( x => x . Name == split [ i ] ) ;
37+ if ( currentCategory == null )
38+ {
39+ currentCategory = new ( null , split [ i ] ) ;
40+ Cateories . Add ( currentCategory ) ;
41+ }
42+ }
43+ else
44+ {
45+ var nextCategory = currentCategory . Categories . FirstOrDefault ( x => x . Name == split [ i ] ) ;
46+ if ( nextCategory == null )
47+ {
48+ nextCategory = new ( currentCategory , split [ i ] ) ;
49+ currentCategory . Categories . Add ( nextCategory ) ;
50+ }
51+ currentCategory = nextCategory ;
52+ }
53+ }
54+ }
55+ if ( currentCategory == null )
56+ {
57+ currentCategory = new ( null , "Uncategorized" ) ;
58+ Cateories . Add ( currentCategory ) ;
59+ }
60+
61+ return currentCategory ;
62+ }
63+
64+ public void LogCodes ( int level , CodeCategoryViewModel codeCategory )
65+ {
66+ if ( codeCategory != null )
67+ {
68+ Logger . Debug ( $ "{ new string ( ' ' , level * 2 ) } { codeCategory . Name } /") ;
69+ foreach ( var category in codeCategory . Categories )
70+ LogCodes ( level + 1 , category ) ;
71+ foreach ( var code in codeCategory . Codes )
72+ Logger . Debug ( $ "{ new string ( ' ' , ( level + 1 ) * 2 ) } { code . Code . Name } ") ;
73+ }
74+ }
6875
6976 public void RefreshUI ( )
7077 {
@@ -75,7 +82,7 @@ public void RefreshUI()
7582 return ;
7683
7784 MainViewModel . Codes
78- . Where ( x => x . Type != Foundation . CodeType . Library && x is CSharpCode )
85+ . Where ( x => x . Type != CodeType . Library && x is CSharpCode )
7986 . Cast < CSharpCode > ( )
8087 . Select ( x => new CodeEntryViewModel ( x ) )
8188 . ToList ( )
@@ -94,41 +101,29 @@ private void OnLoaded(object? sender, RoutedEventArgs e)
94101 MainViewModel . PropertyChanged += OnMainViewModelPropertyChanged ;
95102 RefreshUI ( ) ;
96103
97- //// Generate categories
98- //Cateories.Clear();
99- //MainViewModel.Codes
100- // .Select(x => x as CSharpCode)
101- // .Where(x => x != null)
102- // .DistinctBy(x => x!.Category)
103- // .Select(x => new CodeCategoryViewModel(null, x!.Category))
104- // .ToList()
105- // .ForEach(Cateories.Add);
106- //
107- //// Merge categories
108- //var cateories = MergeCategories(Cateories);
109- //
110- //// Test log
111- //foreach (var category in cateories)
112- // LogCodes(0, category);
104+ // Generate categories
105+ Cateories . Clear ( ) ;
106+ foreach ( var code in CodesList )
107+ {
108+ var category = CreateCategory ( code . Code . Category ) ;
109+ category . Codes . Add ( code ) ;
110+ }
111+
112+ // Test log
113+ Logger . Debug ( "Codes:" ) ;
114+ foreach ( var category in Cateories )
115+ LogCodes ( 1 , category ) ;
113116
114117 // Add buttons
115118 if ( MainViewModel . CurrentTabInfo != null )
116119 {
117120 MainViewModel . CurrentTabInfo . Buttons . Clear ( ) ;
118- //MainViewModel.CurrentTabInfo.Buttons.Add(new("Common.Button.SavePlay", Buttons.Y, async (b) =>
119- //{
120- // await MainViewModel.SaveAndRun();
121- //}));
122121 MainViewModel . CurrentTabInfo . Buttons . Add ( new ( "Codes.Button.UpdateCodes" , ButtonsOLD . X , async ( b ) =>
123122 {
124123 b . IsEnabled = false ;
125124 await MainViewModel . UpdateCodesAsync ( true , false ) ;
126125 b . IsEnabled = true ;
127126 } ) ) ;
128- //MainViewModel.CurrentTabInfo.Buttons.Add(new("Common.Button.Select", Buttons.A, (b) =>
129- //{
130- // Logger.Information("Select Pressed");
131- //}));
132127 }
133128 }
134129
0 commit comments