Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using ReactiveUI;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Net.Http;
- using System.Net.Http.Json;
- using System.Text.Json.Serialization;
- using System.Threading.Tasks;
- namespace Installer.ViewModels;
- public partial class MainWindowViewModel : ViewModelBase
- {
- private static HttpClient _client = new();
- private readonly string _url = "https://api.github.com/repos/mekasu0124/Todo/releases/latest";
- // progress bar value
- public double _value;
- // information text for label above progress bar
- public string _currentInformation;
- // button and progress bar show/hide booleans
- public bool _isUpdating;
- public bool _canRun;
- // boolean to know when the entire process is finished
- public bool _isProcessFinished;
- // download the zip folder
- public async Task GetZipFolder()
- {
- IsUpdating = true;
- IsProcessFinished = false;
- // fluff text to start progress bar and get all values needed
- // for correctly and accurately incrementing progress bar
- // asyncronously, or at the same time, with/as actions being
- // executed
- CurrentInformation = "Connecting To Repo";
- IncrementProgress(10);
- CurrentInformation = "Searching For Latest Release Files";
- _client.DefaultRequestHeaders.UserAgent.TryParseAdd("request");
- try
- {
- CurrentInformation = "Downloading Zip Folder";
- var data = await _client.GetFromJsonAsync<ReleaseData>(_url);
- IncrementProgress(10);
- CurrentInformation = "Getting Applications Current Location";
- string projectLocation = Directory.GetCurrentDirectory();
- IncrementProgress(10);
- CurrentInformation = "Extracting Contents";
- var projectZipFolder = data.DownloadUrl;
- // some code to extract files from zip folder
- IncrementProgress(10);
- CurrentInformation = "Updating Files";
- // some code to over write existing files with newly extracted files
- IncrementProgress(10);
- CurrentInformation = "Validating Files";
- // some code to validate files to make sure none are corrupt
- IncrementProgress(10);
- CurrentInformation = "Cleaning Up Files";
- // some code to remove extracted folder, and whatever else comes with cleaning up files
- IncrementProgress(10);
- // fluff text
- CurrentInformation = "Re-enabling Buttons";
- IncrementProgress(10);
- // fluff text
- CurrentInformation = "Exiting Process";
- IncrementProgress(10);
- // Let user know process is complete
- CurrentInformation = "The Update Is Complete!";
- // tell the process it's finished
- IsProcessFinished = true;
- // call increment progress one last time so that
- // the process finished boolean can show the launch
- // application button so user can click to reopen
- // the todo application
- IncrementProgress(10);
- }
- catch (HttpRequestException ex)
- {
- CurrentInformation = $"Error: {ex.Message}";
- }
- }
- // Increment the progress bar. I made this a function to keep
- // cleaner code and made it callable from wherever the process is
- // and it updates the progress bar.
- public void IncrementProgress(double progVal)
- {
- Value += progVal;
- // if the process is finished,
- if (IsProcessFinished)
- {
- // hide the progress bar, and show the Launch Application button
- CanRun = true;
- }
- }
- // launch the Todo application
- public void LaunchApplication()
- {
- try
- {
- var p = new Process();
- p.StartInfo.FileName = "Todo.exe";
- p.Start();
- ExitProgram();
- }
- catch (Exception ex)
- {
- CurrentInformation = ex.Message;
- return;
- }
- }
- public void ExitProgram()
- {
- Environment.Exit(0);
- }
- // instantiating fields
- private double Value
- {
- get => _value;
- set => this.RaiseAndSetIfChanged(ref _value, value);
- }
- private string CurrentInformation
- {
- get => _currentInformation;
- set => this.RaiseAndSetIfChanged(ref _currentInformation, value);
- }
- private bool IsUpdating
- {
- get => _isUpdating;
- set => this.RaiseAndSetIfChanged(ref _isUpdating, value);
- }
- private bool CanRun
- {
- get => _canRun;
- set => this.RaiseAndSetIfChanged(ref _canRun, value);
- }
- private bool IsProcessFinished
- {
- get => _isProcessFinished;
- set => this.RaiseAndSetIfChanged(ref _isProcessFinished, value);
- }
- }
- // class built to create some sort of way to return the information
- // so that we can use the data variable to call what's needed from
- // the api call
- public class ReleaseData
- {
- [JsonPropertyName("browser_download_url")]
- public string DownloadUrl { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment