Browse Source

Fixed installer and hellp there popup bug

CPKreuz 4 years ago
parent
commit
2e520313e1

+ 2 - 1
PixiEditor/Models/Dialogs/ConfirmationDialog.cs

@@ -9,7 +9,8 @@ namespace PixiEditor.Models.Dialogs
         {
             ConfirmationPopup popup = new ConfirmationPopup
             {
-                Body = message
+                Body = message,
+                Topmost = true
             };
             if ((bool)popup.ShowDialog())
             {

+ 16 - 2
PixiEditor/Models/Dialogs/NoticeDialog.cs

@@ -6,9 +6,23 @@ namespace PixiEditor.Models.Dialogs
     {
         public static void Show(string message)
         {
-            NoticePopup popup = new NoticePopup
+            NoticePopup popup = new ()
             {
-                Body = message
+                Body = message,
+                Title = string.Empty,
+                Topmost = true
+            };
+
+            popup.ShowDialog();
+        }
+
+        public static void Show(string message, string title)
+        {
+            NoticePopup popup = new ()
+            {
+                Body = message,
+                Title = title,
+                Topmost = true
             };
 
             popup.ShowDialog();

+ 2 - 2
PixiEditor/Properties/AssemblyInfo.cs

@@ -50,5 +50,5 @@ using System.Windows;
 // You can specify all the values or you can default the Build and Revision Numbers
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("0.2.0.0")]
-[assembly: AssemblyFileVersion("0.2.0.0")]
+[assembly: AssemblyVersion("0.1.4.0")]
+[assembly: AssemblyFileVersion("0.1.4.0")]

+ 93 - 32
PixiEditor/ViewModels/SubViewModels/Main/UpdateViewModel.cs

@@ -57,54 +57,64 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public async Task<bool> CheckForUpdate()
         {
-            return await Task.Run(async () =>
+            bool updateAvailable = await UpdateChecker.CheckUpdateAvailable();
+            bool updateCompatible = await UpdateChecker.IsUpdateCompatible();
+            bool updateFileDoesNotExists = !File.Exists(
+                Path.Join(UpdateDownloader.DownloadLocation, $"update-{UpdateChecker.LatestReleaseInfo.TagName}.zip"));
+            bool updateExeDoesNotExists = !File.Exists(
+                Path.Join(UpdateDownloader.DownloadLocation, $"update-{UpdateChecker.LatestReleaseInfo.TagName}.exe"));
+            if (updateAvailable && updateFileDoesNotExists && updateExeDoesNotExists)
             {
-                bool updateAvailable = await UpdateChecker.CheckUpdateAvailable();
-                bool updateCompatible = await UpdateChecker.IsUpdateCompatible();
-                bool updateFileDoesNotExists = !File.Exists(
-                    Path.Join(UpdateDownloader.DownloadLocation, $"update-{UpdateChecker.LatestReleaseInfo.TagName}.zip"));
-                bool updateExeDoesNotExists = !File.Exists(
-                    Path.Join(UpdateDownloader.DownloadLocation, $"update-{UpdateChecker.LatestReleaseInfo.TagName}.exe"));
-                if (updateAvailable && updateFileDoesNotExists && updateExeDoesNotExists)
+                VersionText = "Downloading update...";
+                if (updateCompatible)
                 {
-                    VersionText = "Downloading update...";
-                    if (updateCompatible)
-                    {
-                        await UpdateDownloader.DownloadReleaseZip(UpdateChecker.LatestReleaseInfo);
-                    }
-                    else
-                    {
-                        await UpdateDownloader.DownloadInstaller(UpdateChecker.LatestReleaseInfo);
-                    }
-
-                    UpdateReadyToInstall = true;
-                    return true;
+                    await UpdateDownloader.DownloadReleaseZip(UpdateChecker.LatestReleaseInfo);
+                }
+                else
+                {
+                    await UpdateDownloader.DownloadInstaller(UpdateChecker.LatestReleaseInfo);
                 }
 
-                return false;
-            });
+                UpdateReadyToInstall = true;
+                return true;
+            }
+
+            return false;
         }
 
-        private async void Owner_OnStartupEvent(object sender, EventArgs e)
+        private static void AskToInstall()
         {
-            if (IPreferences.Current.GetPreference("CheckUpdatesOnStartup", true))
+            string dir = AppDomain.CurrentDomain.BaseDirectory;
+            UpdateDownloader.CreateTempDirectory();
+            bool updateZipExists = Directory.GetFiles(UpdateDownloader.DownloadLocation, "update-*.zip").Length > 0;
+            string[] updateExeFiles = Directory.GetFiles(UpdateDownloader.DownloadLocation, "update-*.exe");
+            bool updateExeExists = updateExeFiles.Length > 0;
+
+            string updaterPath = Path.Join(dir, "PixiEditor.UpdateInstaller.exe");
+
+            if (updateZipExists || updateExeExists)
             {
-                try
+                ViewModelMain.Current.UpdateSubViewModel.UpdateReadyToInstall = true;
+                var result = ConfirmationDialog.Show("Update is ready to install. Do you want to install it now?");
+                if (result == Models.Enums.ConfirmationType.Yes)
                 {
-                    await CheckForUpdate();
-                }
-                catch (System.Net.Http.HttpRequestException)
-                {
-                    NoticeDialog.Show("Could not check if there's an update available");
+                    if (updateZipExists && File.Exists(updaterPath))
+                    {
+                        InstallHeadless(updaterPath);
+                    }
+                    else if (updateExeExists)
+                    {
+                        OpenExeInstaller(updateExeFiles[0]);
+                    }
                 }
             }
         }
 
-        private void RestartApplication(object parameter)
+        private static void InstallHeadless(string updaterPath)
         {
             try
             {
-                ProcessHelper.RunAsAdmin(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "PixiEditor.UpdateInstaller.exe"));
+                ProcessHelper.RunAsAdmin(updaterPath);
                 Application.Current.Shutdown();
             }
             catch (Win32Exception)
@@ -117,6 +127,57 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             }
         }
 
+        private static void OpenExeInstaller(string updateExeFile)
+        {
+            bool alreadyUpdated = AssemblyHelper.GetCurrentAssemblyVersion() ==
+                    updateExeFile.Split('-')[1].Split(".exe")[0];
+
+            if (!alreadyUpdated)
+            {
+                RestartToUpdate(updateExeFile);
+            }
+            else
+            {
+                File.Delete(updateExeFile);
+            }
+        }
+
+        private static void RestartToUpdate(string updateExeFile)
+        {
+            Process.Start(updateExeFile);
+            Application.Current.Shutdown();
+        }
+
+        private static void RestartApplication(object parameter)
+        {
+            try
+            {
+                ProcessHelper.RunAsAdmin(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "PixiEditor.UpdateInstaller.exe"));
+                Application.Current.Shutdown();
+            }
+            catch (Win32Exception)
+            {
+                NoticeDialog.Show("Couldn't update without administrator rights.", "Insufficient permissions");
+            }
+        }
+
+        private async void Owner_OnStartupEvent(object sender, EventArgs e)
+        {
+            if (IPreferences.Current.GetPreference("CheckUpdatesOnStartup", true))
+            {
+                try
+                {
+                    await CheckForUpdate();
+                }
+                catch (System.Net.Http.HttpRequestException)
+                {
+                    NoticeDialog.Show("Could not check if there's an update available");
+                }
+
+                AskToInstall();
+            }
+        }
+
         private void InitUpdateChecker()
         {
             string version = AssemblyHelper.GetCurrentAssemblyVersion();

+ 8 - 1
PixiEditor/Views/Dialogs/HelloTherePopup.xaml.cs

@@ -42,6 +42,8 @@ namespace PixiEditor.Views.Dialogs
 
         public RelayCommand OpenHyperlinkCommand { get => FileViewModel.Owner.MiscSubViewModel.OpenHyperlinkCommand; }
 
+        private bool isClosing;
+
         public HelloTherePopup(FileViewModel fileViewModel)
         {
             DataContext = this;
@@ -54,6 +56,8 @@ namespace PixiEditor.Views.Dialogs
             RecentlyOpenedEmpty = RecentlyOpened.Count == 0;
             RecentlyOpened.CollectionChanged += RecentlyOpened_CollectionChanged;
 
+            Closing += (_, _) => { isClosing = true; };
+
             InitializeComponent();
 
             if (RecentlyOpenedEmpty)
@@ -81,7 +85,10 @@ namespace PixiEditor.Views.Dialogs
         [Conditional("RELEASE")]
         private void CloseIfRelease()
         {
-            Close();
+            if (!isClosing)
+            {
+                Close();
+            }
         }
 
         private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)

+ 3 - 0
PixiEditor/Views/Dialogs/NoticePopup.xaml

@@ -25,6 +25,9 @@
         <TextBlock Grid.Row="1" Text="{Binding Body, ElementName=popup}" HorizontalAlignment="Center"
                    VerticalAlignment="Center" FontSize="18" Foreground="White" />
         <DockPanel Grid.Row="0" Background="{StaticResource MainColor}">
+            <TextBlock Text="{Binding Title, ElementName=popup}" 
+                       FontSize="18" Foreground="White"
+                       VerticalAlignment="Center" Margin="5,0,0,0"/>
             <Button DockPanel.Dock="Right" HorizontalAlignment="Right" Style="{StaticResource CloseButtonStyle}"
                     WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Close"
                     Command="{Binding DataContext.CancelCommand, ElementName=popup}" />

+ 6 - 0
PixiEditor/Views/Dialogs/NoticePopup.xaml.cs

@@ -22,6 +22,12 @@ namespace PixiEditor.Views.Dialogs
         public static readonly DependencyProperty BodyProperty =
             DependencyProperty.Register(nameof(Body), typeof(string), typeof(NoticePopup));
 
+        public new string Title
+        {
+            get => base.Title;
+            set => base.Title = value;
+        }
+
         public string Body
         {
             get => (string)GetValue(BodyProperty);