Skip to content

Commit 283c633

Browse files
committed
Done fixing the order type and light theme config
1 parent e2acd67 commit 283c633

10 files changed

Lines changed: 102 additions & 18 deletions

File tree

CutCode/CutCode.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
<ItemGroup>
4848
<PackageReference Include="AvalonEdit" Version="6.1.2.30" />
49+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
4950
<PackageReference Include="sqlite-net-pcl" Version="1.7.335" />
5051
<PackageReference Include="Stylet" Version="1.3.6" />
5152
</ItemGroup>

CutCode/DataBase/DataBaseManager.cs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using System.Linq;
88
using System.Text;
99
using System.Threading.Tasks;
10+
using Newtonsoft.Json;
11+
using System.IO;
1012

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

CutCode/DataBase/pref.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"IsLightTheme": true,
3+
"SortBy": "Date"
4+
}

CutCode/Interfaces/IDataBase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,11 @@ public interface IDataBase
1919
ObservableCollection<CodeBoxModel> OrderCode(string order);
2020
bool FavModify(CodeBoxModel code);
2121
ObservableCollection<CodeBoxModel> SearchCode(string text, string from);
22+
23+
bool isLightTheme { get; set; }
24+
string sortBy { get; set; }
25+
26+
void ChangeSort(string sort);
27+
void ChangeTheme(bool IsLightTheme);
2228
}
2329
}

CutCode/Models/PrefModel.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace CutCode
9+
{
10+
public class PrefModel
11+
{
12+
[JsonProperty("IsLightTheme")]
13+
public bool IsLightTheme { get; set; }
14+
15+
[JsonProperty("SortBy")]
16+
public string SortBy { get; set; }
17+
}
18+
}

CutCode/ViewModels/AddViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public AddViewModel(IThemeService _themeService, IPageService _pageService, IDat
3030
"Javascript", "Kotlin", "Php", "C", "Ruby", "Rust","Sql", "Swift"
3131
};
3232
leftText = "";
33-
CurrentLang = AllLangs[0];
3433
SetAppearance();
3534
}
3635
private void ThemeChanged(Object sender, EventArgs e)
@@ -141,7 +140,7 @@ public void SaveCommand()
141140
pageService.remoteChange = "Home";
142141
pageService.Page = new CodeViewModel(themeService, pageService, database, codeModel);
143142

144-
title = ""; desc = ""; code = ""; CurrentLang = AllLangs[0];
143+
MainViewModel.Pages[1] = new AddViewModel(themeService, pageService, database);
145144
}
146145
}
147146

@@ -150,7 +149,7 @@ public void CancelCommand()
150149
pageService.remoteChange = "Home";
151150
pageService.Page = MainViewModel.Pages[0];
152151

153-
title = ""; desc = ""; code = ""; CurrentLang = AllLangs[0];
152+
MainViewModel.Pages[1] = new AddViewModel(themeService, pageService, database);
154153
}
155154
}
156155
}

CutCode/ViewModels/HomeViewModel.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public HomeViewModel(IThemeService _themeService, IPageService _pageService, IDa
2727
AllCodes = database.AllCodes;
2828
database.AllCodesUpdated += AllCodesUpdated;
2929

30+
sortby = database.sortBy == "Date" ? 0 : 1;
31+
3032
SetAppearance();
3133
IsSearched = false;
3234

@@ -136,6 +138,7 @@ public Visibility codesVis
136138
get => _codesVis;
137139
set => SetAndNotify(ref _codesVis, value);
138140
}
141+
public int sortby { get; set; }
139142

140143
private string _emptyLabel;
141144
public string emptyLabel
@@ -166,7 +169,11 @@ public string CurrentSort1
166169
set
167170
{
168171
SetAndNotify(ref _CurrentSort1, value);
169-
Application.Current.Dispatcher.Invoke(new Action(() => { ComboBoxItemSelected(value); }));
172+
Application.Current.Dispatcher.Invoke(new Action(() =>
173+
{
174+
ComboBoxItemSelected(value);
175+
database.sortBy = value;
176+
}));
170177
}
171178
}
172179

CutCode/ViewModels/MainViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ public MainViewModel(IThemeService themeService, IPageService _pageService, IDat
2929
{
3030
_themeService = themeService;
3131
_themeService.ThemeChanged += ThemeChanged;
32-
_themeService.IsLightTheme = false;
3332

3433
pageService = _pageService;
3534
pageService.PageChanged += PageChanged;
3635
pageService.PageRemoteChanged += PageRemoteChanged;
3736

3837
database = _dataBase;
38+
_themeService.IsLightTheme = database.isLightTheme;
3939

4040
sideBarBtns = new ObservableCollection<SideBarBtnModel>();
4141

@@ -50,7 +50,7 @@ public MainViewModel(IThemeService themeService, IPageService _pageService, IDat
5050
Pages = new List<Object>() { new HomeViewModel(themeService, pageService, database),
5151
new AddViewModel(themeService, pageService, database),
5252
new FavViewModel(themeService, pageService, database),
53-
new SettingViewModel(_themeService) };
53+
new SettingViewModel(_themeService, database) };
5454
pageService.Page = Pages[0];
5555
}
5656

CutCode/ViewModels/SettingViewModel.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ public class SettingViewModel : Screen
1515
{
1616
public ObservableCollection<ThemeButtonModel> themeBtns { get; set; }
1717
private readonly IThemeService themeService;
18-
19-
public SettingViewModel(IThemeService _themeService)
18+
private readonly IDataBase database;
19+
public SettingViewModel(IThemeService _themeService, IDataBase _database)
2020
{
21+
database = _database;
22+
2123
themeService = _themeService;
2224
themeService.ThemeChanged += ThemeChanged;
2325

@@ -60,7 +62,11 @@ public SolidColorBrush cardBackgroundColor
6062
}
6163
}
6264
}
63-
public void ThemeChangeCommand(string selectedTheme) => themeService.IsLightTheme = selectedTheme == "Light Mode" ? true : false;
65+
public void ThemeChangeCommand(string selectedTheme)
66+
{
67+
themeService.IsLightTheme = selectedTheme == "Light Mode" ? true : false;
68+
database.ChangeTheme(themeService.IsLightTheme);
69+
}
6470

6571
}
6672
}

CutCode/Views/HomeView.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
FontSize="12" FontFamily="{StaticResource poppins_regular}"/>
4444
<ComboBox x:Name="sortCombobox" Grid.Row="1" Height="30" Width="150" Text="Sort by" Margin="5,5,15,5" BorderThickness="0"
4545
Foreground="{Binding searchBarTextColor}" FontFamily="{StaticResource poppins_regular}"
46-
Background="{Binding comboboxBackgroundColor}" SelectedIndex="0"
46+
Background="{Binding comboboxBackgroundColor}" SelectedIndex="{Binding sortby}"
4747
IsDropDownOpen="false" Style="{StaticResource comboBoxStyle}"
4848
SelectedValue="{Binding CurrentSort1}" ItemsSource="{Binding Sorts}"
4949
BorderBrush="{Binding comboboxHoverColor}">

0 commit comments

Comments
 (0)