Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AppVersionChecker.cs « Structures « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 93d6bf1451a12801c23624a4d0e0105eca343647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 *                     GNU AFFERO GENERAL PUBLIC LICENSE
 *                       Version 3, 19 November 2007
 *  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 *  Everyone is permitted to copy and distribute verbatim copies
 *  of this license document, but changing it is not allowed.
 */
using System;
using System.Diagnostics;
using System.Net;
using Avalonia.Threading;
using UVtools.Core;
using UVtools.Core.Objects;

namespace UVtools.WPF.Structures
{
    public class AppVersionChecker : BindableBase
    {
        private string _version;
        private string _url;

        public string Version
        {
            get => _version;
            set
            {
                if(!RaiseAndSetIfChanged(ref _version, value)) return;
                RaisePropertyChanged(nameof(VersionAnnouncementText));
                RaisePropertyChanged(nameof(HaveNewVersion));
            }
        }

        public string VersionAnnouncementText => $"New version {_version} is available!";

        public string Url => $"{About.Website}/releases/tag/{_version}";

        public bool HaveNewVersion => !string.IsNullOrEmpty(Version);

        public bool Check()
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    string htmlCode = client.DownloadString($"{About.Website}/releases");
                    const string searchFor = "/releases/tag/";
                    var startIndex = htmlCode.IndexOf(searchFor, StringComparison.InvariantCultureIgnoreCase) +
                                     searchFor.Length;
                    var endIndex = htmlCode.IndexOf("\"", startIndex, StringComparison.InvariantCultureIgnoreCase);
                    var version = htmlCode.Substring(startIndex, endIndex - startIndex);
                    if (string.Compare(version, $"v{AppSettings.AssemblyVersion}", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        Dispatcher.UIThread.InvokeAsync(() =>
                        {
                            Version = version;;
                        });
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }

            return false;
        }
    }
}