10
10
using Newtonsoft . Json ;
11
11
using System . IO ;
12
12
using System . Security . AccessControl ;
13
+ using System . IO . Compression ;
13
14
14
15
namespace CutCode
15
16
{
@@ -21,18 +22,16 @@ public class DataBaseManager : IDataBase
21
22
private readonly IThemeService themeService ;
22
23
23
24
private string prefpath { get ; set ; }
25
+ private string dbpath { get ; set ; }
24
26
#region Set region
25
27
public DataBaseManager ( IThemeService _themeService )
26
28
{
27
29
var appDataPath = Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ;
28
30
var path = Path . Combine ( appDataPath , "CutCode" ) ;
29
31
Directory . CreateDirectory ( path ) ;
30
- var dbpath = Path . Combine ( path , "DataBase.db" ) ;
32
+ dbpath = Path . Combine ( path , "DataBase.db" ) ;
31
33
prefpath = Path . Combine ( path , "pref.json" ) ;
32
34
33
- _db = new SQLiteConnection ( dbpath ) ;
34
- _db . CreateTable < CodeTable > ( ) ;
35
-
36
35
if ( File . Exists ( prefpath ) )
37
36
{
38
37
string pref = File . ReadAllText ( prefpath ) ;
@@ -44,12 +43,18 @@ public DataBaseManager(IThemeService _themeService)
44
43
{
45
44
isLightTheme = true ;
46
45
sortBy = "Date" ;
47
- prefModel = new PrefModel ( ) { IsLightTheme = isLightTheme , SortBy = sortBy } ;
46
+ prefModel = new PrefModel ( ) { IsLightTheme = isLightTheme , SortBy = sortBy } ;
48
47
UpdatePref ( ) ;
49
48
}
50
49
51
50
themeService = _themeService ;
52
51
themeService . IsLightTheme = isLightTheme ;
52
+ OpenDB ( ) ;
53
+ }
54
+ private void OpenDB ( )
55
+ {
56
+ _db = new SQLiteConnection ( dbpath ) ;
57
+ _db . CreateTable < CodeTable > ( ) ;
53
58
54
59
AllCodes = new ObservableCollection < CodeBoxModel > ( ) ;
55
60
@@ -65,6 +70,8 @@ public DataBaseManager(IThemeService _themeService)
65
70
if ( code . isFav ) lst . Add ( code ) ;
66
71
}
67
72
FavCodes = lst ;
73
+
74
+ PropertyChanged ( ) ;
68
75
}
69
76
#endregion
70
77
@@ -74,11 +81,11 @@ public DataBaseManager(IThemeService _themeService)
74
81
public bool isLightTheme { get ; set ; }
75
82
public string sortBy { get ; set ; }
76
83
77
- public void ChangeSort ( string sort )
84
+ public void ChangeSort ( string sort )
78
85
{
79
86
prefModel . SortBy = sort ;
80
87
UpdatePref ( ) ;
81
- }
88
+ }
82
89
public void ChangeTheme ( bool IsLightTheme )
83
90
{
84
91
prefModel . IsLightTheme = IsLightTheme ;
@@ -96,7 +103,7 @@ private void UpdatePref()
96
103
private int GetIndex ( CodeBoxModel code )
97
104
{
98
105
int ind = 0 ;
99
- foreach ( var c in AllCodes )
106
+ foreach ( var c in AllCodes )
100
107
{
101
108
if ( c . id == code . id ) break ;
102
109
ind ++ ;
@@ -106,18 +113,18 @@ private int GetIndex(CodeBoxModel code)
106
113
107
114
public event EventHandler AllCodesUpdated ;
108
115
public event EventHandler FavCodesUpdated ;
109
- public void PropertyChanged ( )
116
+ public void PropertyChanged ( )
110
117
{
111
118
var lst = new ObservableCollection < CodeBoxModel > ( ) ;
112
119
foreach ( var code in AllCodes )
113
120
{
114
121
if ( code . isFav ) lst . Add ( code ) ;
115
122
}
116
123
FavCodes = lst ;
117
-
124
+
118
125
AllCodesUpdated ? . Invoke ( this , EventArgs . Empty ) ;
119
126
FavCodesUpdated ? . Invoke ( this , EventArgs . Empty ) ;
120
- }
127
+ }
121
128
122
129
public CodeBoxModel AddCode ( string title , string desc , string code , string langType )
123
130
{
@@ -147,7 +154,7 @@ public bool EditCode(CodeBoxModel code)
147
154
try
148
155
{
149
156
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 )
151
158
{
152
159
dbCode . title = code . title ;
153
160
dbCode . desc = code . desc ;
@@ -184,7 +191,7 @@ public bool DelCode(CodeBoxModel code)
184
191
185
192
private List < string > AllOrderKind = new List < string > ( )
186
193
{
187
- "Alphabet" , "Date" , "All languages" , "Python" , "C++" , "C#" , "CSS" , "Dart" , "Golang" ,
194
+ "Alphabet" , "Date" , "All languages" , "Python" , "C++" , "C#" , "CSS" , "Dart" , "Golang" ,
188
195
"Html" , "Java" , "Javascript" , "Kotlin" , "Php" , "C" , "Ruby" , "Rust" , "Sql" , "Swift"
189
196
} ;
190
197
@@ -195,7 +202,7 @@ public async Task<ObservableCollection<CodeBoxModel>> OrderCode(string order)
195
202
196
203
var currentCodes = AllCodes ;
197
204
198
- if ( ind > 2 )
205
+ if ( ind > 2 )
199
206
{
200
207
lst = new ObservableCollection < CodeBoxModel > ( ) ;
201
208
foreach ( var code in currentCodes )
@@ -206,7 +213,7 @@ public async Task<ObservableCollection<CodeBoxModel>> OrderCode(string order)
206
213
else
207
214
{
208
215
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 ( ) ) ;
210
217
else lst = AllCodes ;
211
218
212
219
if ( ind == 0 || ind == 1 ) ChangeSort ( AllOrderKind [ ind ] ) ;
@@ -242,11 +249,43 @@ public async Task<ObservableCollection<CodeBoxModel>> SearchCode(string text, st
242
249
var currentCode = from == "Home" ? AllCodes : FavCodes ;
243
250
var newCodesList = currentCode . Where ( x => x . title . ToLower ( ) . Contains ( text . ToLower ( ) ) ) . ToList ( ) ;
244
251
var newCodes = new ObservableCollection < CodeBoxModel > ( ) ;
245
- foreach ( var code in newCodesList )
252
+ foreach ( var code in newCodesList )
246
253
{
247
254
newCodes . Add ( code ) ;
248
255
}
249
256
return newCodes ;
250
257
}
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
+ }
251
290
}
252
291
}
0 commit comments