Skip to content

Commit

Permalink
Added App Updating
Browse files Browse the repository at this point in the history
  • Loading branch information
RetroDriven committed Sep 26, 2022
1 parent 3f81f37 commit 7f0d3f5
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 4 deletions.
16 changes: 13 additions & 3 deletions Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 129 additions & 0 deletions Form1.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
using pannella.analoguepocket;
using System.Diagnostics;
using System.Net;
using System.Net.Http.Headers;
using System.Security.Policy;
using System.Text.Json;
using System.Windows.Forms;

namespace Pocket_Updater
{
public partial class Form1 : Form
{
private const string VERSION = "1.3.0";
private const string API_URL = "https://api.github.com/repos/RetroDriven/Pocket_Updater/releases";
private const string RELEASE_URL = "https://github.com/RetroDriven/Pocket_Updater/releases/latest";

public Form1()
{
InitializeComponent();

//Check for Internet Connection and App Updates
try
{
using (WebClient client2 = new WebClient())
{
using (client2.OpenRead("http://www.google.com/"))
{
_ = CheckVersion_Load();
}
}
}
catch
{
MessageBox.Show("Failed to check for App Updates!", "No Internet Connection Detected", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Expand Down Expand Up @@ -48,5 +74,108 @@ private void updateLogToolStripMenuItem_Click(object sender, EventArgs e)
MessageBox.Show("Pocket Update Log Not Found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public async Task CheckVersion_Load()
{

if (await CheckVersion())
{
try
{
using (WebClient client = new WebClient())
{
using (client.OpenRead("http://www.google.com/"))
{
DialogResult dialogResult = MessageBox.Show("There is a New Version Available!\n\n Would you like to Download it?", "App Update", MessageBoxButtons.YesNo,MessageBoxIcon.Information);
if (dialogResult == DialogResult.Yes)
{
Process.Start("explorer", RELEASE_URL);
Close();

}
else if (dialogResult == DialogResult.No)
{
//do something else
}
}
}
}
catch
{
MessageBox.Show("No Internet Connection Detected!","Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

}
async static Task<bool> CheckVersion()
{
try
{
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(API_URL)

};
var agent = new ProductInfoHeaderValue("Pocket-Updater", "1.0");
request.Headers.UserAgent.Add(agent);
var response = await client.SendAsync(request).ConfigureAwait(false);
response.EnsureSuccessStatusCode();

var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
List<Github.Release>? releases = JsonSerializer.Deserialize<List<Github.Release>>(responseBody);

string tag_name = releases[0].tag_name;
string? v = SemverUtil.FindSemver(tag_name);
if (v != null)
{
return SemverUtil.SemverCompare(v, VERSION);

}
return false;
}
catch (HttpRequestException e)
{
return false;

}
}

private async void checkForAppUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
using (WebClient client2 = new WebClient())
{
using (client2.OpenRead("http://www.google.com/"))
{

if (await CheckVersion())
{
DialogResult dialogResult = MessageBox.Show("There is a New Version Available!\n\n Would you like to Download it?", "App Update", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dialogResult == DialogResult.Yes)
{
Process.Start("explorer", RELEASE_URL);
Close();

}
else if (dialogResult == DialogResult.No)
{
//do something else
}
}
else
{
MessageBox.Show("You are using the most recent App (v" + VERSION + ")", "No Updates Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
catch
{
MessageBox.Show("Failed to check for App Updates!", "No Internet Connection Detected", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
2 changes: 1 addition & 1 deletion Pocket_Updater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Version>1.2.1</Version>
<Version>1.3.0</Version>
<RootNamespace>Pocket_Updater</RootNamespace>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
Expand Down

0 comments on commit 7f0d3f5

Please sign in to comment.