Skip to content

Commit

Permalink
style: Исправлено форматирование
Browse files Browse the repository at this point in the history
  • Loading branch information
Kibnet committed Oct 31, 2023
1 parent 98e271e commit 8d9b540
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 120 deletions.
3 changes: 2 additions & 1 deletion src/Unlimotion.ViewModel/DbUpdatedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Unlimotion.ViewModel
{
public class DbUpdatedEventArgs : EventArgs {
public class DbUpdatedEventArgs : EventArgs
{
public List<UpdatedTask> UpdatedTasks { get; set; } = new List<UpdatedTask>();
}
}
63 changes: 40 additions & 23 deletions src/Unlimotion.ViewModel/FileDbWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

namespace Unlimotion.ViewModel
{
public class FileDbWatcher : IDatabaseWatcher {
public class FileDbWatcher : IDatabaseWatcher
{
private readonly string _path;
private readonly HashSet<UpdatedTask> _updatedTasks = new();
private FileSystemWatcher _watcher;
Expand All @@ -22,76 +23,92 @@ public class FileDbWatcher : IDatabaseWatcher {
public bool IsEnabled { get; set; }
public event EventHandler<DbUpdatedEventArgs>? OnDatabaseUpdated;

public FileDbWatcher(string path) {
if (string.IsNullOrEmpty(path)) {
public FileDbWatcher(string path)
{
if (string.IsNullOrEmpty(path))
{
_path = String.Empty;
return;
}
if (!Directory.Exists(path)) {
if (!Directory.Exists(path))
{
throw new DirectoryNotFoundException("Directory does not exist: " + path);
}
_path = path;
}

public async Task Start() {
public async Task Start()
{
if (string.IsNullOrEmpty(_path) || IsEnabled) return;
_watcher = new FileSystemWatcher(_path);

_watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;

_watcher.Changed += OnChanged;
_watcher.Created += OnCreated;
_watcher.Deleted += OnDeleted;
_watcher.Disposed += (s, e) => {
_watcher.Disposed += (s, e) =>
{
_ct.Cancel();
};
//todo Добавить логер и логировать ошибки
_watcher.Error += OnError;
_watcher.IncludeSubdirectories = true;
_watcher.EnableRaisingEvents = true;

_ct = new ();
_ct = new();

while (!_ct.Token.IsCancellationRequested) {
while (!_ct.Token.IsCancellationRequested)
{
GenerateEvent();
await Task.Delay(1000);
}

_watcher?.Dispose();
IsEnabled = false;
}

public void Stop() {
public void Stop()
{
_ct?.Cancel();
}

private void OnError(object sender, ErrorEventArgs e) {
private void OnError(object sender, ErrorEventArgs e)
{
Debug.WriteLine("Ошибка в файлВачере!!!");
}

private void AddTaskToCollection(UpdatedTask task) {
lock (_utLock) {

private void AddTaskToCollection(UpdatedTask task)
{
lock (_utLock)
{
_updatedTasks.Add(task);
}
}

private void OnChanged(object sender, FileSystemEventArgs e) {
if (e.ChangeType == WatcherChangeTypes.Changed) {
private void OnChanged(object sender, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Changed)
{
AddTaskToCollection(new UpdatedTask(e.FullPath!, UpdatingTaskType.TaskChanged,
File.GetLastWriteTime(e.FullPath)));
}
}

private void OnDeleted(object sender, FileSystemEventArgs e) {

private void OnDeleted(object sender, FileSystemEventArgs e)
{
AddTaskToCollection(new UpdatedTask(e.FullPath!, UpdatingTaskType.TaskDeleted));
}

private void OnCreated(object sender, FileSystemEventArgs e) {
private void OnCreated(object sender, FileSystemEventArgs e)
{
AddTaskToCollection(new UpdatedTask(e.FullPath!, UpdatingTaskType.TaskCreated));
}

private void GenerateEvent() {
lock (_utLock) {

private void GenerateEvent()
{
lock (_utLock)
{
OnDatabaseUpdated?.Invoke(this, new DbUpdatedEventArgs() { UpdatedTasks = _updatedTasks.ToList() });
_updatedTasks.Clear();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Unlimotion.ViewModel/IDatabaseWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using System.Threading.Tasks;

namespace Unlimotion.ViewModel;
public interface IDatabaseWatcher {
public interface IDatabaseWatcher
{
event EventHandler<DbUpdatedEventArgs>? OnDatabaseUpdated;
bool IsEnabled { get; set; }
Task Start();
Expand Down
6 changes: 4 additions & 2 deletions src/Unlimotion.ViewModel/IFileTaskStorage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Unlimotion.ViewModel {
public interface IFileTaskStorage {
namespace Unlimotion.ViewModel
{
public interface IFileTaskStorage
{
TaskItem? LoadFromFile(string filePath);
}
}
12 changes: 8 additions & 4 deletions src/Unlimotion.ViewModel/Models/UpdatedTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@

namespace Unlimotion.ViewModel.Models
{
public class UpdatedTask{
public class UpdatedTask
{
public DateTime UpdatedDateTime { get; }
/// <summary>
/// В случае с таском, полученным из файла Id - полный путь к файлу<br/>
/// В случае с таском, полученным из Базы Данных, Id - это идентификатор записи в БД
/// </summary>
public string Id { get; }
public UpdatingTaskType UpdatingType { get; }
public UpdatedTask(string id, UpdatingTaskType updatingType) {
public UpdatedTask(string id, UpdatingTaskType updatingType)
{
Id = id;
UpdatingType = updatingType;
UpdatedDateTime = DateTime.Now;
}

public UpdatedTask(string id, UpdatingTaskType updatingType, DateTime updatedDateTime) {
public UpdatedTask(string id, UpdatingTaskType updatingType, DateTime updatedDateTime)
{
Id = id;
UpdatingType = updatingType;
UpdatedDateTime = updatedDateTime;
}

public override int GetHashCode() {
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Unlimotion.ViewModel/Models/UpdatingTaskType.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace Unlimotion.ViewModel.Models
{
public enum UpdatingTaskType {
public enum UpdatingTaskType
{
TaskCreated,
TaskDeleted,
TaskDeleted,
TaskChanged
}
}
Loading

0 comments on commit 8d9b540

Please sign in to comment.