169 lines
5.3 KiB
C#
169 lines
5.3 KiB
C#
using System.Collections.Generic;
|
|
using Runtime;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using ZGame;
|
|
|
|
namespace HK.FUJIFILM
|
|
{
|
|
public class ShoppingCartUI : UIBase
|
|
{
|
|
[SerializeField] private UpMenuItem goUpMenu;
|
|
[SerializeField] private ScrollRect scrCarts;
|
|
[SerializeField] private TMP_Text txtTotalQty;
|
|
[SerializeField] private TMP_Text txtSubTotal;
|
|
[SerializeField] private TMP_Text txtDiscount;
|
|
[SerializeField] private TMP_Text txtGrandTotal;
|
|
[SerializeField] private Button btnCheckout;
|
|
|
|
List<ShopCartItem> items = new List<ShopCartItem>();
|
|
|
|
public override void OnInit()
|
|
{
|
|
base.OnInit();
|
|
|
|
#region AutoGen_Init
|
|
|
|
goUpMenu = GetValue<UpMenuItem>("goUpMenu");
|
|
scrCarts = GetValue<ScrollRect>("scrCarts");
|
|
txtTotalQty = GetValue<TMP_Text>("txtTotalQty");
|
|
txtSubTotal = GetValue<TMP_Text>("txtSubTotal");
|
|
txtDiscount = GetValue<TMP_Text>("txtDiscount");
|
|
txtGrandTotal = GetValue<TMP_Text>("txtGrandTotal");
|
|
btnCheckout = GetValue<Button>("btnCheckout");
|
|
|
|
scrCarts.onValueChanged.AddListener(OnValueChangedscrCarts);
|
|
btnCheckout.onClick.AddListener(OnClickbtnCheckout);
|
|
|
|
#endregion
|
|
|
|
goUpMenu.ReturnAction += ClickReturn;
|
|
EventManager.Instance.Subscribe(AddCommodityToShoppingCartEventArgs.EventId,
|
|
AddCommodityToShoppingCartEvent);
|
|
EventManager.Instance.Subscribe(ClearShoppingCartEventArgs.EventId, ClearShoppingCartEvent);
|
|
}
|
|
|
|
private void ClearShoppingCartEvent(object sender, GameEventArgs e)
|
|
{
|
|
foreach (var item in items)
|
|
{
|
|
GameObject.Destroy(item.gameObject);
|
|
}
|
|
|
|
items.Clear();
|
|
}
|
|
|
|
private void AddCommodityToShoppingCartEvent(object sender, GameEventArgs e)
|
|
{
|
|
var args = e as AddCommodityToShoppingCartEventArgs;
|
|
}
|
|
|
|
private void ClickReturn()
|
|
{
|
|
// UIManager.Instance.BackLast();
|
|
}
|
|
|
|
public override void OnOpen(UIBase lastUI)
|
|
{
|
|
base.OnOpen(lastUI);
|
|
goUpMenu.OnShow();
|
|
UpdateShoppingCart();
|
|
}
|
|
|
|
void UpdateShoppingCart()
|
|
{
|
|
foreach (var item in items)
|
|
{
|
|
GameObject.Destroy(item.gameObject);
|
|
}
|
|
|
|
items.Clear();
|
|
|
|
var shoppingCart = ShoppingCartManager.Instance.ShoppingCart;
|
|
|
|
foreach (var cartData in shoppingCart.datas.Values)
|
|
{
|
|
Gen(cartData);
|
|
}
|
|
|
|
txtTotalQty.text = $"{shoppingCart.totalQuantity}";
|
|
txtSubTotal.text = $"{shoppingCart.itemsSubTotal}";
|
|
txtDiscount.text = $"{shoppingCart.discount}";
|
|
txtGrandTotal.text = $"{shoppingCart.totalAmount}";
|
|
}
|
|
|
|
void OnlyUpdateMsg()
|
|
{
|
|
var shoppingCart = ShoppingCartManager.Instance.ShoppingCart;
|
|
|
|
txtTotalQty.text = $"{shoppingCart.totalQuantity}";
|
|
txtSubTotal.text = $"{shoppingCart.itemsSubTotal}";
|
|
txtDiscount.text = $"{shoppingCart.discount}";
|
|
txtGrandTotal.text = $"{shoppingCart.totalAmount}";
|
|
}
|
|
|
|
void Gen(ShoppingCartData shoppingCartData)
|
|
{
|
|
var productScriptableObject = ShoppingCartManager.Instance.GetProduct(shoppingCartData);
|
|
|
|
string assetPath = null;
|
|
assetPath = string.IsNullOrEmpty(productScriptableObject.productShopCartItemPfbPath)
|
|
? AssetConst.Assets_Res_FUJIFILM_Prefabs_UI_Item_ShopCartItem_prefab
|
|
: productScriptableObject.productShopCartItemPfbPath;
|
|
|
|
var go = ResourcesManager.Instance.LoadGameObject(assetPath, scrCarts.content);
|
|
var item = go.GetComponent<ShopCartItem>(); // ShopCartItem
|
|
item.SetData(shoppingCartData);
|
|
item.OnUpdateCallback += OnlyUpdateMsg;
|
|
item.OnDeleteCallback += OnlyUpdateMsg;
|
|
go.SetActive(true);
|
|
items.Add(item);
|
|
}
|
|
|
|
#region AutoGen_Method
|
|
|
|
private void OnValueChangedscrCarts(Vector2 v)
|
|
{
|
|
}
|
|
|
|
private void OnClickbtnCheckout()
|
|
{
|
|
if (ShoppingCartManager.Instance.ShoppingCart.datas.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
OnPause();
|
|
// UIManager.Instance.CloseAll();
|
|
UIManager.Instance.ShowUI(nameof(SubmitMessageUI), this);
|
|
}
|
|
|
|
#endregion
|
|
|
|
public override void OnDispose()
|
|
{
|
|
base.OnDispose();
|
|
|
|
EventManager.Instance.Unsubscribe(AddCommodityToShoppingCartEventArgs.EventId,
|
|
AddCommodityToShoppingCartEvent);
|
|
EventManager.Instance.Unsubscribe(ClearShoppingCartEventArgs.EventId, ClearShoppingCartEvent);
|
|
|
|
#region AutoGen_Dispose
|
|
|
|
scrCarts.onValueChanged.RemoveListener(OnValueChangedscrCarts);
|
|
btnCheckout.onClick.RemoveListener(OnClickbtnCheckout);
|
|
|
|
goUpMenu = null;
|
|
scrCarts = null;
|
|
txtTotalQty = null;
|
|
txtSubTotal = null;
|
|
txtDiscount = null;
|
|
txtGrandTotal = null;
|
|
btnCheckout = null;
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
} |