1010using Newtonsoft . Json ;
1111using System . IO ;
1212using System . Security . AccessControl ;
13+ using System . IO . Compression ;
1314
1415namespace CutCode
1516{
@@ -21,18 +22,16 @@ public class DataBaseManager : IDataBase
2122 private readonly IThemeService themeService ;
2223
2324 private string prefpath { get ; set ; }
25+ private string dbpath { get ; set ; }
2426 #region Set region
2527 public DataBaseManager ( IThemeService _themeService )
2628 {
2729 var appDataPath = Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ;
2830 var path = Path . Combine ( appDataPath , "CutCode" ) ;
2931 Directory . CreateDirectory ( path ) ;
30- var dbpath = Path . Combine ( path , "DataBase.db" ) ;
32+ dbpath = Path . Combine ( path , "DataBase.db" ) ;
3133 prefpath = Path . Combine ( path , "pref.json" ) ;
3234
33- _db = new SQLiteConnection ( dbpath ) ;
34- _db . CreateTable < CodeTable > ( ) ;
35-
3635 if ( File . Exists ( prefpath ) )
3736 {
3837 string pref = File . ReadAllText ( prefpath ) ;
@@ -44,12 +43,18 @@ public DataBaseManager(IThemeService _themeService)
4443 {
4544 isLightTheme = true ;
4645 sortBy = "Date" ;
47- prefModel = new PrefModel ( ) { IsLightTheme = isLightTheme , SortBy = sortBy } ;
46+ prefModel = new PrefModel ( ) { IsLightTheme = isLightTheme , SortBy = sortBy } ;
4847 UpdatePref ( ) ;
4948 }
5049
5150 themeService = _themeService ;
5251 themeService . IsLightTheme = isLightTheme ;
52+ OpenDB ( ) ;
53+ }
54+ private void OpenDB ( )
55+ {
56+ _db = new SQLiteConnection ( dbpath ) ;
57+ _db . CreateTable < CodeTable > ( ) ;
5358
5459 AllCodes = new ObservableCollection < CodeBoxModel > ( ) ;
5560
@@ -65,6 +70,8 @@ public DataBaseManager(IThemeService _themeService)
6570 if ( code . isFav ) lst . Add ( code ) ;
6671 }
6772 FavCodes = lst ;
73+
74+ PropertyChanged ( ) ;
6875 }
6976 #endregion
7077
@@ -74,11 +81,11 @@ public DataBaseManager(IThemeService _themeService)
7481 public bool isLightTheme { get ; set ; }
7582 public string sortBy { get ; set ; }
7683
77- public void ChangeSort ( string sort )
84+ public void ChangeSort ( string sort )
7885 {
7986 prefModel . SortBy = sort ;
8087 UpdatePref ( ) ;
81- }
88+ }
8289 public void ChangeTheme ( bool IsLightTheme )
8390 {
8491 prefModel . IsLightTheme = IsLightTheme ;
@@ -96,7 +103,7 @@ private void UpdatePref()
96103 private int GetIndex ( CodeBoxModel code )
97104 {
98105 int ind = 0 ;
99- foreach ( var c in AllCodes )
106+ foreach ( var c in AllCodes )
100107 {
101108 if ( c . id == code . id ) break ;
102109 ind ++ ;
@@ -106,18 +113,18 @@ private int GetIndex(CodeBoxModel code)
106113
107114 public event EventHandler AllCodesUpdated ;
108115 public event EventHandler FavCodesUpdated ;
109- public void PropertyChanged ( )
116+ public void PropertyChanged ( )
110117 {
111118 var lst = new ObservableCollection < CodeBoxModel > ( ) ;
112119 foreach ( var code in AllCodes )
113120 {
114121 if ( code . isFav ) lst . Add ( code ) ;
115122 }
116123 FavCodes = lst ;
117-
124+
118125 AllCodesUpdated ? . Invoke ( this , EventArgs . Empty ) ;
119126 FavCodesUpdated ? . Invoke ( this , EventArgs . Empty ) ;
120- }
127+ }
121128
122129 public CodeBoxModel AddCode ( string title , string desc , string code , string langType )
123130 {
@@ -147,7 +154,7 @@ public bool EditCode(CodeBoxModel code)
147154 try
148155 {
149156 var dbCode = _db . Query < CodeTable > ( "select * from CodeTable where Id = ?" , code . id ) . FirstOrDefault ( ) ;
150- if ( dbCode is not null )
157+ if ( dbCode is not null )
151158 {
152159 dbCode . title = code . title ;
153160 dbCode . desc = code . desc ;
@@ -184,7 +191,7 @@ public bool DelCode(CodeBoxModel code)
184191
185192 private List < string > AllOrderKind = new List < string > ( )
186193 {
187- "Alphabet" , "Date" , "All languages" , "Python" , "C++" , "C#" , "CSS" , "Dart" , "Golang" ,
194+ "Alphabet" , "Date" , "All languages" , "Python" , "C++" , "C#" , "CSS" , "Dart" , "Golang" ,
188195 "Html" , "Java" , "Javascript" , "Kotlin" , "Php" , "C" , "Ruby" , "Rust" , "Sql" , "Swift"
189196 } ;
190197
@@ -195,7 +202,7 @@ public async Task<ObservableCollection<CodeBoxModel>> OrderCode(string order)
195202
196203 var currentCodes = AllCodes ;
197204
198- if ( ind > 2 )
205+ if ( ind > 2 )
199206 {
200207 lst = new ObservableCollection < CodeBoxModel > ( ) ;
201208 foreach ( var code in currentCodes )
@@ -206,7 +213,7 @@ public async Task<ObservableCollection<CodeBoxModel>> OrderCode(string order)
206213 else
207214 {
208215 if ( ind == 0 ) lst = new ObservableCollection < CodeBoxModel > ( currentCodes . OrderBy ( x => x . title ) . ToList ( ) ) ;
209- else if ( ind == 1 ) lst = new ObservableCollection < CodeBoxModel > ( currentCodes . OrderBy ( x => x . timestamp ) . ToList ( ) ) ;
216+ else if ( ind == 1 ) lst = new ObservableCollection < CodeBoxModel > ( currentCodes . OrderBy ( x => x . timestamp ) . ToList ( ) ) ;
210217 else lst = AllCodes ;
211218
212219 if ( ind == 0 || ind == 1 ) ChangeSort ( AllOrderKind [ ind ] ) ;
@@ -242,11 +249,43 @@ public async Task<ObservableCollection<CodeBoxModel>> SearchCode(string text, st
242249 var currentCode = from == "Home" ? AllCodes : FavCodes ;
243250 var newCodesList = currentCode . Where ( x => x . title . ToLower ( ) . Contains ( text . ToLower ( ) ) ) . ToList ( ) ;
244251 var newCodes = new ObservableCollection < CodeBoxModel > ( ) ;
245- foreach ( var code in newCodesList )
252+ foreach ( var code in newCodesList )
246253 {
247254 newCodes . Add ( code ) ;
248255 }
249256 return newCodes ;
250257 }
258+
259+ public string ExportData ( string path )
260+ {
261+ if ( Path . GetExtension ( path ) != ".whl" ) return "This type of file are not supported!" ;
262+ _db . Close ( ) ;
263+ var bytes = File . ReadAllBytes ( dbpath ) ;
264+ File . WriteAllBytes ( path , bytes ) ;
265+ OpenDB ( ) ;
266+
267+ return "Successfully exported your codes!" ;
268+ }
269+
270+ public string ImportData ( string path )
271+ {
272+ _db . Close ( ) ;
273+ var currentData = File . ReadAllBytes ( dbpath ) ;
274+
275+ var importingData = File . ReadAllBytes ( path ) ;
276+ File . WriteAllBytes ( dbpath , importingData ) ;
277+ try
278+ {
279+ OpenDB ( ) ;
280+ }
281+ catch
282+ {
283+ _db . Close ( ) ;
284+ File . WriteAllBytes ( dbpath , currentData ) ;
285+ OpenDB ( ) ;
286+ return "Your syncing file is corrupted! We are unable to sync your codes!" ;
287+ }
288+ return "Successfully imported your codes!" ;
289+ }
251290 }
252291}
0 commit comments