Skip to content

Commit

Permalink
remove Stylet.Avalonia
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrxx99 committed Feb 19, 2024
1 parent b1523bb commit 220c768
Show file tree
Hide file tree
Showing 31 changed files with 370 additions and 123 deletions.
7 changes: 6 additions & 1 deletion YoutubeDownloader/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
xmlns:materialDesign="clr-namespace:Material.Styles.Controls;assembly=Material.Styles"
xmlns:themes="clr-namespace:Material.Styles.Themes;assembly=Material.Styles"
xmlns:materialIcons="clr-namespace:Material.Icons.Avalonia;assembly=Material.Icons.Avalonia"
xmlns:framework="using:YoutubeDownloader.Views.Framework"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

<Application.DataTemplates>
<framework:ViewLocator/>
</Application.DataTemplates>


<Application.Styles>
<themes:MaterialTheme BaseTheme="Light" />
<dialogHostAvalonia:DialogHostStyles />
Expand Down
97 changes: 57 additions & 40 deletions YoutubeDownloader/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using System;
using System.Net;
using System.Reflection;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Input.Platform;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Styling;
using AvaloniaWebView;
using Material.Styles.Themes;
using Microsoft.Extensions.DependencyInjection;
using PropertyChanged;
using Stylet;
using StyletIoC;
using YoutubeDownloader.Services;
using YoutubeDownloader.Utils;
using YoutubeDownloader.ViewModels;
using YoutubeDownloader.ViewModels.Framework;
using YoutubeDownloader.Views;
using YoutubeDownloader.Views.Framework;

namespace YoutubeDownloader;

Expand All @@ -34,13 +36,20 @@ public partial class App
}

[DoNotNotify]
public partial class App : StyletApplication<RootViewModel>
public partial class App : Application
{
private readonly IServiceProvider? _serviceProvider;

private static Theme LightTheme { get; } =
Theme.Create(Theme.Light, Color.Parse("#343838"), Color.Parse("#F9A825"));

private static Theme DarkTheme { get; } =
Theme.Create(Theme.Dark, Color.Parse("#E8E8E8"), Color.Parse("#F9A825"));

public App()
{
_serviceProvider = ConfigureServices();
}

public static void SetLightTheme()
{
Expand All @@ -64,27 +73,42 @@ public static void SetDarkTheme()
Current!.Resources["FailedBrush"] = new SolidColorBrush(Colors.OrangeRed);
}

protected override void OnStart()
public override void Initialize()
{
base.OnStart();

// Set the default theme.
// Preferred theme will be set later, once the settings are loaded.
//App.SetLightTheme();

// Increase maximum concurrent connections
ServicePointManager.DefaultConnectionLimit = 20;
}

public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
base.Initialize();
}

public override void OnFrameworkInitializationCompleted()
{
if (_serviceProvider is null)
{
return; // fix for Designer
}

if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
var rootViewModel = ActivatorUtilities.CreateInstance<RootViewModel>(_serviceProvider);

desktop.MainWindow = new RootView { DataContext = rootViewModel, };
}

base.OnFrameworkInitializationCompleted();

// var settinsService = _serviceProvider.GetService<SettingsService>();
//
// settinsService?.Load();
//
// if (settinsService?.IsDarkModeEnabled ?? false)
// {
// SetDarkTheme();
// }
// else
// {
// SetLightTheme();
// }
}

public override void RegisterServices()
Expand All @@ -94,32 +118,25 @@ public override void RegisterServices()
AvaloniaWebViewBuilder.Initialize(config => config.IsInPrivateModeEnabled = true);
}

protected override void ConfigureIoC(IStyletIoCBuilder builder)
protected ServiceProvider ConfigureServices()
{
base.ConfigureIoC(builder);

var set = new SettingsService();

builder.Bind<SettingsService>().ToSelf().InSingletonScope();
builder.Bind<IViewModelFactory>().ToAbstractFactory();
builder
.Bind<IClipboard>()
.ToFactory(ctx =>
(ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)!
.MainWindow!
.Clipboard
);
}

protected override void OnLaunch()
{
base.OnLaunch();
if (
ApplicationLifetime
is IClassicDesktopStyleApplicationLifetime classicDesktopStyleApplicationLifetime
)
{
classicDesktopStyleApplicationLifetime.MainWindow = GetActiveWindow();
}
var services = new ServiceCollection();

services.AddSingleton<SettingsService>();
services.AddSingleton<UpdateService>();
services.AddSingleton<IViewManager, ViewManager>();
services.AddSingleton<DialogManager>();
services.AddSingleton<IViewModelFactory, ViewModelFactory>();
services.AddTransient<IClipboard>(sp =>
sp.GetRequiredService<IViewManager>().GetTopLevel()!.Clipboard!
);
services.AddTransient<IApplicationLifetime>(sp => App.Current!.ApplicationLifetime!);
services.AddTransient<IControlledApplicationLifetime>(sp =>
(App.Current!.ApplicationLifetime! as IControlledApplicationLifetime)!
);

services.AddSingleton(sp => App.Current!.PlatformSettings!);

return services.BuildServiceProvider(true);
}
}
2 changes: 2 additions & 0 deletions YoutubeDownloader/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Avalonia;
using Avalonia.ReactiveUI;
using Avalonia.WebView.Desktop;

namespace YoutubeDownloader;
Expand All @@ -21,5 +22,6 @@ public static AppBuilder BuildAvaloniaApp() =>
.UsePlatformDetect()
.WithInterFont()
.LogToTrace()
.UseReactiveUI()
.UseDesktopWebView();
}
30 changes: 15 additions & 15 deletions YoutubeDownloader/ViewModels/Components/DashboardViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Metadata;
using Gress;
using Gress.Completable;
using Stylet;
using ReactiveUI;
using YoutubeDownloader.Core.Downloading;
using YoutubeDownloader.Core.Resolving;
using YoutubeDownloader.Core.Tagging;
Expand All @@ -17,7 +18,7 @@

namespace YoutubeDownloader.ViewModels.Components;

public class DashboardViewModel : PropertyChangedBase, IDisposable
public class DashboardViewModel : ViewModelBase, IDisposable
{
private readonly IViewModelFactory _viewModelFactory;
private readonly DialogManager _dialogManager;
Expand All @@ -34,7 +35,7 @@ public class DashboardViewModel : PropertyChangedBase, IDisposable

public string? Query { get; set; }

public BindableCollection<DownloadViewModel> Downloads { get; } = [];
public ObservableCollection<DownloadViewModel> Downloads { get; } = [];

public bool IsDownloadsAvailable => Downloads.Any();

Expand All @@ -50,17 +51,15 @@ SettingsService settingsService

_progressMuxer = Progress.CreateMuxer().WithAutoReset();

_settingsService.BindAndInvoke(
o => o.ParallelLimit,
(_, e) => _downloadSemaphore.MaxCount = e.NewValue
);

Progress.Bind(
o => o.Current,
(_, _) => NotifyOfPropertyChange(() => IsProgressIndeterminate)
);

Downloads.Bind(o => o.Count, (_, _) => NotifyOfPropertyChange(() => IsDownloadsAvailable));
_settingsService
.WhenAnyValue(o => o.ParallelLimit)
.Subscribe(v => _downloadSemaphore.MaxCount = v);
Progress
.WhenAnyValue(o => o.Current)
.Subscribe(_ => OnPropertyChanged(nameof(IsProgressIndeterminate)));
Downloads
.WhenAnyValue(o => o.Count)
.Subscribe(_ => OnPropertyChanged(nameof(IsDownloadsAvailable)));
}

public bool CanShowAuthSetup => !IsBusy;
Expand Down Expand Up @@ -277,8 +276,9 @@ or DownloadStatus.Canceled
}
}

public void RestartDownload(DownloadViewModel download)
public void RestartDownload(object parameter)
{
var download = (DownloadViewModel)parameter;
var position = Math.Max(0, Downloads.IndexOf(download));
RemoveDownload(download);

Expand Down
11 changes: 5 additions & 6 deletions YoutubeDownloader/ViewModels/Components/DownloadViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Avalonia.Input.Platform;
using Gress;
using PropertyChanged;
using Stylet;
using ReactiveUI;
using YoutubeDownloader.Core.Downloading;
using YoutubeDownloader.Utils;
using YoutubeDownloader.ViewModels.Dialogs;
Expand All @@ -14,7 +14,7 @@

namespace YoutubeDownloader.ViewModels.Components;

public class DownloadViewModel : PropertyChangedBase, IDisposable
public class DownloadViewModel : ViewModelBase, IDisposable
{
private readonly IViewModelFactory _viewModelFactory;
private readonly DialogManager _dialogManager;
Expand Down Expand Up @@ -56,10 +56,9 @@ IClipboard clipboard
_dialogManager = dialogManager;
_clipboard = clipboard;

Progress.Bind(
o => o.Current,
(_, _) => NotifyOfPropertyChange(() => IsProgressIndeterminate)
);
Progress
.WhenAnyValue(o => o.Current)
.Subscribe(_ => OnPropertyChanged(nameof(IsProgressIndeterminate)));
}

public bool CanCancel => Status is DownloadStatus.Enqueued or DownloadStatus.Started;
Expand Down
12 changes: 6 additions & 6 deletions YoutubeDownloader/ViewModels/Dialogs/AuthSetupViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Stylet;
using ReactiveUI;
using YoutubeDownloader.Services;
using YoutubeDownloader.ViewModels.Framework;

Expand All @@ -28,11 +28,11 @@ public AuthSetupViewModel(SettingsService settingsService)
{
_settingsService = settingsService;

_settingsService.BindAndInvoke(
o => o.LastAuthCookies,
(_, _) => NotifyOfPropertyChange(() => Cookies)
);
_settingsService
.WhenAnyValue(o => o.LastAuthCookies)
.Subscribe(_ => OnPropertyChanged(nameof(Cookies)));

this.BindAndInvoke(o => o.Cookies, (_, _) => NotifyOfPropertyChange(() => IsAuthenticated));
this.WhenAnyValue(o => o.Cookies)
.Subscribe(_ => OnPropertyChanged(nameof(IsAuthenticated)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ IClipboard clipboard
_clipboard = clipboard;
}

public void OnViewLoaded()
protected override void OnViewLoaded()
{
SelectedContainer = _settingsService.LastContainer;
SelectedVideoQualityPreference = _settingsService.LastVideoQualityPreference;
SelectedVideos.CollectionChanged += (sender, args) =>
NotifyOfPropertyChange(nameof(CanConfirm));
SelectedVideos.CollectionChanged += (sender, args) => OnPropertyChanged(nameof(CanConfirm));
}

public async Task CopyTitle() => await _clipboard.SetTextAsync(Title!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ IClipboard clipboard

public VideoDownloadOption? SelectedDownloadOption { get; set; }

public void OnViewLoaded()
protected override void OnViewLoaded()
{
SelectedDownloadOption = AvailableDownloadOptions?.FirstOrDefault(o =>
o.Container == settingsService.LastContainer
Expand Down
4 changes: 2 additions & 2 deletions YoutubeDownloader/ViewModels/Framework/DialogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Platform.Storage;
using DialogHostAvalonia;
using Stylet;
using YoutubeDownloader.Views.Framework;

namespace YoutubeDownloader.ViewModels.Framework;

Expand Down Expand Up @@ -42,7 +42,7 @@ void OnScreenClosed(object? closeSender, EventArgs closeArgs)
await _dialogLock.WaitAsync();
try
{
await DialogHost.Show(view, OnDialogOpened);
await DialogHost.Show(view!, OnDialogOpened);
return dialogScreen.DialogResult;
}
finally
Expand Down
11 changes: 8 additions & 3 deletions YoutubeDownloader/ViewModels/Framework/DialogScreen.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
using System;
using Stylet;

namespace YoutubeDownloader.ViewModels.Framework;

public abstract class DialogScreen<T> : PropertyChangedBase
public abstract class DialogScreen<T> : ViewModelBase
{
public T? DialogResult { get; private set; }

public event EventHandler? Closed;

public void Close(T? dialogResult = default)
public void Close(T? dialogResult)
{
DialogResult = dialogResult;
Closed?.Invoke(this, EventArgs.Empty);
}

public void Close()
{
// ReSharper disable once IntroduceOptionalParameters.Global
Close(default);
}
}

public abstract class DialogScreen : DialogScreen<bool?>;
Loading

0 comments on commit 220c768

Please sign in to comment.