Skip to content

Commit 47b3e85

Browse files
committed
Pre-select category when adding new snippet.
Improves the "Add Snippet" workflow by automatically selecting the currently active category from the TreeView.
1 parent 8659015 commit 47b3e85

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

src/CodeSnip/MainViewModel.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public partial class MainViewModel : ObservableObject
4545
[ObservableProperty]
4646
private Snippet? _selectedSnippet;
4747

48+
[ObservableProperty]
49+
private Category? _selectedCategory;
50+
4851
[ObservableProperty]
4952
private string _statusMessage = "Ready";
5053

@@ -619,7 +622,7 @@ await DialogService.Instance.ShowMessageAsync("Cannot Add Snippet",
619622
return;
620623
}
621624

622-
var vm = new SnippetViewModel(false, new Snippet(), [.. Languages], _databaseService);
625+
var vm = new SnippetViewModel(false, new Snippet(), [.. Languages], _databaseService, SelectedCategory);
623626

624627
_flyoutService.ShowFlyout("flySnippet", vm, " Add new snippet");
625628
}
@@ -953,6 +956,7 @@ public async Task ChangeSelectedSnippetAsync(Snippet? newSnippet)
953956

954957
SelectedSnippet = newSnippet;
955958
EditingSnippet = newSnippet;
959+
SelectedCategory = newSnippet.Category;
956960

957961
_isInternalTextUpdate = true;
958962
EditorText = newSnippet.Code ?? string.Empty;

src/CodeSnip/MainWindow.xaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using CodeSnip.Services;
33
using CodeSnip.Services.Exporters;
44
using CodeSnip.Views.HighlightingEditorView;
5+
using CodeSnip.Views.LanguageCategoryView;
56
using CodeSnip.Views.SnippetView;
67
using ControlzEx.Theming;
78
using ICSharpCode.AvalonEdit;
@@ -257,6 +258,10 @@ private async void TreeView_SelectedItemChanged(object sender, RoutedPropertyCha
257258

258259
textEditor.Document.UndoStack.ClearAll();
259260
}
261+
else if (e.NewValue is Category category)
262+
{
263+
mainViewModel.SelectedCategory = category;
264+
}
260265
}
261266

262267
private async void FormatDfmt_Click(object sender, RoutedEventArgs e)

src/CodeSnip/Views/SnippetView/SnippetView.xaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@
4949
<Grid Grid.Row="1">
5050
<Grid.RowDefinitions>
5151
<RowDefinition Height="Auto"/>
52-
<!-- Red za Title -->
52+
<!-- Title -->
5353
<RowDefinition Height="Auto"/>
5454
<!-- txtTitle -->
5555
<RowDefinition Height="Auto"/>
56-
<!-- Red za Description -->
56+
<!-- Description -->
5757
<RowDefinition Height="*"/>
5858
<!-- txtDescription -->
5959
<RowDefinition Height="Auto"/>
60-
<!-- Red za Tag -->
60+
<!-- Tag -->
6161
<RowDefinition Height="Auto"/>
6262
<!-- txtTag -->
6363
</Grid.RowDefinitions>
@@ -77,7 +77,7 @@
7777
</Grid>
7878
<!-- Blok 3 -->
7979
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,20,0,0">
80-
<Button x:Name="btnOk" Content="OK" Width="60" Command="{Binding SaveCommand}"/>
80+
<Button x:Name="btnOk" Content="Save" Width="60" Command="{Binding SaveCommand}" IsEnabled="{Binding SaveCommand.CanExecute}"/>
8181
<Button Content="Cancel" Margin="20,0,0,0" Width="60" Command="{Binding CancelCommand}"/>
8282
</StackPanel>
8383
</Grid>

src/CodeSnip/Views/SnippetView/SnippetViewModel.cs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ public partial class SnippetViewModel : ObservableObject, IDataErrorInfo
1515
[ObservableProperty]
1616
private ObservableCollection<Language> _languages = [];
1717

18-
[ObservableProperty]
19-
private ObservableCollection<Category> _categories = [];
20-
2118
[ObservableProperty]
2219
private ObservableCollection<Category> _availableCategories = [];
2320

@@ -36,6 +33,7 @@ public partial class SnippetViewModel : ObservableObject, IDataErrorInfo
3633
private string? _selectedLanguageName;
3734

3835
[ObservableProperty]
36+
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
3937
private string? _title = string.Empty;
4038

4139
public string Error => "";
@@ -44,10 +42,9 @@ public string this[string _property]
4442
{
4543
get
4644
{
47-
if (_property == nameof(Title))
45+
if (_property == nameof(Title) && string.IsNullOrWhiteSpace(Title))
4846
{
49-
if (string.IsNullOrWhiteSpace(Title))
50-
return "Title cannot be empty";
47+
return "Title cannot be empty";
5148
}
5249
return "";
5350
}
@@ -57,7 +54,8 @@ public SnippetViewModel(
5754
bool isEditMode,
5855
Snippet? snippet,
5956
List<Language>? languages,
60-
DatabaseService? databaseService)
57+
DatabaseService? databaseService,
58+
Category? preselectedCategory = null)
6159
{
6260
ArgumentNullException.ThrowIfNull(databaseService);
6361

@@ -67,12 +65,19 @@ public SnippetViewModel(
6765
Languages = new ObservableCollection<Language>(languages ?? new List<Language>());
6866
Snippet = snippet;
6967

68+
if (preselectedCategory != null)
69+
{
70+
SelectedCategory = preselectedCategory;
71+
}
72+
7073
InitializeSelections();
7174
}
7275

7376
public bool CanSave()
7477
{
75-
return string.IsNullOrWhiteSpace(this[nameof(Title)]);
78+
// The command can be executed if there are no validation errors for the Title
79+
// and a category is selected.
80+
return string.IsNullOrEmpty(this[nameof(Title)]) && SelectedCategory != null;
7681
}
7782

7883
private void InitializeSelections()
@@ -86,9 +91,17 @@ private void InitializeSelections()
8691
}
8792
else
8893
{
89-
SelectedLanguage = Languages.FirstOrDefault();
90-
AvailableCategories = new ObservableCollection<Category>(SelectedLanguage?.Categories ?? []);
91-
SelectedCategory = AvailableCategories.FirstOrDefault();
94+
if (SelectedCategory != null) // A category was pre-selected
95+
{
96+
SelectedLanguage = SelectedCategory.Language;
97+
AvailableCategories = new ObservableCollection<Category>(SelectedLanguage?.Categories ?? []);
98+
}
99+
else // No pre-selection, start from scratch
100+
{
101+
SelectedLanguage = Languages.FirstOrDefault();
102+
AvailableCategories = new ObservableCollection<Category>(SelectedLanguage?.Categories ?? []);
103+
SelectedCategory = AvailableCategories.FirstOrDefault();
104+
}
92105
}
93106
}
94107

@@ -97,7 +110,7 @@ partial void OnSelectedLanguageChanged(Language? value)
97110
AvailableCategories = new ObservableCollection<Category>(value?.Categories ?? new ObservableCollection<Category>());
98111
SelectedLanguageName = value?.Name;
99112

100-
if (!IsEditMode)
113+
if (!IsEditMode && SelectedCategory == null)
101114
SelectedCategory = AvailableCategories.FirstOrDefault();
102115
}
103116

@@ -110,14 +123,11 @@ partial void OnSelectedCategoryChanged(Category? value)
110123
}
111124
}
112125

113-
[RelayCommand]
126+
[RelayCommand(CanExecute = nameof(CanSave))]
114127
private void Save()
115128
{
116129
try
117130
{
118-
if (!CanSave())
119-
return;
120-
121131
if (SelectedLanguage != null && SelectedCategory != null && Snippet != null)
122132
{
123133
Snippet.Title = Title ?? string.Empty;

0 commit comments

Comments
 (0)