Skip to content

Commit

Permalink
Enrollment WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ashokgelal committed Apr 25, 2017
1 parent 9825c05 commit 1f56a9c
Show file tree
Hide file tree
Showing 15 changed files with 312 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/Magpie/Magpie.Example/Magpie.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@
<Name>Magpie</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Resource Include="magpie.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- 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.
Expand Down
4 changes: 2 additions & 2 deletions src/Magpie/Magpie.Example/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ private void ForceCheck_OnClick(object sender, RoutedEventArgs e)

private static AppInfo MakeAppInfo(int id)
{
var appInfo = new AppInfo("https://dl.dropbox.com/s/j6i7s64ooice8rt/appcast.json", id);
appInfo.SetAppIcon("Magpie.Example", "logo64x64.tiff");
var appInfo = new AppInfo("https://dl.dropbox.com/s/byii1f3d7qqliwx/appcast_email_enrollment.json?dl=0", id);
appInfo.SetAppIcon("MagpieExample", "magpie.png");
return appInfo;
}

Expand Down
Binary file added src/Magpie/Magpie.Example/magpie.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/Magpie/Magpie.latest.nupkg
Binary file not shown.
13 changes: 13 additions & 0 deletions src/Magpie/Magpie/Magpie.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@
<Compile Include="Services\AnalyticsLogger.cs" />
<Compile Include="Services\AppInfo.cs" />
<Compile Include="Services\AssemblyAccessor.cs" />
<Compile Include="Services\CheckState.cs" />
<Compile Include="Services\DefaultRemoteContentDownloader.cs" />
<Compile Include="Services\MainAssembly.cs" />
<Compile Include="Services\SignatureVerifier.cs" />
<Compile Include="Models\Enrollment.cs" />
<Compile Include="ViewModels\DownloadWindowViewModel.cs" />
<Compile Include="Services\Helpers.cs" />
<Compile Include="Services\MagicStrings.cs" />
Expand All @@ -81,9 +83,13 @@
<Compile Include="ViewModels\DelegateCommand.cs" />
<Compile Include="ViewModels\MainWindowViewModel.cs" />
<Compile Include="ViewModels\SignatureVerificationWindowViewModel.cs" />
<Compile Include="ViewModels\EnrollmentViewModel.cs" />
<Compile Include="Views\DownloadWindow.xaml.cs">
<DependentUpon>DownloadWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Views\EnrollmentWindow.xaml.cs">
<DependentUpon>EnrollmentWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Views\ErrorWindow.xaml.cs">
<DependentUpon>ErrorWindow.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -138,6 +144,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\EnrollmentWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\ErrorWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand All @@ -164,6 +174,9 @@
<ItemGroup>
<Resource Include="Resources\download.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\mail.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- 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.
Expand Down
3 changes: 3 additions & 0 deletions src/Magpie/Magpie/Models/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class Channel
[DataMember(Name = "dsa_signature", IsRequired = false)]
public string DSASignature { get; private set; }

[DataMember(Name = "requires_enrollment", IsRequired = false)]
public bool RequiresEnrollment { get; private set; }

// Dates example:
// e.g. January 30, 2015 18:15:00 +0200
// 10/03/2015
Expand Down
15 changes: 15 additions & 0 deletions src/Magpie/Magpie/Models/Enrollment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace MagpieUpdater.Models
{
public class Enrollment
{
public Channel Channel { get; private set; }
public bool IsRequired { get; internal set; }
public bool IsEnrolled { get; internal set; }
public string Email { get; internal set; }

public Enrollment(Channel channel)
{
Channel = channel;
}
}
}
20 changes: 15 additions & 5 deletions src/Magpie/Magpie/Resources/Styles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<SolidColorBrush x:Key="_magpieSkipBtnFgBrush" Color="Crimson" />
<SolidColorBrush x:Key="_magpieRemindLaterBtnBgBrush" Color="Transparent" />
<SolidColorBrush x:Key="_magpieRemindLaterBtnFgBrush" Color="#404040" />
<SolidColorBrush x:Key="_magpieSecondaryBtnBgBrush" Color="Transparent" />
<SolidColorBrush x:Key="_magpieSecondaryBtnFgBrush" Color="#404040" />
<SolidColorBrush x:Key="_magpieHeaderFgBrush" Color="Black" />
<SolidColorBrush x:Key="_magpieMetadataLblFgBrush" Color="#777777" />
<SolidColorBrush x:Key="_magpieMetadataValueFgBrush" Color="#222222" />
Expand Down Expand Up @@ -50,33 +52,41 @@
</Setter.Value>
</Setter>
</Style>

<Style x:Key="_magpiePrimaryBtn"
BasedOn="{StaticResource _magpieButtonBase}"
TargetType="Button">
<Setter Property="Background" Value="{DynamicResource _magpiePrimaryBtnBgBrush}" />
<Setter Property="IsDefault" Value="True" />
</Style>
<Style x:Key="_magpieSkipBtn"

<Style x:Key="_magpieSecondaryBtn"
BasedOn="{StaticResource _magpieButtonBase}"
TargetType="Button">
<Setter Property="FontSize" Value="14" />
<Setter Property="Background" Value="{DynamicResource _magpieSecondaryBtnBgBrush}" />
<Setter Property="Foreground" Value="{DynamicResource _magpieSecondaryBtnFgBrush}" />
</Style>

<Style x:Key="_magpieSkipBtn"
BasedOn="{StaticResource _magpieSecondaryBtn}"
TargetType="Button">
<Setter Property="Background" Value="{DynamicResource _magpieSkipBtnBgBrush}" />
<Setter Property="Foreground" Value="{DynamicResource _magpieSkipBtnFgBrush}" />
</Style>

<Style x:Key="_magpieRemindLaterBtn"
BasedOn="{StaticResource _magpieButtonBase}"
BasedOn="{StaticResource _magpieSecondaryBtn}"
TargetType="Button">
<Setter Property="FontSize" Value="14" />
<Setter Property="Background" Value="{DynamicResource _magpieRemindLaterBtnBgBrush}" />
<Setter Property="Foreground" Value="{DynamicResource _magpieRemindLaterBtnFgBrush}" />
</Style>

<Style x:Key="_magpieDownloadNowBtn"
BasedOn="{StaticResource _magpiePrimaryBtn}"
TargetType="Button">
<Setter Property="Padding" Value="10 6" />
<Setter Property="FontWeight" Value="500" />
<Setter Property="Padding" Value="10 6" />
<Setter Property="FontWeight" Value="500" />
</Style>

<Style x:Key="_magpieWindowHeader" TargetType="TextBlock">
Expand Down
Binary file added src/Magpie/Magpie/Resources/mail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/Magpie/Magpie/Services/AppInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class AppInfo
public int SubscribedChannel { get; set; }
public bool DisableMagpieBranding { get; set; }
public bool InteropWithWinForm { get; set; }
public bool RequiresEnrollment { get; set; }

public void SetAppIcon(string imageNamespace, string imagePath)
{
Expand Down
9 changes: 9 additions & 0 deletions src/Magpie/Magpie/Services/CheckState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MagpieUpdater.Services
{
internal enum CheckState
{
InBackground,
Force,
ChannelSwitch
}
}
64 changes: 53 additions & 11 deletions src/Magpie/Magpie/Services/Magpie.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using MagpieUpdater.Interfaces;
Expand Down Expand Up @@ -34,32 +35,32 @@ public Magpie(AppInfo appInfo, IDebuggingInfoLogger debuggingInfoLogger = null,

public async void CheckInBackground(string appcastUrl = null, bool showDebuggingWindow = false)
{
await Check(appcastUrl ?? AppInfo.AppCastUrl, AppInfo.SubscribedChannel, showDebuggingWindow)
.ConfigureAwait(false);
await Check(appcastUrl ?? AppInfo.AppCastUrl, CheckState.InBackground, AppInfo.SubscribedChannel,
showDebuggingWindow).ConfigureAwait(false);
}

public async void ForceCheckInBackground(string appcastUrl = null, bool showDebuggingWindow = false)
{
await Check(appcastUrl ?? AppInfo.AppCastUrl, AppInfo.SubscribedChannel, showDebuggingWindow, true)
.ConfigureAwait(false);
await Check(appcastUrl ?? AppInfo.AppCastUrl, CheckState.Force, AppInfo.SubscribedChannel,
showDebuggingWindow).ConfigureAwait(false);
}

public async void SwitchSubscribedChannel(int channelId, bool showDebuggingWindow = false)
{
AppInfo.SubscribedChannel = channelId;
await Check(AppInfo.AppCastUrl, channelId, showDebuggingWindow, true).ConfigureAwait(false);
await Check(AppInfo.AppCastUrl, CheckState.ChannelSwitch, channelId, showDebuggingWindow)
.ConfigureAwait(false);
}

private async Task Check(string appcastUrl, int channelId = 1, bool showDebuggingWindow = false,
bool forceCheck = false)
private async Task Check(string appcastUrl, CheckState checkState, int channelId = 1, bool showDebuggingWindow = false)
{
_logger.Log(string.Format("Starting fetching remote channel content from address: {0}", appcastUrl));
try
{
var data = await RemoteContentDownloader.DownloadStringContent(appcastUrl, _logger).ConfigureAwait(true);
if (string.IsNullOrWhiteSpace(data))
{
if (forceCheck)
if (checkState == CheckState.Force)
{
ShowErrorWindow();
}
Expand All @@ -68,13 +69,26 @@ private async Task Check(string appcastUrl, int channelId = 1, bool showDebuggin

var appcast = ParseAppcast(data);
OnRemoteAppcastAvailableEvent(new SingleEventArgs<RemoteAppcast>(appcast));

if (checkState == CheckState.ChannelSwitch)
{
var enrollment = Enroll(appcast, channelId);
// OnEnrolledEvent(enrollment);
if (enrollment.IsRequired && !enrollment.IsEnrolled)
{
// todo: return false
return;
}
}

var channelToUpdateFrom = BestChannelFinder.Find(channelId, appcast.Channels);
if (UpdateDecider.ShouldUpdate(channelToUpdateFrom, forceCheck))

if (UpdateDecider.ShouldUpdate(channelToUpdateFrom, checkState == CheckState.Force))
{
_analyticsLogger.LogUpdateAvailable(channelToUpdateFrom);
await ShowUpdateWindow(channelToUpdateFrom);
}
else if (forceCheck)
else if (checkState == CheckState.Force)
{
ShowNoUpdatesWindow();
}
Expand All @@ -89,6 +103,34 @@ private async Task Check(string appcastUrl, int channelId = 1, bool showDebuggin
}
}

private Enrollment Enroll(RemoteAppcast appcast, int channelId)
{
var channel = appcast.Channels.FirstOrDefault(c => c.Id == channelId);
var enrollment = new Enrollment(channel);
if (channel == null)
{
return enrollment;
}

enrollment.IsRequired = channel.RequiresEnrollment;
if (enrollment.IsRequired)
{
ShowEnrollmentWindow(enrollment);
}

// check enrollment
return enrollment;
}

protected virtual void ShowEnrollmentWindow(Enrollment enrollment)
{
var viewModel = new EnrollmentViewModel(enrollment, AppInfo);
var window = new EnrollmentWindow { DataContext = viewModel };
SetOwner(window);
OnWindowWillBeDisplayed(window, enrollment.Channel);
window.ShowDialog();
}

protected virtual async Task ShowUpdateWindow(Channel channel)
{
var viewModel = new MainWindowViewModel(AppInfo, _logger, RemoteContentDownloader, _analyticsLogger);
Expand Down Expand Up @@ -159,7 +201,7 @@ protected virtual async Task ShowDownloadWindow(Channel channel)

var savedAt = await viewModel.StartAsync(channel, artifactPath).ConfigureAwait(true);
finishedDownloading[0] = true;
((DelegateCommand)viewModel.ContinueWithInstallationCommand).RaiseCanExecuteChanged();
((DelegateCommand) viewModel.ContinueWithInstallationCommand).RaiseCanExecuteChanged();

if (string.IsNullOrWhiteSpace(savedAt))
{
Expand Down
90 changes: 90 additions & 0 deletions src/Magpie/Magpie/ViewModels/EnrollmentViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using MagpieUpdater.Models;
using MagpieUpdater.Properties;
using MagpieUpdater.Services;

namespace MagpieUpdater.ViewModels
{
internal class EnrollmentViewModel : BindableBase
{
private string _emailAddress;
private readonly Enrollment _enrollment;
private string _channelName;
private string _appIconPath;

public string AppIconPath
{
get { return _appIconPath; }
set { SetProperty(ref _appIconPath, value); }
}

public string EmailAddress
{
get { return _emailAddress; }
set
{
_emailAddress = value;
SetProperty(ref _emailAddress, value);
EnrollCommand.RaiseCanExecuteChanged();
}
}

public string ChannelName
{
get { return _channelName; }
set
{
_channelName = value;
SetProperty(ref _channelName, value);

}
}

public DelegateCommand EnrollCommand { get; set; }

public EnrollmentViewModel(Enrollment enrollment, AppInfo appInfo)
{
_enrollment = enrollment;
AppIconPath = appInfo.AppIconPath;
ChannelName = _enrollment.Channel.Build;
EnrollCommand = new DelegateCommand(EnrollCommandHandler, CanEnroll);
}

private bool CanEnroll(object obj)
{
return ValidateEmailFormat();
}

private void EnrollCommandHandler(object obj)
{
_enrollment.IsEnrolled = true;
_enrollment.Email = EmailAddress;
}

private bool ValidateEmailFormat()
{
if (string.IsNullOrWhiteSpace(EmailAddress))
{
return false;
}
try
{
#pragma warning disable 168
var m = new MailAddress(EmailAddress);
#pragma warning restore 168
return true;
}
catch (FormatException)
{
return false;
}
}

}
}
Loading

0 comments on commit 1f56a9c

Please sign in to comment.