✅ getting app to check repo for latest release, updating if versions don't match
I've created a help channel for this before, but at this point I'm lost. I'm wanting to set my app up so that when the user launches it, it runs a check to see if the user has the matching version to the latest github repo release. I am using Avalonia MVVM and I'm wanting to incorporate this into my Todo app first. https://github.com/mekasu0124/Todo The only problem is, I have no idea what file to do this in, or how to work with the github thing to achieve what I'm wanting to achieve. Thanks in advance
98 Replies
if I can get help learning how to do it in my Todo app, then I can replicate it and put it in my other apps on my own, I believe.
MainWindowView.axaml and MainWindowViewModel.cs dictates what is showing on the screen.
here, the MainWindowViewModel.cs obtains the list of items to display on the screen. I have a feeling that my update checker would happen in this file (likely calling an external class to do so) and either rendering the home screen like normal if the versions match, or rendering the "There is an update available, click ok to update" if the versions don't match. I just don't know how to implement that
https://github.com/octokit/octokit.net any thoughts on using this?
GitHub
GitHub - octokit/octokit.net: A GitHub API client library for .NET
A GitHub API client library for .NET . Contribute to octokit/octokit.net development by creating an account on GitHub.
You can use this API endpoint
https://api.github.com/repos/mekasu0124/Todo/releases/latest
.ok. I created a new file in my Services folder and named it Update.cs How can I use that endpoing api?
I can give you example.
I appreciate it 🙂
That's all the syntax I know in my head from HttpClient.
You can make the response string constant.
It'd be better to reuse a static instance of HttpClient and use
.GetAsJsonAsync<T>()
maybe, but yeah, this looks fineI definitely appreciate it. My goal now is to take the examples you gave and make them working code. Once they're working code, I'll be building an
UpdateAvailableScreeView.axaml
file that I would like to have display when an update is available instead of the home screenQuestion is, where to call it in the Avalonia app so that it runs on startup
I wrote it in Discord :(
And shows a notification that the app is out of date
Well, yes, sure
Good question, isn't there a base startup class?
I can use MainWindowViewModel.cs to run the update checker, however, I have a problem
how am I to define CurrentAppVersion? and what goes in
["prop here"]
?Also, just noticed that
Newtonsoft.Json
dependency 💀
You'd define the version... somewhere
And prop here
would be how you get the version from the response you get from the API
So you'll need to either hit up the docs or call the endpoint with something like Insomnia, or even open it in the browser, to see where the version number is sitting
so here's where I'm at
I've commented where the program will need to automatically know certain things. As far as CurrentAppVersion, the program will need to know that on it's own from the api call.
so how can I do this?
and to clarify, the
IsUpdateAvaliable
boolean is for the program to know whether or not to show the usercontrol.
and the variable IsUpdating
is so that the usercontrol know to update the splash screen and when to close it
the view that I'll be creating for this is a loading screen. It'll tell the user that an update is available and they'll click continue. When they click continue, it shows a progress bar that updates as the application is doing the update
and the CurrentAppVersion
i'd like to programmtically get from the program without me having to manually update it
I'm just not going to worry about it anymore. I can't figure this out, and it's really starting to frustrate me. Thanks for your help so farUnknown User•15mo ago
Message Not Public
Sign In & Join Server To View
I got rid of all of it. I'm still really new to C# and avalonia in general. I"m great with axaml files because it's simliar to building a website and I picked that part up rather quickly. The C# part of it is really hard for me and I'm really new to it. I removed everything because half of what you said doesn't make sense to me. I'm not trying to annoy anyone or piss anyone off, but I need someone to teach me how to do this stuff. I cannot find any guides to do this with and the documentation I do find doesn't give any examples on what to do. I have tried using a regular console app to try and get information from the github as well, and I even fail at doing that, so at this point I'm just defeated. I either need someone to help teach me from step one until finished, or I'm just not going to waste my time with this language anymore. I'm spending more time trying to beg for help than actually getting it and I'm just over it
So, going to this link: https://api.github.com/repos/mekasu0124/Todo/releases/latest shows us some data about the latest release. We want to use that
We can take either
tag_name
as the version, or we can simply check the published_at
date
If either is higher than what we saved in the app during compilation, that means a new version existsok I'm with you so far
so what is my first step?
To get the data we can use
HttpClient
like we did here
in their example
Almost
Instead of going through all the song and dance with stringly-typed properties and what not, we can do it simpler
Luckily, both the tag name and the publish date are top-level properties, so creating a class we can deserialize the data to will be super easy
ok question. Since this is
Update.cs
file is in my /Services
folder, does the public partial class Update
need to be type casted to ViewModelBase
or something else?
betNo clue
I'm still trying to google around how to do something on app startup, control load, whatever in Avalonia
Let's leave it for now
We can use this class to pull out only the properties we want from the JSON
How's it going?
like so
we're working through it
Nice.
I have school in an hour :(
All in all, you would have some
With
buildDate
being the date the current version of the app was built at
You could probably do some csproj shenanigans to make it available and automatically saved
Avalonia's docs have a tutorial on loading data on startup: https://docs.avaloniaui.net/docs/next/tutorials/music-store-app/load-data-at-startuphere's where I'm at
Yeah, that's the easy part lol
Honestly... I think it would be easiest to run this code on button click, at least for the time being
Just create a new command, run this code inside of it, bind it to a button
Well, yeah, to be expected
The URL should be this
And here's a blogpost I found on how to get the build date: https://intellitect.com/blog/build-date-net-application/
That said... the release will be always created after the build
So it's probably not the best way and you should rely on the tag instead
ok I'm sorry. Nuget told me that I needed to update Avalonia and some other packages and I did. The project was built on 0.10.21 and now everything has updated to version 11. How do I roll it back?
And manually storing the version, maybe in some
version.txt
file
Why do you want to roll it back?because otherwise, I have to pause what we're doing here and rewrite the project for version 11
Choosing a different version in the nuget package manager should work, though
Worst case scenario, rollback to the last commit
I'm just going to have to rewrite to version 11....this is ridiculous
i don't see why anything should need to be rewritten
what you could do as well is use the commit hash by the way
it's a bit more involved and requires very meticulous version control (and probably publishing from CI)
you would store the hash in some embedded text resource and fetch the commit hash of the latest release instead of a tag number or release date
this allows you to have "dev builds" as well, which sync to the latest commit instead of the latest release
because updating to version 11 has caused all these errors
all these 5 errors? 😄
like if all of this is fixable, then I won't re-write it. I just don't know how to fix it
I have an avalonia version 11 application that is built with community toolkit, and the ViewModel.cs files are all written that same way, but yet this app is erroring
so how can I fix these errors?
I fixed the view locator, but I do not know why these axamal.cs files are erroring. I checked my version 11 project, and they're written the exact same way
I'm just going to continue with you and working on this. Maybe these errors will resolve themselves at some point
so just create a version.txt file in the root of the project?
For example, yeah
or would it serve better in the Services folder?
and if we're relying on just the tag name, then do I need to do this?
No, you don't need the date
Instead, you would read the tag name from the file and compare it with
data.TagName
ok what about this?
Yep, looks about right. Besides that ternary
ok what did I do wrong?
genuine question
condition ? iftrue : iffalse
Your condition is always true
return data.TagName == version ? true : false;
Yep
ok cool
Or...
return data.TagName == version;
Since it returns true or false anywayok bet. I'll switch it to that. So this
GetAsJsonAsync<ReleaseData>(url);
how do I instantiate that GetAsJsonAsync
part?what
It's a method
You don't instantiate methods
Ah, you're probably missing a namespace
See if quick fixes can just import it
You need
using System.Net.Http.Json
And I made a mistake, too. It's GetFromJsonAsync
not GetAsJsonAsync
ok bet. that's resolved now
so what's our next step?
Next step is to call it... somewhere
Probably on button click
That seems the easiest
ok so in my Views folder, I've made a new file
NewVersionScreen.axaml
. It's a simple screen that will show on the home screen instead of the TodoListViewModel. My first problem is the second label. How can I get access to the tag name for the string, and it doesn't like the prefixed dollar sign
here is where it loads the content on launch. So I figured we could run the check in this same function and then do a conditional render based off the boolean value that is returned from /Services/Update.cs
and here's where I'm at with that new screens view modelWell, to get the version number from our method we will need to modify it, so it returns more than just that boolean
You could make some
record VersionData(string CurrentVersion, string LatestVersion, bool IsNewer)
for example
And return thatNo
A record is like a class
Then you use it with
new VersionInfo(foo, bar, baz)
And return that
You can make a class instead if you're more comfortable with themhow close am I?
how do I make it a class because apparently it's not liking me returning that record since it's not a boolean
Maybe just change the return type...?
like that?
A class won't be a boolean either
ok so change <bool> to <record>
No
To
<VersionData>
ok bet
ok so the view model. I have to create a new VersionData?
I'll pick this back up tomorrow. I have to go to bed. I've been awake for 21 hours so far. I'll ping you tomorrow
Sure thing
ok I'm back. Sorry about that
this is my
/Services/Update.cs
file. Am I missing anything here?Looks good
and to understand the
public record VersionData();
I can add more to that right? So if I wanted to also get the DateTime object that the release was made at, I can add in
right?Yep
and then fix my return to say
return new VersionData(version, data.TagName, data.ReleaseDate, data.TagName == version);
and update my ReleaseData to include the DateTime? ReleaseDate
. Am I on the right track so far?Not sure why you decided to make those properties nullable, but yes
do they not need to be?
Do you expect any of that data to be
null
?no. ok I got you
I'm still getting these errors. I've corrected the ViewLocator.cs, but I'm not understanding why I'm getting these errors
ok give me about 5 maybe 10 minutes. I'm just going to very quickly rewrite the project. It won't take long, promise
Sure
ok so I've re-written, and I'm believing there is a difference here between version 0.10.21 and 11.0.2
it's telling me that for the
.Subscribe
part `Cannot convert lambda expression to type 'IObserver<TodoItem>' because it is not a delegate type. How can I fix this?
nevermind. fixed it. I was missing an import
ok I'm sorry for the wait. I am rewritten and ready to go. I've got the Update.cs file brought over, the version.txt brought over in the services folder. I have built a NewVersionScreen.axaml page and I have a NewVersionViewModel.cs file
so how do I get access to the information in Update.cs in my ViewModel.cs file?Well, you can run it on button click, for example
I would want the
InformationText
to display above the buttons though?
so when the user clicks the Ok button, it will show a loading screen. The loading screen will have a progress bar that gets updated as the upgrade happens.I'd say let's start with a "check for new updates" button
Let's not get into downloading the files and stuff yet
ok I have my button
now what?
Call the method to get the update datain your command
Get the data
And bind it to some label or something
I'll be honest, I'm not too familliar with how Avalonia works
well I don't know either. There isn't a youtube video that I can watch to do what I'm wanting to do, and you're the only one wiling to help me, but you're not familiar with it either so I don't know.
You could try asking in #gui
ok