Browse Source

Added restart button and improved version comaprison

flabbet 4 years ago
parent
commit
bf63fc84ac

+ 25 - 1
PixiEditor.UpdateModule/UpdateChecker.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Globalization;
 using System.Net.Http;
 using System.Text.Json;
 using System.Threading.Tasks;
@@ -24,8 +25,31 @@ namespace PixiEditor.UpdateModule
         }
 
         public bool CheckUpdateAvailable(ReleaseInfo latestRelease)
+        {          
+            return latestRelease.WasDataFetchSuccessfull && VersionBigger(CurrentVersionTag, latestRelease.TagName);
+        }
+
+        /// <summary>
+        /// Compares version strings and returns true if newVer > originalVer
+        /// </summary>
+        /// <param name="originalVer"></param>
+        /// <param name="newVer"></param>
+        /// <returns></returns>
+        public static bool VersionBigger(string originalVer, string newVer)
+        {
+            if(ParseVersionString(originalVer, out float ver1))
+            {
+                if (ParseVersionString(newVer, out float ver2))
+                {
+                    return ver2 > ver1;
+                }
+            }
+            return false;
+        }
+
+        private static bool ParseVersionString(string versionString, out float version)
         {
-            return latestRelease.WasDataFetchSuccessfull && latestRelease.TagName != CurrentVersionTag;
+            return float.TryParse(versionString.Replace(".", "").Insert(1, "."), NumberStyles.Any, CultureInfo.InvariantCulture, out version);
         }
 
         public async Task<ReleaseInfo> GetLatestReleaseInfo()

+ 3 - 3
PixiEditor/Properties/AssemblyInfo.cs

@@ -8,7 +8,7 @@ using System.Windows;
 [assembly: AssemblyTitle("PixiEditor")]
 [assembly: AssemblyDescription("A lighweighted Pixel Art editor.")]
 [assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("")]
+[assembly: AssemblyCompany("PixiEditor")]
 [assembly: AssemblyProduct("PixiEditor")]
 [assembly: AssemblyCopyright("Copyright Krzysztof Krysiński © 2018 - 2020")]
 [assembly: AssemblyTrademark("")]
@@ -50,5 +50,5 @@ using System.Windows;
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
 
-[assembly: AssemblyVersion("0.1.2.0")]
-[assembly: AssemblyFileVersion("0.1.2.0")]
+[assembly: AssemblyVersion("0.1.3.0")]
+[assembly: AssemblyFileVersion("0.1.3.0")]

+ 23 - 2
PixiEditor/ViewModels/ViewModelMain.cs

@@ -84,6 +84,7 @@ namespace PixiEditor.ViewModels
         public RelayCommand OpenHyperlinkCommand { get; set; }
         public RelayCommand ZoomCommand { get; set; }
         public RelayCommand ChangeToolSizeCommand { get; set; }
+        public RelayCommand RestartApplicationCommand { get; set; }
 
 
         private double _mouseXonCanvas;
@@ -195,6 +196,18 @@ namespace PixiEditor.ViewModels
             }
         }
 
+        private bool _updateReadyToInstall = false;
+
+        public bool UpdateReadyToInstall
+        {
+            get => _updateReadyToInstall;
+            set
+            {
+                _updateReadyToInstall = value;
+                RaisePropertyChanged(nameof(UpdateReadyToInstall));
+            }
+        }
+
         public BitmapManager BitmapManager { get; set; }
         public PixelChangesController ChangesController { get; set; }
 
@@ -258,6 +271,7 @@ namespace PixiEditor.ViewModels
             OpenHyperlinkCommand = new RelayCommand(OpenHyperlink);
             ZoomCommand = new RelayCommand(ZoomViewport);
             ChangeToolSizeCommand = new RelayCommand(ChangeToolSize);
+            RestartApplicationCommand = new RelayCommand(RestartApplication);
             ToolSet = new ObservableCollection<Tool>
             {
                 new MoveTool(), new PenTool(), new SelectTool(), new FloodFill(), new LineTool(),
@@ -312,7 +326,13 @@ namespace PixiEditor.ViewModels
             BitmapManager.PrimaryColor = PrimaryColor;
             ActiveSelection = new Selection(Array.Empty<Coordinates>());
             Current = this;
-            InitUpdateChecker();            
+            InitUpdateChecker();
+        }
+
+        private void RestartApplication(object parameter)
+        {
+            Process.Start(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "PixiEditor.UpdateInstaller.exe"));
+            Application.Current.Shutdown();
         }
 
         public async Task<bool> CheckForUpdate()
@@ -325,7 +345,8 @@ namespace PixiEditor.ViewModels
                 {
                     VersionText = "Downloading update...";
                     await UpdateDownloader.DownloadReleaseZip(UpdateChecker.LatestReleaseInfo);
-                    VersionText = "Restart to install update";
+                    VersionText = "to install update";
+                    UpdateReadyToInstall = true;
                     return true;
                 }
                 return false;

+ 7 - 3
PixiEditor/Views/MainWindow.xaml

@@ -391,8 +391,12 @@
                 <TextBlock Margin="4,0,10,0" Text="{Binding MouseYOnCanvas, Converter={StaticResource DoubleToIntConverter}}" Foreground="White" FontSize="16"/>
             </StackPanel>
         </DockPanel>
-        <TextBlock Padding="10" HorizontalAlignment="Right"
-                   Foreground="White" FontSize="16" VerticalAlignment="Center" Grid.Row="3"
-                   Grid.Column="3" Text="{Binding VersionText}"/>
+        <StackPanel Margin="10,0,0,0" VerticalAlignment="Center" Grid.Row="3"
+                       Grid.Column="3" Orientation="Horizontal">
+            <Button Style="{StaticResource BaseDarkButton}" 
+                    Visibility="{Binding UpdateReadyToInstall, Converter={StaticResource BoolToVisibilityConverter}}" FontSize="14" Height="20" Command="{Binding RestartApplicationCommand}">Restart</Button>
+            <TextBlock VerticalAlignment="Center" Padding="10" HorizontalAlignment="Right"
+                       Foreground="White" FontSize="14"  Text="{Binding VersionText}" />
+        </StackPanel>
     </Grid>
 </Window>

+ 5 - 6
PixiEditor/Views/MainWindow.xaml.cs

@@ -19,7 +19,7 @@ namespace PixiEditor
             InitializeComponent();
             StateChanged += MainWindowStateChangeRaised;
             MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
-           viewModel = ((ViewModelMain)DataContext);
+            viewModel = ((ViewModelMain)DataContext);
             viewModel.CloseAction = Close;
         }
 
@@ -70,11 +70,10 @@ namespace PixiEditor
         {
             string dir = AppDomain.CurrentDomain.BaseDirectory;
             bool updateFileExists = Directory.GetFiles(dir, "update-*.zip").Length > 0;
-            if (updateFileExists)
-            {
-                string path =
-                    Path.Join(dir, "PixiEditor.UpdateInstaller.exe");
-                Process.Start(path);
+            string updaterPath = Path.Join(dir, "PixiEditor.UpdateInstaller.exe");
+            if (updateFileExists && File.Exists(updaterPath))
+            {                
+                Process.Start(updaterPath);
                 Close();
             }
         }

+ 17 - 4
PixiEditorTests/UpdateModuleTests/UpdateCheckerTests.cs

@@ -13,13 +13,26 @@ namespace PixiEditorTests.UpdateModuleTests
     {
         [Theory]
         [InlineData("0.1.2", "0.1.2", false)]
-        [InlineData("0.5.", "0.1.2", true)]
-        [InlineData(null, "0.1.2", true)]
-        public void TestThatCheckUpdateAvailableChecksCorrectly(string newVersion, string currentVersion, bool expectedValue)
+        [InlineData("0.5", "0.1.2", false)]
+        [InlineData("0.1.3", "0.1.2", false)]
+        [InlineData("0.1.2", "0.1.3", true)]
+        [InlineData("0.2.1", "0.1.3", false)]
+        public void TestThatCheckUpdateAvailableChecksCorrectly(string currentVersion, string newVersion, bool expectedValue)
         {
             UpdateChecker checker = new UpdateChecker(currentVersion);
-            bool result = checker.CheckUpdateAvailable(new ReleaseInfo() { TagName = newVersion });
+            bool result = checker.CheckUpdateAvailable(new ReleaseInfo(true) { TagName = newVersion });
             Assert.True(result == expectedValue);
         }
+
+        [Theory]
+        [InlineData("0.1.2", "0.1.2", false)]
+        [InlineData("0.5", "0.1.2", false)]
+        [InlineData("0.1.3", "0.1.2", false)]
+        [InlineData("0.1.2", "0.1.3", true)]
+        [InlineData("0.2.1", "0.1.3", false)]
+        public void CheckThatVersionBiggerComparesCorrectly(string currentVersion, string newVersion, bool expectedValue)
+        {
+            Assert.True(UpdateChecker.VersionBigger(currentVersion, newVersion) == expectedValue);
+        }
     }
 }