Skip to content

Commit

Permalink
Data/category vm and StorageHandler reworked
Browse files Browse the repository at this point in the history
  • Loading branch information
arty-F committed Feb 23, 2020
1 parent f795b3b commit d36b019
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 173 deletions.
6 changes: 2 additions & 4 deletions SharpPasswordManager/Handlers/IStorageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ namespace SharpPasswordManager.Handlers
/// <typeparam name="TData"></typeparam>
public interface IStorageHandler<TCategory, TData>
{
int CurrentCategoryIndex { get; set; }

List<TCategory> GetCategories();
List<TData> GetData();
List<TData> GetData(TCategory ofCategory);
void RemoveCategory(TCategory category);
void RemoveData(TData data);
void AddCategory(TCategory category);
void AddData(TData data);
void AddData(TData data, TCategory toCategory);
void ReplaceCategory(TCategory oldCategory, TCategory newCategory);
void ReplaceData(TData oldData, TData newData);
}
Expand Down
93 changes: 45 additions & 48 deletions SharpPasswordManager/Handlers/StorageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using SharpPasswordManager.DL.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SharpPasswordManager.Handlers
{
Expand All @@ -14,12 +13,6 @@ public class StorageHandler : IStorageHandler<CategoryModel, DataModel>
private IStorageController<CategoryModel> categoryController;
private IStorageController<DataModel> dataController;

private Dictionary<DataModel, int> usingData = new Dictionary<DataModel, int>();
/// <summary>
/// Selected model of <see cref="CategoryModel"/>.
/// </summary>
public int CurrentCategoryIndex { get; set; } = -1;

private Random random = new Random();

/// <summary>
Expand All @@ -46,16 +39,9 @@ public void AddCategory(CategoryModel category)
/// Write <seealso cref="DataModel"/> to data storage contoller at defined random index. Then write that index to CurrentCategory index collection, and save changes of category storage controller.
/// </summary>
/// <param name="category">Adding data</param>
public void AddData(DataModel data)
public void AddData(DataModel data, CategoryModel toCategory)
{
List<int> usingIndexes = new List<int>();
for (int i = 0; i < categoryController.Count(); i++)
{
foreach (var index in categoryController.Get(i).DataIndexes)
{
usingIndexes.Add(index);
}
}
List<int> usingIndexes = GetUsingDataIndexes();

int maxValue = dataController.Count();
int newIndex = random.Next(0, maxValue);
Expand All @@ -64,9 +50,18 @@ public void AddData(DataModel data)

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

CategoryModel category = categoryController.Get(CurrentCategoryIndex);
category.DataIndexes.Add(newIndex);
categoryController.PasteAt(CurrentCategoryIndex, category);
List<CategoryModel> categories = categoryController.ToList();

for (int i = 0; i < categories.Count; i++)
{
if (categories[i].Equals(toCategory))
{
CategoryModel newCategory = new CategoryModel { DataIndexes = new List<int>(toCategory.DataIndexes), Name = toCategory.Name };
newCategory.DataIndexes.Add(newIndex);
categoryController.PasteAt(i, newCategory);
break;
}
}
}

/// <summary>
Expand All @@ -82,20 +77,19 @@ public List<CategoryModel> GetCategories()
/// Get list of data referenced in CurrentCategory.
/// </summary>
/// <returns></returns>
public List<DataModel> GetData()
public List<DataModel> GetData(CategoryModel ofCategory)
{
List<DataModel> dataList = new List<DataModel>();
CategoryModel currentCategory = categoryController.Get(CurrentCategoryIndex);
dataList.Capacity = currentCategory.DataIndexes.Count();

foreach (var index in currentCategory.DataIndexes)
if (ofCategory.DataIndexes.Count == 0)
return dataList;

dataList.Capacity = ofCategory.DataIndexes.Count;
foreach (var dataIndex in ofCategory.DataIndexes)
{
DataModel data = dataController.Get(SecureManager.GetIndexOf(index));
dataList.Add(data);
dataList.Add(dataController.Get(SecureManager.GetIndexOf(dataIndex)));
}

SetUsingData(currentCategory.DataIndexes);

return dataList;
}

Expand All @@ -114,23 +108,20 @@ public void RemoveCategory(CategoryModel category)
/// <param name="data">Data to remove.</param>
public void RemoveData(DataModel data)
{
int index = -1;
foreach (var item in usingData)
List<CategoryModel> categories = GetCategories();
for (int i = 0; i < categories.Count; i++)
{
if (item.Key.Equals(data))
foreach (var dataIndex in categories[i].DataIndexes)
{
index = item.Value;
break;
if (dataController.Get(SecureManager.GetIndexOf(dataIndex)).Equals(data))
{
CategoryModel newCategory = new CategoryModel { DataIndexes = new List<int>(categories[i].DataIndexes), Name = categories[i].Name };
newCategory.DataIndexes.Remove(dataIndex);
categoryController.PasteAt(i, newCategory);
return;
}
}
}

if (index > -1)
{
CategoryModel currentCategory = categoryController.Get(CurrentCategoryIndex);
currentCategory.DataIndexes.Remove(index);
categoryController.PasteAt(CurrentCategoryIndex, currentCategory);
SetUsingData(currentCategory.DataIndexes);
}
}

/// <summary>
Expand All @@ -153,24 +144,30 @@ public void ReplaceCategory(CategoryModel oldCategory, CategoryModel newCategory
/// </summary>
public void ReplaceData(DataModel oldData, DataModel newData)
{
foreach (var item in usingData)
List<int> usingIndexes = GetUsingDataIndexes();

foreach (var index in usingIndexes)
{
if (item.Key.Equals(oldData))
if (dataController.Get(SecureManager.GetIndexOf(index)).Equals(oldData))
{
dataController.PasteAt(SecureManager.GetIndexOf(item.Value), newData);
SetUsingData(usingData.Values.ToList());
dataController.PasteAt(SecureManager.GetIndexOf(index), newData);
break;
}
}
}

private void SetUsingData(List<int> indexes)
private List<int> GetUsingDataIndexes()
{
usingData.Clear();
foreach (var index in indexes)
List<int> usingIndexes = new List<int>();
List<CategoryModel> categories = categoryController.ToList();
foreach (var category in categories)
{
usingData.Add(dataController.Get(SecureManager.GetIndexOf(index)), index);
foreach (var index in category.DataIndexes)
{
usingIndexes.Add(index);
}
}
return usingIndexes;
}
}
}
100 changes: 45 additions & 55 deletions SharpPasswordManager/ViewModels/CategoryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace SharpPasswordManager.ViewModels
{
public class CategoryViewModel : INotifyPropertyChanged
{
public delegate void CategoryChangeHandler();
public delegate void CategoryChangeHandler(CategoryModel category);
public event CategoryChangeHandler OnCategoryChanged;

IStorageHandler<CategoryModel, DataModel> storageHandler;
public ObservableCollection<CategoryModel> CategoriesList { get; set; }
public ObservableCollection<CategoryModel> CategoriesList { get; set; } = new ObservableCollection<CategoryModel>();

private CategoryModel selectedCategory;
public CategoryModel SelectedCategory
Expand All @@ -26,12 +25,7 @@ public CategoryModel SelectedCategory
set
{
selectedCategory = value;
if (selectedCategory == null)
storageHandler.CurrentCategoryIndex = -1;
else
storageHandler.CurrentCategoryIndex = CategoriesList.IndexOf(SelectedCategory);

OnCategoryChanged?.Invoke();
OnCategoryChanged?.Invoke(selectedCategory);
}
}

Expand All @@ -43,27 +37,41 @@ public CategoryViewModel(IStorageHandler<CategoryModel, DataModel> storageHandle

private void GetCategories()
{
int index = -1;
if (SelectedCategory != null)
index = CategoriesList.IndexOf(SelectedCategory);

CategoriesList.Clear();

try
{
CategoriesList = new ObservableCollection<CategoryModel>(storageHandler.GetCategories());
}
catch (FileNotFoundException ex)
{
MessageBox.Show($"File not found {ex.Message}.");
}
{ MessageBox.Show($"File not found {ex.Message}."); }
catch (InvalidOperationException ex)
{
MessageBox.Show($"Can't read data from file {ex.Message}.");
}
{ MessageBox.Show($"Can't read data from file {ex.Message}."); }
catch (Exception ex)
{ MessageBox.Show($"Something is wrong {ex.Message}."); }

OnPropertyChanged(nameof(CategoriesList));

if (index > -1 && index < CategoriesList.Count)
SelectedCategory = CategoriesList[index];
else
SelectedCategory = null;

OnPropertyChanged(nameof(SelectedCategory));
}

public CategoryModel ShareSelectedCategory()
{
return SelectedCategory;
}

public void DataChanged()
{
GetCategories();
SelectedCategory = CategoriesList[storageHandler.CurrentCategoryIndex];
OnPropertyChanged(nameof(CategoriesList));
}

private ICommand addCategoryCmd;
Expand All @@ -87,20 +95,15 @@ private void AddCategory()
try
{
storageHandler.AddCategory(newCategory);
GetCategories();
}
catch (FileNotFoundException ex)
{
MessageBox.Show($"File not found {ex.Message}.");
}
{ MessageBox.Show($"File not found {ex.Message}."); }
catch (InvalidOperationException ex)
{
MessageBox.Show($"Can't save data to file {ex.Message}.");
}
{ MessageBox.Show($"Can't write data to file {ex.Message}."); }
catch (Exception ex)
{ MessageBox.Show($"Something is wrong {ex.Message}."); }

SelectedCategory = CategoriesList.LastOrDefault();
OnCategoryChanged?.Invoke();
OnPropertyChanged(nameof(SelectedCategory));
GetCategories();
}
}

Expand All @@ -123,21 +126,17 @@ private void DeleteCategory()
{
try
{
storageHandler.RemoveCategory(selectedCategory);
GetCategories();
storageHandler.RemoveCategory(SelectedCategory);
}
catch (FileNotFoundException ex)
{
MessageBox.Show($"File not found {ex.Message}.");
}
{ MessageBox.Show($"File not found {ex.Message}."); }
catch (InvalidOperationException ex)
{
MessageBox.Show($"Can't save data to file {ex.Message}.");
}
{ MessageBox.Show($"Can't save data to file {ex.Message}."); }
catch (Exception ex)
{ MessageBox.Show($"Something is wrong {ex.Message}."); }

SelectedCategory = null;
OnCategoryChanged?.Invoke();
OnPropertyChanged(nameof(CategoriesList));
GetCategories();
}
}

Expand All @@ -151,34 +150,25 @@ public ICommand EditCategoryCmd
}
private void EditCategory()
{
CategoryModel oldCategory = new CategoryModel();
oldCategory.Name = selectedCategory.Name;
oldCategory.DataIndexes = new List<int>(selectedCategory.DataIndexes);

CategoryValidateViewModel validateVM = new CategoryValidateViewModel(ref selectedCategory);
CategoryModel newCategory = new CategoryModel { DataIndexes = new List<int>(SelectedCategory?.DataIndexes), Name = SelectedCategory.Name };
CategoryValidateViewModel validateVM = new CategoryValidateViewModel(ref newCategory);
Views.CategoryValidateView validateView = new Views.CategoryValidateView();
validateView.DataContext = validateVM;
validateView.ShowDialog();

if (!selectedCategory.Equals(oldCategory))
if (!SelectedCategory.Equals(newCategory))
{
try
{
storageHandler.ReplaceCategory(oldCategory, selectedCategory);
GetCategories();
storageHandler.ReplaceCategory(SelectedCategory, newCategory);
}
catch (FileNotFoundException ex)
{
MessageBox.Show($"File not found {ex.Message}.");
}
{ MessageBox.Show($"File not found {ex.Message}."); }
catch (InvalidOperationException ex)
{
MessageBox.Show($"Can't save data to file {ex.Message}.");
}

SelectedCategory = CategoriesList[storageHandler.CurrentCategoryIndex];
OnCategoryChanged?.Invoke();
OnPropertyChanged(nameof(CategoriesList));
{ MessageBox.Show($"Can't save data to file {ex.Message}."); }
catch (Exception ex)
{ MessageBox.Show($"Something is wrong {ex.Message}."); }
GetCategories();
}
}

Expand Down
Loading

0 comments on commit d36b019

Please sign in to comment.