Skip to content

Commit

Permalink
Added the possibility to detect if a website is down or not (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Peyronnet committed Mar 21, 2021
1 parent 15dc39d commit 2accab1
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
4 changes: 2 additions & 2 deletions InternetTest/InternetTest/Pages/DownDetectorPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
<TextBlock x:Name="ConnectionStatusTxt" d:Text="InternetStatusGoHere" HorizontalAlignment="Center" FontWeight="Bold" FontSize="16" Margin="0 10 0 0" Foreground="{Binding Source={StaticResource Foreground1}}"/>
<TextBox x:Name="WebsiteTxt" Style="{DynamicResource TextBoxStyle1}" Margin="10" Padding="5" Background="{x:Null}" BorderBrush="{Binding Source={StaticResource AccentColor}}" SelectionBrush="{Binding Source={StaticResource AccentColor}}" CaretBrush="{Binding Source={StaticResource Foreground1}}"/>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<Button x:Name="CheckBtn" Click="CheckBtn_Click" Content="{x:Static lang:Resources.Test}" HorizontalAlignment="Center" Padding="10,5,10,5" Style="{StaticResource TabButtonStyle}" Foreground="{Binding Source={StaticResource WindowButtonsHoverForeground1}}" Background="{Binding Source={StaticResource AccentColor}}" FontWeight="Bold" Margin="0,10,10,0"/>
<Button x:Name="OpenBrowserBtn" Click="OpenBrowserBtn_Click" Content="{x:Static lang:Resources.OpenBrowser}" HorizontalAlignment="Center" Padding="10,5,10,5" Style="{StaticResource TabButtonStyle}" Foreground="{Binding Source={StaticResource Foreground1}}" Background="{Binding Source={StaticResource Background2}}" FontWeight="Bold" Margin="0,10,0,0"/>
<Button x:Name="CheckBtn" Click="CheckBtn_Click" Content="{x:Static lang:Resources.Check}" HorizontalAlignment="Center" Padding="10,5,10,5" Style="{StaticResource TabButtonStyle}" Foreground="{Binding Source={StaticResource WindowButtonsHoverForeground1}}" Background="{Binding Source={StaticResource AccentColor}}" FontWeight="Bold" Margin="0,10,10,0"/>
<Button x:Name="OpenBrowserBtn" Click="OpenBrowserBtn_Click" Content="{x:Static lang:Resources.OpenInBrowser}" HorizontalAlignment="Center" Padding="10,5,10,5" Style="{StaticResource TabButtonStyle}" Foreground="{Binding Source={StaticResource Foreground1}}" Background="{Binding Source={StaticResource Background2}}" FontWeight="Bold" Margin="0,10,0,0"/>
</StackPanel>
</StackPanel>
</Grid>
Expand Down
53 changes: 52 additions & 1 deletion InternetTest/InternetTest/Pages/DownDetectorPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ MIT License
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using LeoCorpLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -48,9 +49,59 @@ public DownDetectorPage()
InitializeComponent();
}

private void CheckBtn_Click(object sender, RoutedEventArgs e)
/// <summary>
/// Launch a network test.
/// </summary>
/// <param name="customSite">Leave empty if you don't want to specify a custom website.</param>
private async void Test(string customSite)
{
if (string.IsNullOrEmpty(customSite)) // If a custom site isn't specified
{
if (await NetworkConnection.IsAvailableAsync()) // If there is Internet
{
ConnectionStatusTxt.Text = Properties.Resources.Connected; // Set text of the label
InternetIconTxt.Text = "\uF299"; // Set the icon
InternetIconTxt.Foreground = new SolidColorBrush { Color = (Color)ColorConverter.ConvertFromString(App.Current.Resources["Green"].ToString()) }; // Set the foreground
}
else
{
ConnectionStatusTxt.Text = Properties.Resources.NotConnected; // Set text of the label
InternetIconTxt.Text = "\uF36E"; // Set the icon
InternetIconTxt.Foreground = new SolidColorBrush { Color = (Color)ColorConverter.ConvertFromString(App.Current.Resources["Red"].ToString()) }; // Set the foreground
}
}
else
{
if (await NetworkConnection.IsAvailableTestSiteAsync(customSite)) // If there is Internet
{
ConnectionStatusTxt.Text = Properties.Resources.Connected; // Set text of the label
InternetIconTxt.Text = "\uF299"; // Set the icon
InternetIconTxt.Foreground = new SolidColorBrush { Color = (Color)ColorConverter.ConvertFromString(App.Current.Resources["Green"].ToString()) }; // Set the foreground
}
else
{
ConnectionStatusTxt.Text = Properties.Resources.NotConnected; // Set text of the label
InternetIconTxt.Text = "\uF36E"; // Set the icon
InternetIconTxt.Foreground = new SolidColorBrush { Color = (Color)ColorConverter.ConvertFromString(App.Current.Resources["Red"].ToString()) }; // Set the foreground
}
}
}

private string FormatURL(string url)
{
if (!url.Contains("https://") || !url.Contains("http://")) // If there isn't http(s)
{
return "https://" + url; // Add the https://
}
else
{
return url; // Do nothing
}
}

private void CheckBtn_Click(object sender, RoutedEventArgs e)
{
Test(FormatURL(WebsiteTxt.Text)); // Test
}

private void OpenBrowserBtn_Click(object sender, RoutedEventArgs e)
Expand Down
18 changes: 18 additions & 0 deletions InternetTest/InternetTest/Properties/Resources.Designer.cs

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

6 changes: 6 additions & 0 deletions InternetTest/InternetTest/Properties/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,10 @@
<data name="WarningIPAPI" xml:space="preserve">
<value>InternetTest is using IP-API.com to get IPs' informations.</value>
</data>
<data name="Check" xml:space="preserve">
<value>Check</value>
</data>
<data name="OpenInBrowser" xml:space="preserve">
<value>Open in a browser</value>
</data>
</root>
6 changes: 6 additions & 0 deletions InternetTest/InternetTest/Properties/Resources.fr-FR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,10 @@
<data name="WarningIPAPI" xml:space="preserve">
<value>InternetTest utilise IP-API.com pour obtenir les informations des IPs.</value>
</data>
<data name="Check" xml:space="preserve">
<value>Vérifier</value>
</data>
<data name="OpenInBrowser" xml:space="preserve">
<value>Ouvrir dans un navigateur</value>
</data>
</root>
6 changes: 6 additions & 0 deletions InternetTest/InternetTest/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,10 @@
<data name="WarningIPAPI" xml:space="preserve">
<value>InternetTest is using IP-API.com to get IPs' informations.</value>
</data>
<data name="Check" xml:space="preserve">
<value>Check</value>
</data>
<data name="OpenInBrowser" xml:space="preserve">
<value>Open in a browser</value>
</data>
</root>

0 comments on commit 2accab1

Please sign in to comment.