Skip to content

Commit e2acd67

Browse files
committed
Done with database!
1 parent 263a755 commit e2acd67

10 files changed

Lines changed: 199 additions & 78 deletions

File tree

CutCode/Controllers/SearchBarControl.xaml.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,14 @@ public ICommand SearchCommand
200200

201201
private void TextChanged(object sender, TextChangedEventArgs e)
202202
{
203-
Text = searchBox.Text;
204-
exitBtn.Visibility = Visibility.Hidden;
205-
circularBar.Visibility = string.IsNullOrEmpty(searchBox.Text) ? Visibility.Hidden : Visibility.Visible;
206-
activityTimer.IsEnabled = true;
203+
if (searchBox.Text.Count() == 0) ClearClicked(null, null);
204+
else
205+
{
206+
Text = searchBox.Text;
207+
exitBtn.Visibility = Visibility.Hidden;
208+
circularBar.Visibility = string.IsNullOrEmpty(searchBox.Text) ? Visibility.Hidden : Visibility.Visible;
209+
activityTimer.IsEnabled = true;
210+
}
207211
}
208212

209213
private void ClearClicked(object sender, EventArgs e)

CutCode/DataBase/DataBaseManager.cs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ public bool DelCode(CodeBoxModel code)
135135
"Html", "Java", "Javascript", "Kotlin", "Php", "C", "Ruby", "Rust","Sql", "Swift"
136136
};
137137

138-
public ObservableCollection<CodeBoxModel> OrderCode(string order, ObservableCollection<CodeBoxModel> codes)
138+
public ObservableCollection<CodeBoxModel> OrderCode(string order)
139139
{
140140
int ind = AllOrderKind.IndexOf(order);
141141
ObservableCollection<CodeBoxModel> lst;
142142

143-
var currentCodes = codes;
143+
var currentCodes = AllCodes;
144144

145145
if (ind > 2)
146146
{
@@ -154,17 +154,10 @@ public ObservableCollection<CodeBoxModel> OrderCode(string order, ObservableColl
154154
{
155155
if (ind == 0) lst = new ObservableCollection<CodeBoxModel>(currentCodes.OrderBy(x => x.title).ToList());
156156
else if(ind == 1) lst = new ObservableCollection<CodeBoxModel>(currentCodes.OrderBy(x => x.timestamp).ToList());
157-
else
158-
{
159-
var dbcodes = _db.Query<CodeTable>("SELECT * From CodeTable");
160-
lst = new ObservableCollection<CodeBoxModel>();
161-
foreach(var c in dbcodes)
162-
{
163-
lst.Add(new CodeBoxModel(c.Id, c.title, c.desc, c.isFav, c.lang, c.code, c.timestamp, themeService));
164-
}
165-
}
157+
else lst = AllCodes;
166158
}
167159

160+
if (ind < 2) AllCodes = lst;
168161
return lst;
169162
}
170163

@@ -191,9 +184,40 @@ public bool FavModify(CodeBoxModel code)
191184
return true;
192185
}
193186

194-
public ObservableCollection<CodeBoxModel> SearchCode(string text, string from, ObservableCollection<CodeBoxModel> codes)
187+
public ObservableCollection<CodeBoxModel> SearchCode(string text, string from)
195188
{
196-
return new ObservableCollection<CodeBoxModel>();
189+
var currentCode = from == "Home" ? AllCodes : FavCodes;
190+
191+
var lst = new ObservableCollection<CodeBoxModel>();
192+
foreach(var code in currentCode)
193+
{
194+
bool add = false;
195+
196+
int f = 0;
197+
if (code.title.Length >= text.Length)
198+
{
199+
foreach (var i in code.title)
200+
{
201+
if (text.Contains(i))
202+
{
203+
text.Remove(text.IndexOf(i));
204+
f++;
205+
}
206+
}
207+
}
208+
else continue;
209+
210+
if (code.title == text) add = true;
211+
else if (code.title.Length < 3)
212+
{
213+
if (f >= 1) add = true;
214+
}
215+
else if (f >= 3) add = true;
216+
217+
if (add) lst.Add(code);
218+
}
219+
220+
return lst;
197221
}
198222
}
199223
}

CutCode/Interfaces/IDataBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public interface IDataBase
1616
CodeBoxModel AddCode(string title, string desc, string code, string langType);
1717
bool EditCode(CodeBoxModel code);
1818
bool DelCode(CodeBoxModel code);
19-
ObservableCollection<CodeBoxModel> OrderCode(string order, ObservableCollection<CodeBoxModel> codes);
19+
ObservableCollection<CodeBoxModel> OrderCode(string order);
2020
bool FavModify(CodeBoxModel code);
21-
ObservableCollection<CodeBoxModel> SearchCode(string text, string from, ObservableCollection<CodeBoxModel> codes);
21+
ObservableCollection<CodeBoxModel> SearchCode(string text, string from);
2222
}
2323
}

CutCode/Models/CodeBoxModel.cs

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,71 +9,37 @@
99

1010
namespace CutCode
1111
{
12-
public class CodeBoxModel : INotifyPropertyChanged
12+
public class CodeBoxModel : Screen
1313
{
14-
public event PropertyChangedEventHandler PropertyChanged;
15-
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
16-
{
17-
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
18-
}
19-
2014
public int id { get; set; }
21-
private string _title { get; set; }
15+
private string _title;
2216
public string title
2317
{
2418
get => _title;
25-
set
26-
{
27-
if(_title != value)
28-
{
29-
_title = value;
30-
NotifyPropertyChanged();
31-
}
32-
}
19+
set => SetAndNotify(ref _title, value);
3320
}
3421

35-
private string _desc { get; set; }
22+
private string _desc;
3623
public string desc
3724
{
3825
get => _desc;
39-
set
40-
{
41-
if (_desc != value)
42-
{
43-
_desc = value;
44-
NotifyPropertyChanged();
45-
}
46-
}
26+
set => SetAndNotify(ref _desc, value);
4727
}
4828

49-
private bool _isFav { get; set; }
29+
private bool _isFav;
5030
public bool isFav
5131
{
5232
get => _isFav;
53-
set
54-
{
55-
if (_isFav != value)
56-
{
57-
_isFav = value;
58-
NotifyPropertyChanged();
59-
}
60-
}
33+
set => SetAndNotify(ref _isFav, value);
6134
}
6235

6336
public string langType { get; set; }
6437

65-
private string _code { get; set; }
38+
private string _code;
6639
public string code
6740
{
6841
get => _code;
69-
set
70-
{
71-
if (_code != value)
72-
{
73-
_code = value;
74-
NotifyPropertyChanged();
75-
}
76-
}
42+
set => SetAndNotify(ref _code, value);
7743
}
7844
public long timestamp { get; set; }
7945
public string date { get; set; }

CutCode/ViewModels/AddViewModel.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,16 @@ public string leftText
104104
set => SetAndNotify(ref _leftText, value);
105105
}
106106

107+
private int ind;
108+
107109
private string _CurrentLang;
108110
public string CurrentLang
109111
{
110112
get => _CurrentLang;
111113
set
112114
{
113-
SetAndNotify(ref _CurrentLang, SyntaxLanguages[AllLangs.IndexOf(value)]);
115+
ind = AllLangs.IndexOf(value);
116+
SetAndNotify(ref _CurrentLang, SyntaxLanguages[ind]);
114117
}
115118
}
116119

@@ -133,7 +136,7 @@ public void SaveCommand()
133136
else
134137
{
135138
leftText = "";
136-
var codeModel = database.AddCode(title, desc, code, AllLangs[SyntaxLanguages.IndexOf(CurrentLang)]);
139+
var codeModel = database.AddCode(title, desc, code, AllLangs[ind]);
137140

138141
pageService.remoteChange = "Home";
139142
pageService.Page = new CodeViewModel(themeService, pageService, database, codeModel);

CutCode/ViewModels/FavViewModel.cs

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Text;
88
using System.Threading.Tasks;
9+
using System.Windows;
910
using System.Windows.Media;
1011

1112
namespace CutCode
@@ -27,10 +28,9 @@ public FavViewModel(IThemeService _themeService, IPageService _pageService, IDat
2728
database = _database;
2829
AllCodes = database.FavCodes;
2930
database.FavCodesUpdated += FavCodesUpdated;
30-
}
31-
32-
private void FavCodesUpdated(object sender, EventArgs e) => AllCodes = database.FavCodes;
3331

32+
VisChange();
33+
}
3434
private void ThemeChanged(object sender, EventArgs e)
3535
{
3636
SetAppearance();
@@ -43,7 +43,54 @@ private void SetAppearance()
4343
searchBarHoverColor = themeService.IsLightTheme ? ColorCon.Convert("#D0D1D2") : ColorCon.Convert("#373737");
4444
}
4545

46-
public ObservableCollection<CodeBoxModel> AllCodes { get; set; }
46+
private void FavCodesUpdated(object sender, EventArgs e)
47+
{
48+
AllCodes = database.FavCodes;
49+
VisChange();
50+
}
51+
52+
private void VisChange(string text = "You don't have any favourite notes :(")
53+
{
54+
if (AllCodes.Count == 0)
55+
{
56+
labelVis = Visibility.Visible;
57+
codesVis = Visibility.Hidden;
58+
emptyLabel = text;
59+
}
60+
else
61+
{
62+
labelVis = Visibility.Hidden;
63+
codesVis = Visibility.Visible;
64+
}
65+
}
66+
67+
68+
private Visibility _labelVis;
69+
public Visibility labelVis
70+
{
71+
get => _labelVis;
72+
set => SetAndNotify(ref _labelVis, value);
73+
}
74+
private Visibility _codesVis;
75+
public Visibility codesVis
76+
{
77+
get => _codesVis;
78+
set => SetAndNotify(ref _codesVis, value);
79+
}
80+
81+
private string _emptyLabel;
82+
public string emptyLabel
83+
{
84+
get => _emptyLabel;
85+
set => SetAndNotify(ref _emptyLabel, value);
86+
}
87+
88+
private ObservableCollection<CodeBoxModel> _AllCodes;
89+
public ObservableCollection<CodeBoxModel> AllCodes
90+
{
91+
get => _AllCodes;
92+
set => SetAndNotify(ref _AllCodes, value);
93+
}
4794

4895
private SolidColorBrush _searchBarBackground;
4996
public SolidColorBrush searchBarBackground
@@ -86,7 +133,16 @@ public bool IsSearched
86133
public void SearchCommand(string text)
87134
{
88135
IsSearched = false;
89-
AllCodes = database.SearchCode(text, "Fav", AllCodes);
136+
if (text == "")
137+
{
138+
AllCodes = database.FavCodes;
139+
VisChange();
140+
}
141+
else
142+
{
143+
AllCodes = database.SearchCode(text, "Home");
144+
if (AllCodes.Count == 0) VisChange("Not found :(");
145+
}
90146
IsSearched = true;
91147
}
92148

0 commit comments

Comments
 (0)