Skip to content

Commit

Permalink
added avatar + enhanced ux
Browse files Browse the repository at this point in the history
  • Loading branch information
Enn3Developer committed May 27, 2024
1 parent faa25fd commit ff24950
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
51 changes: 49 additions & 2 deletions ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
using System.Security.AccessControl;
using System.Text.Json;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using mcswlib.ServerStatus;
using NewRGB.Data;
using NewRGB.Views;
using ProjBobcat.Class.Model;
using ProjBobcat.Class.Model.LauncherProfile;
using ProjBobcat.Class.Model.Mojang;
Expand Down Expand Up @@ -39,6 +43,7 @@ public class MainViewModel : ViewModelBase
private string _serverInfo = "0/20";
private bool _needsJava;
private bool _isPlayEnabled;
private Bitmap? _profileAvatar;

public MainViewModel()
{
Expand Down Expand Up @@ -93,6 +98,12 @@ public bool IsPlayEnabled
private set => this.RaiseAndSetIfChanged(ref _isPlayEnabled, value);
}

public Bitmap? ProfileAvatar
{
get => _profileAvatar;
private set => this.RaiseAndSetIfChanged(ref _profileAvatar, value);
}

private async Task<string?> CheckJava()
{
UpdateProgress(0.0f, "Checking Java version", false);
Expand Down Expand Up @@ -400,6 +411,10 @@ await _updateManager.DownloadUpdatesAsync(_updateInfo,
await CheckForge(javaPath);
UpdateProgress(1.0f, "Launching RGBcraft", false);
await Launch(javaPath);
await Task.Delay(25000);
UpdateProgress(1.0f, "Hiding launcher", false);
await Task.Delay(2000);
if (MainWindow.Instance != null) MainWindow.Instance.WindowState = WindowState.Minimized;
UpdateProgress(1.0f, "Ready");
}
}
Expand Down Expand Up @@ -453,15 +468,47 @@ private async Task InstallUpdate()
UpdateProgress(1.0f, "Update installed");
}

private async Task DownloadProfileAvatar()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (X11; Linux x86_64; rv:126.0) Gecko/20100101 Firefox/126.0");
var path = Path.Combine(DataManager.Instance.DataPath, "avatar.png");
var response = await httpClient.GetAsync($"http://skins.rgbcraft.com/api/helm/{Username}/1024");
response.EnsureSuccessStatusCode();
await using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, 1024, true))
{
await response.Content.CopyToAsync(stream);
}

response.Dispose();

await using (var stream = File.OpenRead(path))
{
ProfileAvatar = Bitmap.DecodeToWidth(stream, 48);
}
}

public async Task AsyncOnLoaded()
{
UpdateProgress(0.0f, "Checking for launcher updates", false);
var profileAvatarPath = Path.Combine(DataManager.Instance.DataPath, "avatar.png");
if (File.Exists(profileAvatarPath))
{
await using var stream = File.OpenRead(profileAvatarPath);
ProfileAvatar = Bitmap.DecodeToWidth(stream, 48);
}

await DownloadProfileAvatar();

var factory = new ServerStatusFactory();
factory.ServerChanged += (sender, _) =>
{
var srv = (ServerStatus)sender!;
ServerInfo = $"{srv.PlayerCount}/{srv.MaxPlayerCount}";
Dispatcher.UIThread.Invoke(() =>
{
var srv = (ServerStatus)sender!;
ServerInfo = $"{srv.PlayerCount}/{srv.MaxPlayerCount}";
});
};
factory.Make("kamino.a-centauri.com", 25565, false, "One");
factory.StartAutoUpdate();
Expand Down
11 changes: 5 additions & 6 deletions Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
</Button>

<StackPanel HorizontalAlignment="Right">
<StackPanel Orientation="Horizontal">
<Label Content="{CompiledBinding Username}" />
<Label IsVisible="False">IMMAGINE</Label>
<!-- <Image asyncImageLoader:ImageLoader.Source="http://skins.rgbcraft.com/api/helm/Enn3DevPlayer" /> -->
<StackPanel Orientation="Horizontal" Spacing="10">
<Label Content="{CompiledBinding Username}" VerticalAlignment="Center" FontSize="16" />
<Image Source="{CompiledBinding ProfileAvatar}" />
</StackPanel>
<StackPanel HorizontalAlignment="Right" IsVisible="False">
<Label>SUS</Label>
Expand All @@ -44,8 +43,8 @@

<Panel VerticalAlignment="Bottom">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Spacing="5">
<Label FontSize="16" FontWeight="Bold">Server</Label>
<Label Content="{CompiledBinding ServerInfo}" />
<Label FontSize="16" FontWeight="Bold" VerticalContentAlignment="Center">Server</Label>
<Label Content="{CompiledBinding ServerInfo}" VerticalContentAlignment="Center" />
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Spacing="20">
<Button Command="{CompiledBinding PlayBtn}" IsEnabled="{CompiledBinding IsPlayEnabled}">
Expand Down
3 changes: 3 additions & 0 deletions Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ namespace NewRGB.Views;

public partial class MainWindow : Window
{
public static MainWindow? Instance { get; private set; }

public MainWindow()
{
InitializeComponent();
Instance = this;
}
}

0 comments on commit ff24950

Please sign in to comment.