77using System . Linq ;
88using System . Text ;
99using System . Threading . Tasks ;
10+ using Newtonsoft . Json ;
11+ using System . IO ;
1012
1113namespace CutCode
1214{
@@ -16,6 +18,8 @@ public class DataBaseManager : IDataBase
1618 public ObservableCollection < CodeBoxModel > FavCodes { get ; set ; }
1719 private SQLiteConnection _db ;
1820 private readonly IThemeService themeService ;
21+
22+ private string prefpath = "pref.json" ;
1923 public DataBaseManager ( IThemeService _themeService )
2024 {
2125 var path = "DataBase.db" ;
@@ -38,8 +42,47 @@ public DataBaseManager(IThemeService _themeService)
3842 if ( code . isFav ) lst . Add ( code ) ;
3943 }
4044 FavCodes = lst ;
45+
46+ if ( File . Exists ( prefpath ) )
47+ {
48+ string pref = File . ReadAllText ( prefpath ) ;
49+ prefModel = JsonConvert . DeserializeObject < PrefModel > ( pref ) ;
50+ isLightTheme = prefModel . IsLightTheme ;
51+ sortBy = prefModel . SortBy ;
52+ }
53+ else
54+ {
55+ isLightTheme = true ;
56+ sortBy = "Date" ;
57+ prefModel = new PrefModel ( ) { IsLightTheme = isLightTheme , SortBy = sortBy } ;
58+ UpdatePref ( ) ;
59+ }
60+ }
61+
62+ #region Preference region
63+
64+ private PrefModel prefModel = new PrefModel ( ) ;
65+ public bool isLightTheme { get ; set ; }
66+ public string sortBy { get ; set ; }
67+
68+ public void ChangeSort ( string sort )
69+ {
70+ prefModel . SortBy = sort ;
71+ UpdatePref ( ) ;
72+ }
73+ public void ChangeTheme ( bool IsLightTheme )
74+ {
75+ prefModel . IsLightTheme = IsLightTheme ;
76+ UpdatePref ( ) ;
77+ }
78+ private void UpdatePref ( )
79+ {
80+ var json = JsonConvert . SerializeObject ( prefModel ) ;
81+ File . WriteAllText ( prefpath , json ) ;
4182 }
4283
84+ #endregion
85+
4386 private int GetIndex ( CodeBoxModel code )
4487 {
4588 int ind = 0 ;
@@ -155,6 +198,8 @@ public ObservableCollection<CodeBoxModel> OrderCode(string order)
155198 if ( ind == 0 ) lst = new ObservableCollection < CodeBoxModel > ( currentCodes . OrderBy ( x => x . title ) . ToList ( ) ) ;
156199 else if ( ind == 1 ) lst = new ObservableCollection < CodeBoxModel > ( currentCodes . OrderBy ( x => x . timestamp ) . ToList ( ) ) ;
157200 else lst = AllCodes ;
201+
202+ if ( ind == 0 || ind == 1 ) ChangeSort ( AllOrderKind [ ind ] ) ;
158203 }
159204
160205 if ( ind < 2 ) AllCodes = lst ;
@@ -189,10 +234,9 @@ public ObservableCollection<CodeBoxModel> SearchCode(string text, string from)
189234 var currentCode = from == "Home" ? AllCodes : FavCodes ;
190235
191236 var lst = new ObservableCollection < CodeBoxModel > ( ) ;
192- foreach ( var code in currentCode )
237+ var lst2 = new ObservableCollection < CodeBoxModel > ( ) ;
238+ foreach ( var code in currentCode )
193239 {
194- bool add = false ;
195-
196240 int f = 0 ;
197241 if ( code . title . Length >= text . Length )
198242 {
@@ -207,16 +251,15 @@ public ObservableCollection<CodeBoxModel> SearchCode(string text, string from)
207251 }
208252 else continue ;
209253
210- if ( code . title == text ) add = true ;
254+ if ( code . title == text ) lst . Add ( code ) ;
211255 else if ( code . title . Length < 3 )
212256 {
213- if ( f >= 1 ) add = true ;
257+ if ( f >= 1 ) lst2 . Add ( code ) ;
214258 }
215- else if ( f >= 3 ) add = true ;
216-
217- if ( add ) lst . Add ( code ) ;
259+ else if ( f >= 3 ) lst2 . Add ( code ) ;
218260 }
219261
262+ lst . Concat ( lst2 ) ;
220263 return lst ;
221264 }
222265 }
0 commit comments