Skip to content

Commit

Permalink
Added the possibility to test multiple websites (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Peyronnet committed Aug 13, 2022
1 parent 5dc9920 commit 6dc7d9e
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 9 deletions.
26 changes: 26 additions & 0 deletions InternetTest/InternetTest/Classes/DownDetectorTestResult.cs
@@ -0,0 +1,26 @@
/*
MIT License
Copyright (c) Léo Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

namespace InternetTest.Classes;
public record DownDetectorTestResult(int Code, int Time, string Message);
58 changes: 49 additions & 9 deletions InternetTest/InternetTest/Pages/DownDetectorPage.xaml.cs
Expand Up @@ -22,11 +22,13 @@ MIT License
SOFTWARE.
*/
using InternetTest.Classes;
using InternetTest.UserControls;
using LeoCorpLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -47,6 +49,8 @@ namespace InternetTest.Pages
/// </summary>
public partial class DownDetectorPage : Page
{
DownDetectorTestResult CurrentResult { get; set; }
internal int TotalWebsites { get; set; } = 1;
public DownDetectorPage()
{
InitializeComponent();
Expand All @@ -60,19 +64,43 @@ private void InitUI()

private async void TestBtn_Click(object sender, RoutedEventArgs e)
{
// Check if the URL is valid
if (!WebsiteTxt.Text.StartsWith("http"))
{
WebsiteTxt.Text = "https://" + WebsiteTxt.Text;
}

if (!Global.IsUrlValid(WebsiteTxt.Text)) return;

TotalWebsites = DownDetectorItemDisplayer.Children.Count + ((!string.IsNullOrEmpty(WebsiteTxt.Text)) ? 1 : 0);
TestBtn.Content = $"{Properties.Resources.Test} ({TotalWebsites})";

// Test the current website
CurrentResult = await LaunchTest(WebsiteTxt.Text);

// If there are any ohther websites, test them
for (int i = 0; i < DownDetectorItemDisplayer.Children.Count; i++)
{
if (DownDetectorItemDisplayer.Children[i] is DownDetectorItem item)
{
item.DownDetectorTestResult = await LaunchTest(item.WebsiteTxt.Text);
}
}
}

internal async Task<DownDetectorTestResult> LaunchTest(string url)
{
if (!url.StartsWith("http"))
{
url = "https://" + url;
}

if (!Global.IsUrlValid(url)) return new(0, 0, "Invalid URL");

// Show the "Waiting" screen
StatusIconTxt.Text = "\uF2DE";
StatusIconTxt.Foreground = new SolidColorBrush(Global.GetColorFromResource("Gray"));
StatusTxt.Text = Properties.Resources.TestInProgress;

if (await NetworkConnection.IsAvailableAsync(WebsiteTxt.Text))
if (await NetworkConnection.IsAvailableAsync(url))
{
// Update icon and text
StatusIconTxt.Text = "\uF299"; // Update the icon
Expand All @@ -84,16 +112,17 @@ private async void TestBtn_Click(object sender, RoutedEventArgs e)
DispatcherTimer dispatcherTimer = new() { Interval = TimeSpan.FromMilliseconds(1) };
dispatcherTimer.Tick += (o, e) => time++;
dispatcherTimer.Start();
int statusCode = await NetworkConnection.GetWebPageStatusCodeAsync(WebsiteTxt.Text);

int statusCode = await NetworkConnection.GetWebPageStatusCodeAsync(url);

dispatcherTimer.Stop();
DetailsStatusTxt.Text = statusCode.ToString();

string message = await NetworkConnection.GetWebPageStatusDescriptionAsync(WebsiteTxt.Text);
string message = await NetworkConnection.GetWebPageStatusDescriptionAsync(url);
DetailsMessageTxt.Text = message;

DetailsTimeTxt.Text = $"{time}ms"; // Update the time
return new(statusCode, time, message);
}
else
{
Expand All @@ -109,15 +138,17 @@ private async void TestBtn_Click(object sender, RoutedEventArgs e)
dispatcherTimer.Tick += (o, e) => time++;
dispatcherTimer.Start();

int statusCode = await NetworkConnection.GetWebPageStatusCodeAsync(WebsiteTxt.Text);
int statusCode = await NetworkConnection.GetWebPageStatusCodeAsync(url);

dispatcherTimer.Stop();
DetailsStatusTxt.Text = statusCode.ToString();

string message = await NetworkConnection.GetWebPageStatusDescriptionAsync(WebsiteTxt.Text);
string message = await NetworkConnection.GetWebPageStatusDescriptionAsync(url);
DetailsMessageTxt.Text = message;

DetailsTimeTxt.Text = $"{time}ms"; // Update the time

return new(statusCode, time, message);
}
}

Expand All @@ -128,12 +159,21 @@ private void BrowserBtn_Click(object sender, RoutedEventArgs e)

private void InfoBtn_Click(object sender, RoutedEventArgs e)
{

DetailsStatusTxt.Text = CurrentResult.Code.ToString(); // Set the text
DetailsTimeTxt.Text = $"{CurrentResult.Time}ms"; // Set the text
DetailsMessageTxt.Text = CurrentResult.Message; // Set the text
}

private void AddBtn_Click(object sender, RoutedEventArgs e)
{
if (!WebsiteTxt.Text.StartsWith("http"))
{
WebsiteTxt.Text = "https://" + WebsiteTxt.Text;
}
DownDetectorItemDisplayer.Children.Add(new DownDetectorItem(DownDetectorItemDisplayer, WebsiteTxt.Text, CurrentResult));

TotalWebsites = DownDetectorItemDisplayer.Children.Count + ((!string.IsNullOrEmpty(WebsiteTxt.Text)) ? 1 : 0);
TestBtn.Content = $"{Properties.Resources.Test} ({TotalWebsites})";
}
}
}
21 changes: 21 additions & 0 deletions InternetTest/InternetTest/UserControls/DownDetectorItem.xaml
@@ -0,0 +1,21 @@
<UserControl x:Class="InternetTest.UserControls.DownDetectorItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:InternetTest.UserControls"
mc:Ignorable="d" FontFamily="../Fonts/#Hauora"
Height="Auto" Margin="0" Width="300">
<Grid d:Background="#fff">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox Background="Transparent" x:Name="WebsiteTxt" Padding="2" Margin="5" BorderThickness="0" FontWeight="Bold" Foreground="{Binding Source={StaticResource DarkGray}}" d:Text="https://bing.com" VerticalAlignment="Center"/>
<StackPanel Grid.Column="1" Orientation="Horizontal">
<Button x:Name="TestSiteBtn" Content="&#xF606;" Background="Transparent" BorderThickness="0" Padding="4" Margin="2" FontFamily="../Fonts/#FluentSystemIcons-Regular" Style="{DynamicResource ToolButton}" VerticalAlignment="Center" Click="TestSiteBtn_Click"/>
<Button x:Name="InfoBtn" Content="&#xF4A4;" Background="Transparent" BorderThickness="0" Padding="4" Margin="2" FontFamily="../Fonts/#FluentSystemIcons-Regular" Style="{DynamicResource ToolButton}" VerticalAlignment="Center" Click="InfoBtn_Click"/>
<Button x:Name="DeleteBtn" Content="&#xF34D;" Background="Transparent" BorderThickness="0" Padding="4" Margin="2" FontFamily="../Fonts/#FluentSystemIcons-Regular" Style="{DynamicResource ToolButton}" VerticalAlignment="Center" Click="DeleteBtn_Click"/>
</StackPanel>
</Grid>
</UserControl>
86 changes: 86 additions & 0 deletions InternetTest/InternetTest/UserControls/DownDetectorItem.xaml.cs
@@ -0,0 +1,86 @@
/*
MIT License
Copyright (c) Léo Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using InternetTest.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace InternetTest.UserControls;
/// <summary>
/// Interaction logic for DownDetectorItem.xaml
/// </summary>
public partial class DownDetectorItem : UserControl
{
StackPanel ParentStackPanel { get; init; }
internal DownDetectorTestResult DownDetectorTestResult { get; set; }
string URL { get; set; }
public DownDetectorItem(StackPanel parentElement, string url, DownDetectorTestResult downDetectorTestResult)
{
InitializeComponent();
ParentStackPanel = parentElement; // Save the parent element
URL = url; // Save the URL
DownDetectorTestResult = downDetectorTestResult; // Save the result

InitUI(); // load the UI
}

private void InitUI()
{
WebsiteTxt.Text = URL; // Set text
}

private async void TestSiteBtn_Click(object sender, RoutedEventArgs e)
{
if (!WebsiteTxt.Text.StartsWith("http"))
{
WebsiteTxt.Text = "https://" + WebsiteTxt.Text;
}
DownDetectorTestResult = await Global.DownDetectorPage.LaunchTest(WebsiteTxt.Text); // Launch the test
}

private void InfoBtn_Click(object sender, RoutedEventArgs e)
{
Global.DownDetectorPage.DetailsStatusTxt.Text = DownDetectorTestResult.Code.ToString(); // Set the text
Global.DownDetectorPage.DetailsTimeTxt.Text = $"{DownDetectorTestResult.Time}ms"; // Set the text
Global.DownDetectorPage.DetailsMessageTxt.Text = DownDetectorTestResult.Message; // Set the text
}

private void DeleteBtn_Click(object sender, RoutedEventArgs e)
{
ParentStackPanel.Children.Remove(this); // Delete this element from the parent element
Global.DownDetectorPage.TotalWebsites--; // Update the total websites
Global.DownDetectorPage.TestBtn.Content = $"{Properties.Resources.Test} ({Global.DownDetectorPage.TotalWebsites})"; // Update the text
}
}

0 comments on commit 6dc7d9e

Please sign in to comment.