Skip to content

Commit 7989d5e

Browse files
committed
Added Command to codebox controller
1 parent e43db5c commit 7989d5e

8 files changed

Lines changed: 62 additions & 25 deletions

File tree

CutCode/Controllers/CodeBoxControl.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
</UserControl.Resources>
3131

32-
<Button x:Name="card" Style="{DynamicResource ButtonStyle}" BorderThickness="0" Height="110" Margin="0,0,15,0">
32+
<Button x:Name="card" Style="{DynamicResource ButtonStyle}" BorderThickness="0" Height="110" Margin="0,0,15,0" Click="ButtonClicked">
3333
<Grid>
3434
<Grid.ColumnDefinitions>
3535
<ColumnDefinition Width="Auto"/>

CutCode/Controllers/CodeBoxControl.xaml.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public CodeBoxControl()
3636
InitializeComponent();
3737
}
3838

39+
public void ButtonClicked(object sender, EventArgs e) => Command?.Execute(CommandParameter);
40+
3941
#region ThemeService property
4042
public static readonly DependencyProperty ThemeServiceProperty =
4143
DependencyProperty.Register("ThemeService", typeof(IThemeService), typeof(CodeBoxControl),
@@ -172,5 +174,20 @@ public ICommand Command
172174
set => SetValue(CommandProperty, value);
173175
}
174176
#endregion
177+
178+
#region CommandParameter property
179+
public static readonly DependencyProperty CommandParameterProperty =
180+
DependencyProperty.Register("CommandParameter", typeof(CodeBoxModel), typeof(CodeBoxControl),
181+
new FrameworkPropertyMetadata(null));
182+
183+
public CodeBoxModel CommandParameter
184+
{
185+
get => (CodeBoxModel)GetValue(CommandParameterProperty);
186+
set
187+
{
188+
SetValue(CommandParameterProperty, value);
189+
}
190+
}
191+
#endregion
175192
}
176193
}

CutCode/DataBase/DBManager.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ namespace CutCode
99
public class DBManager
1010
{
1111
public List<CodeBoxModel> AllCodes { get; set; }
12-
public DBManager()
12+
private readonly IThemeService themeService;
13+
public DBManager(IThemeService _themeService)
1314
{
14-
15+
themeService = _themeService;
1516
}
1617

17-
public static CodeBoxModel AddCode(string _title, string _desc, string _lang, string _code)
18+
public CodeBoxModel AddCode(string _title, string _desc, string _lang, string _code)
1819
{
19-
var _id = 0; // will be set later ...
20-
return new CodeBoxModel(_title, _desc, false, _lang);
20+
///var _id = 0; // will be set later ...
21+
return new CodeBoxModel(2, "C++ binary search", "blah blah binary blah ... ye and ok \n so what??", false, "cpp", "print('Hello world')", 1628136352, themeService);
2122
}
2223

23-
public static bool DelCode(CodeBoxModel code)
24+
public bool DelCode(CodeBoxModel code)
2425
{
2526
try
2627
{
@@ -33,12 +34,12 @@ public static bool DelCode(CodeBoxModel code)
3334
return true;
3435
}
3536

36-
public static List<CodeBoxModel> SearchCode(string searchText)
37+
public List<CodeBoxModel> SearchCode(string searchText)
3738
{
3839
return new List<CodeBoxModel>();
3940
}
4041

41-
public static bool EditCode(CodeBoxModel newCode)
42+
public bool EditCode(CodeBoxModel newCode)
4243
{
4344
try
4445
{

CutCode/Models/CodeBoxModel.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ namespace CutCode
99
{
1010
public class CodeBoxModel
1111
{
12-
public CodeBoxModel(string __title, string __desc, bool __isFav, string __langType)
12+
public CodeBoxModel(int _id, string _title, string _desc, bool _isFav, string _langType, string _code, long _created, IThemeService _themeService)
1313
{
14-
title = __title;
15-
desc = __desc;
16-
isFav = __isFav;
17-
langType = __langType;
14+
id = _id;
15+
title = _title;
16+
desc = _desc;
17+
isFav = _isFav;
18+
langType = _langType;
19+
code = _code;
20+
// do something with the time long here ...
21+
dateCreated = "12:01 AM";
22+
23+
themeService = _themeService;
1824
// there are something to be changed in the initializer ...
1925
}
2026
public int id { get; set; }
@@ -24,5 +30,7 @@ public CodeBoxModel(string __title, string __desc, bool __isFav, string __langTy
2430
public string langType { get; set; }
2531
public string code { get; set; }
2632
public long created { get; set; }
33+
public string dateCreated { get; set; }
34+
public IThemeService themeService { get; set; }
2735
}
2836
}

CutCode/ViewModels/FavViewModel.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public FavViewModel(IThemeService _themeService)
2222
IsSearched = false;
2323

2424
AllCodes = new ObservableCollection<CodeBoxModel>();
25-
var code1 = new CodeBoxModel("Pyqt5 scroll bar", "pyqt5 custom scroll bar made in python ok??", true, "python", themeService);
26-
var code2 = new CodeBoxModel("C++ binary search", "blah blah binary blah ... ye and ok \n so what??", false, "cpp", themeService);
27-
var code3 = new CodeBoxModel("wpf sample combo box", "combo box style that is responseive to the ui and also \n and ok ?? blah ... ", true, "csharp", themeService);
25+
var code1 = new CodeBoxModel(1, "Pyqt5 scroll bar", "pyqt5 custom scroll bar made in python ok??", true, "python", "print('Hello world')", 1628136352, themeService);
26+
var code2 = new CodeBoxModel(2, "C++ binary search", "blah blah binary blah ... ye and ok \n so what??", false, "cpp", "print('Hello world')", 1628136352, themeService);
27+
var code3 = new CodeBoxModel(3, "wpf sample combo box", "combo box style that is responseive to the ui and also \n and ok ?? blah ... ", true, "csharp", "print('Hello world')", 1628136352, themeService);
2828
AllCodes.Add(code1);
2929
AllCodes.Add(code2);
3030
AllCodes.Add(code3);
@@ -91,5 +91,11 @@ public async void SearchCommand(string text)
9191
await Task.Delay(TimeSpan.FromSeconds(1)); // this will be changed to the search process
9292
IsSearched = true;
9393
}
94+
95+
public void CodeSelectCommand(CodeBoxModel code)
96+
{
97+
Trace.WriteLine("Item selected");
98+
Trace.WriteLine(code.title);
99+
}
94100
}
95101
}

CutCode/ViewModels/HomeViewModel.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@ public HomeViewModel(IThemeService _themeService, IPageService _pageService)
2424
SetAppearance();
2525
IsSearched = false;
2626

27-
var db = new DBManager();
28-
//AllCodes = db.AllCodes;
29-
3027
AllCodes = new ObservableCollection<CodeBoxModel>();
31-
var code1 = new CodeBoxModel("Pyqt5 scroll bar", "pyqt5 custom scroll bar made in python ok??", true, "python");
32-
var code2 = new CodeBoxModel("C++ binary search", "blah blah binary blah ... ye and ok \n so what??", false, "cpp");
33-
var code3 = new CodeBoxModel("wpf sample combo box", "combo box style that is responseive to the ui and also \n and ok ?? blah ... ", true, "csharp");
28+
var code1 = new CodeBoxModel(1, "Pyqt5 scroll bar", "pyqt5 custom scroll bar made in python ok??", true, "python", "print('Hello world')", 1628136352, themeService);
29+
var code2 = new CodeBoxModel(2, "C++ binary search", "blah blah binary blah ... ye and ok \n so what??", false, "cpp", "print('Hello world')", 1628136352, themeService);
30+
var code3 = new CodeBoxModel(3, "wpf sample combo box", "combo box style that is responseive to the ui and also \n and ok ?? blah ... ", true, "csharp", "print('Hello world')", 1628136352, themeService);
3431
AllCodes.Add(code1);
3532
AllCodes.Add(code2);
3633
AllCodes.Add(code3);
@@ -123,5 +120,11 @@ public async void SearchCommand(string text)
123120
await Task.Delay(TimeSpan.FromSeconds(1)); // this will be changed to the search process
124121
IsSearched = true;
125122
}
123+
124+
public void CodeSelectCommand(CodeBoxModel code)
125+
{
126+
Trace.WriteLine("Item selected");
127+
Trace.WriteLine(code.title);
128+
}
126129
}
127130
}

CutCode/Views/FavView.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
<ItemsControl.ItemTemplate>
3838
<DataTemplate>
3939
<local:CodeBoxControl Margin="5,5,5,5" Title="{Binding title}" Desc="{Binding desc}"
40-
IsFavourite="{Binding isFav}" LangType="{Binding langType}" ThemeService="{Binding themeService}"/>
40+
Command="{s:Action CodeSelectCommand}" CommandParameter="{Binding .}"
41+
IsFavourite="{Binding isFav}" LangType="{Binding langType}" ThemeService="{Binding themeService}"/>
4142
</DataTemplate>
4243
</ItemsControl.ItemTemplate>
4344
</ItemsControl>

CutCode/Views/HomeView.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@
9191
<ItemsControl.ItemTemplate>
9292
<DataTemplate>
9393
<local:CodeBoxControl Margin="5,5,5,5" Title="{Binding title}" Desc="{Binding desc}"
94-
IsFavourite="{Binding isFav}" LangType="{Binding langType}" ThemeService="{Binding themeService}"/>
94+
Command="{s:Action CodeSelectCommand}" CommandParameter="{Binding .}"
95+
IsFavourite="{Binding isFav}" LangType="{Binding langType}" ThemeService="{Binding themeService}"/>
9596
</DataTemplate>
9697
</ItemsControl.ItemTemplate>
9798
</ItemsControl>

0 commit comments

Comments
 (0)