Skip to content

Commit f795b3b

Browse files
committed
Need to fix data and category vm. Exceptions when work with data and then select category.
1 parent 4636f99 commit f795b3b

9 files changed

Lines changed: 60 additions & 74 deletions

File tree

SharpPasswordManager/Handlers/IStorageHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace SharpPasswordManager.Handlers
99
/// <typeparam name="TData"></typeparam>
1010
public interface IStorageHandler<TCategory, TData>
1111
{
12-
TCategory CurrentCategory { get; set; }
12+
int CurrentCategoryIndex { get; set; }
1313

1414
List<TCategory> GetCategories();
1515
List<TData> GetData();

SharpPasswordManager/Handlers/StorageHandler.cs

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public class StorageHandler : IStorageHandler<CategoryModel, DataModel>
1818
/// <summary>
1919
/// Selected model of <see cref="CategoryModel"/>.
2020
/// </summary>
21-
public CategoryModel CurrentCategory { get; set; }
21+
public int CurrentCategoryIndex { get; set; } = -1;
22+
2223
private Random random = new Random();
2324

2425
/// <summary>
@@ -47,9 +48,6 @@ public void AddCategory(CategoryModel category)
4748
/// <param name="category">Adding data</param>
4849
public void AddData(DataModel data)
4950
{
50-
if (CurrentCategory == null)
51-
return;
52-
5351
List<int> usingIndexes = new List<int>();
5452
for (int i = 0; i < categoryController.Count(); i++)
5553
{
@@ -66,16 +64,9 @@ public void AddData(DataModel data)
6664

6765
dataController.PasteAt(SecureManager.GetIndexOf(newIndex), data);
6866

69-
for (int i = 0; i < categoryController.Count(); i++)
70-
{
71-
CategoryModel category = categoryController.Get(i);
72-
if (category.Equals(CurrentCategory))
73-
{
74-
category.DataIndexes.Add(newIndex);
75-
categoryController.PasteAt(i, category);
76-
break;
77-
}
78-
}
67+
CategoryModel category = categoryController.Get(CurrentCategoryIndex);
68+
category.DataIndexes.Add(newIndex);
69+
categoryController.PasteAt(CurrentCategoryIndex, category);
7970
}
8071

8172
/// <summary>
@@ -93,23 +84,19 @@ public List<CategoryModel> GetCategories()
9384
/// <returns></returns>
9485
public List<DataModel> GetData()
9586
{
96-
if (CurrentCategory != null)
97-
{
98-
List<DataModel> dataList = new List<DataModel>();
99-
dataList.Capacity = CurrentCategory.DataIndexes.Count();
87+
List<DataModel> dataList = new List<DataModel>();
88+
CategoryModel currentCategory = categoryController.Get(CurrentCategoryIndex);
89+
dataList.Capacity = currentCategory.DataIndexes.Count();
10090

101-
foreach (var index in CurrentCategory.DataIndexes)
102-
{
103-
DataModel data = dataController.Get(SecureManager.GetIndexOf(index));
104-
dataList.Add(data);
105-
}
91+
foreach (var index in currentCategory.DataIndexes)
92+
{
93+
DataModel data = dataController.Get(SecureManager.GetIndexOf(index));
94+
dataList.Add(data);
95+
}
10696

107-
SetUsingData(CurrentCategory.DataIndexes);
97+
SetUsingData(currentCategory.DataIndexes);
10898

109-
return dataList;
110-
}
111-
else
112-
return new List<DataModel>();
99+
return dataList;
113100
}
114101

115102
/// <summary>
@@ -139,17 +126,10 @@ public void RemoveData(DataModel data)
139126

140127
if (index > -1)
141128
{
142-
for (int i = 0; i < categoryController.Count(); i++)
143-
{
144-
CategoryModel category = categoryController.Get(i);
145-
if (category.DataIndexes.Contains(index))
146-
{
147-
category.DataIndexes.Remove(index);
148-
categoryController.PasteAt(i, category);
149-
SetUsingData(category.DataIndexes);
150-
break;
151-
}
152-
}
129+
CategoryModel currentCategory = categoryController.Get(CurrentCategoryIndex);
130+
currentCategory.DataIndexes.Remove(index);
131+
categoryController.PasteAt(CurrentCategoryIndex, currentCategory);
132+
SetUsingData(currentCategory.DataIndexes);
153133
}
154134
}
155135

SharpPasswordManager/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System.Reflection;
2-
using System.Resources;
3-
using System.Runtime.CompilerServices;
42
using System.Runtime.InteropServices;
53
using System.Windows;
64

SharpPasswordManager/Properties/app.manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
2020
</requestedPrivileges>
2121
<applicationRequestMinimum>
22-
<PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
2322
<defaultAssemblyRequest permissionSetReference="Custom" />
23+
<PermissionSet class="System.Security.PermissionSet" version="1" ID="Custom" SameSite="site" Unrestricted="true" />
2424
</applicationRequestMinimum>
2525
</security>
2626
</trustInfo>

SharpPasswordManager/SharpPasswordManager.csproj

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,16 @@
6464
<ManifestKeyFile>SharpPasswordManager_TemporaryKey.pfx</ManifestKeyFile>
6565
</PropertyGroup>
6666
<PropertyGroup>
67-
<GenerateManifests>true</GenerateManifests>
67+
<GenerateManifests>false</GenerateManifests>
6868
</PropertyGroup>
6969
<PropertyGroup>
7070
<TargetZone>LocalIntranet</TargetZone>
7171
</PropertyGroup>
72-
<PropertyGroup>
73-
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
74-
</PropertyGroup>
72+
<PropertyGroup />
7573
<PropertyGroup>
7674
<SignManifests>false</SignManifests>
7775
</PropertyGroup>
76+
<PropertyGroup />
7877
<ItemGroup>
7978
<Reference Include="MaterialDesignColors, Version=1.2.2.920, Culture=neutral, processorArchitecture=MSIL">
8079
<HintPath>..\packages\MaterialDesignColors.1.2.2\lib\net45\MaterialDesignColors.dll</HintPath>
@@ -262,9 +261,7 @@
262261
</BootstrapperPackage>
263262
</ItemGroup>
264263
<ItemGroup>
265-
<Resource Include="Logo.png">
266-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
267-
</Resource>
264+
<Resource Include="Views\Logo.png" />
268265
</ItemGroup>
269266
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
270267
<Import Project="..\packages\MaterialDesignThemes.3.0.1\build\MaterialDesignThemes.targets" Condition="Exists('..\packages\MaterialDesignThemes.3.0.1\build\MaterialDesignThemes.targets')" />

SharpPasswordManager/ViewModels/CategoryViewModel.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ public CategoryModel SelectedCategory
2626
set
2727
{
2828
selectedCategory = value;
29-
storageHandler.CurrentCategory = selectedCategory;
29+
if (selectedCategory == null)
30+
storageHandler.CurrentCategoryIndex = -1;
31+
else
32+
storageHandler.CurrentCategoryIndex = CategoriesList.IndexOf(SelectedCategory);
33+
3034
OnCategoryChanged?.Invoke();
3135
}
3236
}
@@ -51,14 +55,14 @@ private void GetCategories()
5155
{
5256
MessageBox.Show($"Can't read data from file {ex.Message}.");
5357
}
58+
5459
OnPropertyChanged(nameof(CategoriesList));
5560
}
5661

5762
public void DataChanged()
5863
{
59-
int index = CategoriesList.IndexOf(SelectedCategory);
6064
GetCategories();
61-
SelectedCategory = CategoriesList[index];
65+
SelectedCategory = CategoriesList[storageHandler.CurrentCategoryIndex];
6266
OnPropertyChanged(nameof(CategoriesList));
6367
}
6468

@@ -93,8 +97,9 @@ private void AddCategory()
9397
{
9498
MessageBox.Show($"Can't save data to file {ex.Message}.");
9599
}
100+
96101
SelectedCategory = CategoriesList.LastOrDefault();
97-
storageHandler.CurrentCategory = SelectedCategory;
102+
OnCategoryChanged?.Invoke();
98103
OnPropertyChanged(nameof(SelectedCategory));
99104
}
100105
}
@@ -119,6 +124,7 @@ private void DeleteCategory()
119124
try
120125
{
121126
storageHandler.RemoveCategory(selectedCategory);
127+
GetCategories();
122128
}
123129
catch (FileNotFoundException ex)
124130
{
@@ -128,11 +134,10 @@ private void DeleteCategory()
128134
{
129135
MessageBox.Show($"Can't save data to file {ex.Message}.");
130136
}
137+
131138
SelectedCategory = null;
132-
storageHandler.CurrentCategory = SelectedCategory;
133-
GetCategories();
134-
OnPropertyChanged(nameof(CategoriesList));
135139
OnCategoryChanged?.Invoke();
140+
OnPropertyChanged(nameof(CategoriesList));
136141
}
137142
}
138143

@@ -157,7 +162,6 @@ private void EditCategory()
157162

158163
if (!selectedCategory.Equals(oldCategory))
159164
{
160-
int index = CategoriesList.IndexOf(selectedCategory);
161165
try
162166
{
163167
storageHandler.ReplaceCategory(oldCategory, selectedCategory);
@@ -171,8 +175,10 @@ private void EditCategory()
171175
{
172176
MessageBox.Show($"Can't save data to file {ex.Message}.");
173177
}
174-
SelectedCategory = CategoriesList[index];
175-
storageHandler.CurrentCategory = SelectedCategory;
178+
179+
SelectedCategory = CategoriesList[storageHandler.CurrentCategoryIndex];
180+
OnCategoryChanged?.Invoke();
181+
OnPropertyChanged(nameof(CategoriesList));
176182
}
177183
}
178184

SharpPasswordManager/ViewModels/DataViewModel.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,25 @@ public void CategoryChanged()
4242

4343
private void GetData()
4444
{
45-
try
45+
if (storageHandler.CurrentCategoryIndex > -1)
4646
{
47-
DataList = new ObservableCollection<DataModel>(storageHandler.GetData());
48-
}
49-
catch (FileNotFoundException ex)
50-
{
51-
MessageBox.Show($"File not found {ex.Message}.");
52-
DataList = null;
53-
}
54-
catch (InvalidOperationException ex)
55-
{
56-
MessageBox.Show($"Can't read data from file {ex.Message}.");
57-
DataList = null;
47+
try
48+
{
49+
DataList = new ObservableCollection<DataModel>(storageHandler.GetData());
50+
}
51+
catch (FileNotFoundException ex)
52+
{
53+
MessageBox.Show($"File not found {ex.Message}.");
54+
DataList = null;
55+
}
56+
catch (InvalidOperationException ex)
57+
{
58+
MessageBox.Show($"Can't read data from file {ex.Message}.");
59+
DataList = null;
60+
}
5861
}
62+
else
63+
DataList = new ObservableCollection<DataModel>();
5964

6065
OnPropertyChanged(nameof(DataList));
6166
}
@@ -70,7 +75,7 @@ public ICommand AddDataCmd
7075
}
7176
private void AddData()
7277
{
73-
if (storageHandler.CurrentCategory == null)
78+
if (storageHandler.CurrentCategoryIndex == -1)
7479
return;
7580

7681
Views.DataValidateView validateView = new Views.DataValidateView();

SharpPasswordManager/ViewModels/PasswordCheckViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private void AccessCheck(object parameter)
4747
foreach (Window item in Application.Current.Windows)
4848
if (item.DataContext == this)
4949
item.Close();
50-
mainView.ShowDialog();
50+
mainView.Show();
5151
}
5252
else
5353
MessageBox.Show("Wrong password.");

0 commit comments

Comments
 (0)