1
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using BITKit.UX;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.Apps
|
||||
{
|
||||
public class UXApplicationService : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private bool externalDownload;
|
||||
[SerializeField, SerializeReference, SubclassSelector]
|
||||
private IApplicationService applicationService;
|
||||
[SerializeField] private UIDocument document;
|
||||
|
||||
[UXBindPath("container")]
|
||||
private VisualElement _container;
|
||||
[UXBindPath("download-button")]
|
||||
private Button _downloadButton;
|
||||
[UXBindPath("cancel-download-button")]
|
||||
private Button _cancelDownloadButton;
|
||||
[UXBindPath("install-button")]
|
||||
private Button _installButton;
|
||||
[UXBindPath("latest-version-label")]
|
||||
private Label _latestVersionLabel;
|
||||
[UXBindPath("download-bar")]
|
||||
private ProgressBar _downloadBar;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
UXUtils.Inject(this);
|
||||
applicationService.OnLatestVersionCheck += OnLatestVersionCheck;
|
||||
applicationService.OnDownloadLatest+= OnDownloadLatest;
|
||||
applicationService.OnDownloadProgress += OnDownloadProgress;
|
||||
applicationService.OnDownloadComplete += OnDownloadComplete;
|
||||
|
||||
_downloadButton.clicked+=ConfirmDownload;
|
||||
_cancelDownloadButton.clicked+=OnCancelDownload;
|
||||
destroyCancellationToken.Register(Dispose);
|
||||
|
||||
_container.Entry("entry-container");
|
||||
}
|
||||
|
||||
private void Dispose()
|
||||
{
|
||||
applicationService.OnLatestVersionCheck -= OnLatestVersionCheck;
|
||||
applicationService.OnDownloadLatest-= OnDownloadLatest;
|
||||
applicationService.OnDownloadProgress -= OnDownloadProgress;
|
||||
applicationService.OnDownloadComplete -= OnDownloadComplete;
|
||||
}
|
||||
private void OnDownloadLatest(string obj)
|
||||
{
|
||||
_container.Entry("download-container");
|
||||
}
|
||||
|
||||
private void ConfirmDownload()
|
||||
{
|
||||
if (externalDownload)
|
||||
{
|
||||
Application.OpenURL(applicationService.DownloadLatestUrl);
|
||||
}
|
||||
else
|
||||
{
|
||||
applicationService.DownloadLatestVersionAsync().Forget();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDownloadComplete(string obj)
|
||||
{
|
||||
_container.Entry("complete-container");
|
||||
}
|
||||
|
||||
private void OnDownloadProgress(float obj)
|
||||
{
|
||||
//设置为百分比
|
||||
_downloadBar.value = obj;
|
||||
//保留两位数小数点
|
||||
_downloadBar.title = $"{obj * 100:0.00}%";
|
||||
}
|
||||
|
||||
private void OnLatestVersionCheck(string obj)
|
||||
{
|
||||
if (Application.version != obj)
|
||||
{
|
||||
_latestVersionLabel.text =obj;
|
||||
_container.Entry("entryDownload-container");
|
||||
}
|
||||
else
|
||||
{
|
||||
_container.Entry(null);
|
||||
}
|
||||
}
|
||||
private void OnCancelDownload()
|
||||
{
|
||||
_container.Entry(null);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user