mirror of
https://onedev.site.tesses.net/crosslang/crosslangdevstudio
synced 2026-02-08 09:15:45 +00:00
New dev build
This commit is contained in:
88
ViewModels/InstallPackageDialogViewModel.cs
Normal file
88
ViewModels/InstallPackageDialogViewModel.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using Avalonia.Markup.Xaml.Templates;
|
||||
using Avalonia.Media;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using CrossLangDevStudio.Messages;
|
||||
using CrossLangDevStudio.Views;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CrossLangDevStudio.ViewModels;
|
||||
|
||||
public partial class InstallPackageDialogViewModel : ViewModelBase
|
||||
{
|
||||
public InstallPackageMessage Message { get; }
|
||||
[ObservableProperty]
|
||||
private int _versionIndex = 0;
|
||||
public ObservableCollection<VersionObject> Versions { get; } = new ObservableCollection<VersionObject>();
|
||||
|
||||
public InstallPackageDialogViewModel(InstallPackageMessage msg)
|
||||
{
|
||||
Message = msg;
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
var res = await Message.Client.GetStringAsync($"{msg.Server.TrimEnd('/')}/api/v1/versions?name={HttpUtility.UrlEncode(msg.Name)}");
|
||||
if (!string.IsNullOrWhiteSpace(res))
|
||||
{
|
||||
var res2 = JsonConvert.DeserializeObject<VersionsMessage>(res);
|
||||
if (res2 is not null && res2.Success)
|
||||
{
|
||||
foreach (var res3 in res2.Versions)
|
||||
{
|
||||
Versions.Add(res3.ToObject());
|
||||
}
|
||||
VersionIndex = 0;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
[RelayCommand]
|
||||
private void Install()
|
||||
{
|
||||
if(VersionIndex < Versions.Count)
|
||||
WeakReferenceMessenger.Default.Send(new InstallPackageResponseMessage(Versions[VersionIndex].Version));
|
||||
}
|
||||
[RelayCommand]
|
||||
private void Cancel()
|
||||
{
|
||||
WeakReferenceMessenger.Default.Send(new InstallPackageResponseMessage(null));
|
||||
}
|
||||
public partial class VersionObject(CrossLangVersion version, DateTime date) : ObservableObject
|
||||
{
|
||||
public CrossLangVersion Version => version;
|
||||
[ObservableProperty]
|
||||
private string _text = $"{version} at {date.ToShortDateString()}";
|
||||
}
|
||||
|
||||
public class VersionsMessage
|
||||
{
|
||||
[JsonProperty("success")]
|
||||
public bool Success { get; set; }
|
||||
[JsonProperty("versions")]
|
||||
public List<VersionMessage> Versions { get; set; } = new();
|
||||
|
||||
public class VersionMessage
|
||||
{
|
||||
[JsonProperty("version")]
|
||||
public CrossLangVersion Version { get; set; }
|
||||
|
||||
[JsonProperty("uploadTime")]
|
||||
public long UploadTime { get; set; } = 0;
|
||||
|
||||
public VersionObject ToObject()
|
||||
{
|
||||
|
||||
return new VersionObject(Version, DateTimeOffset.FromUnixTimeSeconds(UploadTime).LocalDateTime);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user