Skip to content

Commit

Permalink
Added Synethia code (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Peyronnet committed Aug 13, 2022
1 parent 1379e08 commit 2419774
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 12 deletions.
15 changes: 12 additions & 3 deletions InternetTest/InternetTest/Classes/Global.cs
Expand Up @@ -39,7 +39,7 @@ public static class Global
public static HomePage HomePage { get; set; } = new();
public static StatusPage StatusPage { get; set; } = new();

public static SynethiaConfig SynethiaConfig { get; set; }
public static SynethiaConfig SynethiaConfig { get; set; } = new();

public static string GetHiSentence
{
Expand Down Expand Up @@ -169,8 +169,17 @@ public static List<ActionInfo> GetMostRelevantActions(SynethiaConfig synethiaCon
{ AppActions.Test, "TEXT_HERE" },
};

public static Color GetColorFromResource(string resourceName)
public static Color GetColorFromResource(string resourceName) => (Color)ColorConverter.ConvertFromString(Application.Current.Resources[resourceName].ToString());

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
return (Color)ColorConverter.ConvertFromString(Application.Current.Resources[resourceName].ToString());
if (depObj == null) yield return (T)Enumerable.Empty<T>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject ithChild = VisualTreeHelper.GetChild(depObj, i);
if (ithChild == null) continue;
if (ithChild is T t) yield return t;
foreach (T childOfChild in FindVisualChildren<T>(ithChild)) yield return childOfChild;
}
}
}
36 changes: 30 additions & 6 deletions InternetTest/InternetTest/Classes/SynethiaConfig.cs
Expand Up @@ -32,6 +32,19 @@ MIT License
namespace InternetTest.Classes;
public class SynethiaConfig
{
public SynethiaConfig()
{
StatusPageInfo = new();
DownDetectorPageInfo = new();
MyIPPageInfo = new();
LocateIPPageInfo = new();
PingPageInfo = new();
IPConfigPageInfo = new();
WiFiPasswordsPageInfo = new();

ActionInfos = Enumerable.Empty<ActionInfo>().ToList();
}

public PageInfo StatusPageInfo { get; set; }
public PageInfo DownDetectorPageInfo { get; set; }
public PageInfo MyIPPageInfo { get; set; }
Expand All @@ -43,11 +56,22 @@ public class SynethiaConfig
public List<ActionInfo> ActionInfos { get; set; }
}

public record PageInfo(
int EnterUnixTime,
int LeaveUnixTime,
int TotalTimeSpent,
int InteractionCount,
double Score);
public class PageInfo
{
public PageInfo()
{
EnterUnixTime = 0;
LeaveUnixTime = 0;
TotalTimeSpent = 0;
InteractionCount = 0;
Score = 0;
}

public int EnterUnixTime { get; set; }
public int LeaveUnixTime { get; set; }
public int TotalTimeSpent { get; set; }
public int InteractionCount { get; set; }
public double Score { get; set; }
}

public record ActionInfo(AppActions Action, int UsageCount);
15 changes: 15 additions & 0 deletions InternetTest/InternetTest/MainWindow.xaml.cs
Expand Up @@ -22,6 +22,8 @@ MIT License
SOFTWARE.
*/
using InternetTest.Classes;
using InternetTest.Pages;
using LeoCorpLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -221,10 +223,12 @@ private void UnCheckAllButton()

private void StatusPageBtn_Click(object sender, RoutedEventArgs e)
{
LeavePage();
UnCheckAllButton(); // Reset all states
CheckButton(StatusPageBtn);

PageDisplayer.Content = Global.StatusPage; // Display the status page
Global.SynethiaConfig.StatusPageInfo.EnterUnixTime = Env.UnixTime; // Update the last entered time
}

private void DownDetectorPageBtn_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -265,6 +269,7 @@ private void WifiPasswordsPageBtn_Click(object sender, RoutedEventArgs e)

private void HomePageBtn_Click(object sender, RoutedEventArgs e)
{
LeavePage();
UnCheckAllButton(); // Reset all states
CheckButton(HomePageBtn, true);

Expand All @@ -282,4 +287,14 @@ private void SettingsPageBtn_Click(object sender, RoutedEventArgs e)
UnCheckAllButton(); // Reset all states
CheckButton(SettingsPageBtn, true);
}

private void LeavePage()
{
if (PageDisplayer.Content is StatusPage)
{
Global.SynethiaConfig.StatusPageInfo.LeaveUnixTime = Env.UnixTime;
Global.SynethiaConfig.StatusPageInfo.TotalTimeSpent += Global.SynethiaConfig.StatusPageInfo.LeaveUnixTime - Global.SynethiaConfig.StatusPageInfo.EnterUnixTime;
Global.SynethiaConfig.StatusPageInfo.Score = Global.SynethiaConfig.StatusPageInfo.TotalTimeSpent * (Global.SynethiaConfig.StatusPageInfo.InteractionCount > 0 ? Global.SynethiaConfig.StatusPageInfo.InteractionCount / 2d : 1d); // Calculate the score
}
}
}
4 changes: 2 additions & 2 deletions InternetTest/InternetTest/Pages/StatusPage.xaml
Expand Up @@ -69,7 +69,7 @@

<TextBlock Text="{x:Static lang:Resources.TimeElapsed}" Foreground="{Binding Source={StaticResource DarkGray}}" FontWeight="ExtraBold" />
<StackPanel Grid.Row="1" Orientation="Horizontal">
<TextBlock Text="&#xF6CF;" Margin="0 0 5 0" FontFamily="../Fonts/#FluentSystemIcons-Regular" VerticalAlignment="Center" FontSize="22"/>
<TextBlock Text="&#xF6CF;" Margin="0 0 5 0" FontFamily="../Fonts/#FluentSystemIcons-Regular" VerticalAlignment="Center" FontSize="22"/>
<TextBlock x:Name="DetailsTimeTxt" Text="0ms" VerticalAlignment="Center" FontWeight="ExtraBold" FontSize="18"/>
</StackPanel>
</Grid>
Expand All @@ -88,7 +88,7 @@

<TextBlock Text="{x:Static lang:Resources.StatusMessage}" Foreground="{Binding Source={StaticResource DarkGray}}" FontWeight="ExtraBold" />
<StackPanel Grid.Row="1" Orientation="Horizontal">
<TextBlock Text="&#xF300;" Margin="0 0 5 0" FontFamily="../Fonts/#FluentSystemIcons-Regular" VerticalAlignment="Center" FontSize="22"/>
<TextBlock Text="&#xF300;" Margin="0 0 5 0" FontFamily="../Fonts/#FluentSystemIcons-Regular" VerticalAlignment="Center" FontSize="22"/>
<TextBlock x:Name="DetailsMessageTxt" Text="N/A" VerticalAlignment="Center" FontWeight="ExtraBold" FontSize="18"/>
</StackPanel>
</Grid>
Expand Down
51 changes: 50 additions & 1 deletion InternetTest/InternetTest/Pages/StatusPage.xaml.cs
Expand Up @@ -47,17 +47,66 @@ namespace InternetTest.Pages;
/// </summary>
public partial class StatusPage : Page
{
bool codeInjected = false;
public StatusPage()
{
InitializeComponent();
InitUI(); // Load the UI
Loaded += (o, e) => InjectSynethiaCode();
}

private void InjectSynethiaCode()
{
if (codeInjected) return;

foreach (Button b in Global.FindVisualChildren<Button>(this))
{
b.Click += (sender, e) =>
{
Global.SynethiaConfig.StatusPageInfo.InteractionCount++;
};
}

// For each TextBox of the page
foreach (TextBox textBox in Global.FindVisualChildren<TextBox>(this))
{
textBox.GotFocus += (o, e) =>
{
Global.SynethiaConfig.StatusPageInfo.InteractionCount++;
};
}

// For each CheckBox/RadioButton of the page
foreach (CheckBox checkBox in Global.FindVisualChildren<CheckBox>(this))
{
checkBox.Checked += (o, e) =>
{
Global.SynethiaConfig.StatusPageInfo.InteractionCount++;
};
checkBox.Unchecked += (o, e) =>
{
Global.SynethiaConfig.StatusPageInfo.InteractionCount++;
};
}

foreach (RadioButton radioButton in Global.FindVisualChildren<RadioButton>(this))
{
radioButton.Checked += (o, e) =>
{
Global.SynethiaConfig.StatusPageInfo.InteractionCount++;
};
radioButton.Unchecked += (o, e) =>
{
Global.SynethiaConfig.StatusPageInfo.InteractionCount++;
};
}
}

private void InitUI()
{
TitleTxt.Text = $"{Properties.Resources.WebUtilities} > {Properties.Resources.Status}"; // Set the title
}

private async void LaunchTest(string? customSite = null)
{
try
Expand Down

0 comments on commit 2419774

Please sign in to comment.