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

156 lines
4.7 KiB
C#

using asap.core;
using GameCore;
using System;
using TMPro;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
public class MailSlot : MonoBehaviour
{
public TMP_Text
text_name,
text_info,
text_expireTime,
text_time;
Head btn_head;
GameObject
go_normal,
go_normalDone,
go_reward,
go_rewardDone,
go_bg,
go_bg_done;
Button btn;
GameMail data;
private static TimeSpan _MaxExpireTime = TimeSpan.FromDays(400);
private static TimeSpan _CountDownTime = TimeSpan.FromDays(1);
private void Awake()
{
text_name = transform.Find("info/text_name").GetComponent<TMP_Text>();
text_info = transform.Find("info/text_info").GetComponent<TMP_Text>();
text_time = transform.Find("text_time").GetComponent<TMP_Text>();
text_expireTime= transform.Find("text_overdue").GetComponent<TMP_Text>();
btn_head = transform.Find("btn_head").GetComponent<Head>();
go_normal = transform.Find("icon_mail").gameObject;
go_normalDone = transform.Find("icon_mail_done").gameObject;
go_reward = transform.Find("icon_gift").gameObject;
go_rewardDone = transform.Find("icon_gift_done").gameObject;
go_bg = transform.Find("bg").gameObject;
go_bg_done = transform.Find("bg_done").gameObject;
btn=transform.transform.Find("btn").GetComponent<Button>();
btn_head.gameObject.SetActive(false);
}
private void Start()
{
btn.OnClickAsObservable().Subscribe(_ => OnClick()).AddTo(this);
}
async void OnClick()
{
bool canOpen = false;
if(!data.IsRead)
{
UIManager.ShowWaitingBlock("MailSlot::OnClick");
var mailService = GContext.container.Resolve<IInGameMailService>();
if (await mailService.MarkRead(data))
{
SetState();
canOpen = true;
}
UIManager.HideWaitingBlock("MailSlot::OnClick");
}
else
{
canOpen = true;
}
if(canOpen)
{
var mailInfoPanel = await UIManager.Instance.ShowUI(UITypes.FishingMailInfoPopupPanel);
mailInfoPanel.GetComponent<FishingMailInfoPopupPanel>().SetData(data, refreshMailList);
}
}
private Action refreshMailList;
public void SetData(GameMail data, Action refreshMailList)
{
this.refreshMailList = refreshMailList;
this.data = data;
gameObject.SetActive(true);
text_name.text = data.Title;
SetState();
SetTimer();
}
private void SetTimer()
{
var utcNowOffset = ZZTimeHelper.UtcNow();
text_time.text = LocalizationMgr.GetFormatTextValue("UI_FishingMailPopupPanel_6", ConvertTools.ConvertTime3(utcNowOffset - data.SendTime));
var expireTime = data.ExpireTime - utcNowOffset;
if (expireTime > _MaxExpireTime)
text_expireTime.text = string.Empty;
else
text_expireTime.text = LocalizationMgr.GetFormatTextValue("UI_FishingMailPopupPanel_5", ConvertTools.ConvertTime3(expireTime));
if(expireTime > _CountDownTime)
{
Observable.Interval(TimeSpan.FromSeconds(1))
.TakeWhile( _=>data.ExpireTime>= utcNowOffset)
.Subscribe(_ => {
text_time.text = LocalizationMgr.GetFormatTextValue("UI_FishingMailPopupPanel_6", ConvertTools.ConvertTime3(utcNowOffset - data.SendTime));
text_expireTime.text = LocalizationMgr.GetFormatTextValue("UI_FishingMailPopupPanel_5", ConvertTools.ConvertTime3(data.ExpireTime - utcNowOffset));
},
_=>{
gameObject.SetActive(false);
}).AddTo(this);
}
}
private void SetState()
{
if (data.IsDeleted)
{
gameObject.SetActive(false);
}
else
{
gameObject.SetActive(true);
if (data.HasDrops)
{
go_normal.SetActive(false);
go_normalDone.SetActive(false);
go_reward.SetActive(!data.HasReward);
go_rewardDone.SetActive(data.HasReward);
go_bg.SetActive(!data.HasReward);
go_bg_done.SetActive(data.HasReward);
}
else
{
go_reward.SetActive(false);
go_rewardDone.SetActive(false);
go_normal.SetActive(!data.IsRead);
go_normalDone.SetActive(data.IsRead);
go_bg.SetActive(!data.IsRead);
go_bg_done.SetActive(data.IsRead);
}
}
}
}