Add analytics service base setup to UWP sample app

このコミットが含まれているのは:
Sergio Pedri committed 2022-01-05 20:49:38 +01:00
1 親 1477e50c01
コミット c8d597018a
11ファイル変更
+198 -17

検索条件に一致するファイルはありません

+49 -2
ファイルの表示
@@ -1,4 +1,13 @@
using ComputeSharp.SwapChain.Uwp.Views;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using CommunityToolkit.Mvvm.DependencyInjection;
using ComputeSharp.SwapChain.Core.Services;
using ComputeSharp.SwapChain.Core.ViewModels;
using ComputeSharp.SwapChain.Uwp.Services;
using ComputeSharp.SwapChain.Uwp.Views;
using Microsoft.Extensions.DependencyInjection;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Core;
using Windows.UI;
@@ -13,7 +22,7 @@ namespace ComputeSharp.SwapChain.Uwp;
sealed partial class App : Application
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
@@ -26,6 +35,7 @@ sealed partial class App : Application
{
if (Window.Current.Content is not MainView)
{
ConfigureServices();
StyleTitleBar();
ExpandViewIntoTitleBar();
@@ -38,6 +48,43 @@ sealed partial class App : Application
}
}
/// <summary>
/// Configures the services for the application.
/// </summary>
private static void ConfigureServices()
{
ServiceCollection services = new();
#if RELEASE
if (Debugger.IsAttached)
{
services.AddSingleton<IAnalyticsService, DebugAnalyticsService>();
}
else
{
using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ComputeSharp.SwapChain.Uwp.Assets.ServiceTokens.AppCenter.txt");
using StreamReader reader = new(stream);
string appCenterSecret = reader.ReadToEnd().Trim();
if (Guid.TryParse(appCenterSecret, out _))
{
services.AddSingleton<IAnalyticsService>(new AppCenterService(appCenterSecret));
}
else
{
services.AddSingleton<IAnalyticsService, DebugAnalyticsService>();
}
}
#else
services.AddSingleton<IAnalyticsService, DebugAnalyticsService>();
#endif
services.AddTransient<MainViewModel>();
Ioc.Default.ConfigureServices(services.BuildServiceProvider());
}
/// <summary>
/// Styles the title bar buttons according to the theme in use
/// </summary>
+14
ファイルの表示
@@ -124,6 +124,7 @@
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\SkipLocalsInitAttribute.cs" />
<Compile Include="Services\AppCenterService.cs" />
<Compile Include="Views\MainView.xaml.cs">
<DependentUpon>MainView.xaml</DependentUpon>
</Compile>
@@ -146,6 +147,7 @@
<Link>Assets\Textures\RustyMetal.png</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<EmbeddedResource Include="Assets\ServiceTokens\AppCenter.txt" />
<Content Include="Assets\SmallTile.scale-100.png" />
<Content Include="Assets\SmallTile.scale-125.png" />
<Content Include="Assets\SmallTile.scale-150.png" />
@@ -204,6 +206,15 @@
<PackageReference Include="CommunityToolkit.Mvvm">
<Version>7.1.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.AppCenter.Analytics">
<Version>4.4.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.AppCenter.Crashes">
<Version>4.4.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
<Version>6.0.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.13</Version>
</PackageReference>
@@ -232,6 +243,9 @@
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(SolutionDir)\build\CreateBlankAppCenterToken.ps1</PreBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
+41
ファイルの表示
@@ -0,0 +1,41 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using ComputeSharp.SwapChain.Core.Services;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
#nullable enable
namespace ComputeSharp.SwapChain.Uwp.Services;
/// <summary>
/// An <see cref="IAnalyticsService"/> implementation using AppCenter.
/// </summary>
public sealed class AppCenterService : IAnalyticsService
{
/// <summary>
/// The maximum length for any property name and value
/// </summary>
/// <remarks>It's 125, but one character is reserved for the leading '|' to indicate trimming.</remarks>
private const int PropertyStringMaxLength = 124;
/// <inheritdoc/>
public AppCenterService(string secret)
{
AppCenter.Start(secret, typeof(Crashes), typeof(Analytics));
}
/// <inheritdoc/>
public void Log(string title, params (string Property, string Value)[]? data)
{
IDictionary<string, string>? properties = data?.ToDictionary(
pair => pair.Property,
pair => pair.Value.Length <= PropertyStringMaxLength
? pair.Value
: $"|{pair.Value.Substring(pair.Value.Length - PropertyStringMaxLength)}");
Analytics.TrackEvent(title, properties);
}
}
-4
ファイルの表示
@@ -11,16 +11,12 @@
xmlns:media="using:Microsoft.Toolkit.Uwp.UI.Media"
xmlns:models="using:ComputeSharp.SwapChain.Core.Models"
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
xmlns:viewmodels="using:ComputeSharp.SwapChain.Core.ViewModels"
d:DesignHeight="300"
d:DesignWidth="400"
SizeChanged="UserControl_SizeChanged"
mc:Ignorable="d">
<Grid x:Name="Root">
<Grid.DataContext>
<viewmodels:MainViewModel x:Name="ViewModel" />
</Grid.DataContext>
<Grid.Resources>
<!--
+8
ファイルの表示
@@ -1,4 +1,6 @@
using System;
using CommunityToolkit.Mvvm.DependencyInjection;
using ComputeSharp.SwapChain.Core.ViewModels;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
@@ -13,8 +15,14 @@ public sealed partial class MainView : UserControl
public MainView()
{
this.InitializeComponent();
this.DataContext = Ioc.Default.GetRequiredService<MainViewModel>();
}
/// <summary>
/// Gets the <see cref="MainViewModel"/> instance for the current view.
/// </summary>
public MainViewModel ViewModel => (MainViewModel)DataContext;
// Opens the shader selection panel
private void OpenShaderSelectionPanelButton_Click(object sender, RoutedEventArgs e)
{