76 lines
2.4 KiB
C#
76 lines
2.4 KiB
C#
using System;
|
|
using GameCore;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UniRx;
|
|
using asap.core;
|
|
public class TurntableAdButton : MonoBehaviour
|
|
{
|
|
[SerializeField] private Button _btnAd;
|
|
[SerializeField] private TMP_Text _textTime, _textFree;
|
|
private TurntableAdPopupPanel _adPanel;
|
|
private IDisposable _timer;
|
|
private PlayerShopData _playerShopData;
|
|
private void Awake()
|
|
{
|
|
_playerShopData = GContext.container.Resolve<PlayerShopData>();
|
|
}
|
|
private void Start()
|
|
{
|
|
_btnAd.onClick.AddListener(async () => await OpenAddPanel());
|
|
InitializeButton();
|
|
}
|
|
private void InitializeButton()
|
|
{
|
|
DateTime nextAdTime = _playerShopData.GetTurnTableNextAdTime();
|
|
TimeSpan time = nextAdTime - ZZTimeHelper.UtcNow().UtcNowOffset();
|
|
if (_playerShopData.IsTurntableAdMaxOut())
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
if (time.TotalSeconds > 1)
|
|
{
|
|
SetTimer();
|
|
_textTime.gameObject.SetActive(true);
|
|
_textFree.gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
_textTime.gameObject.SetActive(false);
|
|
_textFree.gameObject.SetActive(true);
|
|
}
|
|
|
|
}
|
|
private async System.Threading.Tasks.Task OpenAddPanel()
|
|
{
|
|
_adPanel = (await UIManager.Instance.ShowUI(UITypes.TurntableAdPopupPanel))
|
|
.GetComponent<TurntableAdPopupPanel>();
|
|
_adPanel.OnClosePanel += OnAdPanelClose;
|
|
}
|
|
private void SetTimer()
|
|
{
|
|
_timer?.Dispose();
|
|
DateTime nextAdTime = DateTime.Parse(_playerShopData.TurntableAdData.time);
|
|
TimeSpan remainingTime = nextAdTime - ZZTimeHelper.UtcNow().UtcNowOffset();
|
|
_textTime.text = ConvertTools.ConvertTime2(remainingTime);
|
|
_timer = Observable.Interval(TimeSpan.FromSeconds(1.0f))
|
|
.Subscribe( _ =>{
|
|
TimeSpan remainingTime = nextAdTime - ZZTimeHelper.UtcNow().UtcNowOffset();
|
|
_textTime.text = ConvertTools.ConvertTime2(remainingTime);
|
|
if (remainingTime.TotalSeconds <= 1f)
|
|
{
|
|
_textTime.gameObject.SetActive(false);
|
|
_textFree.gameObject.SetActive(true);
|
|
}
|
|
});
|
|
}
|
|
private void OnAdPanelClose()
|
|
{
|
|
InitializeButton();
|
|
if (_playerShopData.IsTurntableAdMaxOut())
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
} |