Skip to content

Commit e379871

Browse files
committed
Improved error handling in Home page (#687)
1 parent d3299b6 commit e379871

File tree

5 files changed

+105
-57
lines changed

5 files changed

+105
-57
lines changed

InternetTest/InternetTest/Helpers/NetworkHelper.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,22 @@ public static Network GetCurrentNetwork()
5656

5757
public static int GetCurrentSpeed()
5858
{
59-
NetworkInterface[] adapters = [.. NetworkInterface.GetAllNetworkInterfaces().OrderByDescending(x => x.GetIPStatistics().BytesReceived)];
60-
foreach (NetworkInterface adapter in adapters)
59+
try
6160
{
62-
if (adapter.OperationalStatus == OperationalStatus.Up && adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback)
61+
NetworkInterface[] adapters = [.. NetworkInterface.GetAllNetworkInterfaces().OrderByDescending(x => x.GetIPStatistics().BytesReceived)];
62+
foreach (NetworkInterface adapter in adapters)
6363
{
64-
long speed = adapter.Speed;
65-
if (speed > 0)
64+
if (adapter.OperationalStatus == OperationalStatus.Up && adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback)
6665
{
67-
return (int)(speed / 1_000_000); // Convert to Mbps
66+
long speed = adapter.Speed;
67+
if (speed > 0)
68+
{
69+
return (int)(speed / 1_000_000); // Convert to Mbps
70+
}
6871
}
6972
}
7073
}
74+
catch { }
7175
return 0; // No active network found or speed is zero
7276
}
7377
}

InternetTest/InternetTest/Models/Ip.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,19 @@ public static async Task<Ip> GetIp(string ip)
102102
}
103103
catch
104104
{
105-
return new();
105+
return new()
106+
{
107+
Isp = Properties.Resources.Unknown,
108+
City = Properties.Resources.Unknown,
109+
Country = Properties.Resources.Unknown,
110+
Lat = 0,
111+
Lon = 0,
112+
RegionName = Properties.Resources.Unknown,
113+
Status = Properties.Resources.Failed,
114+
Timezone = Properties.Resources.Unknown,
115+
Zip = Properties.Resources.Unknown,
116+
Query = ip
117+
};
106118
}
107119
}
108120

InternetTest/InternetTest/Models/Settings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Settings()
3939
MapProvider = MapProvider.OpenStreetMap;
4040
DefaultPage = AppPages.Home;
4141
ShowNotficationWhenUpdateAvailable = true;
42-
CheckUpdateOnStart = true;
42+
CheckUpdateOnStart = false;
4343
UseHttps = true;
4444
IsFirstRun = true;
4545
TestSite = "https://leocorporation.dev";

InternetTest/InternetTest/ViewModels/HomePageViewModel.cs

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -125,35 +125,14 @@ public HomePageViewModel(Settings settings, ActivityHistory history, MainViewMod
125125
_settings = settings;
126126
_mainViewModel = mainViewModel;
127127

128-
// Get status
129128
LoadStatusCard();
130-
131-
// Get WiFi information
132-
string? ssid = NetworkHelper.GetCurrentWifiSSID();
133-
int signalQuality = ssid != null ? NetworkHelper.GetCurrentNetwork().SignalQuality : 0;
134-
WiFiName = ssid ?? (connected ? Properties.Resources.Ethernet : Properties.Resources.NotConnectedS);
135-
WiFiIcon = ssid != null ? GetWiFiIcon(signalQuality) : (connected ? "\uF35A" : "\uFB69");
136-
WiFiStrengthText = $"{Properties.Resources.SignalQuality} - {(ssid != null ? signalQuality : 100)}%";
137-
138-
// Get IP address
129+
LoadWiFiInfo();
139130
LoadIpAddress();
140131

141132
// Network speed
142-
Speed = $"~{NetworkHelper.GetCurrentSpeed()} Mbps";
143-
144-
// Load connection details
145-
var networkInterface = NetworkInterface.GetAllNetworkInterfaces()
146-
.Where(x => x.OperationalStatus == OperationalStatus.Up)
147-
.OrderByDescending(x => x.GetIPStatistics().BytesReceived)
148-
.FirstOrDefault();
149-
if (networkInterface != null)
150-
{
151-
var ipProps = WindowsIpConfig.FromNetworkInterface(networkInterface);
133+
Speed = connected ? $"~{NetworkHelper.GetCurrentSpeed()} Mbps" : Properties.Resources.Unknown;
152134

153-
LocalIp = ipProps?.IPv4Address ?? Properties.Resources.Unknown;
154-
Gateway = ipProps?.IPv4Gateway ?? Properties.Resources.Unknown;
155-
Dns = string.Join("\n", networkInterface.GetIPProperties().DnsAddresses.Select(x => x.ToString().Replace("%16", ""))) ?? Properties.Resources.Unknown;
156-
}
135+
LoadConnectionDetails();
157136

158137
// Load history
159138
History = new(history.Activity.OrderByDescending(x => x.Date).Select(x => new HistoryItemViewModel(x)));
@@ -183,9 +162,35 @@ internal async void LoadStatusCard()
183162
return;
184163
}
185164

186-
connected = await Internet.IsAvailableAsync(_settings.TestSite);
187-
StatusText = connected ? Properties.Resources.ConnectedS : Properties.Resources.NotConnectedS;
188-
StatusColor = connected ? ThemeHelper.GetSolidColorBrush("Green") : ThemeHelper.GetSolidColorBrush("Red");
165+
try
166+
{
167+
connected = await Internet.IsAvailableAsync(_settings.TestSite);
168+
StatusText = connected ? Properties.Resources.ConnectedS : Properties.Resources.NotConnectedS;
169+
StatusColor = connected ? ThemeHelper.GetSolidColorBrush("Green") : ThemeHelper.GetSolidColorBrush("Red");
170+
}
171+
catch
172+
{
173+
connected = false;
174+
StatusText = connected ? Properties.Resources.ConnectedS : Properties.Resources.NotConnectedS;
175+
StatusColor = connected ? ThemeHelper.GetSolidColorBrush("Green") : ThemeHelper.GetSolidColorBrush("Red");
176+
}
177+
}
178+
179+
private void LoadWiFiInfo()
180+
{
181+
try
182+
{
183+
string? ssid = NetworkHelper.GetCurrentWifiSSID();
184+
int signalQuality = ssid != null ? NetworkHelper.GetCurrentNetwork().SignalQuality : 0;
185+
WiFiName = ssid ?? (connected ? Properties.Resources.Ethernet : Properties.Resources.NotConnectedS);
186+
WiFiIcon = ssid != null ? GetWiFiIcon(signalQuality) : (connected ? "\uF35A" : "\uFB69");
187+
WiFiStrengthText = $"{Properties.Resources.SignalQuality} - {(ssid != null ? signalQuality : 100)}%";
188+
}
189+
catch
190+
{
191+
WiFiName = connected ? Properties.Resources.Ethernet : Properties.Resources.NotConnectedS;
192+
WiFiStrengthText = Properties.Resources.Unknown;
193+
}
189194
}
190195

191196
private static string GetWiFiIcon(int signalQuality) => signalQuality switch
@@ -196,6 +201,31 @@ internal async void LoadStatusCard()
196201
_ => "\uF8B3"
197202
};
198203

204+
private void LoadConnectionDetails()
205+
{
206+
try
207+
{
208+
var networkInterface = NetworkInterface.GetAllNetworkInterfaces()
209+
.Where(x => x.OperationalStatus == OperationalStatus.Up)
210+
.OrderByDescending(x => x.GetIPStatistics().BytesReceived)
211+
.FirstOrDefault();
212+
if (networkInterface != null)
213+
{
214+
var ipProps = WindowsIpConfig.FromNetworkInterface(networkInterface);
215+
216+
LocalIp = ipProps?.IPv4Address ?? Properties.Resources.Unknown;
217+
Gateway = ipProps?.IPv4Gateway ?? Properties.Resources.Unknown;
218+
Dns = string.Join("\n", networkInterface.GetIPProperties().DnsAddresses.Select(x => x.ToString().Replace("%16", ""))) ?? Properties.Resources.Unknown;
219+
}
220+
}
221+
catch
222+
{
223+
LocalIp = Properties.Resources.Unknown;
224+
Gateway = Properties.Resources.Unknown;
225+
Dns = Properties.Resources.Unknown;
226+
}
227+
}
228+
199229
void ISensitiveViewModel.ToggleConfidentialMode(bool confidentialMode)
200230
{
201231
ConfidentialMode = confidentialMode;

InternetTest/InternetTest/ViewModels/MainViewModel.cs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3636

3737
namespace InternetTest.ViewModels;
3838

39-
40-
4139
public class MainViewModel : ViewModelBase
4240
{
4341
private SidebarViewModel _sidebarViewModel;
@@ -164,31 +162,35 @@ public void Pin(object? obj)
164162

165163
private async void CheckUpdates()
166164
{
167-
if (!Settings.CheckUpdateOnStart) return;
168-
if (!await Internet.IsAvailableAsync()) return;
169-
var lastVersion = await Update.GetLastVersionAsync(Context.UpdateVersionUrl);
170-
if (!Update.IsAvailable(Context.Version, lastVersion)) return;
171-
172-
var notify = new TaskbarIcon()
173-
{
174-
Icon = System.Drawing.Icon.ExtractAssociatedIcon(AppDomain.CurrentDomain.BaseDirectory + @"\InternetTest.exe"),
175-
ToolTipText = "InternetTest",
176-
};
177-
notify.TrayBalloonTipClosed += (s, e) => { notify.Dispose(); };
178-
notify.TrayBalloonTipClicked += (s, e) =>
165+
try
179166
{
167+
if (!Settings.CheckUpdateOnStart) return;
168+
if (!await Internet.IsAvailableAsync()) return;
169+
var lastVersion = await Update.GetLastVersionAsync(Context.UpdateVersionUrl);
170+
if (!Update.IsAvailable(Context.Version, lastVersion)) return;
171+
172+
var notify = new TaskbarIcon()
173+
{
174+
Icon = System.Drawing.Icon.ExtractAssociatedIcon(AppDomain.CurrentDomain.BaseDirectory + @"\InternetTest.exe"),
175+
ToolTipText = "InternetTest",
176+
};
177+
notify.TrayBalloonTipClosed += (s, e) => { notify.Dispose(); };
178+
notify.TrayBalloonTipClicked += (s, e) =>
179+
{
180180
#if PORTABLE
181181
MessageBox.Show(Properties.Resources.PortableNoAutoUpdates, $"{Properties.Resources.InstallVersion} {lastVersion}", MessageBoxButton.OK, MessageBoxImage.Information);
182182
return;
183183
#else
184-
if (MessageBox.Show(Properties.Resources.InstallConfirmMsg, $"{Properties.Resources.InstallVersion} {lastVersion}", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
185-
{
186-
Sys.ExecuteAsAdmin(Directory.GetCurrentDirectory() + @"\Xalyus Updater.exe"); // Start the updater
187-
Application.Current.Shutdown(); // Close
188-
}
184+
if (MessageBox.Show(Properties.Resources.InstallConfirmMsg, $"{Properties.Resources.InstallVersion} {lastVersion}", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
185+
{
186+
Sys.ExecuteAsAdmin(Directory.GetCurrentDirectory() + @"\Xalyus Updater.exe"); // Start the updater
187+
Application.Current.Shutdown(); // Close
188+
}
189189
#endif
190-
notify.Dispose();
191-
};
192-
notify.ShowBalloonTip(Properties.Resources.Updates, Properties.Resources.AvailableUpdates, BalloonIcon.Info);
190+
notify.Dispose();
191+
};
192+
notify.ShowBalloonTip(Properties.Resources.Updates, Properties.Resources.AvailableUpdates, BalloonIcon.Info);
193+
}
194+
catch { }
193195
}
194196
}

0 commit comments

Comments
 (0)