@@ -13,6 +13,7 @@ namespace CutCode
1313 public class DataBaseManager : IDataBase
1414 {
1515 public ObservableCollection < CodeBoxModel > AllCodes { get ; set ; }
16+ public ObservableCollection < CodeBoxModel > FavCodes { get ; set ; }
1617 private SQLiteConnection _db ;
1718 private readonly IThemeService themeService ;
1819 public DataBaseManager ( IThemeService _themeService )
@@ -30,6 +31,7 @@ public DataBaseManager(IThemeService _themeService)
3031 {
3132 AllCodes . Add ( new CodeBoxModel ( c . Id , c . title , c . desc , c . isFav , c . lang , c . code , c . timestamp , themeService ) ) ;
3233 }
34+ PropertyChanged ( ) ;
3335 }
3436
3537 private int GetIndex ( CodeBoxModel code )
@@ -43,9 +45,20 @@ private int GetIndex(CodeBoxModel code)
4345 return ind ;
4446 }
4547
46- public event EventHandler PropertyChanged ;
47- public void AllCodesPropertyChanged ( ) => PropertyChanged ? . Invoke ( this , EventArgs . Empty ) ;
48-
48+ public event EventHandler AllCodesUpdated ;
49+ public event EventHandler FavCodesUpdated ;
50+ public void PropertyChanged ( )
51+ {
52+ var lst = new ObservableCollection < CodeBoxModel > ( ) ;
53+ foreach ( var code in AllCodes )
54+ {
55+ if ( code . isFav ) lst . Add ( code ) ;
56+ }
57+ FavCodes = lst ;
58+
59+ AllCodesUpdated . Invoke ( AllCodes , EventArgs . Empty ) ;
60+ FavCodesUpdated . Invoke ( FavCodes , EventArgs . Empty ) ;
61+ }
4962
5063 public CodeBoxModel AddCode ( string title , string desc , string code , string langType )
5164 {
@@ -65,7 +78,7 @@ public CodeBoxModel AddCode(string title, string desc, string code, string langT
6578 int id = ( int ) SQLite3 . LastInsertRowid ( _db . Handle ) ;
6679 var codeModel = new CodeBoxModel ( id , title , desc , false , langType , code , time , themeService ) ;
6780 AllCodes . Add ( codeModel ) ;
68- AllCodesPropertyChanged ( ) ;
81+ PropertyChanged ( ) ;
6982
7083 return codeModel ;
7184 }
@@ -85,7 +98,7 @@ public bool EditCode(CodeBoxModel code)
8598 _db . Update ( dbCode ) ;
8699 } ) ;
87100 AllCodes [ GetIndex ( code ) ] = code ;
88- AllCodesPropertyChanged ( ) ;
101+ PropertyChanged ( ) ;
89102 }
90103 }
91104 catch
@@ -101,7 +114,7 @@ public bool DelCode(CodeBoxModel code)
101114 {
102115 _db . Delete < CodeTable > ( code . id ) ;
103116 AllCodes . Remove ( code ) ;
104- AllCodesPropertyChanged ( ) ;
117+ PropertyChanged ( ) ;
105118 }
106119 catch
107120 {
@@ -115,32 +128,35 @@ public bool DelCode(CodeBoxModel code)
115128 "Alphabet" , "Date" , "All languages" , "Python" , "C++" , "C#" , "CSS" , "Dart" , "Golang" ,
116129 "Html" , "Java" , "Javascript" , "Kotlin" , "Php" , "C" , "Ruby" , "Rust" , "Sql" , "Swift"
117130 } ;
118- public void OrderCode ( string order )
131+
132+ public ObservableCollection < CodeBoxModel > OrderCode ( string order , ObservableCollection < CodeBoxModel > codes )
119133 {
120134 int ind = AllOrderKind . IndexOf ( order ) ;
121135 ObservableCollection < CodeBoxModel > lst ;
122136
137+ var currentCodes = codes ;
138+
123139 if ( ind > 2 )
124140 {
125141 lst = new ObservableCollection < CodeBoxModel > ( ) ;
126- foreach ( var code in AllCodes ) if ( code . langType == order ) lst . Add ( code ) ;
142+ foreach ( var code in currentCodes ) if ( code . langType == order ) lst . Add ( code ) ;
127143 }
128144 else
129145 {
130- if ( ind == 0 ) lst = new ObservableCollection < CodeBoxModel > ( AllCodes . OrderBy ( x => x . title ) . ToList ( ) ) ;
131- else if ( ind == 1 ) lst = new ObservableCollection < CodeBoxModel > ( AllCodes . OrderBy ( x => x . timestamp ) . ToList ( ) ) ;
146+ if ( ind == 0 ) lst = new ObservableCollection < CodeBoxModel > ( currentCodes . OrderBy ( x => x . title ) . ToList ( ) ) ;
147+ else if ( ind == 1 ) lst = new ObservableCollection < CodeBoxModel > ( currentCodes . OrderBy ( x => x . timestamp ) . ToList ( ) ) ;
132148 else
133149 {
134- var codes = _db . Query < CodeTable > ( "SELECT * From CodeTable" ) ;
150+ var dbcodes = _db . Query < CodeTable > ( "SELECT * From CodeTable" ) ;
135151 lst = new ObservableCollection < CodeBoxModel > ( ) ;
136- foreach ( var c in codes )
152+ foreach ( var c in dbcodes )
137153 {
138154 lst . Add ( new CodeBoxModel ( c . Id , c . title , c . desc , c . isFav , c . lang , c . code , c . timestamp , themeService ) ) ;
139155 }
140156 }
141157 }
142- AllCodes = lst ;
143- AllCodesPropertyChanged ( ) ;
158+
159+ return lst ;
144160 }
145161
146162 public bool FavModify ( CodeBoxModel code )
@@ -156,7 +172,7 @@ public bool FavModify(CodeBoxModel code)
156172 _db . Update ( dbCode ) ;
157173 } ) ;
158174 AllCodes [ GetIndex ( code ) ] = code ;
159- AllCodesPropertyChanged ( ) ;
175+ PropertyChanged ( ) ;
160176 }
161177 }
162178 catch
@@ -166,9 +182,9 @@ public bool FavModify(CodeBoxModel code)
166182 return true ;
167183 }
168184
169- public void SearchCode ( string text )
185+ public ObservableCollection < CodeBoxModel > SearchCode ( string text , string from , ObservableCollection < CodeBoxModel > codes )
170186 {
171-
187+ return codes ;
172188 }
173189 }
174190}
0 commit comments