Guest User

Untitled

a guest
Sep 3rd, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. using ReactiveUI;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Net.Http;
  7. using System.Net.Http.Json;
  8. using System.Text.Json.Serialization;
  9. using System.Threading.Tasks;
  10.  
  11. namespace Installer.ViewModels;
  12.  
  13. public partial class MainWindowViewModel : ViewModelBase
  14. {
  15.     private static HttpClient _client = new();
  16.     private readonly string _url = "https://api.github.com/repos/mekasu0124/Todo/releases/latest";
  17.    
  18.     // progress bar value
  19.     public double _value;
  20.     // information text for label above progress bar
  21.     public string _currentInformation;
  22.     // button and progress bar show/hide booleans
  23.     public bool _isUpdating;
  24.     public bool _canRun;
  25.     // boolean to know when the entire process is finished
  26.     public bool _isProcessFinished;
  27.  
  28.     // download the zip folder
  29.     public async Task GetZipFolder()
  30.     {
  31.         IsUpdating = true;
  32.         IsProcessFinished = false;
  33.  
  34.         // fluff text to start progress bar and get all values needed
  35.         // for correctly and accurately incrementing progress bar
  36.         // asyncronously, or at the same time, with/as actions being
  37.         // executed
  38.         CurrentInformation = "Connecting To Repo";
  39.         IncrementProgress(10);
  40.  
  41.         CurrentInformation = "Searching For Latest Release Files";
  42.         _client.DefaultRequestHeaders.UserAgent.TryParseAdd("request");
  43.         try
  44.         {
  45.             CurrentInformation = "Downloading Zip Folder";
  46.             var data = await _client.GetFromJsonAsync<ReleaseData>(_url);
  47.             IncrementProgress(10);
  48.  
  49.             CurrentInformation = "Getting Applications Current Location";
  50.             string projectLocation = Directory.GetCurrentDirectory();
  51.             IncrementProgress(10);
  52.  
  53.             CurrentInformation = "Extracting Contents";
  54.             var projectZipFolder = data.DownloadUrl;
  55.             // some code to extract files from zip folder
  56.             IncrementProgress(10);
  57.  
  58.             CurrentInformation = "Updating Files";
  59.             // some code to over write existing files with newly extracted files
  60.             IncrementProgress(10);
  61.  
  62.             CurrentInformation = "Validating Files";
  63.             // some code to validate files to make sure none are corrupt
  64.             IncrementProgress(10);
  65.  
  66.             CurrentInformation = "Cleaning Up Files";
  67.             // some code to remove extracted folder, and whatever else comes with cleaning up files
  68.             IncrementProgress(10);
  69.  
  70.             // fluff text
  71.             CurrentInformation = "Re-enabling Buttons";
  72.             IncrementProgress(10);
  73.  
  74.             // fluff text
  75.             CurrentInformation = "Exiting Process";
  76.             IncrementProgress(10);
  77.  
  78.             // Let user know process is complete
  79.             CurrentInformation = "The Update Is Complete!";
  80.             // tell the process it's finished
  81.             IsProcessFinished = true;
  82.             // call increment progress one last time so that
  83.             // the process finished boolean can show the launch
  84.             // application button so user can click to reopen
  85.             // the todo application
  86.             IncrementProgress(10);
  87.         }
  88.         catch (HttpRequestException ex)
  89.         {
  90.             CurrentInformation = $"Error: {ex.Message}";
  91.         }
  92.     }
  93.  
  94.     // Increment the progress bar. I made this a function to keep
  95.     // cleaner code and made it callable from wherever the process is
  96.     // and it updates the progress bar.
  97.     public void IncrementProgress(double progVal)
  98.     {
  99.         Value += progVal;
  100.  
  101.         // if the process is finished,
  102.         if (IsProcessFinished)
  103.         {
  104.             // hide the progress bar, and show the Launch Application button
  105.             CanRun = true;
  106.         }
  107.     }
  108.  
  109.     // launch the Todo application
  110.     public void LaunchApplication()
  111.     {
  112.         try
  113.         {
  114.             var p = new Process();
  115.             p.StartInfo.FileName = "Todo.exe";
  116.             p.Start();
  117.             ExitProgram();
  118.         }
  119.         catch (Exception ex)
  120.         {
  121.             CurrentInformation = ex.Message;
  122.             return;
  123.         }
  124.     }
  125.  
  126.     public void ExitProgram()
  127.     {
  128.         Environment.Exit(0);
  129.     }
  130.  
  131.     // instantiating fields
  132.     private double Value
  133.     {
  134.         get => _value;
  135.         set => this.RaiseAndSetIfChanged(ref _value, value);
  136.     }
  137.  
  138.     private string CurrentInformation
  139.     {
  140.         get => _currentInformation;
  141.         set => this.RaiseAndSetIfChanged(ref _currentInformation, value);
  142.     }
  143.  
  144.     private bool IsUpdating
  145.     {
  146.         get => _isUpdating;
  147.         set => this.RaiseAndSetIfChanged(ref _isUpdating, value);
  148.     }
  149.  
  150.     private bool CanRun
  151.     {
  152.         get => _canRun;
  153.         set => this.RaiseAndSetIfChanged(ref _canRun, value);
  154.     }
  155.  
  156.     private bool IsProcessFinished
  157.     {
  158.         get => _isProcessFinished;
  159.         set => this.RaiseAndSetIfChanged(ref _isProcessFinished, value);
  160.     }
  161. }
  162.  
  163. // class built to create some sort of way to return the information
  164. // so that we can use the data variable to call what's needed from
  165. // the api call
  166. public class ReleaseData
  167. {
  168.     [JsonPropertyName("browser_download_url")]
  169.     public string DownloadUrl { get; set; }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment