From bb0d4fd723398f9de278ee750546860d3e95cc59 Mon Sep 17 00:00:00 2001 From: Cinka Date: Sun, 12 Jan 2025 15:15:01 +0300 Subject: [PATCH] - tweak: open content --- .../ViewModels/ContentBrowserViewModel.cs | 72 +++++++++++++++---- .../ViewModels/ExceptionViewModel.cs | 1 + .../ViewModels/InfoPopupViewModel.cs | 1 + .../ViewModels/LoadingContextViewModel.cs | 1 + Nebula.Launcher/ViewModels/MainViewModel.cs | 5 +- .../ViewModels/PopupViewModelBase.cs | 3 +- .../ViewModels/ServerEntryModelView.cs | 1 + .../ViewModels/ServerListViewModel.cs | 27 +++++-- Nebula.Launcher/Views/MainView.axaml | 1 + Nebula.Shared/Services/FileService.cs | 12 ++-- Nebula.Shared/Services/HubService.cs | 9 +++ 11 files changed, 106 insertions(+), 27 deletions(-) diff --git a/Nebula.Launcher/ViewModels/ContentBrowserViewModel.cs b/Nebula.Launcher/ViewModels/ContentBrowserViewModel.cs index bce3d68..5737321 100644 --- a/Nebula.Launcher/ViewModels/ContentBrowserViewModel.cs +++ b/Nebula.Launcher/ViewModels/ContentBrowserViewModel.cs @@ -2,7 +2,9 @@ using System; using System.Collections.Frozen; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Linq; using System.Threading.Tasks; using Avalonia.Media; @@ -25,6 +27,7 @@ public sealed partial class ContentBrowserViewModel : ViewModelBase private readonly IServiceProvider _provider; private readonly ContentService _contentService; private readonly CancellationService _cancellationService; + private readonly FileService _fileService; private readonly DebugService _debugService; private readonly PopupMessageService _popupService; public ObservableCollection Entries { get; } = new(); @@ -42,14 +45,36 @@ public sealed partial class ContentBrowserViewModel : ViewModelBase get => _selectedEntry; set { - _selectedEntry = value; + if (value is { Item: not null }) + { + if (_fileService.ContentFileApi.TryOpen(value.Item.Value.Hash, out var stream)) + { + var ext = Path.GetExtension(value.Item.Value.Path); + + var myTempFile = Path.Combine(Path.GetTempPath(), "tempie"+ ext); + + using(var sw = new FileStream(myTempFile, FileMode.Create, FileAccess.Write, FileShare.None)) + { + stream.CopyTo(sw); + } + stream.Dispose(); + + var startInfo = new ProcessStartInfo(myTempFile) + { + UseShellExecute = true + }; + + Process.Start(startInfo); + } + return; + } + Entries.Clear(); + _selectedEntry = value; + + if (value == null) return; - Console.WriteLine("Entries clear!"); - - if(value == null) return; - - foreach (var (_,entryCh) in value.Childs) + foreach (var (_, entryCh) in value.Childs) { Entries.Add(entryCh); } @@ -71,11 +96,22 @@ public sealed partial class ContentBrowserViewModel : ViewModelBase _provider = provider; _contentService = contentService; _cancellationService = cancellationService; + _fileService = fileService; _debugService = debugService; _popupService = popupService; + + foreach (var info in hubService.ServerList) + { + _root.Add(new ContentEntry(this, ToContentUrl(info.Address.ToRobustUrl()),info.Address)); + } hubService.HubServerChangedEventArgs += HubServerChangedEventArgs; hubService.HubServerLoaded += GoHome; + + if (!hubService.IsUpdating) + { + GoHome(); + } } private void GoHome() @@ -99,9 +135,9 @@ public sealed partial class ContentBrowserViewModel : ViewModelBase }; } - public async void Go(ContentPath path) + public async void Go(ContentPath path, bool appendHistory = true) { - if (path.Pathes.Count == 0) + if (path.Pathes.Count <= 1) { SearchText = ""; GoHome(); @@ -112,8 +148,8 @@ public sealed partial class ContentBrowserViewModel : ViewModelBase { return; } - - SearchText = path.Path; + + var oriPath = path.Clone(); path.Pathes.RemoveAt(0); path.Pathes.RemoveAt(0); @@ -136,21 +172,26 @@ public sealed partial class ContentBrowserViewModel : ViewModelBase throw new Exception("Not found!"); } - SelectedEntry = centry; + if (appendHistory) + { + AppendHistory(SearchText); + } + SearchText = oriPath.Path; + SelectedEntry = centry; } catch (Exception e) { Console.WriteLine(e); + SearchText = oriPath.Path; _popupService.Popup(e); - //throw; } } public void OnBackEnter() { - Go(new ContentPath(GetHistory())); + Go(new ContentPath(GetHistory()), false); } public void OnGoEnter() @@ -347,5 +388,10 @@ public struct ContentPath return Pathes.Last(); } + public ContentPath Clone() + { + return new ContentPath(Pathes.ToList()); + } + public string Path => string.Join("/", Pathes); } diff --git a/Nebula.Launcher/ViewModels/ExceptionViewModel.cs b/Nebula.Launcher/ViewModels/ExceptionViewModel.cs index eed2337..f1d5d06 100644 --- a/Nebula.Launcher/ViewModels/ExceptionViewModel.cs +++ b/Nebula.Launcher/ViewModels/ExceptionViewModel.cs @@ -18,6 +18,7 @@ public class ExceptionViewModel : PopupViewModelBase public ExceptionViewModel(IServiceProvider serviceProvider) : base(serviceProvider){} public override string Title => "Oopsie! Some shit is happened now!"; + public override bool IsClosable => true; public ObservableCollection Errors { get; } = new(); diff --git a/Nebula.Launcher/ViewModels/InfoPopupViewModel.cs b/Nebula.Launcher/ViewModels/InfoPopupViewModel.cs index 6e36b5e..7ded051 100644 --- a/Nebula.Launcher/ViewModels/InfoPopupViewModel.cs +++ b/Nebula.Launcher/ViewModels/InfoPopupViewModel.cs @@ -17,6 +17,7 @@ public partial class InfoPopupViewModel : PopupViewModelBase } public override string Title => "Info"; + public override bool IsClosable => true; [ObservableProperty] private string _infoText = "Test"; diff --git a/Nebula.Launcher/ViewModels/LoadingContextViewModel.cs b/Nebula.Launcher/ViewModels/LoadingContextViewModel.cs index 69a4fdd..5dd981d 100644 --- a/Nebula.Launcher/ViewModels/LoadingContextViewModel.cs +++ b/Nebula.Launcher/ViewModels/LoadingContextViewModel.cs @@ -13,6 +13,7 @@ public sealed partial class LoadingContextViewModel : PopupViewModelBase, ILoadi public LoadingContextViewModel(IServiceProvider provider) : base(provider){} public string LoadingName { get; set; } = "Loading..."; + public override bool IsClosable => false; public override string Title => LoadingName; diff --git a/Nebula.Launcher/ViewModels/MainViewModel.cs b/Nebula.Launcher/ViewModels/MainViewModel.cs index 6f0547a..afdb140 100644 --- a/Nebula.Launcher/ViewModels/MainViewModel.cs +++ b/Nebula.Launcher/ViewModels/MainViewModel.cs @@ -46,7 +46,7 @@ public partial class MainViewModel : ViewModelBase [ new ListItemTemplate(typeof(AccountInfoViewModel), "Account", "Account"), new ListItemTemplate(typeof(ServerListViewModel), "HomeRegular", "Servers"), - new ListItemTemplate(typeof(ContentBrowserViewModel), "HomeRegular", "Content") + new ListItemTemplate(typeof(ContentBrowserViewModel), "GridRegular", "Content") ]; [ObservableProperty] @@ -65,6 +65,8 @@ public partial class MainViewModel : ViewModelBase [ObservableProperty] private string _currentTitle = "Default"; + [ObservableProperty] private bool _isPopupClosable = true; + [ObservableProperty] private ListItemTemplate? _selectedListItem; @@ -88,6 +90,7 @@ public partial class MainViewModel : ViewModelBase { CurrentPopup = viewModelBase; CurrentTitle = viewModelBase.Title; + IsPopupClosable = viewModelBase.IsClosable; OnOpenRequired(); } else diff --git a/Nebula.Launcher/ViewModels/PopupViewModelBase.cs b/Nebula.Launcher/ViewModels/PopupViewModelBase.cs index cac33a6..d3f9509 100644 --- a/Nebula.Launcher/ViewModels/PopupViewModelBase.cs +++ b/Nebula.Launcher/ViewModels/PopupViewModelBase.cs @@ -7,6 +7,8 @@ namespace Nebula.Launcher.ViewModels; public abstract class PopupViewModelBase : ViewModelBase, IDisposable { private readonly IServiceProvider _serviceProvider; + public abstract string Title { get; } + public abstract bool IsClosable { get; } public PopupViewModelBase() { @@ -17,7 +19,6 @@ public abstract class PopupViewModelBase : ViewModelBase, IDisposable _serviceProvider = serviceProvider; } - public abstract string Title { get; } public void Dispose() { _serviceProvider.GetService()?.ClosePopup(this); diff --git a/Nebula.Launcher/ViewModels/ServerEntryModelView.cs b/Nebula.Launcher/ViewModels/ServerEntryModelView.cs index 5419db8..16f7cdb 100644 --- a/Nebula.Launcher/ViewModels/ServerEntryModelView.cs +++ b/Nebula.Launcher/ViewModels/ServerEntryModelView.cs @@ -265,6 +265,7 @@ public sealed class LogPopupModelView : PopupViewModelBase } public override string Title => "LOG"; + public override bool IsClosable => true; public ObservableCollection Logs { get; } = new(); diff --git a/Nebula.Launcher/ViewModels/ServerListViewModel.cs b/Nebula.Launcher/ViewModels/ServerListViewModel.cs index afe00c5..7f7f41b 100644 --- a/Nebula.Launcher/ViewModels/ServerListViewModel.cs +++ b/Nebula.Launcher/ViewModels/ServerListViewModel.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; using Nebula.Launcher.ViewHelper; using Nebula.Launcher.Views.Pages; @@ -33,9 +34,20 @@ public partial class ServerListViewModel : ViewModelBase { _serviceProvider = serviceProvider; _hubService = hubService; + + foreach (var info in _hubService.ServerList) + { + UnsortedServers.Add(info); + } + hubService.HubServerChangedEventArgs += HubServerChangedEventArgs; hubService.HubServerLoaded += HubServerLoaded; OnSearchChange += OnChangeSearch; + + if (!hubService.IsUpdating) + { + SortServers(); + } } private void HubServerLoaded() @@ -72,12 +84,15 @@ public partial class ServerListViewModel : ViewModelBase private void SortServers() { - ServerInfos.Clear(); - UnsortedServers.Sort(new ServerComparer()); - foreach (var server in UnsortedServers.Where(CheckServerThink)) + Task.Run(() => { - ServerInfos.Add(CreateServerView(server)); - } + ServerInfos.Clear(); + UnsortedServers.Sort(new ServerComparer()); + foreach (var server in UnsortedServers.Where(CheckServerThink)) + { + ServerInfos.Add(CreateServerView(server)); + } + }); } private bool CheckServerThink(ServerHubInfo hubInfo) @@ -100,7 +115,7 @@ public partial class ServerListViewModel : ViewModelBase public void UpdateRequired() { - _hubService.UpdateHub(); + Task.Run(_hubService.UpdateHub); } } diff --git a/Nebula.Launcher/Views/MainView.axaml b/Nebula.Launcher/Views/MainView.axaml index d9f9b67..fe6e17e 100644 --- a/Nebula.Launcher/Views/MainView.axaml +++ b/Nebula.Launcher/Views/MainView.axaml @@ -110,6 +110,7 @@