Skip to content

Commit fe3243f

Browse files
committed
Added History system
1 parent 55bbb1e commit fe3243f

File tree

6 files changed

+110
-9
lines changed

6 files changed

+110
-9
lines changed

InternetTest/InternetTest/App.xaml.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@ namespace InternetTest;
3333
/// </summary>
3434
public partial class App : Application
3535
{
36-
protected override void OnStartup(StartupEventArgs e)
36+
protected override async void OnStartup(StartupEventArgs e)
3737
{
3838
// Load settings
3939
Settings settings = new();
4040
settings.Load();
4141

42+
ActivityHistory history = await ActivityHistory.LoadAsync();
43+
4244
ThemeHelper.ChangeTheme(settings.Theme);
4345
Context.ChangeLanguage(settings.Language);
4446

4547
// Set the main window
4648
MainWindow = new MainWindow();
47-
MainViewModel mvm = new(settings, MainWindow);
49+
MainViewModel mvm = new(settings, history, MainWindow);
4850
MainWindow.DataContext = mvm;
4951

5052
MainWindow.Show();
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) Léo Corporation
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
using InternetTest.Helpers;
25+
using System.IO;
26+
using System.Xml.Serialization;
27+
28+
namespace InternetTest.Models;
29+
30+
public class ActivityHistory
31+
{
32+
public List<Activity> Activity { get; set; }
33+
34+
public ActivityHistory()
35+
{
36+
Activity = [];
37+
}
38+
39+
public static async Task<ActivityHistory> LoadAsync()
40+
{
41+
// Load from XML File
42+
var filePath = $@"{Context.DefaultStoragePath}\Activity.xml";
43+
if (File.Exists(filePath))
44+
{
45+
return await XmlHelper.DeserializeAsync<ActivityHistory>(filePath) ?? new ActivityHistory();
46+
}
47+
else
48+
{
49+
var history = new ActivityHistory();
50+
history.Save();
51+
return history;
52+
}
53+
}
54+
55+
public void Save()
56+
{
57+
var filePath = $@"{Context.DefaultStoragePath}\Activity.xml";
58+
59+
XmlSerializer xmlSerializer = new(typeof(ActivityHistory));
60+
StreamWriter streamWriter = new(filePath);
61+
xmlSerializer.Serialize(streamWriter, this);
62+
63+
streamWriter.Dispose();
64+
}
65+
}
66+
67+
public class Activity
68+
{
69+
public string Name { get; init; }
70+
public string Result { get; init; }
71+
public bool Success { get; init; }
72+
public DateTime Date { get; init; }
73+
74+
public Activity(string name, string result, bool success, DateTime date)
75+
{
76+
Name = name;
77+
Result = result;
78+
Success = success;
79+
Date = date;
80+
}
81+
82+
public Activity()
83+
{
84+
Name = string.Empty;
85+
Result = string.Empty;
86+
Success = false;
87+
Date = DateTime.MinValue;
88+
}
89+
}

InternetTest/InternetTest/ViewModels/Components/SidebarViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private void SettingsPage(object? obj)
7474

7575
private void DownDetectorPage(object? obj)
7676
{
77-
_mainViewModel.CurrentViewModel = new DownDetectorPageViewModel(_mainViewModel.Settings);
77+
_mainViewModel.CurrentViewModel = new DownDetectorPageViewModel(_mainViewModel.Settings, _mainViewModel.History);
7878
}
7979

8080
private void DnsToolsPage(object? obj)

InternetTest/InternetTest/ViewModels/Components/WebsiteItemViewModel.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
*/
2424
using InternetTest.Commands;
2525
using InternetTest.Helpers;
26+
using InternetTest.Models;
2627
using PeyrSharp.Core;
2728
using System.Collections.ObjectModel;
2829
using System.Windows.Input;
@@ -56,9 +57,11 @@ public class WebsiteItemViewModel : ViewModelBase
5657
});
5758

5859
private readonly DownDetectorPageViewModel _downDetectorPageViewModel;
59-
public WebsiteItemViewModel(string url, DownDetectorPageViewModel downDetectorPageViewModel)
60+
private readonly ActivityHistory _history;
61+
public WebsiteItemViewModel(string url, DownDetectorPageViewModel downDetectorPageViewModel, ActivityHistory history)
6062
{
6163
_downDetectorPageViewModel = downDetectorPageViewModel;
64+
_history = history;
6265

6366
Url = url;
6467
StatusBackground = ThemeHelper.GetSolidColorBrush("LightAccent");
@@ -98,6 +101,8 @@ internal async void TestAsync()
98101
new(Properties.Resources.StatusMessage, statusInfo.StatusDescription, 0, 0),
99102
new(Properties.Resources.TimeElapsed, $"{(endTime - startTime).TotalMilliseconds:0} ms", 0,1 ),
100103
];
104+
105+
_history.Activity.Add(new Activity(Url, statusInfo.StatusCode.ToString(), statusInfo.StatusCode is >= 200 and < 300, DateTime.Now));
101106
}
102107
catch { }
103108
ShowStatusCode = true;

InternetTest/InternetTest/ViewModels/DownDetectorPageViewModel.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class DownDetectorPageViewModel : ViewModelBase
6565

6666
if (Websites.Any(x => x.Url == Site)) return; // already exists
6767

68-
Websites.Add(new WebsiteItemViewModel(Site, this));
68+
Websites.Add(new WebsiteItemViewModel(Site, this, _history));
6969
Site = string.Empty;
7070
});
7171

@@ -77,6 +77,7 @@ public class DownDetectorPageViewModel : ViewModelBase
7777
site.TestAsync();
7878
}
7979
IsTesting = false;
80+
_history.Save();
8081
});
8182

8283
public ICommand LaunchScheduledCommand => new RelayCommand(o =>
@@ -127,11 +128,13 @@ public class DownDetectorPageViewModel : ViewModelBase
127128
public ICommand ClearCommand => new RelayCommand(o => Websites.Clear());
128129

129130
private readonly Settings _settings;
130-
public DownDetectorPageViewModel(Settings settings)
131+
private readonly ActivityHistory _history;
132+
public DownDetectorPageViewModel(Settings settings, ActivityHistory history)
131133
{
132134
_settings = settings;
135+
_history = history;
133136
TimeInterval = _settings.DefaultTimeInterval ?? 10;
134-
Websites = [.. _settings.DownDetectorWebsites?.Select(x => new WebsiteItemViewModel(x, this)) ?? []];
137+
Websites = [.. _settings.DownDetectorWebsites?.Select(x => new WebsiteItemViewModel(x, this, _history)) ?? []];
135138
Websites.CollectionChanged += (s, e) =>
136139
{
137140
_settings.DownDetectorWebsites = [.. Websites.Select(x => x.Url)];

InternetTest/InternetTest/ViewModels/MainViewModel.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public bool ConfidentialMode
9191
public string ConfidentialTooltip { get => _confidentialTooltip; set { _confidentialTooltip = value; OnPropertyChanged(nameof(ConfidentialTooltip)); } }
9292

9393
public Settings Settings { get; set; }
94+
public ActivityHistory History { get; set; }
9495
public static string Version => Context.Version;
9596
private readonly Window _mainWindow;
9697

@@ -99,9 +100,10 @@ public bool ConfidentialMode
99100
{
100101
ConfidentialMode = !ConfidentialMode;
101102
});
102-
public MainViewModel(Settings settings, Window mainWindow)
103+
public MainViewModel(Settings settings, ActivityHistory history, Window mainWindow)
103104
{
104105
Settings = settings;
106+
History = history;
105107
_mainWindow = mainWindow;
106108
_sidebarViewModel = new(this);
107109

@@ -110,7 +112,7 @@ public MainViewModel(Settings settings, Window mainWindow)
110112

111113
CurrentViewModel = Settings.DefaultPage switch
112114
{
113-
AppPages.DownDetector => new DownDetectorPageViewModel(Settings),
115+
AppPages.DownDetector => new DownDetectorPageViewModel(Settings, History),
114116
AppPages.DnsTool => new DnsToolsPageViewModel(),
115117
AppPages.WiFiNetworks => new WiFiPageViewModel(Settings),
116118
AppPages.WiFiPasswords => new WiFiPageViewModel(Settings),

0 commit comments

Comments
 (0)