ProjectDDD/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuView.cs

127 lines
4.5 KiB
C#
Raw Normal View History

2025-07-25 07:58:53 +00:00
using System.Collections.Generic;
using System.Threading.Tasks;
2025-07-25 07:58:53 +00:00
using UnityEngine;
2025-07-29 18:18:16 +00:00
using UnityEngine.AddressableAssets;
2025-07-25 07:58:53 +00:00
namespace DDD
{
2025-07-27 19:32:41 +00:00
public class TodayMenuView : MonoBehaviour, IEventHandler<TodayMenuAddedEvent>, IEventHandler<TodayMenuRemovedEvent>
2025-07-25 07:58:53 +00:00
{
[SerializeField] private Transform _todayFoodContent;
[SerializeField] private Transform _todayDrinkContent;
2025-07-27 19:32:41 +00:00
2025-07-29 15:56:47 +00:00
private List<ItemSlotUi> _foodSlots;
private List<ItemSlotUi> _drinkSlots;
2025-07-25 07:58:53 +00:00
2025-07-27 19:51:11 +00:00
private RestaurantManagementSo _restaurantManagementSo;
2025-07-25 07:58:53 +00:00
2025-07-29 15:56:47 +00:00
private void Start()
2025-07-25 07:58:53 +00:00
{
_ = Initialize();
2025-07-29 15:56:47 +00:00
}
private void OnDestroy()
{
EventBus.Unregister<TodayMenuAddedEvent>(this);
EventBus.Unregister<TodayMenuRemovedEvent>(this);
}
2025-07-27 19:32:41 +00:00
private async Task Initialize()
2025-07-29 15:56:47 +00:00
{
2025-07-27 19:51:11 +00:00
_restaurantManagementSo = await AssetManager.LoadAsset<RestaurantManagementSo>(DataConstants.RestaurantManagementSo);
2025-07-29 15:56:47 +00:00
Debug.Assert(_restaurantManagementSo != null, "_restaurantManagementSo != null");
2025-07-25 07:58:53 +00:00
foreach (Transform child in _todayFoodContent)
{
Destroy(child.gameObject);
}
2025-07-27 19:51:11 +00:00
int maxFoodCount = _restaurantManagementSo.MaxFoodCount;
2025-07-29 15:56:47 +00:00
_foodSlots = new List<ItemSlotUi>(maxFoodCount);
2025-07-27 19:51:11 +00:00
for (int i = 0; i < _restaurantManagementSo.MaxFoodCount; i++)
2025-07-25 07:58:53 +00:00
{
2025-07-27 22:55:07 +00:00
var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayFoodContent);
2025-07-29 15:56:47 +00:00
var slot = go.GetComponent<ItemSlotUi>();
2025-07-29 18:52:13 +00:00
await slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
2025-07-27 19:32:41 +00:00
var todayMenuInteractor = go.GetComponent<TodayMenuInteractor>();
todayMenuInteractor.Initialize(TodayMenuEventType.Remove);
2025-07-25 07:58:53 +00:00
_foodSlots.Add(slot);
}
2025-07-27 19:32:41 +00:00
2025-07-25 07:58:53 +00:00
foreach (Transform child in _todayDrinkContent)
{
Destroy(child.gameObject);
}
2025-07-27 19:32:41 +00:00
2025-07-27 19:51:11 +00:00
int maxDrinkCount = _restaurantManagementSo.MaxDrinkCount;
2025-07-29 15:56:47 +00:00
_drinkSlots = new List<ItemSlotUi>(maxDrinkCount);
2025-07-27 19:51:11 +00:00
for (int i = 0; i < _restaurantManagementSo.MaxDrinkCount; i++)
2025-07-25 07:58:53 +00:00
{
2025-07-27 22:55:07 +00:00
var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayDrinkContent);
2025-07-29 15:56:47 +00:00
var slot = go.GetComponent<ItemSlotUi>();
2025-07-29 18:52:13 +00:00
await slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
2025-07-27 19:32:41 +00:00
var todayMenuInteractor = go.GetComponent<TodayMenuInteractor>();
todayMenuInteractor.Initialize(TodayMenuEventType.Remove);
2025-07-25 07:58:53 +00:00
_drinkSlots.Add(slot);
}
2025-07-27 19:32:41 +00:00
UpdateView();
2025-07-29 15:56:47 +00:00
EventBus.Register<TodayMenuAddedEvent>(this);
EventBus.Register<TodayMenuRemovedEvent>(this);
2025-07-27 19:32:41 +00:00
}
public void Invoke(TodayMenuAddedEvent evt)
{
UpdateView();
2025-07-25 07:58:53 +00:00
}
2025-07-27 19:32:41 +00:00
public void Invoke(TodayMenuRemovedEvent evt)
{
UpdateView();
}
2025-07-25 07:58:53 +00:00
2025-07-27 19:32:41 +00:00
private void UpdateView()
2025-07-25 07:58:53 +00:00
{
2025-07-28 11:33:04 +00:00
int foodIndex = 0;
foreach (var kvp in _restaurantManagementSo.FoodRecipeIds)
2025-07-25 07:58:53 +00:00
{
2025-07-28 11:33:04 +00:00
if (foodIndex >= _foodSlots.Count) break;
string recipeId = kvp.Key;
var model = ItemViewModelFactory.CreateByRecipeId(recipeId);
2025-07-29 15:56:47 +00:00
var foodSlot = _foodSlots[foodIndex];
2025-07-29 18:52:13 +00:00
_ = foodSlot.Initialize(model, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
2025-07-29 15:56:47 +00:00
foodSlot.SetCount(kvp.Value);
2025-07-28 11:33:04 +00:00
foodIndex++;
}
for (int i = foodIndex; i < _foodSlots.Count; i++)
{
2025-07-29 18:52:13 +00:00
_ = _foodSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
2025-07-25 07:58:53 +00:00
}
2025-07-28 11:33:04 +00:00
int drinkIndex = 0;
foreach (var kvp in _restaurantManagementSo.DrinkRecipeIds)
{
if (drinkIndex >= _drinkSlots.Count) break;
string recipeId = kvp.Key;
var model = ItemViewModelFactory.CreateByRecipeId(recipeId);
2025-07-29 15:56:47 +00:00
var drinkSlot = _drinkSlots[drinkIndex];
2025-07-29 18:52:13 +00:00
_ = drinkSlot.Initialize(model, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
2025-07-29 15:56:47 +00:00
drinkSlot.SetCount(kvp.Value);
2025-07-28 11:33:04 +00:00
drinkIndex++;
}
for (int i = drinkIndex; i < _drinkSlots.Count; i++)
2025-07-25 07:58:53 +00:00
{
2025-07-29 18:52:13 +00:00
_ = _drinkSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
2025-07-25 07:58:53 +00:00
}
}
}
}