Skip to content

Commit

Permalink
Add a basic example client for Freshli Lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbiggred committed Apr 27, 2021
1 parent 438bfdc commit 12af0ae
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 3 deletions.
20 changes: 17 additions & 3 deletions Corgibytes.Freshli.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@

Microsoft Visual Studio Solution File, Format Version 12.00
#
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Corgibytes.Freshli", "Corgibytes.Freshli.Lib\Corgibytes.Freshli.Lib.csproj", "{E03D125E-55CD-4FA4-AEC3-80CDB3BED745}"
# Visual Studio Version 16
VisualStudioVersion = 16.0.31129.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Corgibytes.Freshli.Lib", "Corgibytes.Freshli.Lib\Corgibytes.Freshli.Lib.csproj", "{E03D125E-55CD-4FA4-AEC3-80CDB3BED745}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Corgibytes.Freshli.Test", "Corgibytes.Freshli.Lib.Test\Corgibytes.Freshli.Lib.Test.csproj", "{7C5BE77F-B9FA-495A-828C-ABA44F6536C6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Corgibytes.Freshli.Lib.Test", "Corgibytes.Freshli.Lib.Test\Corgibytes.Freshli.Lib.Test.csproj", "{7C5BE77F-B9FA-495A-828C-ABA44F6536C6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleClient", "ExampleClient\ExampleClient.csproj", "{2EB4454A-48B4-4A03-A2A5-47E888654AC9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -19,5 +23,15 @@ Global
{7C5BE77F-B9FA-495A-828C-ABA44F6536C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C5BE77F-B9FA-495A-828C-ABA44F6536C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C5BE77F-B9FA-495A-828C-ABA44F6536C6}.Release|Any CPU.Build.0 = Release|Any CPU
{2EB4454A-48B4-4A03-A2A5-47E888654AC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2EB4454A-48B4-4A03-A2A5-47E888654AC9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2EB4454A-48B4-4A03-A2A5-47E888654AC9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2EB4454A-48B4-4A03-A2A5-47E888654AC9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {815F602F-03CC-488A-AAA4-40E49E6620B5}
EndGlobalSection
EndGlobal
12 changes: 12 additions & 0 deletions ExampleClient/App.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ExampleClient"
x:Class="ExampleClient.App">
<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>

<Application.Styles>
<FluentTheme Mode="Light"/>
</Application.Styles>
</Application>
29 changes: 29 additions & 0 deletions ExampleClient/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using ExampleClient.ViewModels;
using ExampleClient.Views;

namespace ExampleClient
{
public class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel(),
};
}

base.OnFrameworkInitializationCompleted();
}
}
}
Binary file added ExampleClient/Assets/avalonia-logo.ico
Binary file not shown.
26 changes: 26 additions & 0 deletions ExampleClient/ExampleClient.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<Folder Include="Models\" />
<AvaloniaResource Include="Assets\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.2" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.2" />
<PackageReference Include="Avalonia.Diagnostics" Version="0.10.2" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Corgibytes.Freshli.Lib\Corgibytes.Freshli.Lib.csproj" />
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions ExampleClient/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.ReactiveUI;
using System;

namespace ExampleClient
{
class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);

// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.LogToTrace()
.UseReactiveUI();
}
}
32 changes: 32 additions & 0 deletions ExampleClient/ViewLocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using ExampleClient.ViewModels;
using System;

namespace ExampleClient
{
public class ViewLocator : IDataTemplate
{
public bool SupportsRecycling => false;

public IControl Build(object data)
{
var name = data.GetType().FullName!.Replace("ViewModel", "View");
var type = Type.GetType(name);

if (type != null)
{
return (Control)Activator.CreateInstance(type)!;
}
else
{
return new TextBlock { Text = "Not Found: " + name };
}
}

public bool Match(object data)
{
return data is ViewModelBase;
}
}
}
63 changes: 63 additions & 0 deletions ExampleClient/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Reactive;
using Corgibytes.Freshli.Lib;
using ReactiveUI;

namespace ExampleClient.ViewModels {
/// <summary>
/// Allow developers to run Freshli Lib commands.
/// </summary>
public class MainWindowViewModel : ViewModelBase {
/// <summary>
/// The path to the Git repo to run Freshli against.
/// </summary>
public string GitPath { get; set; }

/// <summary>
/// Show the results from Freshli.
/// </summary>
public string Results {
get => _results;
set => this.RaiseAndSetIfChanged(ref _results, value);
}
private string _results;

/// <summary>
/// Close the window.
/// </summary>
public ReactiveCommand<Unit, Unit> CloseCommand { get; }

/// <summary>
/// Run Freshli.
/// </summary>
public ReactiveCommand<Unit, Unit> OnRunFreshliClick { get; }

/// <summary>
/// Initialize the view model.
/// </summary>
public MainWindowViewModel() {
_results = "";

GitPath = "";
Results = "";

OnRunFreshliClick = ReactiveCommand.Create(RunFreshli);
CloseCommand = ReactiveCommand.Create(execute: () => { });
}

/// <summary>
/// Run Freshli and show the results to the user.
/// </summary>
private void RunFreshli() {
Results = $"Starting Freshli run for '{GitPath}'\n";

var runner = new Runner();
var metricResults = runner.Run(GitPath);

foreach (var mr in metricResults) {
Results += mr.ToString();
}

Results += "Finished!\n";
}
}
}
11 changes: 11 additions & 0 deletions ExampleClient/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Text;

namespace ExampleClient.ViewModels
{
public class ViewModelBase : ReactiveObject
{
}
}
29 changes: 29 additions & 0 deletions ExampleClient/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:ExampleClient.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="ExampleClient.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="ExampleClient"
Width="800"
Height="450">

<Design.DataContext>
<vm:MainWindowViewModel />
</Design.DataContext>

<DockPanel>
<Label DockPanel.Dock="Top" HorizontalAlignment="Center" FontSize="20">Freshli-Lib Example Client</Label>
<DockPanel Margin="10">
<DockPanel DockPanel.Dock="Top" Margin="10">
<Label DockPanel.Dock="Left">Git Repo:</Label>
<Button DockPanel.Dock="Right" Command="{Binding OnRunFreshliClick}" Margin="5, 0">Run Freshli</Button>
<TextBox Text="{Binding GitPath}" HorizontalAlignment="Stretch" />
</DockPanel>
<Button DockPanel.Dock="Bottom" Command="{Binding CloseCommand}" HorizontalAlignment="Right">Close</Button>
<TextBox DockPanel.Dock="Top" Text="{Binding Results, Mode=TwoWay}" IsReadOnly="True" Margin="0 5" />
</DockPanel>
</DockPanel>
</Window>
33 changes: 33 additions & 0 deletions ExampleClient/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Diagnostics;
using System.Reactive;
using Avalonia;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using ExampleClient.ViewModels;
using ReactiveUI;
using Avalonia.Reactive;


namespace ExampleClient.Views {
public class MainWindow : ReactiveWindow<MainWindowViewModel> {

public MainWindow() {
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif

// Close the Window when the close command is triggered.
this.WhenActivated(block: d => {
Debug.Assert(ViewModel != null, nameof(ViewModel) + " != null");
d(ViewModel.CloseCommand.Subscribe(onNext: (unit) => Close()));
}
);
}

private void InitializeComponent() {
AvaloniaXamlLoader.Load(this);
}
}
}
11 changes: 11 additions & 0 deletions ExampleClient/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
To use the Avalonia CI feed to get unstable packages, move this file to the root of your solution.
-->

<configuration>
<packageSources>
<add key="AvaloniaCI" value="https://www.myget.org/F/avalonia-ci/api/v2" />
</packageSources>
</configuration>

0 comments on commit 12af0ae

Please sign in to comment.