Browse Source

server panels refactoring

Veloe 2 years ago
parent
commit
d548f5e9be

+ 21 - 60
VeloeMinecraftLauncher/ViewModels/MainWindowViewModel.cs

@@ -34,6 +34,8 @@ public class MainWindowViewModel : ViewModelBase
 {
     public MainWindowViewModel()
     {
+        _serverPanels = new();
+
         Task.Run(async () =>
         {
             try
@@ -164,19 +166,9 @@ public class MainWindowViewModel : ViewModelBase
 
                     _connection.Reconnecting += reconnecting;
                     _connection.Reconnected += reconnected;
-
-                    if (Avalonia.Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop && desktop.MainWindow is not null)
-                    {
-#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
-                        Dispatcher.UIThread.InvokeAsync(() =>
-                        {
-                            var stackpanel = desktop.MainWindow.GetControl<StackPanel>("ServersStackPanel");
-                            foreach (var server in serverNames)
-                                stackpanel.Children.Add(CreateServerPanel(server));
-                        });
-#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
-
-                    }
+                        
+                    foreach (var server in serverNames)
+                        ServerPanels.Add(CreateServerPanel(server));
 
                     await _connection.StartAsync();
 
@@ -273,25 +265,24 @@ public class MainWindowViewModel : ViewModelBase
     DownloadedVersion _startedVersion;
 
     ObservableCollection<DownloadedVersion> _downloadedVersions;
+    ObservableCollection<ServerPanelModel> _serverPanels;
 
-    public ObservableCollection<DownloadedVersion> DownloadedVersions { 
-        get => _downloadedVersions; 
-        set
-        {
-            this.RaiseAndSetIfChanged(ref _downloadedVersions, value);
-        }
+    public ObservableCollection<DownloadedVersion> DownloadedVersions 
+    {
+        get => _downloadedVersions;
+        set => this.RaiseAndSetIfChanged(ref _downloadedVersions, value);
+    }
+
+    public ObservableCollection<ServerPanelModel> ServerPanels
+    {
+        get => _serverPanels;
+        set => this.RaiseAndSetIfChanged(ref _serverPanels, value);
     }
 
     public DownloadedVersion DownloadedVersion
     {
         get => _downloadedVersion;
-        set
-        {
-            this.RaiseAndSetIfChanged(ref _downloadedVersion, value);
-            //Settings.lastChosenVersion = value.version;
-            //logger.Debug("Version choosen: {0}",value.version);
-            //logger.Debug("Version json: {0}", value.path);
-        }
+        set => this.RaiseAndSetIfChanged(ref _downloadedVersion, value);
     }
 
     public int DownloadedIndex
@@ -836,46 +827,16 @@ public class MainWindowViewModel : ViewModelBase
 
     }
 
-    private Panel CreateServerPanel(string name)
+    private ServerPanelModel CreateServerPanel(string name)
     {
-        ServerPanelModel serverPanelModel = new(name, "Wait for update...", "No players.",string.Empty);
-
-        Panel panel = new Panel() 
-        { 
-            VerticalAlignment = Avalonia.Layout.VerticalAlignment.Top, 
-            HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch, 
-            Height = 75, 
-            Width = 150, 
-            Margin = Avalonia.Thickness.Parse("0 0 10 10")
-        };
-
-        panel.Children.Add(new Border() 
-        { 
-            Background = Brushes.Black, 
-            Opacity = 0.2, 
-            CornerRadius = Avalonia.CornerRadius.Parse("15") 
-        });
-
-        var tip = new ToolTip()
-        {
-            Content = new TextBlock() {TextWrapping = TextWrapping.Wrap, [!TextBlock.TextProperty] = new Binding { Source = serverPanelModel,Path = nameof(serverPanelModel.Tip) } }
-        };
-
-        ToolTip.SetTip(panel, tip);
-
-        StackPanel textPanel = new() { VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center, HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center };
-
-        textPanel.Children.Add(new TextBlock() { VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center, HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center, [!TextBlock.TextProperty] = new Binding { Source = serverPanelModel, Path = nameof(serverPanelModel.Status) } });
-        textPanel.Children.Add(new TextBlock() { FontSize = 20, VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center, HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center, [!TextBlock.TextProperty] = new Binding { Source = serverPanelModel, Path = nameof(serverPanelModel.Players) } });
-        
-        panel.Children.Add(textPanel);
+        ServerPanelModel serverPanelModel = new(name, "Wait for update...", "No players.","-/-");
 
         System.Timers.Timer timeoutTimer = new System.Timers.Timer(30000);
 
         timeoutTimer.Elapsed += (object? source, ElapsedEventArgs e) => 
         { 
             serverPanelModel.Status = $"{serverPanelModel.Name}: Offline"; 
-            serverPanelModel.Players = string.Empty; 
+            serverPanelModel.Players = "-/-"; 
         };
         timeoutTimer.Start();
 
@@ -901,7 +862,7 @@ public class MainWindowViewModel : ViewModelBase
             timeoutTimer.Start();
         });
 
-        return panel;
+        return serverPanelModel;
     }
 
     public void OnClosing(object sender, CancelEventArgs args)

+ 44 - 5
VeloeMinecraftLauncher/Views/MainWindow.axaml

@@ -1,6 +1,7 @@
 <Window xmlns="https://github.com/avaloniaui"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="using:VeloeMinecraftLauncher.ViewModels"
+		xmlns:m="using:VeloeMinecraftLauncher.Models"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 		xmlns:titlebars="using:VeloeMinecraftLauncher.Views.TitleBar"
@@ -74,11 +75,49 @@
 					<TabItem
 						Header="Servers"
 						VerticalContentAlignment="Center">
-						<StackPanel
-							Name = "ServersStackPanel"
-							Orientation="Horizontal"
-							HorizontalAlignment="Stretch">
-						</StackPanel>						
+						<ItemsControl 
+							ItemsSource="{Binding ServerPanels}">
+							<ItemsControl.ItemsPanel>
+								<ItemsPanelTemplate>
+									<UniformGrid Columns="5" Rows="4"/>
+								</ItemsPanelTemplate>
+							</ItemsControl.ItemsPanel>
+							<ItemsControl.ItemTemplate>
+								<DataTemplate DataType="m:ServerPanelModel">
+									<Panel
+										VerticalAlignment="Top"
+										HorizontalAlignment="Stretch"
+										Height="75"
+										Width="150"
+										Margin="0 0 10 10">
+										<Border
+											Background="Black"
+											Opacity="0.2"
+											CornerRadius="15"/>
+										<ToolTip.Tip>
+											<TextBlock 
+												Text="{Binding Tip}"
+												TextWrapping="Wrap"/>
+										</ToolTip.Tip>
+										<UniformGrid
+											Columns="1"
+											Rows="2"
+											VerticalAlignment="Center"
+											HorizontalAlignment="Center">
+											<TextBlock
+												VerticalAlignment="Center"
+												HorizontalAlignment="Center"
+												Text="{Binding Status}"/>
+											<TextBlock
+												VerticalAlignment="Center"
+												HorizontalAlignment="Center"
+												FontSize="20"
+												Text="{Binding Players}"/>
+										</UniformGrid>
+									</Panel>
+								</DataTemplate>
+							</ItemsControl.ItemTemplate>			
+						</ItemsControl>
 					</TabItem>
 					<TabItem>
 						<TabItem.Header>