Развитие системы растений
This commit is contained in:
1 parent
1b578067f3
commit
ba49ef5015
218 files changed
+70415
-1519
No files matched your search
@@ -3,19 +3,38 @@ using System.Collections.Generic;
|
||||
|
||||
namespace Code.Calendar.DataStructures
|
||||
{
|
||||
public struct Plant
|
||||
public struct Plant : IEquatable<Plant>
|
||||
{
|
||||
public string Name;
|
||||
public string IconPath;
|
||||
public int Id;
|
||||
public Guid Id;
|
||||
|
||||
public bool Equals(Plant other) => Id == other.Id;
|
||||
|
||||
public override bool Equals(object obj) => obj is Plant other && Equals(other);
|
||||
|
||||
public override int GetHashCode() => Id.GetHashCode();
|
||||
}
|
||||
public struct Task
|
||||
public struct Task : IEquatable<Task>
|
||||
{
|
||||
public Guid Id;
|
||||
|
||||
public bool Active;
|
||||
|
||||
public string Description;
|
||||
public DateTime Date;
|
||||
|
||||
public bool IsPeriodic;
|
||||
public int PeriodicInterval;
|
||||
public int PlantId;
|
||||
public DateTime PeriodicUntil;
|
||||
|
||||
public Guid PlantId;
|
||||
public List<string> PhotoPaths;
|
||||
|
||||
public bool Equals(Task other) => Id == other.Id;
|
||||
|
||||
public override bool Equals(object obj) => obj is Task other && Equals(other);
|
||||
|
||||
public override int GetHashCode() => Id.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Calendar.DataStructures
|
||||
{
|
||||
public interface IPlantService
|
||||
{
|
||||
void AddPlant(Plant plant);
|
||||
void DeletePlant(Plant plant);
|
||||
Plant GetPlant(Guid plantId);
|
||||
List<Plant> GetPlants();
|
||||
void EditPlant(Guid guid, Plant newPlant);
|
||||
}
|
||||
|
||||
public class PlantService : IDisposable, IPlantService
|
||||
{
|
||||
private readonly EventBus _eventBus;
|
||||
|
||||
private List<Plant> _plants;
|
||||
|
||||
public PlantService(EventBus eventBus)
|
||||
{
|
||||
_eventBus = eventBus;
|
||||
|
||||
LoadJsonTasks().Forget();
|
||||
}
|
||||
|
||||
private async UniTaskVoid LoadJsonTasks()
|
||||
{
|
||||
var path = Path.Combine(Application.persistentDataPath, "Plants.json");
|
||||
|
||||
if (File.Exists(path))
|
||||
{
|
||||
using StreamReader reader = new(path);
|
||||
|
||||
_plants = JsonConvert.DeserializeObject<List<Plant>>(await reader.ReadToEndAsync()) ?? new List<Plant>();
|
||||
}
|
||||
else
|
||||
{
|
||||
await using (File.Create(path)) { }
|
||||
}
|
||||
|
||||
_eventBus.RaiseEvent<IPlantsServiceInitHandle>(x=> x.PlantsLoaded());
|
||||
}
|
||||
|
||||
private bool _inAction, _reUseRequest;
|
||||
|
||||
private async UniTaskVoid SaveJsonTasks()
|
||||
{
|
||||
if (_inAction)
|
||||
{
|
||||
_reUseRequest = true;
|
||||
return;
|
||||
}
|
||||
|
||||
_inAction = true;
|
||||
|
||||
var json = JsonConvert.SerializeObject(_plants, Formatting.Indented);
|
||||
|
||||
await using StreamWriter writer = new(Path.Combine(Application.persistentDataPath, "Plants.json"));
|
||||
|
||||
await writer.WriteAsync(json).AsUniTask();
|
||||
|
||||
_inAction = false;
|
||||
|
||||
if(!_reUseRequest) _eventBus.RaiseEvent<IPlantsServiceInitHandle>(x=> x.PlantsSaved());
|
||||
else SaveJsonTasks().Forget();
|
||||
}
|
||||
|
||||
public void AddPlant(Plant plant)
|
||||
{
|
||||
plant.Id = Guid.NewGuid();
|
||||
|
||||
_plants.Add(plant);
|
||||
|
||||
SaveJsonTasks().Forget();
|
||||
}
|
||||
|
||||
public void EditPlant(Guid guid, Plant newPlant)
|
||||
{
|
||||
_plants[_plants.FindIndex(x => x.Id == guid)] = newPlant;
|
||||
|
||||
SaveJsonTasks().Forget();
|
||||
}
|
||||
public void DeletePlant(Plant plant)
|
||||
{
|
||||
_plants.Remove(plant);
|
||||
|
||||
SaveJsonTasks().Forget();
|
||||
}
|
||||
|
||||
public Plant GetPlant(Guid plantId) => _plants.FirstOrDefault(x => x.Id == plantId);
|
||||
public List<Plant> GetPlants() => _plants;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
SaveJsonTasks().Forget();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IPlantsServiceInitHandle : IGlobalSubscriber
|
||||
{
|
||||
void PlantsLoaded();
|
||||
void PlantsSaved();
|
||||
}
|
||||
public interface IPlantsServiceHandle : IGlobalSubscriber
|
||||
{
|
||||
void PlantAdded();
|
||||
void PlantRemoved();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eb3e195da68845ea872e2eff73582745
|
||||
timeCreated: 1772116279
|
||||
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Calendar.DataStructures
|
||||
{
|
||||
public interface ITaskService
|
||||
{
|
||||
void AddTask(Task task);
|
||||
void DeleteTask(Task task);
|
||||
List<Task> GetByPlant(Guid plantId);
|
||||
List<Task> GetTasks();
|
||||
List<Task> GetByDate(DateTime date);
|
||||
void EditTask(Guid guid, Task newTask);
|
||||
}
|
||||
|
||||
public class TaskService : IDisposable, ITaskService
|
||||
{
|
||||
private readonly EventBus _eventBus;
|
||||
|
||||
private List<Task> _tasks;
|
||||
|
||||
public TaskService(EventBus eventBus)
|
||||
{
|
||||
_eventBus = eventBus;
|
||||
|
||||
LoadJsonTasks().Forget();
|
||||
}
|
||||
|
||||
private async UniTaskVoid LoadJsonTasks()
|
||||
{
|
||||
var path = Path.Combine(Application.persistentDataPath, "Tasks.json");
|
||||
|
||||
if (File.Exists(path))
|
||||
{
|
||||
using StreamReader reader = new(path);
|
||||
|
||||
_tasks = JsonConvert.DeserializeObject<List<Task>>(await reader.ReadToEndAsync()) ?? new List<Task>();
|
||||
}
|
||||
else
|
||||
{
|
||||
await using (File.Create(path)) { }
|
||||
}
|
||||
|
||||
_eventBus.RaiseEvent<ITaskServiceHandle>(x => x.TasksLoaded());
|
||||
}
|
||||
|
||||
private bool _inAction, _reUseRequest;
|
||||
|
||||
private async UniTaskVoid SaveJsonTasks()
|
||||
{
|
||||
if (_inAction)
|
||||
{
|
||||
_reUseRequest = true;
|
||||
return;
|
||||
}
|
||||
|
||||
_inAction = true;
|
||||
|
||||
var json = JsonConvert.SerializeObject(_tasks, Formatting.Indented);
|
||||
|
||||
await using StreamWriter writer = new(Path.Combine(Application.persistentDataPath, "Tasks.json"));
|
||||
|
||||
await writer.WriteAsync(json).AsUniTask();
|
||||
|
||||
_inAction = false;
|
||||
|
||||
if (!_reUseRequest) _eventBus.RaiseEvent<ITaskServiceHandle>(x => x.TasksSaved());
|
||||
else SaveJsonTasks().Forget();
|
||||
}
|
||||
|
||||
public void AddTask(Task task)
|
||||
{
|
||||
_tasks.Add(task);
|
||||
|
||||
task.Id = Guid.NewGuid();
|
||||
|
||||
SaveJsonTasks().Forget();
|
||||
}
|
||||
|
||||
public void EditTask(Guid guid, Task newTask)
|
||||
{
|
||||
_tasks[_tasks.FindIndex(x => x.Id == guid)] = newTask;
|
||||
|
||||
SaveJsonTasks().Forget();
|
||||
}
|
||||
|
||||
public void DeleteTask(Task task)
|
||||
{
|
||||
_tasks.Remove(task);
|
||||
SaveJsonTasks().Forget();
|
||||
}
|
||||
|
||||
public List<Task> GetByPlant(Guid plantId) => _tasks.Where(x => x.PlantId == plantId).ToList();
|
||||
|
||||
public List<Task> GetByDate(DateTime date)
|
||||
{
|
||||
var byDay = _tasks
|
||||
.Where(x => x.Date.Day == date.Day)
|
||||
.ToList();
|
||||
|
||||
var periodicOnes = _tasks
|
||||
.Where(x => x.IsPeriodic && x.Date.Day + new TimeSpan(0, x.PeriodicInterval, 0).Days == date.Day)
|
||||
.ToList();
|
||||
|
||||
byDay.AddRange(periodicOnes);
|
||||
|
||||
return byDay;
|
||||
}
|
||||
|
||||
public List<Task> GetTasks() => _tasks;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
SaveJsonTasks().Forget();
|
||||
}
|
||||
}
|
||||
|
||||
public interface ITaskServiceHandle : IGlobalSubscriber
|
||||
{
|
||||
void TasksLoaded();
|
||||
void TasksSaved();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e0a70b7566234451ad60128860af9517
|
||||
timeCreated: 1772113905
|
||||
Reference in new issue
Block a user