Skip to content

Commit 9d7471b

Browse files
committed
Done with syncing feature
1 parent 33596d1 commit 9d7471b

File tree

4 files changed

+93
-19
lines changed

4 files changed

+93
-19
lines changed

CutCode/DataBase/DataBaseManager.cs

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Newtonsoft.Json;
1111
using System.IO;
1212
using System.Security.AccessControl;
13+
using System.IO.Compression;
1314

1415
namespace 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
}

CutCode/Interfaces/IDataBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ public interface IDataBase
2525

2626
void ChangeSort(string sort);
2727
void ChangeTheme(bool IsLightTheme);
28+
29+
string ExportData(string path);
30+
string ImportData(string path);
2831
}
2932
}

CutCode/ViewModels/MainViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public MainViewModel(IWindowManager _windowManager,IThemeService themeService, I
5858
new AddViewModel(_themeService, pageService, _dataBase),
5959
new FavViewModel(_themeService, pageService, _dataBase),
6060
new ShareViewModel(_themeService, pageService, _dataBase),
61-
new SettingViewModel(_themeService, _dataBase) };
61+
new SettingViewModel(_themeService, _dataBase, notifyManager) };
6262
pageService.Page = Pages[0];
6363
}
6464

CutCode/ViewModels/SettingViewModel.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@
33
using System.Collections.Generic;
44
using System.Collections.ObjectModel;
55
using System.ComponentModel;
6+
using System.Diagnostics;
67
using System.Linq;
78
using System.Runtime.CompilerServices;
89
using System.Text;
910
using System.Threading.Tasks;
11+
using System.Windows.Forms;
1012
using System.Windows.Media;
1113

1214
namespace CutCode
1315
{
14-
public class SettingViewModel : Screen
16+
public class SettingViewModel : Stylet.Screen
1517
{
1618
public ObservableCollection<ThemeButtonModel> themeBtns { get; set; }
1719
public ObservableCollection<SyncBtnModel> SyncBtns { get; set; }
1820
private readonly IThemeService themeService;
1921
private readonly IDataBase database;
20-
public SettingViewModel(IThemeService _themeService, IDataBase _database)
22+
private readonly INotificationManager notificationManager;
23+
public SettingViewModel(IThemeService _themeService, IDataBase _database, INotificationManager _notificationManager)
2124
{
2225
database = _database;
26+
notificationManager = _notificationManager;
2327

2428
themeService = _themeService;
2529
themeService.ThemeChanged += ThemeChanged;
@@ -90,7 +94,35 @@ public void ThemeChangeCommand(string selectedTheme)
9094

9195
public void SyncCommand(string syncType)
9296
{
97+
string message = "";
9398

99+
if(syncType == "Import")
100+
{
101+
var fileDialog = new OpenFileDialog();
102+
fileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
103+
fileDialog.DefaultExt = "whl";
104+
fileDialog.Filter = "whl files (*.whl)|*.whl";
105+
fileDialog.ShowDialog();
106+
107+
if (!string.IsNullOrEmpty(fileDialog.FileName))
108+
{
109+
message = database.ImportData(fileDialog.FileName);
110+
}
111+
}
112+
else
113+
{
114+
var fileDialog = new SaveFileDialog();
115+
fileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
116+
fileDialog.DefaultExt = "whl";
117+
fileDialog.FileName = $"Export_CutCode_{DateTime.Now.ToString("yyyyMMddhhmmss")}.whl";
118+
fileDialog.Filter = "whl files (*.whl)|*.whl";
119+
fileDialog.ShowDialog();
120+
if (!string.IsNullOrEmpty(fileDialog.FileName))
121+
{
122+
message = database.ExportData(fileDialog.FileName);
123+
}
124+
}
125+
if(!string.IsNullOrEmpty(message)) notificationManager.CreateNotification(message, 4);
94126
}
95127
}
96128
}

0 commit comments

Comments
 (0)