Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions SS14.Launcher/Models/ServerStatus/ClassicServerListCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public ClassicServerListCache()
AllServers = new ReadOnlyObservableCollection<ClassicServerStatusData>(_allServers);
}

public ClassicServerStatusData GetStatusFor(string address)
{
foreach (var server in _allServers)
if (server.Address == address)
return server;

return new ClassicServerStatusData("Unknown Server", address, 0, "Fetching...", "");
}

public async Task Refresh()
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
using SS14.Launcher.Models;
using SS14.Launcher.Models.ServerStatus;
using SS14.Launcher.Utility;
using SS14.Launcher.Models.Data;
using Microsoft.Toolkit.Mvvm.Messaging;

namespace SS14.Launcher.ViewModels.MainWindowTabs;

public class ClassicServerEntryViewModel : ViewModelBase
public class ClassicServerEntryViewModel : ViewModelBase, IRecipient<FavoritesChanged>
{
private readonly MainWindowViewModel _mainWindow;
private readonly ClassicServerStatusData _server;
private readonly DataManager? _cfg;

public FavoriteServer? Favorite { get; }

public string Name => _server.Name;
public string Address => _server.Address;
Expand All @@ -32,12 +37,62 @@ public bool IsExpanded

public ReactiveCommand<System.Reactive.Unit, System.Reactive.Unit> ConnectCommand { get; }

public ClassicServerEntryViewModel(MainWindowViewModel mainWindow, ClassicServerStatusData server)
public ClassicServerEntryViewModel(MainWindowViewModel mainWindow, ClassicServerStatusData server, FavoriteServer? favorite = null, DataManager? cfg = null)
{
_mainWindow = mainWindow;
_server = server;
Favorite = favorite;
_cfg = cfg;

ConnectCommand = ReactiveCommand.Create(Connect);

WeakReferenceMessenger.Default.Register(this);
}

public bool ViewedInFavoritesPane { get; set; }

public bool IsFavorite => _cfg?.FavoriteServers.Lookup(Address).HasValue ?? false;

public string FavoriteButtonText => IsFavorite
? LocalizationManager.Instance.GetString("server-entry-remove-favorite")
: LocalizationManager.Instance.GetString("server-entry-add-favorite");

public void FavoriteButtonPressed()
{
if (_cfg == null) return;
if (IsFavorite)
_cfg.RemoveFavoriteServer(_cfg.FavoriteServers.Lookup(Address).Value);
else
{
var fav = new FavoriteServer(Name, Address);
_cfg.AddFavoriteServer(fav);
}

_cfg.CommitConfig();
}

public void FavoriteRaiseButtonPressed()
{
if (_cfg == null || !IsFavorite)
return;

_cfg.ReorderFavoriteServer(_cfg.FavoriteServers.Lookup(Address).Value, 1);
_cfg.CommitConfig();
}

public void FavoriteLowerButtonPressed()
{
if (_cfg == null || !IsFavorite)
return;

_cfg.ReorderFavoriteServer(_cfg.FavoriteServers.Lookup(Address).Value, -1);
_cfg.CommitConfig();
}

public void Receive(FavoritesChanged message)
{
this.RaisePropertyChanged(nameof(IsFavorite));
this.RaisePropertyChanged(nameof(FavoriteButtonText));
}

private void Connect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class ClassicServerListTabViewModel : MainWindowTabViewModel
{
private readonly MainWindowViewModel _mainWindow;
private readonly ClassicServerListCache _cache;
private readonly SS14.Launcher.Models.Data.DataManager _cfg;

private readonly LocalizationManager _loc = LocalizationManager.Instance;

Expand All @@ -37,6 +38,7 @@ public ClassicServerListTabViewModel(MainWindowViewModel mainWindow)
{
_mainWindow = mainWindow;
_cache = Locator.Current.GetRequiredService<ClassicServerListCache>();
_cfg = Locator.Current.GetRequiredService<SS14.Launcher.Models.Data.DataManager>();
RefreshPressed = ReactiveCommand.CreateFromTask(_cache.Refresh);

// Initial populate if any
Expand All @@ -59,7 +61,7 @@ private void UpdateList()

foreach (var s in sorted)
{
AllServers.Add(new ClassicServerEntryViewModel(_mainWindow, s));
AllServers.Add(new ClassicServerEntryViewModel(_mainWindow, s, null, _cfg));
}
}

Expand Down
37 changes: 29 additions & 8 deletions SS14.Launcher/ViewModels/MainWindowTabs/HomePageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,47 @@ public class HomePageViewModel : MainWindowTabViewModel
private readonly DataManager _cfg;
private readonly ServerStatusCache _statusCache = new ServerStatusCache();
private readonly ServerListCache _serverListCache;
private readonly ClassicServerListCache _classicServerListCache;

public HomePageViewModel(MainWindowViewModel mainWindowViewModel)
{
MainWindowViewModel = mainWindowViewModel;
_cfg = Locator.Current.GetRequiredService<DataManager>();
_serverListCache = Locator.Current.GetRequiredService<ServerListCache>();
_classicServerListCache = Locator.Current.GetRequiredService<ClassicServerListCache>();

_cfg.FavoriteServers
.Connect()
.Select(x =>
new ServerEntryViewModel(MainWindowViewModel, _statusCache.GetStatusFor(x.Address), x, _statusCache, _cfg, Locator.Current.GetRequiredService<LoginManager>())
{ ViewedInFavoritesPane = true, IsExpanded = _cfg.ExpandedServers.Contains(x.Address) })
{
if (x.Address.StartsWith("byond://"))
{
return (IViewModelBase) new ClassicServerEntryViewModel(MainWindowViewModel, _classicServerListCache.GetStatusFor(x.Address), x, _cfg)
{ ViewedInFavoritesPane = true, IsExpanded = _cfg.ExpandedServers.Contains(x.Address) };
}

return (IViewModelBase) new ServerEntryViewModel(MainWindowViewModel, _statusCache.GetStatusFor(x.Address), x, _statusCache, _cfg, Locator.Current.GetRequiredService<LoginManager>())
{ ViewedInFavoritesPane = true, IsExpanded = _cfg.ExpandedServers.Contains(x.Address) };
})
.OnItemAdded(a =>
{
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
if (IsSelected) _statusCache.InitialUpdateStatus(a.CacheData);
if (IsSelected)
{
if (a is ServerEntryViewModel svm)
_statusCache.InitialUpdateStatus(svm.CacheData);
}
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
})
.Sort(Comparer<ServerEntryViewModel>.Create((a, b) =>
.Sort(Comparer<IViewModelBase>.Create((a, b) =>
{
var dc = a.Favorite!.Position.CompareTo(b.Favorite!.Position);
return dc != 0 ? -dc : string.Compare(a.Name, b.Name, StringComparison.CurrentCultureIgnoreCase);
var afav = (a as ServerEntryViewModel)?.Favorite ?? (a as ClassicServerEntryViewModel)?.Favorite;
var bfav = (b as ServerEntryViewModel)?.Favorite ?? (b as ClassicServerEntryViewModel)?.Favorite;
var aName = (a as ServerEntryViewModel)?.Name ?? (a as ClassicServerEntryViewModel)?.Name;
var bName = (b as ServerEntryViewModel)?.Name ?? (b as ClassicServerEntryViewModel)?.Name;

var dc = afav!.Position.CompareTo(bfav!.Position);
return dc != 0 ? -dc : string.Compare(aName, bName, StringComparison.CurrentCultureIgnoreCase);
}))
.Bind(out var favorites)
.Subscribe(_ =>
Expand All @@ -55,7 +74,7 @@ public HomePageViewModel(MainWindowViewModel mainWindowViewModel)
Favorites = favorites;
}

public ReadOnlyObservableCollection<ServerEntryViewModel> Favorites { get; }
public ReadOnlyObservableCollection<IViewModelBase> Favorites { get; }
public ObservableCollection<ServerEntryViewModel> Suggestions { get; } = new();

[Reactive] public bool FavoritesEmpty { get; private set; } = true;
Expand Down Expand Up @@ -110,13 +129,15 @@ public void RefreshPressed()
{
_statusCache.Refresh();
_serverListCache.RequestRefresh();
_classicServerListCache.Refresh();
}

public override void Selected()
{
foreach (var favorite in Favorites)
{
_ = _statusCache.InitialUpdateStatus(favorite.CacheData);
if (favorite is ServerEntryViewModel svm)
_ = _statusCache.InitialUpdateStatus(svm.CacheData);
}
_serverListCache.RequestInitialUpdate();
}
Expand Down
33 changes: 32 additions & 1 deletion SS14.Launcher/Views/MainWindowTabs/ClassicServerEntryView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mainWindowTabs="clr-namespace:SS14.Launcher.ViewModels.MainWindowTabs"
xmlns:loc="clr-namespace:SS14.Launcher.Localization"
xmlns:views="clr-namespace:SS14.Launcher.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="50"
x:Class="SS14.Launcher.Views.MainWindowTabs.ClassicServerEntryView">
<Design.DataContext>
<mainWindowTabs:ClassicServerEntryViewModel />
</Design.DataContext>

<UserControl.Styles>
<Style Selector="Button.FavoriteButton > views|IconLabel">
<Setter Property="Icon" Value="{DynamicResource 'ButtonIcon-star-outline'}" />
</Style>
<Style Selector="Button.FavoriteButton.IsFavorite > views|IconLabel">
<Setter Property="Icon" Value="{DynamicResource 'ButtonIcon-star'}" />
</Style>
</UserControl.Styles>

<Panel>
<Expander Name="Expando" Classes="NoPad" IsExpanded="{Binding IsExpanded}">
<Expander.Header>
Expand Down Expand Up @@ -43,7 +53,28 @@
</Grid>
</Expander.Header>
<DockPanel Margin="10">
<TextBlock Text="{Binding Status}" TextWrapping="Wrap" />
<TextBlock DockPanel.Dock="Top" Text="{Binding Status}" TextWrapping="Wrap" />

<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0, 8, 0, 0">
<Button IsVisible="{Binding ViewedInFavoritesPane}"
Classes="OpenBoth"
Content="{loc:Loc server-entry-raise}"
Command="{Binding FavoriteRaiseButtonPressed}" />
<Button IsVisible="{Binding ViewedInFavoritesPane}"
Classes="OpenBoth"
Content="{loc:Loc server-entry-lower}"
Command="{Binding FavoriteLowerButtonPressed}" />

<Button Name="FavoriteButton"
Classes="FavoriteButton"
Classes.OpenLeft="{Binding ViewedInFavoritesPane}"
Classes.IsFavorite="{Binding IsFavorite}"
Command="{Binding FavoriteButtonPressed}">
<views:IconLabel Name="FavoriteButtonIconLabel"
Content="{Binding FavoriteButtonText}"
Height="20" />
</Button>
</StackPanel>
</DockPanel>
</Expander>
</Panel>
Expand Down
9 changes: 5 additions & 4 deletions SS14.Launcher/Views/MainWindowTabs/ServerList.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Avalonia.Controls;
using Avalonia.Metadata;
using Serilog;
using SS14.Launcher.ViewModels;
using SS14.Launcher.ViewModels.MainWindowTabs;

namespace SS14.Launcher.Views.MainWindowTabs;
Expand Down Expand Up @@ -60,16 +61,16 @@ public bool SpinnerVisible
set => SetAndRaise(SpinnerVisibleProperty, ref _spinnerVisible, value);
}

public static readonly DirectProperty<ServerList, IReadOnlyCollection<ServerEntryViewModel>> ListProperty =
AvaloniaProperty.RegisterDirect<ServerList, IReadOnlyCollection<ServerEntryViewModel>>(
public static readonly DirectProperty<ServerList, IReadOnlyCollection<IViewModelBase>> ListProperty =
AvaloniaProperty.RegisterDirect<ServerList, IReadOnlyCollection<IViewModelBase>>(
nameof(List),
o => o.List,
(o, v) => o.List = v
);

private IReadOnlyCollection<ServerEntryViewModel> _serverList = Array.Empty<ServerEntryViewModel>();
private IReadOnlyCollection<IViewModelBase> _serverList = Array.Empty<IViewModelBase>();

public IReadOnlyCollection<ServerEntryViewModel> List
public IReadOnlyCollection<IViewModelBase> List
{
get => _serverList;
set => SetAndRaise(ListProperty, ref _serverList, value);
Expand Down
Loading