Skip to content

Commit

Permalink
Add updater for legacy app
Browse files Browse the repository at this point in the history
  • Loading branch information
irusanov committed Jun 12, 2021
1 parent 5f97e5d commit d319fc6
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 10 deletions.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions WPF-no-themes/App.xaml.cs
Expand Up @@ -15,6 +15,7 @@ public partial class App : Application
internal static Mutex instanceMutex = null;
internal bool createdNew = false;
public readonly AppSettings settings = new AppSettings().Load();
public Updater updater;

protected override void OnStartup(StartupEventArgs e)
{
Expand All @@ -28,6 +29,8 @@ protected override void OnStartup(StartupEventArgs e)
Environment.Exit(0);
}

updater = new Updater(settings);

GC.KeepAlive(instanceMutex);
base.OnStartup(e);
SplashWindow.Start(settings);
Expand Down
4 changes: 2 additions & 2 deletions WPF-no-themes/Properties/BuildNumberTemplate.cs
Expand Up @@ -5,5 +5,5 @@

using System.Reflection;

[assembly: AssemblyVersion("1.2.4.277")]
[assembly: AssemblyFileVersion("1.2.4.277")]
[assembly: AssemblyVersion("1.2.4.284")]
[assembly: AssemblyFileVersion("1.2.4.284")]
182 changes: 182 additions & 0 deletions WPF-no-themes/Updater.cs
@@ -0,0 +1,182 @@
using AutoUpdaterDotNET;
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Xml.Serialization;
using ZenTimings.Windows;

namespace ZenTimings
{
public partial class Updater
{
public event EventHandler UpdateCheckCompleteEvent;

//public static int status = 0;
private static bool manual = false;
private static string ChangelogText { get; set; }
private const string url = "https://zentimings.protonrom.com/AutoUpdater.xml";

protected virtual void OnUpdateCheckCompleteEvent(EventArgs e)
{
// Make a temporary copy of the event to avoid possibility of
// a race condition if the last subscriber unsubscribes
// immediately after the null check and before the event is raised.
UpdateCheckCompleteEvent?.Invoke(this, e);
}

public Updater(AppSettings settingsInstance)
{
Init(settingsInstance);
}

public void Init(AppSettings settings)
{
AutoUpdater.RunUpdateAsAdmin = true;
AutoUpdater.Synchronous = true;
AutoUpdater.LetUserSelectRemindLater = false;
AutoUpdater.RemindLaterTimeSpan = RemindLaterFormat.Days;
AutoUpdater.RemindLaterAt = 3;
AutoUpdater.DownloadPath = Environment.CurrentDirectory;
AutoUpdater.PersistenceProvider = new UpdaterPersistenceProvider(settings);
//status = 1;
}

public void CheckForUpdate(bool manualUpdate = false)
{
/*if (status == 0)
{
Init((Application.Current as App).settings);
}*/

AutoUpdater.ParseUpdateInfoEvent += AutoUpdaterOnParseUpdateInfoEvent;
AutoUpdater.CheckForUpdateEvent += AutoUpdaterOnCheckForUpdateEvent;

if (!manualUpdate)
{
SplashWindow.Loading("Checking for updates");
}

manual = manualUpdate;

AutoUpdater.Start(url);
}

private void AutoUpdaterOnParseUpdateInfoEvent(ParseUpdateInfoEventArgs args)
{
try
{
ChangelogText = $"\nChangelog{Environment.NewLine}";

using (StringReader txtReader = new StringReader(args.RemoteData))
{
XmlSerializer xmls = new XmlSerializer(typeof(UpdaterArgs));
UpdaterArgs updaterArgs = xmls.Deserialize(txtReader) as UpdaterArgs;

args.UpdateInfo = new UpdateInfoEventArgs
{
CurrentVersion = updaterArgs.Version,
DownloadURL = updaterArgs.Url,
ChangelogURL = updaterArgs.Changelog,
Mandatory = new Mandatory
{
Value = manual,
UpdateMode = Mode.Normal,
},
CheckSum = new CheckSum
{
Value = updaterArgs.Checksum.Value,
HashingAlgorithm = updaterArgs.Checksum.algorithm
}
};

foreach (string change in updaterArgs.Changes)
{
ChangelogText += $" - {change}{Environment.NewLine}";
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

private void AutoUpdaterOnCheckForUpdateEvent(UpdateInfoEventArgs args)
{
if (args.Error == null)
{
if (args.IsUpdateAvailable)
{
MessageBoxResult result = MessageBox.Show(
$"There is new version {args.CurrentVersion} available.{Environment.NewLine}" +
$"You are using version {args.InstalledVersion}.{Environment.NewLine}" +
$"{ChangelogText}{Environment.NewLine}" +
$"Do you want to update the application now?",
@"Update Available",
MessageBoxButton.YesNo
);

if (!manual)
{
SplashWindow.splash.Hide();
}

if (result.Equals(MessageBoxResult.Yes))
{
try
{
if (AutoUpdater.DownloadUpdate(args))
{
if (!manual)
{
SplashWindow.Stop();
}
Application.Current.Shutdown();
Environment.Exit(0);
}
}
catch (Exception exception)
{
MessageBox.Show(
exception.Message,
exception.GetType().ToString(),
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}

if (!manual)
{
SplashWindow.splash.Show();
}
}
else if (manual)
{
OnUpdateCheckCompleteEvent(new EventArgs());
}
}
else
{
if (args.Error is WebException)
{
MessageBox.Show(
@"There is a problem reaching update server. Please check your internet connection and try again later.",
@"Update Check Failed",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
else
{
MessageBox.Show(
args.Error.Message,
args.Error.GetType().ToString(),
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
AutoUpdater.ParseUpdateInfoEvent -= AutoUpdaterOnParseUpdateInfoEvent;
AutoUpdater.CheckForUpdateEvent -= AutoUpdaterOnCheckForUpdateEvent;
}
}
}
33 changes: 33 additions & 0 deletions WPF-no-themes/Windows/AboutDialog.xaml
Expand Up @@ -13,6 +13,27 @@
WindowStartupLocation="CenterOwner"
Icon="/ZenTimings-legacy;component/Resources/icon-16.png">
<StackPanel x:Name="AboutWindowContent">
<Popup
x:Name="aboutWindowPopup"
Panel.ZIndex="100"
PlacementTarget="{Binding ElementName=AboutWindowContent}"
VerticalAlignment="Center"
Placement="Relative"
HorizontalAlignment="Center"
VerticalOffset="0"
AllowsTransparency="True"
Opacity="0.8"
PopupAnimation="Fade"
ClipToBounds="True"
StaysOpen="False"
ScrollViewer.VerticalScrollBarVisibility="Disabled" MouseDown="AboutWindowPopup_MouseDown">
<TextBlock
Name="optionsPopupText"
Background="{DynamicResource SuccessBackground}"
Padding="10" Opacity="0.95">
ZenTimings is up to date.
</TextBlock>
</Popup>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
Expand Down Expand Up @@ -85,6 +106,18 @@
</TabItem>
</TabControl>

<Button
x:Name="CheckUpdateBtn"
Grid.Column="0"
Grid.Row="1"
Content="Check for Update"
IsCancel="False"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Width="Auto"
Margin="10 10 0 10"
Click="CheckUpdateBtn_Click" />

<Button
Grid.Column="1"
Grid.Row="1"
Expand Down
39 changes: 39 additions & 0 deletions WPF-no-themes/Windows/AboutDialog.xaml.cs
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Reflection;
using System.Windows;
using System.Windows.Threading;

namespace ZenTimings.Windows
{
Expand All @@ -11,6 +12,9 @@ namespace ZenTimings.Windows
/// </summary>
public partial class AboutDialog : Window
{
private static readonly Updater updater = (Application.Current as App).updater;
private DispatcherTimer notificationTimer;

public AboutDialog()
{
var AssemblyTitle = ((AssemblyTitleAttribute)Attribute.GetCustomAttribute(
Expand Down Expand Up @@ -74,12 +78,47 @@ public AboutDialog()
}

Modules.ItemsSource = appModules;
updater.UpdateCheckCompleteEvent += Updater_UpdateCheckCompleteEvent;
}

private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}

private void CheckUpdateBtn_Click(object sender, System.Windows.RoutedEventArgs e)
{
updater.CheckForUpdate(true);
}

private void Updater_UpdateCheckCompleteEvent(object sender, EventArgs e)
{
if (notificationTimer != null)
{
if (notificationTimer.IsEnabled) notificationTimer.Stop();
}

notificationTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(6000)
};

notificationTimer.Tick += new EventHandler((s, x) =>
{
notificationTimer.Stop();
aboutWindowPopup.IsOpen = false;
});

notificationTimer.Start();

aboutWindowPopup.Width = AboutWindowContent.ActualWidth;
aboutWindowPopup.IsOpen = true;
}

private void AboutWindowPopup_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
aboutWindowPopup.IsOpen = false;
}
}
}
11 changes: 10 additions & 1 deletion WPF-no-themes/Windows/OptionsDialog.xaml
Expand Up @@ -7,7 +7,7 @@
mc:Ignorable="d"
x:Name="OptionsWindow"
Title="Options"
Height="150"
Height="173.664"
Width="306"
ResizeMode="NoResize"
WindowStartupLocation="CenterOwner"
Expand Down Expand Up @@ -50,6 +50,7 @@
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>

<CheckBox
Expand Down Expand Up @@ -82,6 +83,14 @@
Content="ms"
VerticalAlignment="Center"
Padding="5 0" FontWeight="Normal" />

<CheckBox
x:Name="checkBoxCheckUpdate"
Grid.Column="0"
Grid.Row="2"
Grid.ColumnSpan="3"
Margin="0 5 5 5"
Content="Check For Updates On Startup" />
</Grid>

<Border Style="{DynamicResource Separator}" Margin="0 20 0 0"/>
Expand Down
2 changes: 2 additions & 0 deletions WPF-no-themes/Windows/OptionsDialog.xaml.cs
Expand Up @@ -30,6 +30,7 @@ public OptionsDialog(AppSettings settings, DispatcherTimer timer)
checkBoxAutoRefresh.IsChecked = settings.AutoRefresh;
checkBoxAutoRefresh.IsEnabled = settings.AdvancedMode;
checkBoxAdvancedMode.IsChecked = settings.AdvancedMode;
checkBoxCheckUpdate.IsChecked = settings.CheckForUpdates;
numericUpDownRefreshInterval.IsEnabled = settings.AutoRefresh && settings.AdvancedMode;
msText.IsEnabled = numericUpDownRefreshInterval.IsEnabled;
}
Expand All @@ -52,6 +53,7 @@ private void ButtonSettingsApply_Click(object sender, RoutedEventArgs e)
settingsInstance.AutoRefresh = (bool)checkBoxAutoRefresh.IsChecked;
settingsInstance.AutoRefreshInterval = Convert.ToInt32(numericUpDownRefreshInterval.Text);
settingsInstance.AdvancedMode = (bool)checkBoxAdvancedMode.IsChecked;
settingsInstance.CheckForUpdates = (bool)checkBoxCheckUpdate.IsChecked;

settingsInstance.Save();

Expand Down

0 comments on commit d319fc6

Please sign in to comment.