66using System . Net ;
77using System . Text ;
88using System . Threading ;
9+ using System . Threading . Tasks ;
910using System . Windows ;
1011using System . Windows . Controls ;
1112using System . Windows . Input ;
@@ -47,6 +48,12 @@ public partial class MainWindow : UWPHost.Window {
4748 private readonly string VERSION_MANIFEST_URL =
4849 "https://raw.githubusercontent.com/CyanCoding/Project-Tracker/master/install-resources/version.json" ;
4950
51+ private readonly string NEXT_VERSION_MANIFEST_URL =
52+ "https://raw.githubusercontent.com/CyanCoding/Project-Tracker/cyancoding-settings-branch/install-resources/next-version.json" ;
53+
54+ private readonly string NEXT_VERSION_INFO = Environment . GetFolderPath
55+ ( Environment . SpecialFolder . LocalApplicationData ) + "/Project Tracker/next-version.json" ;
56+
5057 private int addingType = 0 ;
5158 private string duration ;
5259 private string icon ;
@@ -60,6 +67,7 @@ public partial class MainWindow : UWPHost.Window {
6067 private bool isSettingsWindowDisplaying = false ;
6168 private int itemIndex = 0 ;
6269 private int itemsAdded = 0 ;
70+ private bool updateResponse = false ;
6371 // The amount of items added to the scrollviewer
6472 private string percent ;
6573
@@ -1212,6 +1220,8 @@ private void SetSelectedProject() {
12121220 isSettingsWindowDisplaying = false ;
12131221 }
12141222
1223+ settingsGrid . Visibility = Visibility . Hidden ;
1224+
12151225 border1 . Style = ( Style ) TryFindResource ( "hoverOver" ) ;
12161226 border2 . Style = ( Style ) TryFindResource ( "hoverOver" ) ;
12171227 border3 . Style = ( Style ) TryFindResource ( "hoverOver" ) ;
@@ -1359,7 +1369,7 @@ private void SetSelectedProject() {
13591369 }
13601370 }
13611371 else if ( ! isSettingsWindowDisplaying ) { // There's no projects
1362- // Hide all items
1372+ // Hide all items
13631373 displayingTitle . Visibility = Visibility . Hidden ;
13641374 displayingImage . Visibility = Visibility . Hidden ;
13651375 settingsImage . Visibility = Visibility . Hidden ;
@@ -1445,8 +1455,6 @@ private void settingsImage_PreviewMouseDown(object sender, MouseButtonEventArgs
14451455 /// Startup function that runs when code execution starts.
14461456 /// </summary>
14471457 private void Startup ( ) {
1448- // Item positioning
1449-
14501458 if ( ! Directory . Exists ( APPDATA_DIRECTORY ) ) { // Create AppData directory
14511459 Directory . CreateDirectory ( APPDATA_DIRECTORY ) ;
14521460 }
@@ -1514,16 +1522,27 @@ private void Startup() {
15141522 if ( File . Exists ( VERSION_INFO ) ) {
15151523 File . Delete ( VERSION_INFO ) ;
15161524 }
1517-
1518- // Download latest version information
1519- try {
1520- WebClient client = new WebClient ( ) ;
1521- client . DownloadFileCompleted += new AsyncCompletedEventHandler ( Update ) ;
1522- client . DownloadFileAsync ( new Uri ( VERSION_MANIFEST_URL ) , VERSION_INFO ) ;
1523- }
1524- catch ( WebException ) {
1525- // Couldn't download update file. Possible their wifi isn't working
1525+ if ( File . Exists ( NEXT_VERSION_INFO ) ) {
1526+ File . Delete ( NEXT_VERSION_INFO ) ;
15261527 }
1528+
1529+ Thread thread = new Thread ( ( ) =>
1530+ {
1531+ Dispatcher . Invoke ( new Action ( ( ) =>
1532+ {
1533+ // Download latest version information
1534+ try {
1535+ WebClient client = new WebClient ( ) ;
1536+ client . DownloadFileCompleted += new AsyncCompletedEventHandler ( Update ) ;
1537+ client . DownloadFileAsync ( new Uri ( VERSION_MANIFEST_URL ) , VERSION_INFO ) ;
1538+ }
1539+ catch ( WebException ) {
1540+ // Couldn't download update file. Possible their wifi isn't working
1541+ }
1542+ } ) ) ;
1543+ } ) ;
1544+ thread . Start ( ) ;
1545+
15271546 }
15281547
15291548 /// <summary>
@@ -1619,15 +1638,50 @@ private void TypeImagePressed(object sender, MouseButtonEventArgs e) {
16191638 /// Notifies the user of an update if one is available.
16201639 /// </summary>
16211640 private void Update ( object sender , AsyncCompletedEventArgs e ) {
1641+ // Download future version information
1642+ try {
1643+ WebClient client = new WebClient ( ) ;
1644+ client . DownloadFileCompleted += new AsyncCompletedEventHandler ( NextVersionDownloadComplete ) ;
1645+ client . DownloadFileAsync ( new Uri ( NEXT_VERSION_MANIFEST_URL ) , NEXT_VERSION_INFO ) ;
1646+ }
1647+ catch ( WebException ) {
1648+ // Couldn't download update file. Possible their wifi isn't working
1649+ nextVersionLabel . Content = "Couldn't get next release data." ;
1650+ latestVersionLabel . Content = "Latest version: unavailable" ;
1651+ nextVersionReleaseLabel . Visibility = Visibility . Hidden ;
1652+ nextVersionFeaturesLabel . Visibility = Visibility . Hidden ;
1653+ nextVersionUpdateOneLabel . Visibility = Visibility . Hidden ;
1654+ nextVersionUpdateTwoLabel . Visibility = Visibility . Hidden ;
1655+ nextVersionUpdateThreeLabel . Visibility = Visibility . Hidden ;
1656+ updateResponse = true ;
1657+ }
1658+
16221659 string json = File . ReadAllText ( VERSION_INFO ) ;
16231660
1661+ if ( json == "" && isSettingsWindowDisplaying ) { // Didn't download the entire file properly
1662+ System . Windows . Forms . MessageBox . Show ( "Please connect to the Internet to check for an update." ,
1663+ "No Internet Connection" ,
1664+ System . Windows . Forms . MessageBoxButtons . OK ,
1665+ System . Windows . Forms . MessageBoxIcon . Error ) ;
1666+ updateResponse = true ;
1667+ latestVersionLabel . Content = "Latest version: unavailable" ;
1668+ return ;
1669+ }
1670+ else if ( json == "" ) {
1671+ nextVersionLabel . Content = "Couldn't get next release data." ;
1672+ updateResponse = true ;
1673+ return ;
1674+ }
1675+
16241676 UpdateManifest . Rootobject update =
16251677 JsonConvert . DeserializeObject < UpdateManifest . Rootobject > ( json ) ;
16261678
1679+ latestVersionLabel . Visibility = Visibility . Visible ;
1680+ latestVersionLabel . Content = "Latest version: " + update . Version ;
1681+
16271682 if ( float . Parse ( CURRENT_VERSION ) < float . Parse ( update . Version ) ) { // Update is available
16281683 Thread thread = new Thread ( ( ) =>
16291684 {
1630- Thread . Sleep ( 5000 ) ;
16311685 Dispatcher . Invoke ( new Action ( ( ) =>
16321686 {
16331687 updateGrid . Visibility = Visibility . Visible ;
@@ -1641,9 +1695,76 @@ private void Update(object sender, AsyncCompletedEventArgs e) {
16411695 } ) ) ;
16421696 } ) ;
16431697 thread . Start ( ) ;
1698+ updateResponse = true ;
1699+ }
1700+ else if ( isSettingsWindowDisplaying ) {
1701+ System . Windows . Forms . MessageBox . Show ( "You're running the latest version of the Project Tracker!" ,
1702+ "No New Update" ,
1703+ System . Windows . Forms . MessageBoxButtons . OK ,
1704+ System . Windows . Forms . MessageBoxIcon . Information ) ;
1705+ updateResponse = true ;
16441706 }
16451707 }
16461708
1709+ /// <summary>
1710+ /// Runs when the next version file has been downloaded.
1711+ /// Sets the next update info in the settings.
1712+ /// </summary>
1713+ private void NextVersionDownloadComplete ( object sender , AsyncCompletedEventArgs e ) {
1714+ string json = File . ReadAllText ( NEXT_VERSION_INFO ) ;
1715+
1716+ if ( json == "" && isSettingsWindowDisplaying ) { // Didn't download the entire file properly
1717+ // Couldn't download update file. Possible their wifi isn't working
1718+ nextVersionLabel . Content = "Couldn't get next release data." ;
1719+ latestVersionLabel . Content = "Latest version: unavailable" ;
1720+ nextVersionReleaseLabel . Visibility = Visibility . Hidden ;
1721+ nextVersionFeaturesLabel . Visibility = Visibility . Hidden ;
1722+ nextVersionUpdateOneLabel . Visibility = Visibility . Hidden ;
1723+ nextVersionUpdateTwoLabel . Visibility = Visibility . Hidden ;
1724+ nextVersionUpdateThreeLabel . Visibility = Visibility . Hidden ;
1725+ updateResponse = true ;
1726+ return ;
1727+ }
1728+ else if ( json == "" ) {
1729+ updateResponse = true ;
1730+ return ;
1731+ }
1732+
1733+ NextVersionManifest . Rootobject nextVersionInfo =
1734+ JsonConvert . DeserializeObject < NextVersionManifest . Rootobject > ( json ) ;
1735+ nextVersionLabel . Visibility = Visibility . Visible ;
1736+ nextVersionLabel . Content = "Version: " + nextVersionInfo . Version ;
1737+
1738+ nextVersionReleaseLabel . Visibility = Visibility . Visible ;
1739+ if ( nextVersionInfo . ReleaseDateConfirmed == "false" ) {
1740+ nextVersionReleaseLabel . Content = "Estimated release date: " + nextVersionInfo . EstimatedRelease ;
1741+ }
1742+ else {
1743+ nextVersionReleaseLabel . Content = "Release date: " + nextVersionInfo . EstimatedRelease ;
1744+ }
1745+
1746+ nextVersionUpdateOneLabel . Visibility = Visibility . Visible ;
1747+ nextVersionUpdateTwoLabel . Visibility = Visibility . Hidden ;
1748+ nextVersionUpdateThreeLabel . Visibility = Visibility . Hidden ;
1749+
1750+ if ( nextVersionInfo . NewFeatures [ 0 ] == "" ) {
1751+ nextVersionUpdateOneLabel . Content = "Feature list coming soon..." ;
1752+ }
1753+ else {
1754+ nextVersionUpdateOneLabel . Content = "1. " + nextVersionInfo . NewFeatures [ 0 ] ;
1755+ }
1756+
1757+ if ( nextVersionInfo . NewFeatures [ 1 ] != "" ) {
1758+ nextVersionUpdateTwoLabel . Visibility = Visibility . Visible ;
1759+ nextVersionUpdateTwoLabel . Content = "2. " + nextVersionInfo . NewFeatures [ 1 ] ;
1760+ }
1761+
1762+ if ( nextVersionInfo . NewFeatures [ 2 ] != "" ) {
1763+ nextVersionUpdateThreeLabel . Visibility = Visibility . Visible ;
1764+ nextVersionUpdateThreeLabel . Content = "3. " + nextVersionInfo . NewFeatures [ 2 ] ;
1765+ }
1766+ updateResponse = true ;
1767+ }
16471768 /// <summary>
16481769 /// Runs when the user presses the update button when an update is available.
16491770 /// </summary>
@@ -2024,9 +2145,63 @@ private void SettingsButtonMouseDown(object sender, MouseButtonEventArgs e) {
20242145 // Set values
20252146 displayingImage . Source = ( ImageSource ) TryFindResource ( "settingsDrawingImage" ) ;
20262147 displayingTitle . Content = "Settings" ;
2148+ currentVersionLabel . Content = "Installed version: " + CURRENT_VERSION ;
2149+
20272150 isSettingsWindowDisplaying = true ;
20282151 selectedIndex = 0 ;
20292152 SetSelectedProject ( ) ;
2153+ settingsGrid . Visibility = Visibility . Visible ;
2154+
2155+ }
2156+
2157+ private void UpdateButtonMouseDown ( object sender , MouseButtonEventArgs e ) {
2158+ updateButton1 . IsEnabled = false ;
2159+
2160+ if ( File . Exists ( VERSION_INFO ) ) {
2161+ File . Delete ( VERSION_INFO ) ;
2162+ }
2163+ if ( File . Exists ( NEXT_VERSION_INFO ) ) {
2164+ File . Delete ( NEXT_VERSION_INFO ) ;
2165+ }
2166+
2167+ DisableUpdateButton ( ) ;
2168+ // Download latest version information
2169+ try {
2170+ updateButton1 . IsEnabled = false ;
2171+ updateResponse = false ;
2172+
2173+ WebClient updateClient = new WebClient ( ) ;
2174+ updateClient . DownloadFileCompleted += new AsyncCompletedEventHandler ( Update ) ;
2175+ updateClient . DownloadFileAsync ( new Uri ( VERSION_MANIFEST_URL ) , VERSION_INFO ) ;
2176+ }
2177+ catch ( WebException ) {
2178+ // Couldn't download update file. Possible their wifi isn't working
2179+ updateResponse = true ;
2180+ }
2181+ }
2182+
2183+
2184+ /// <summary>
2185+ /// Waits for 15 seconds before allowing the user to check for an update again.
2186+ /// </summary>
2187+ /// <returns></returns>
2188+ private async Task DisableUpdateButton ( ) {
2189+ for ( int i = 15 ; i >= 0 ; i -- ) {
2190+ updateButton1 . Content = i . ToString ( ) ;
2191+ await Task . Delay ( 1000 ) ;
2192+ }
2193+ if ( updateResponse ) {
2194+ updateButton1 . Content = "Check for an update" ;
2195+ updateButton1 . IsEnabled = true ;
2196+ }
2197+ else {
2198+ updateButton1 . Content = "Please wait..." ;
2199+ while ( ! updateResponse ) {
2200+ await Task . Delay ( 1000 ) ;
2201+ }
2202+ updateButton1 . Content = "Check for an update" ;
2203+ updateButton1 . IsEnabled = true ;
2204+ }
20302205 }
20312206 }
20322207}
0 commit comments