PlantList
This commit is contained in:
1 parent
ba49ef5015
commit
7c207ef5b9
44 files changed
+2795
-1147
No files matched your search
@@ -1,18 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Code.Infrastructure.Configs;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
|
||||
namespace Code.Calendar.Constants
|
||||
{
|
||||
// Написать кодрайтер, который будет находить конфиги и добавлять их сюда пока написано само
|
||||
public class ConstantImagesService
|
||||
public interface IConstantImagesService
|
||||
{
|
||||
UniTaskVoid LoadImage(ConstantImageType imageType, Action<Sprite> callback);
|
||||
void ReleaseImage(ConstantImageType imageType);
|
||||
}
|
||||
|
||||
public class ConstantImagesService : IConstantImagesService
|
||||
{
|
||||
private readonly EventBus _eventBus;
|
||||
|
||||
public List<ConstantImageConfig> Configs { get; private set; }
|
||||
public Dictionary<ConstantImageType, ConstantImageConfig> Configs { get; private set; }
|
||||
|
||||
public ConstantImagesService(EventBus eventBus, IConfigLocator configLocator)
|
||||
{
|
||||
@@ -20,10 +29,10 @@ namespace Code.Calendar.Constants
|
||||
|
||||
var label = configLocator.TryGetManyConfigAssetReferences<ConstantImageConfig>();
|
||||
|
||||
LoadImage(label).Forget();
|
||||
LoadImages(label).Forget();
|
||||
}
|
||||
|
||||
private async UniTaskVoid LoadImage(string imagesLabel)
|
||||
private async UniTaskVoid LoadImages(string imagesLabel)
|
||||
{
|
||||
if ((await Addressables.LoadResourceLocationsAsync(imagesLabel).Task).Count != 0)
|
||||
{
|
||||
@@ -32,13 +41,29 @@ namespace Code.Calendar.Constants
|
||||
|
||||
await images.ToUniTask();
|
||||
|
||||
Configs = images.Result.ToList();
|
||||
Configs = images.Result.ToDictionary(x=> x.imageType);
|
||||
}
|
||||
|
||||
_eventBus.RaiseEvent<IConstantImagesServiceHandle>(x=> x.ImagesLoaded());
|
||||
}
|
||||
}
|
||||
|
||||
Dictionary<ConstantImageType, AsyncOperationHandle<Sprite>> _spritesHandles = new();
|
||||
public async UniTaskVoid LoadImage(ConstantImageType imageType, Action<Sprite> callback)
|
||||
{
|
||||
if(!_spritesHandles.ContainsKey(imageType))
|
||||
_spritesHandles.Add(imageType, Addressables.LoadAssetAsync<Sprite>(Configs[imageType].image));
|
||||
|
||||
await _spritesHandles[imageType].ToUniTask();
|
||||
|
||||
callback?.Invoke(_spritesHandles[imageType].Result);
|
||||
}
|
||||
|
||||
public void ReleaseImage(ConstantImageType imageType)
|
||||
{
|
||||
_spritesHandles[imageType].Release();
|
||||
_spritesHandles.Remove(imageType);
|
||||
}
|
||||
}
|
||||
public interface IConstantImagesServiceHandle : IGlobalSubscriber
|
||||
{
|
||||
void ImagesLoaded();
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace Code.Calendar.DataStructures
|
||||
public bool Active;
|
||||
|
||||
public string Description;
|
||||
public DateTime Date;
|
||||
public DateTime Date; // Если периодичная, то дата первого мероприятия
|
||||
|
||||
public bool IsPeriodic;
|
||||
public int PeriodicInterval;
|
||||
|
||||
@@ -28,11 +28,18 @@ namespace Code.Calendar.DataStructures
|
||||
{
|
||||
_eventBus = eventBus;
|
||||
|
||||
LoadJsonTasks().Forget();
|
||||
LoadJsonPlants().Forget();
|
||||
}
|
||||
|
||||
private async UniTaskVoid LoadJsonTasks()
|
||||
private async UniTaskVoid LoadJsonPlants()
|
||||
{
|
||||
if (_inAction)
|
||||
{
|
||||
_reUseLoadRequest = true;
|
||||
return;
|
||||
}
|
||||
_inAction = true;
|
||||
|
||||
var path = Path.Combine(Application.persistentDataPath, "Plants.json");
|
||||
|
||||
if (File.Exists(path))
|
||||
@@ -46,16 +53,19 @@ namespace Code.Calendar.DataStructures
|
||||
await using (File.Create(path)) { }
|
||||
}
|
||||
|
||||
_eventBus.RaiseEvent<IPlantsServiceInitHandle>(x=> x.PlantsLoaded());
|
||||
_inAction = false;
|
||||
|
||||
if(!_reUseLoadRequest) _eventBus.RaiseEvent<IPlantsServiceInitHandle>(x=> x.PlantsLoaded());
|
||||
else LoadJsonPlants().Forget();
|
||||
}
|
||||
|
||||
private bool _inAction, _reUseRequest;
|
||||
private bool _inAction, _reUseSaveRequest, _reUseLoadRequest;
|
||||
|
||||
private async UniTaskVoid SaveJsonTasks()
|
||||
private async UniTaskVoid SaveJsonPlants()
|
||||
{
|
||||
if (_inAction)
|
||||
{
|
||||
_reUseRequest = true;
|
||||
_reUseSaveRequest = true;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -69,8 +79,8 @@ namespace Code.Calendar.DataStructures
|
||||
|
||||
_inAction = false;
|
||||
|
||||
if(!_reUseRequest) _eventBus.RaiseEvent<IPlantsServiceInitHandle>(x=> x.PlantsSaved());
|
||||
else SaveJsonTasks().Forget();
|
||||
if(!_reUseSaveRequest) _eventBus.RaiseEvent<IPlantsServiceInitHandle>(x=> x.PlantsSaved());
|
||||
else SaveJsonPlants().Forget();
|
||||
}
|
||||
|
||||
public void AddPlant(Plant plant)
|
||||
@@ -79,20 +89,20 @@ namespace Code.Calendar.DataStructures
|
||||
|
||||
_plants.Add(plant);
|
||||
|
||||
SaveJsonTasks().Forget();
|
||||
SaveJsonPlants().Forget();
|
||||
}
|
||||
|
||||
public void EditPlant(Guid guid, Plant newPlant)
|
||||
{
|
||||
_plants[_plants.FindIndex(x => x.Id == guid)] = newPlant;
|
||||
|
||||
SaveJsonTasks().Forget();
|
||||
SaveJsonPlants().Forget();
|
||||
}
|
||||
public void DeletePlant(Plant plant)
|
||||
{
|
||||
_plants.Remove(plant);
|
||||
|
||||
SaveJsonTasks().Forget();
|
||||
SaveJsonPlants().Forget();
|
||||
}
|
||||
|
||||
public Plant GetPlant(Guid plantId) => _plants.FirstOrDefault(x => x.Id == plantId);
|
||||
@@ -100,7 +110,7 @@ namespace Code.Calendar.DataStructures
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
SaveJsonTasks().Forget();
|
||||
SaveJsonPlants().Forget();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -76,9 +76,9 @@ namespace Code.Calendar.DataStructures
|
||||
|
||||
public void AddTask(Task task)
|
||||
{
|
||||
_tasks.Add(task);
|
||||
|
||||
task.Id = Guid.NewGuid();
|
||||
|
||||
_tasks.Add(task);
|
||||
|
||||
SaveJsonTasks().Forget();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a8d3a5935f1c497bb56de2d94ef01cdd
|
||||
timeCreated: 1772468475
|
||||
@@ -0,0 +1,31 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class SelectedDate {
|
||||
public static System.DateTime date = System.DateTime.Now;
|
||||
}
|
||||
public class DateCallback : AndroidJavaProxy
|
||||
{
|
||||
public DateCallback() : base("android.app.DatePickerDialog$OnDateSetListener") { }
|
||||
|
||||
public void onDateSet(AndroidJavaObject view, int year, int monthOfYear, int dayOfMonth) {
|
||||
SelectedDate.date = new System.DateTime(year, monthOfYear + 1, dayOfMonth);
|
||||
}
|
||||
}
|
||||
public class ExampleClass : MonoBehaviour
|
||||
{
|
||||
private AndroidJavaObject activity;
|
||||
|
||||
void PickDate(){
|
||||
new AndroidJavaObject("android.app.DatePickerDialog", activity, new DateCallback(), SelectedDate.date.Year, SelectedDate.date.Month - 1, SelectedDate.date.Day).Call("show");
|
||||
}
|
||||
|
||||
void OnGUI()
|
||||
{
|
||||
if (GUI.Button(new Rect(10, 10, 450, 100), System.String.Format("{0:yyyy-MM-dd}", SelectedDate.date)))
|
||||
{
|
||||
activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
|
||||
activity.Call("runOnUiThread", new AndroidJavaRunnable(PickDate));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dcb51e6febb24b66870aad7e44df62e0
|
||||
timeCreated: 1772468482
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e8cc1a025b3f422bb7187c11271c961f
|
||||
timeCreated: 1772560436
|
||||
@@ -0,0 +1,12 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Calendar.OtherUIElements
|
||||
{
|
||||
public class AddPlantButton : MonoBehaviour
|
||||
{
|
||||
public void OpenAddPlant()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 67f6127a30aa48ea8157228ad37df8a6
|
||||
timeCreated: 1772560454
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using Code.Calendar.Constants;
|
||||
using Code.Calendar.DataStructures;
|
||||
using Code.Infrastructure.UI;
|
||||
using Reflex.Attributes;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Code.Calendar.Panels
|
||||
{
|
||||
public class PlantAsListElement : UIPanel
|
||||
{
|
||||
[Inject] private IConstantImagesService _constantImagesService;
|
||||
[SerializeField] private Image image;
|
||||
[SerializeField] private TMP_Text plantName;
|
||||
public Guid PlantId { get; private set; }
|
||||
|
||||
public void SetPlant(Plant plant)
|
||||
{
|
||||
PlantId = plant.Id;
|
||||
plantName.text = plant.Name;
|
||||
_constantImagesService.LoadImage(ConstantImageType.NoPhoto,SetImage);
|
||||
}
|
||||
private void SetImage(Sprite sprite) => image.sprite = sprite;
|
||||
public override void Open(Action callback = null)
|
||||
{
|
||||
callback?.Invoke();
|
||||
}
|
||||
|
||||
public override void Close(Action callback = null)
|
||||
{
|
||||
callback?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 65c40e2bdcb14b3ea164b84b80809b25
|
||||
timeCreated: 1772547821
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Code.Calendar.DataStructures;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Code.Infrastructure.Loadings;
|
||||
using Code.Infrastructure.UI;
|
||||
using Reflex.Attributes;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Calendar.Panels
|
||||
{
|
||||
public class PlantsListPanel : UIPanel, IUIPanelLoaded
|
||||
{
|
||||
[SerializeField] private Transform contentParent;
|
||||
|
||||
private IPlantService _plantService;
|
||||
private IUIService _uiService;
|
||||
|
||||
private EventBus _eventBus;
|
||||
|
||||
private List<PlantAsListElement> _plantsPanels = new();
|
||||
private Stack<Plant> _plantsToCreate;
|
||||
|
||||
[Inject]
|
||||
private void Construct(EventBus eventBus, IPlantService plantService, IUIService uiService)
|
||||
{
|
||||
_eventBus = eventBus;
|
||||
_plantService = plantService;
|
||||
_uiService = uiService;
|
||||
|
||||
eventBus.Subscribe(this);
|
||||
}
|
||||
|
||||
public override void Open(Action callback = null)
|
||||
{
|
||||
_plantsToCreate = new(_plantService.GetPlants());
|
||||
|
||||
for (var i = _plantsToCreate.Count; i > 0 ; i--) _uiService.TryLoadCreatePanel(UIPanelType.PlantAsList, contentParent);
|
||||
|
||||
callback?.Invoke();
|
||||
}
|
||||
|
||||
public override void Close(Action callback = null)
|
||||
{
|
||||
ClearPlants(callback);
|
||||
}
|
||||
|
||||
private Loading _panelClosureLoading;
|
||||
private void ClearPlants(Action callback = null)
|
||||
{
|
||||
if(_panelClosureLoading != null)
|
||||
_panelClosureLoading = new(_plantsPanels.Count + _panelClosureLoading.CurrentProgress, callback);
|
||||
else
|
||||
_panelClosureLoading = new(_plantsPanels.Count, callback);
|
||||
|
||||
foreach (var plantPanel in _plantsPanels) _uiService.TryRemovePanel(plantPanel);
|
||||
}
|
||||
|
||||
public void UISinglePanelLoaded(UIPanelType uiPanelType)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void UIMultiplePanelLoaded(UIPanelType uiPanelType, UIPanel panel)
|
||||
{
|
||||
if (uiPanelType != UIPanelType.PlantAsList) return;
|
||||
|
||||
_plantsPanels.Add((PlantAsListElement)panel);
|
||||
((PlantAsListElement)panel).SetPlant(_plantsToCreate.Pop());
|
||||
if(_plantsToCreate.Count == 0) PlantPanelsLoaded();
|
||||
}
|
||||
|
||||
private void PlantPanelsLoaded()
|
||||
{
|
||||
foreach (var panel in _plantsPanels)
|
||||
{
|
||||
panel.Open();
|
||||
}
|
||||
Debug.LogWarning("PlantPanelsLoaded");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ddc4b25ae5854a83ae0a1ba7f8dffb7d
|
||||
timeCreated: 1772547586
|
||||
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using Code.Calendar.DataStructures;
|
||||
using Code.Infrastructure.UI;
|
||||
using PrimeTween;
|
||||
using Reflex.Attributes;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Code.Calendar.Panels
|
||||
{
|
||||
public class TaskAddPanel : UIPanel
|
||||
{
|
||||
[Inject] private ITaskService _taskService;
|
||||
[SerializeField] private TMP_InputField taskName;
|
||||
[SerializeField] private Toggle isPeriodic;
|
||||
|
||||
public void TryAddPlant()
|
||||
{
|
||||
Task newTask = new()
|
||||
{
|
||||
Active = true,
|
||||
IsPeriodic = isPeriodic.isOn,
|
||||
PeriodicInterval = 0,
|
||||
};
|
||||
_taskService.AddTask(newTask);
|
||||
}
|
||||
|
||||
public override void Open(Action callback = null)
|
||||
{
|
||||
callback?.Invoke();
|
||||
// Tween.Alpha(image, 1f, 1f).OnComplete(target: this, _ => callback?.Invoke());
|
||||
}
|
||||
|
||||
public override void Close(Action callback = null)
|
||||
{
|
||||
callback?.Invoke();
|
||||
// Tween.Alpha(image, 0f, 1f).OnComplete(target: this, _ => callback?.Invoke());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fd466bce2a8b41bbab38fd3ede5b7c35
|
||||
timeCreated: 1772467428
|
||||
@@ -60,7 +60,7 @@ namespace Code.Infrastructure.App
|
||||
containerBuilder.RegisterType(typeof(PlantService), new[] { typeof(IPlantService) }, Lifetime.Singleton,
|
||||
Resolution.Eager);
|
||||
|
||||
containerBuilder.RegisterType(typeof(ConstantImagesService), Lifetime.Singleton,
|
||||
containerBuilder.RegisterType(typeof(ConstantImagesService), new []{typeof(IConstantImagesService)}, Lifetime.Singleton,
|
||||
Resolution.Eager);
|
||||
}
|
||||
|
||||
@@ -114,12 +114,13 @@ namespace Code.Infrastructure.App
|
||||
{
|
||||
_initialLoading.DoProgress();
|
||||
|
||||
_thatRightContainer.Resolve<IUIService>().TryLoadPanel(UIPanelType.Logo);
|
||||
_thatRightContainer.Resolve<IUIService>().TryLoadPanel(UIPanelType.Loading);
|
||||
_thatRightContainer.Resolve<IUIService>().TryLoadCreatePanel(UIPanelType.Logo);
|
||||
_thatRightContainer.Resolve<IUIService>().TryLoadCreatePanel(UIPanelType.Loading);
|
||||
}
|
||||
|
||||
public void UILoaded(UIPanelType uiPanelType)
|
||||
public void UISinglePanelLoaded(UIPanelType uiPanelType)
|
||||
{
|
||||
Debug.LogWarning($@"Loaded {uiPanelType.ToString()}");
|
||||
switch (uiPanelType)
|
||||
{
|
||||
case UIPanelType.Logo:
|
||||
@@ -136,6 +137,11 @@ namespace Code.Infrastructure.App
|
||||
}
|
||||
}
|
||||
|
||||
public void UIMultiplePanelLoaded(UIPanelType uiPanelType, UIPanel panel)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void AudioSourcePoolInitialized()
|
||||
{
|
||||
_initialLoading.DoProgress();
|
||||
@@ -161,6 +167,7 @@ namespace Code.Infrastructure.App
|
||||
|
||||
public void PlantsLoaded()
|
||||
{
|
||||
Debug.LogWarning("PlantsLoaded");
|
||||
_initialLoading.DoProgress();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System.Threading;
|
||||
using Code.Infrastructure.Audio;
|
||||
using Code.Infrastructure.Generators;
|
||||
using Code.Infrastructure.UI;
|
||||
@@ -22,7 +21,7 @@ namespace Code.Infrastructure.App.States
|
||||
|
||||
Context.UIService.TryRemovePanel(UIPanelType.Logo);
|
||||
Context.UIService.TryRemovePanel(UIPanelType.Loading);
|
||||
Context.UIService.TryLoadPanel(UIPanelType.PlantAdd);
|
||||
Context.UIService.TryLoadCreatePanel(UIPanelType.PlantList);
|
||||
// Context.AudioFactory.LoadAudioClips(AudioNames.UISounds);
|
||||
}
|
||||
|
||||
@@ -32,12 +31,18 @@ namespace Code.Infrastructure.App.States
|
||||
_eventBus.Unsubscribe(this);
|
||||
}
|
||||
|
||||
public void UILoaded(UIPanelType uiPanelType)
|
||||
public void UISinglePanelLoaded(UIPanelType uiPanelType)
|
||||
{
|
||||
if (uiPanelType != UIPanelType.PlantAdd) return;
|
||||
if (uiPanelType != UIPanelType.PlantList) return;
|
||||
|
||||
Context.UIService.OpenPanel(uiPanelType);
|
||||
}
|
||||
|
||||
public void UIMultiplePanelLoaded(UIPanelType uiPanelType, UIPanel panel)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void OnReadyToPlay(AudioNames groupName)
|
||||
{
|
||||
// Context.AudioService.PlayRepeatedly(AudioNames.UISounds,0);
|
||||
|
||||
@@ -12,14 +12,16 @@ namespace Code.Infrastructure.UI
|
||||
{
|
||||
public interface IUIFactory
|
||||
{
|
||||
UniTaskVoid LoadPanel(UIPanelType panelType);
|
||||
UniTaskVoid LoadPanel(UIPanelType panelType, Transform parent = null);
|
||||
void ReleasePanel(UIPanel panel);
|
||||
Canvas Canvas { get; }
|
||||
bool CheckIsMultiple(UIPanelType panelType);
|
||||
}
|
||||
|
||||
public class UIFactory : IDisposable, IUIFactory
|
||||
{
|
||||
private Dictionary<UIPanelType, UIPanelSettingsConfig> _configs;
|
||||
public bool CheckIsMultiple(UIPanelType panelType) => _configs[panelType].Multiple;
|
||||
private AssetReference _canvasConfigReference;
|
||||
private string _configsLabel;
|
||||
private EventBus.EventBus _eventBus;
|
||||
@@ -41,9 +43,9 @@ namespace Code.Infrastructure.UI
|
||||
LoadConfigs().Forget();
|
||||
}
|
||||
|
||||
public async UniTaskVoid LoadPanel(UIPanelType panelType)
|
||||
public async UniTaskVoid LoadPanel(UIPanelType panelType, Transform parent = null)
|
||||
{
|
||||
var panel = (await Addressables.InstantiateAsync(_configs[panelType].Prefab, _canvas.transform).ToUniTask())
|
||||
var panel = (await Addressables.InstantiateAsync(_configs[panelType].Prefab, parent ?? _canvas.transform).ToUniTask())
|
||||
.GetComponent<UIPanel>();
|
||||
|
||||
UnityEngine.Debug.LogWarning($"Panel loaded {panelType}");
|
||||
@@ -52,7 +54,7 @@ namespace Code.Infrastructure.UI
|
||||
|
||||
public void ReleasePanel(UIPanel panel)
|
||||
{
|
||||
UnityEngine.Debug.LogWarning($"Panel released {panel.gameObject.name}");
|
||||
Debug.LogWarning($"Panel released {panel.gameObject.name}");
|
||||
Addressables.ReleaseInstance(panel.gameObject);
|
||||
}
|
||||
|
||||
@@ -90,5 +92,6 @@ namespace Code.Infrastructure.UI
|
||||
public interface IUIPanelLoadedHandle : IGlobalSubscriber
|
||||
{
|
||||
void UIPanelLoaded(UIPanel panel, UIPanelType panelType);
|
||||
List<UIPanel> TryGetMultiplePanels(UIPanelType panelType);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ namespace Code.Infrastructure.UI
|
||||
{
|
||||
[ToConfig] public AssetReference Prefab;
|
||||
[ToConfig] public UIPanelType UIPanelType;
|
||||
[ToConfig] public bool Multiple;
|
||||
}
|
||||
|
||||
[Config("Configs/UI/CanvasSettings")]
|
||||
|
||||
@@ -7,6 +7,9 @@ namespace Code.Infrastructure.UI
|
||||
Calendar,
|
||||
Tasks,
|
||||
Plants,
|
||||
PlantAdd
|
||||
PlantAdd,
|
||||
TaskAdd,
|
||||
PlantList,
|
||||
PlantAsList
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Reflex.Core;
|
||||
using Reflex.Extensions;
|
||||
using Reflex.Injectors;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Code.Infrastructure.UI
|
||||
@@ -12,15 +14,17 @@ namespace Code.Infrastructure.UI
|
||||
{
|
||||
void OpenPanel(UIPanelType uiPanelType);
|
||||
void ClosePanel(UIPanelType uiPanelType);
|
||||
void TryLoadPanel(UIPanelType uiPanelType);
|
||||
void TryLoadCreatePanel(UIPanelType uiPanelType, Transform parent = null);
|
||||
void TryRemovePanel(UIPanelType uiPanelType);
|
||||
void SetCanvasCamera(UnityEngine.Camera camera);
|
||||
UIPanel TryGetPanel(UIPanelType panelType);
|
||||
UIPanel TryGetSinglePanel(UIPanelType panelType);
|
||||
void TryRemovePanel(UIPanel panel, Action callback = null);
|
||||
}
|
||||
|
||||
public class UIService : IUIPanelLoadedHandle, IUIService
|
||||
{
|
||||
private Dictionary<UIPanelType, UIPanel> _loadedPanels = new();
|
||||
private Dictionary<UIPanelType, UIPanel> _loadedSinglePanels = new();
|
||||
private Dictionary<UIPanelType, List<UIPanel>> _loadedMultiplePanels = new();
|
||||
|
||||
private EventBus.EventBus _eventBus;
|
||||
private IUIFactory _factory;
|
||||
@@ -32,7 +36,7 @@ namespace Code.Infrastructure.UI
|
||||
eventBus.Subscribe(this);
|
||||
}
|
||||
|
||||
public void SetCanvasCamera(UnityEngine.Camera camera)
|
||||
public void SetCanvasCamera(Camera camera)
|
||||
{
|
||||
_factory.Canvas.worldCamera = camera;
|
||||
_factory.Canvas.planeDistance = 0.3f;
|
||||
@@ -40,49 +44,87 @@ namespace Code.Infrastructure.UI
|
||||
|
||||
public void OpenPanel(UIPanelType uiPanelType)
|
||||
{
|
||||
_loadedPanels[uiPanelType].gameObject.SetActive(true);
|
||||
_loadedPanels[uiPanelType].Open();
|
||||
_loadedSinglePanels[uiPanelType].gameObject.SetActive(true);
|
||||
_loadedSinglePanels[uiPanelType].Open();
|
||||
}
|
||||
|
||||
// TODO Close event
|
||||
public void ClosePanel(UIPanelType uiPanelType)
|
||||
{
|
||||
_loadedPanels[uiPanelType].Close(() => { _loadedPanels[uiPanelType].gameObject.SetActive(false); });
|
||||
_loadedSinglePanels[uiPanelType].Close(() =>
|
||||
{
|
||||
_loadedSinglePanels[uiPanelType].gameObject.SetActive(false);
|
||||
});
|
||||
}
|
||||
|
||||
public void TryLoadPanel(UIPanelType uiPanelType) => _factory.LoadPanel(uiPanelType);
|
||||
public void TryLoadCreatePanel(UIPanelType uiPanelType, Transform parent = null) => _factory.LoadPanel(uiPanelType, parent);
|
||||
|
||||
public void UIPanelLoaded(UIPanel panel, UIPanelType panelType)
|
||||
{
|
||||
_loadedPanels.Add(panelType, panel);
|
||||
if (!_factory.CheckIsMultiple(panelType))
|
||||
_loadedSinglePanels.Add(panelType, panel);
|
||||
else
|
||||
{
|
||||
if (!_loadedMultiplePanels.ContainsKey(panelType))
|
||||
_loadedMultiplePanels.Add(panelType, new List<UIPanel>());
|
||||
_loadedMultiplePanels[panelType].Add(panel);
|
||||
}
|
||||
|
||||
if (panel.InjectMeSenpai)
|
||||
GameObjectInjector.InjectRecursive(panel.gameObject, Container.RootContainer);
|
||||
// GameObjectInjector.InjectRecursive(panel.gameObject, SceneManager.GetActiveScene().GetSceneContainer()); // Скорее всего будут нужны компоненты текущей сцены
|
||||
|
||||
_eventBus.RaiseEvent<IUIPanelLoaded>(ui => ui.UILoaded(panelType));
|
||||
if (!_factory.CheckIsMultiple(panelType)) _eventBus.RaiseEvent<IUIPanelLoaded>(ui => ui.UISinglePanelLoaded(panelType));
|
||||
else _eventBus.RaiseEvent<IUIPanelLoaded>(ui => ui.UIMultiplePanelLoaded(panelType, panel));
|
||||
}
|
||||
|
||||
public void TryRemovePanel(UIPanelType uiPanelType)
|
||||
{
|
||||
_loadedPanels[uiPanelType].Close(() =>
|
||||
_loadedSinglePanels[uiPanelType].Close(() =>
|
||||
{
|
||||
_loadedPanels[uiPanelType].gameObject.SetActive(false);
|
||||
_loadedSinglePanels[uiPanelType].gameObject.SetActive(false);
|
||||
RemovePanel(uiPanelType);
|
||||
});
|
||||
}
|
||||
public void TryRemovePanel(UIPanel panel, Action callback = null)
|
||||
{
|
||||
panel.Close(() =>
|
||||
{
|
||||
panel.gameObject.SetActive(false);
|
||||
RemovePanel(panel);
|
||||
callback?.Invoke();
|
||||
});
|
||||
}
|
||||
|
||||
private void RemovePanel(UIPanelType panelType)
|
||||
{
|
||||
_factory.ReleasePanel(_loadedPanels[panelType]);
|
||||
_loadedPanels.Remove(panelType);
|
||||
if (!_factory.CheckIsMultiple(panelType))
|
||||
{
|
||||
_factory.ReleasePanel(_loadedSinglePanels[panelType]);
|
||||
|
||||
_loadedSinglePanels.Remove(panelType);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var panel in _loadedMultiplePanels[panelType])
|
||||
_factory.ReleasePanel(panel);
|
||||
|
||||
_loadedMultiplePanels.Remove(panelType);
|
||||
}
|
||||
}
|
||||
|
||||
public UIPanel TryGetPanel(UIPanelType panelType) => _loadedPanels.GetValueOrDefault(panelType);
|
||||
private void RemovePanel(UIPanel panel)
|
||||
{
|
||||
_factory.ReleasePanel(panel);
|
||||
}
|
||||
|
||||
public UIPanel TryGetSinglePanel(UIPanelType panelType) => _loadedSinglePanels.GetValueOrDefault(panelType);
|
||||
public List<UIPanel> TryGetMultiplePanels(UIPanelType panelType) => _loadedMultiplePanels.GetValueOrDefault(panelType);
|
||||
}
|
||||
|
||||
public interface IUIPanelLoaded : IGlobalSubscriber
|
||||
{
|
||||
void UILoaded(UIPanelType uiPanelType);
|
||||
void UISinglePanelLoaded(UIPanelType uiPanelType);
|
||||
void UIMultiplePanelLoaded(UIPanelType uiPanelType, UIPanel panel);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user