126 lines
4.5 KiB
C#
126 lines
4.5 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
|
|
namespace DDD
|
|
{
|
|
public class TodayMenuView : MonoBehaviour, IEventHandler<TodayMenuAddedEvent>, IEventHandler<TodayMenuRemovedEvent>
|
|
{
|
|
[SerializeField] private Transform _todayFoodContent;
|
|
[SerializeField] private Transform _todayDrinkContent;
|
|
|
|
private List<ItemSlotUi> _foodSlots;
|
|
private List<ItemSlotUi> _drinkSlots;
|
|
|
|
private RestaurantManagementSo _restaurantManagementSo;
|
|
|
|
private void Start()
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
EventBus.Unregister<TodayMenuAddedEvent>(this);
|
|
EventBus.Unregister<TodayMenuRemovedEvent>(this);
|
|
}
|
|
|
|
private async void Initialize()
|
|
{
|
|
_restaurantManagementSo = await AssetManager.LoadAsset<RestaurantManagementSo>(DataConstants.RestaurantManagementSo);
|
|
Debug.Assert(_restaurantManagementSo != null, "_restaurantManagementSo != null");
|
|
|
|
foreach (Transform child in _todayFoodContent)
|
|
{
|
|
Destroy(child.gameObject);
|
|
}
|
|
|
|
int maxFoodCount = _restaurantManagementSo.MaxFoodCount;
|
|
_foodSlots = new List<ItemSlotUi>(maxFoodCount);
|
|
for (int i = 0; i < _restaurantManagementSo.MaxFoodCount; i++)
|
|
{
|
|
var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayFoodContent);
|
|
var slot = go.GetComponent<ItemSlotUi>();
|
|
await slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
|
|
var todayMenuInteractor = go.GetComponent<TodayMenuInteractor>();
|
|
todayMenuInteractor.Initialize(TodayMenuEventType.Remove);
|
|
|
|
_foodSlots.Add(slot);
|
|
}
|
|
|
|
foreach (Transform child in _todayDrinkContent)
|
|
{
|
|
Destroy(child.gameObject);
|
|
}
|
|
|
|
int maxDrinkCount = _restaurantManagementSo.MaxDrinkCount;
|
|
_drinkSlots = new List<ItemSlotUi>(maxDrinkCount);
|
|
for (int i = 0; i < _restaurantManagementSo.MaxDrinkCount; i++)
|
|
{
|
|
var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayDrinkContent);
|
|
var slot = go.GetComponent<ItemSlotUi>();
|
|
await slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
|
|
var todayMenuInteractor = go.GetComponent<TodayMenuInteractor>();
|
|
todayMenuInteractor.Initialize(TodayMenuEventType.Remove);
|
|
|
|
_drinkSlots.Add(slot);
|
|
}
|
|
|
|
UpdateView();
|
|
|
|
EventBus.Register<TodayMenuAddedEvent>(this);
|
|
EventBus.Register<TodayMenuRemovedEvent>(this);
|
|
}
|
|
|
|
public void Invoke(TodayMenuAddedEvent evt)
|
|
{
|
|
UpdateView();
|
|
}
|
|
|
|
public void Invoke(TodayMenuRemovedEvent evt)
|
|
{
|
|
UpdateView();
|
|
}
|
|
|
|
private void UpdateView()
|
|
{
|
|
int foodIndex = 0;
|
|
foreach (var kvp in _restaurantManagementSo.FoodRecipeIds)
|
|
{
|
|
if (foodIndex >= _foodSlots.Count) break;
|
|
|
|
string recipeId = kvp.Key;
|
|
|
|
var model = ItemViewModelFactory.CreateByRecipeId(recipeId);
|
|
var foodSlot = _foodSlots[foodIndex];
|
|
_ = foodSlot.Initialize(model, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
|
|
foodSlot.SetCount(kvp.Value);
|
|
foodIndex++;
|
|
}
|
|
|
|
for (int i = foodIndex; i < _foodSlots.Count; i++)
|
|
{
|
|
_ = _foodSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
|
|
}
|
|
|
|
int drinkIndex = 0;
|
|
foreach (var kvp in _restaurantManagementSo.DrinkRecipeIds)
|
|
{
|
|
if (drinkIndex >= _drinkSlots.Count) break;
|
|
|
|
string recipeId = kvp.Key;
|
|
|
|
var model = ItemViewModelFactory.CreateByRecipeId(recipeId);
|
|
var drinkSlot = _drinkSlots[drinkIndex];
|
|
_ = drinkSlot.Initialize(model, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
|
|
drinkSlot.SetCount(kvp.Value);
|
|
drinkIndex++;
|
|
}
|
|
|
|
for (int i = drinkIndex; i < _drinkSlots.Count; i++)
|
|
{
|
|
_ = _drinkSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
|
|
}
|
|
}
|
|
}
|
|
} |