Skip to content
This repository has been archived by the owner on Jun 10, 2023. It is now read-only.

Commit

Permalink
Remove AutoFac
Browse files Browse the repository at this point in the history
  • Loading branch information
j2ghz committed Jun 27, 2018
1 parent 02af818 commit a266ff7
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 91 deletions.
38 changes: 0 additions & 38 deletions src/ModSink.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,43 +58,6 @@ private static void AddColCheck(this CommandLineApplication app)
});
}

private static void AddDownload(this CommandLineApplication app)
{
app.Command("download", command =>
{
command.Description = "Downloads a missing files from a repo";
command.HelpOption("-?|-h|--help");
var uriArg = command.Argument("[uri]", "Uri to repo to download");
var pathArg = command.Argument("[path]", "Path to local repo");

command.OnExecute(async () =>
{
var uriStr = uriArg.Value;
var uri = new Uri(uriStr);
var localStr = pathArg.Value;
var localUri = new Uri(localStr);
var downloader = new HttpClientDownloader();
var client = new ClientService(new DownloadService(downloader), new LocalStorageService(localUri),
downloader, new BinaryFormatter());

Console.WriteLine("Downloading repo");
client.GroupUrls.Add(uriStr);
client.Modpacks.Connect().Subscribe(cs =>
{
foreach (var modpack in client.Modpacks.Items)
{
Console.WriteLine($"Scheduling {modpack.Name} [{modpack.Mods.Count} mods]");
client.DownloadMissingFiles(modpack).GetAwaiter().GetResult();
}
});


Console.ReadKey();
return 0;
});
});
}

private static void AddDump(this CommandLineApplication app)
{
app.Command("dump", command =>
Expand Down Expand Up @@ -393,7 +356,6 @@ public static void Main(string[] args)

app.AddColCheck();
app.AddSampleRepo();
app.AddDownload();
app.AddImport();
app.AddDump();
app.AddCheck();
Expand Down
5 changes: 0 additions & 5 deletions src/ModSink.Common/Client/ClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ public ClientService(IDownloadService downloadService, ILocalStorageService loca
.TransformMany(g => g.RepoInfos.Select(r => new Uri(g.BaseUri, r.Uri)))
.TransformAsync(Load<Repo>)
.AsObservableList();
Modpacks = Repos
.Connect()
.TransformMany(r => r.Modpacks)
.AsObservableList();
}

public IDownloader Downloader { get; }
Expand All @@ -48,7 +44,6 @@ public ClientService(IDownloadService downloadService, ILocalStorageService loca
public ISourceList<string> GroupUrls { get; } = new SourceList<string>();
public IDownloadService DownloadService { get; }
public ILocalStorageService LocalStorageService { get; }
public IObservableList<Modpack> Modpacks { get; }
public IObservableList<Repo> Repos { get; }


Expand Down
1 change: 0 additions & 1 deletion src/ModSink.Core/Client/IClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public interface IClientService : IReactiveObject
{
IDownloadService DownloadService { get; }
ILocalStorageService LocalStorageService { get; }
IObservableList<Modpack> Modpacks { get; }
IObservableList<Repo> Repos { get; }
ISourceList<string> GroupUrls { get; }
Task DownloadMissingFiles(Modpack modpack);
Expand Down
50 changes: 22 additions & 28 deletions src/ModSink.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
using System.IO;
using System.Net;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Autofac;
using CountlySDK;
using ModSink.Common;
using ModSink.Common.Client;
using ModSink.Core;
using ModSink.WPF.Helpers;
using ModSink.WPF.Model;
using ModSink.WPF.ViewModel;
using ReactiveUI;
using Serilog;
using Serilog.Debugging;
using Splat;
using Splat.Serilog;
using ILogger = Serilog.ILogger;

namespace ModSink.WPF
{
Expand All @@ -25,33 +27,21 @@ public partial class App : Application
private static string FullVersion => typeof(App).GetTypeInfo().Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;

private IContainer BuildContainer()
private void InitializeDependencyInjection()
{
//TODO: FIX:
ServicePointManager.DefaultConnectionLimit = 10;

var builder = new ContainerBuilder();

builder.RegisterType<BinaryFormatter>().As<IFormatter>().SingleInstance();
builder.Register(_ =>
new LocalStorageService(new Uri(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ModSink_Data"))))
.AsImplementedInterfaces()
.SingleInstance();
builder.RegisterAssemblyTypes(typeof(IModSink).Assembly, typeof(Common.ModSink).Assembly)
.Where(t => t.Name != "LocalStorageService").AsImplementedInterfaces().SingleInstance();

builder.RegisterAssemblyTypes(typeof(App).Assembly).Where(t => t.Name.EndsWith("Model"))
.AsImplementedInterfaces().AsSelf().SingleInstance();
builder.RegisterAssemblyTypes(typeof(App).Assembly).Where(t => t.Name.EndsWith("ViewModel"))
.AsImplementedInterfaces().AsSelf().SingleInstance();
builder.RegisterAssemblyTypes(typeof(App).Assembly).Where(t => t.IsAssignableTo<TabItem>()).AsSelf()
.As<TabItem>().SingleInstance();
builder.RegisterType<MainWindow>().AsSelf().SingleInstance();
Locator.CurrentMutable.InitializeSplat();
Registration.Register(log.ForContext<Splat.ILogger>());
Locator.CurrentMutable.InitializeReactiveUI();
Locator.CurrentMutable.RegisterViewsForViewModels(typeof(App).Assembly);
Locator.CurrentMutable.RegisterLazySingleton(() => new BinaryFormatter());
Locator.CurrentMutable.RegisterLazySingleton(() =>
new LocalStorageService(new Uri(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ModSink_Data"))));

//TODO: Load plugins, waiting on https://stackoverflow.com/questions/46351411

return builder.Build();
}

private void FatalException(Exception e, Type source)
Expand Down Expand Up @@ -82,10 +72,14 @@ protected override void OnStartup(StartupEventArgs e)

base.OnStartup(e);

var container = BuildContainer();
InitializeDependencyInjection();

log.Information("Starting UI");
MainWindow = container.Resolve<MainWindow>();
var cs = new ClientService(new DownloadService(new HttpClientDownloader()), new LocalStorageService(new Uri(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ModSink_Data"))),
new HttpClientDownloader(), new BinaryFormatter());
MainWindow = new MainWindow(new MainWindowViewModel(new DownloadsViewModel(cs), new LibraryViewModel(cs),
new SettingsViewModel(new SettingsModel(cs))));
ShutdownMode = ShutdownMode.OnMainWindowClose;
MainWindow.Show();
}
Expand Down
2 changes: 1 addition & 1 deletion src/ModSink.WPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
d:DataContext="{d:DesignInstance vm:MainWindowViewModel}">
<Controls:MetroTabControl Controls:TabControlHelper.Underlined="TabPanel">
<TabItem Header="Library">
<v:LibraryView DataContext="{Binding LibraryVM}" />
<rx:ViewModelViewHost ViewModel="{Binding LibraryVM}" />
</TabItem>
<TabItem Header="Downloads">
<v:DownloadsView DataContext="{Binding DownloadsVM}" />
Expand Down
3 changes: 1 addition & 2 deletions src/ModSink.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Autofac;
using ModSink.Common.Client;
using ModSink.Common.Client;
using ModSink.Core;
using ModSink.WPF.Helpers;
using ModSink.WPF.View;
Expand Down
3 changes: 1 addition & 2 deletions src/ModSink.WPF/ModSink.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="akavache" Version="6.0.0-alpha0038" />
<PackageReference Include="Autofac" Version="4.8.1" />
<PackageReference Include="Ben.Demystifier" Version="0.1.1" />
<PackageReference Include="Countly" Version="18.1.0" />
<PackageReference Include="DynamicData.ReactiveUI" Version="6.3.1.2409" />
Expand All @@ -43,7 +42,7 @@
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.Literate" Version="3.0.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
<PackageReference Include="Serilog.Sinks.Sentry" Version="2.1.4" />
<PackageReference Include="Splat.Serilog" Version="1.0.3-Beta" />
<PackageReference Include="System.Net.Http" Version="4.3.3" />
<PackageReference Include="GitVersionTask" Version="4.0.0-beta*">
<PrivateAssets>All</PrivateAssets>
Expand Down
22 changes: 16 additions & 6 deletions src/ModSink.WPF/View/LibraryView.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<UserControl x:Class="ModSink.WPF.View.LibraryView"
<rxui:ReactiveUserControl x:TypeArguments="vm:LibraryViewModel" x:Class="ModSink.WPF.View.LibraryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand All @@ -7,19 +7,29 @@
xmlns:local="clr-namespace:ModSink.WPF.View"
xmlns:repoModels="clr-namespace:ModSink.Core.Models.Repo;assembly=ModSink.Core"
xmlns:vm="clr-namespace:ModSink.WPF.ViewModel"
xmlns:rxui="http://reactiveui.net"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="500"
d:DataContext="{d:DesignInstance vm:LibraryViewModel}">
<DockPanel LastChildFill="True">
<DockPanel LastChildFill="True" DockPanel.Dock="Left">
<TextBox Controls:TextBoxHelper.Watermark="Search" DockPanel.Dock="Top" />
<ListBox MinWidth="120" ItemsSource="{Binding Modpacks}" SelectedItem="{Binding SelectedModpack}">
<ListBox.ItemTemplate>
<ListView x:Name="lvModpacks" MinWidth="120" SelectedItem="{Binding SelectedModpack}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Modpack.Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ListView.ItemTemplate>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock FontSize="15" FontWeight="Bold" Text="{Binding Repo}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
</DockPanel>
<DockPanel LastChildFill="True" Margin="5">
<TextBlock Text="{Binding SelectedModpack.Modpack.Name}" FontSize="32" DockPanel.Dock="Top" Padding="5" />
Expand All @@ -44,4 +54,4 @@
</TreeView>
</DockPanel>
</DockPanel>
</UserControl>
</rxui:ReactiveUserControl>
17 changes: 13 additions & 4 deletions src/ModSink.WPF/View/LibraryView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Disposables;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -12,17 +14,24 @@
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ModSink.Core.Models.Repo;
using ModSink.WPF.ViewModel;
using ReactiveUI;

namespace ModSink.WPF.View
{
/// <summary>
/// Interaction logic for LibraryView.xaml
/// </summary>
public partial class LibraryView : UserControl
public partial class LibraryView : ReactiveUserControl<LibraryViewModel>
{
public LibraryView()
{
InitializeComponent();

this.WhenActivated(d =>
{
this.OneWayBind(ViewModel, vm => vm.Modpacks, v => v.lvModpacks.ItemsSource).DisposeWith(d);
Disposable.Create(Debugger.Break).DisposeWith(d);

});
}
}
}
5 changes: 3 additions & 2 deletions src/ModSink.WPF/ViewModel/LibraryViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using DynamicData;
Expand All @@ -16,9 +17,9 @@ public class LibraryViewModel : ReactiveObject
public LibraryViewModel(IClientService clientService)
{
ClientService = clientService;
ClientService.Modpacks
ClientService.Repos
.Connect()
.Transform(m => new ModpackViewModel(m))
.TransformMany(r => r.Modpacks.Select(m=>new ModpackViewModel(m,r)))
.ObserveOnDispatcher()
.Bind(Modpacks)
.Subscribe();
Expand Down
5 changes: 3 additions & 2 deletions src/ModSink.WPF/ViewModel/ModpackViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ namespace ModSink.WPF.ViewModel
{
public class ModpackViewModel
{
public ModpackViewModel(Modpack modpack)
public ModpackViewModel(Modpack modpack, Repo repo)
{
Modpack = modpack;
Size = ByteSize.FromBytes(
Modpack.Mods
.SelectMany(m => m.Mod.Files)
.Select(f => f.Value.Length)
.Aggregate((sum, a) => sum + a)).Humanize("G03");
Repo = repo.BaseUri.ToString();
}

public string Repo { get; }
public Modpack Modpack { get; }

public string Size { get; }
Expand Down

0 comments on commit a266ff7

Please sign in to comment.