Files
back_cantanBuilding/Assets/Scripts/UI/Social/ClubChooseHeirPopupPanel.cs
2026-05-26 16:15:54 +08:00

167 lines
4.9 KiB
C#

using asap.core;
using game;
using GameCore;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
public class ClubChooseHeirPopupPanel : MonoBehaviour
{
Button btn_close;
Button btn_sure;
public Transform content;
public GameObject contentPrefab;
ClubService clubService;
string selectId = "0";
List<HeirItem> heirItems = new List<HeirItem>();
HeirItem curHeirItem;
bool isEnable = true;
private void Awake()
{
clubService = GContext.container.Resolve<ClubService>();
btn_close = transform.Find("btn_close").GetComponent<Button>();
btn_sure = transform.Find("root/btn_sure/btn_green").GetComponent<Button>();
content = transform.Find("root/panel_list/ScrollView/Viewport/Content");
contentPrefab = transform.Find("root/panel_list/ScrollView/Viewport/Content/Item").gameObject;
contentPrefab.SetActive(false);
}
private void OnDestroy()
{
isEnable = false;
}
private void Start()
{
btn_sure.onClick.AddListener(OnSure);
btn_close.onClick.AddListener(ClosePanel);
btn_sure.gameObject.SetActive(false);
OnStart();
}
async void OnStart()
{
var datas = await GContext.container.Resolve<ClubService>().GetClubDetails(clubService.myClubInfo.id);
if (!isEnable)
{
return;
}
if (datas != null && datas.Count > 0)
{
IUserService userService = GContext.container.Resolve<IUserService>();
for (int i = 0; i < datas.Count; i++)
{
SocialInfoItemData clubPlayerInfoData = datas[i];
if (clubPlayerInfoData.playFabId == userService.UserId)
{
continue;
}
GameObject go = Instantiate(contentPrefab, content);
go.SetActive(true);
HeirItem HeirItem = go.AddComponent<HeirItem>();
HeirItem.OnClick = OnClickHeirItem;
HeirItem.SetData(clubPlayerInfoData);
heirItems.Add(HeirItem);
}
if (datas.Count > 1)
{
OnClickHeirItem(heirItems[0]);
}
}
btn_sure.gameObject.SetActive(true);
}
private void OnSure()
{
clubService.QuitClub(selectId);
ClosePanel();
}
void ClosePanel()
{
UIManager.Instance.DestroyUI(UITypes.ClubChooseHeirPopupPanel);
}
void OnClickHeirItem(HeirItem heir)
{
if (curHeirItem != null)
{
curHeirItem.SetSelect(false);
}
curHeirItem = heir;
curHeirItem.SetSelect(true);
selectId = heir.playFabId;
}
}
class HeirItem : MonoBehaviour
{
TMP_Text text_name;
Head head;
TMP_Text text_count;
Button btn_accept;
public string playFabId;
IDisposable disposable;
public Action<HeirItem> OnClick;
GameObject Checkmark;
private void Awake()
{
disposable = GContext.OnEvent<GetClubPlayInfoEvent>().Where(x => x.id == playFabId).Subscribe(SetAvatar);
btn_accept = transform.GetComponent<Button>();
text_name = transform.Find("text_name").GetComponent<TMP_Text>();
head = transform.Find("btn_head").GetComponent<Head>();
text_count = transform.Find("icon_rank/text_num").GetComponent<TMP_Text>();
Checkmark = transform.Find("btn_cherk/Ani_Container/Checkmark").gameObject;
SetSelect(false);
}
private void Start()
{
btn_accept.onClick.AddListener(() =>
{
OnClick?.Invoke(this);
});
}
public void SetSelect(bool active)
{
Checkmark.SetActive(active);
}
public void SetData(SocialInfoItemData data)
{
playFabId = data.playFabId;
IUserService userService = GContext.container.Resolve<IUserService>();
text_count.text = data.lv.ToString();
PlayInfo clubPlayInfo = userService.GetPlayInfo(playFabId);
if (clubPlayInfo != null)
{
text_name.text = clubPlayInfo.name;
head.SetData(clubPlayInfo.avatarUrl);
}
if (data.lv <= 1)
{
SetLv(data, userService);
}
}
async void SetLv(SocialInfoItemData curData, IUserService userService)
{
var (playId, lv) = await userService.GetPlayLv(playFabId);
curData.lv = lv;
if (playId == playFabId)
{
text_count.text = lv.ToString();
}
}
void SetAvatar(GetClubPlayInfoEvent getClubPlayInfoEvent)
{
if (getClubPlayInfoEvent.id == playFabId)
{
text_name.text = getClubPlayInfoEvent.name;
head.SetData(getClubPlayInfoEvent.avatarUrl);
}
}
private void OnDestroy()
{
playFabId = null;
disposable?.Dispose();
disposable = null;
}
}