Skip to content

Commit b6d094b

Browse files
committed
Refactor for cleaner syntax and improved functionality.
Simplified code by reducing nested blocks, switching to `using` statements, and making properties read-only where applicable. Enhanced data handling with a unique user ID generation in `AnalyticsService` and replaced embedded report functionality with a direct link to GitHub. These changes aim to improve code readability, maintainability, and efficiency.
1 parent eadc24d commit b6d094b

File tree

4 files changed

+68
-55
lines changed

4 files changed

+68
-55
lines changed

source/HI2UC/EntryPoint.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,13 @@ public string DoAction(Form parentForm, string srtText, double frame, string uiL
6262
// analytics
6363

6464
var analyticsService = new AnalyticsService();
65-
_ = analyticsService.SendAsync(new Data()
66-
{
67-
OsVersion = Environment.OSVersion.ToString(),
68-
LastActive = DateTimeOffset.Now
69-
}).ConfigureAwait(false);
65+
_ = analyticsService.SendAsync(new Data()).ConfigureAwait(false);
7066

7167
IPlugin hi2Uc = this;
72-
using (var form = new PluginForm(sub, hi2Uc.Name, hi2Uc.Description))
68+
using var form = new PluginForm(sub, hi2Uc.Name, hi2Uc.Description);
69+
if (form.ShowDialog(parentForm) == DialogResult.OK)
7370
{
74-
if (form.ShowDialog(parentForm) == DialogResult.OK)
75-
{
76-
return form.Subtitle;
77-
}
71+
return form.Subtitle;
7872
}
7973

8074
return string.Empty;

source/HI2UC/PluginForm.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,26 @@ public PluginForm(Subtitle subtitle, string name, string description)
7474

7575
private readonly ReportService _reportService = new();
7676

77-
private async void LaunchReportWindow(object sender, EventArgs e)
77+
private void LaunchReportWindow(object sender, EventArgs e)
7878
{
79-
using (var reportForm = new ReportForm(_reportService))
80-
{
81-
if (reportForm.ShowDialog() == DialogResult.OK)
82-
{
83-
try
84-
{
85-
var report = new Report(reportForm.ReportMessage, _subtitle.ToText());
86-
if (await _reportService.ReportAsync(report).ConfigureAwait(false))
87-
{
88-
MessageBox.Show("Thank you for your report!");
89-
}
90-
}
91-
catch (Exception exception)
92-
{
93-
MessageBox.Show(exception.Message);
94-
}
95-
}
96-
}
97-
// Process.Start("https://github.com/SubtitleEdit/plugins/issues/new");
79+
// using var reportForm = new ReportForm(_reportService);
80+
// if (reportForm.ShowDialog() == DialogResult.OK)
81+
// {
82+
// try
83+
// {
84+
// var report = new Report(reportForm.ReportMessage, _subtitle.ToText());
85+
// if (await _reportService.ReportAsync(report).ConfigureAwait(false))
86+
// {
87+
// MessageBox.Show("Thank you for your report!");
88+
// }
89+
// }
90+
// catch (Exception exception)
91+
// {
92+
// MessageBox.Show(exception.Message);
93+
// }
94+
// }
95+
96+
Process.Start("https://github.com/SubtitleEdit/plugins/issues/new");
9897
}
9998

10099
private void UpdateUiFromConfigs(HiConfigs configs)
Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Net.Http;
3+
using System.Net.NetworkInformation;
4+
using System.Security.Cryptography;
35
using System.Text;
46
using System.Threading.Tasks;
57

@@ -15,15 +17,10 @@ namespace Nikse.SubtitleEdit.PluginLogic.Services;
1517
/// </remarks>
1618
public class AnalyticsService : IDisposable
1719
{
18-
private readonly HttpClient _httpClient;
19-
20-
public AnalyticsService()
20+
private readonly HttpClient _httpClient = new()
2121
{
22-
_httpClient = new HttpClient()
23-
{
24-
BaseAddress = new Uri("https://subtitleedit.ivandrofly.com/api/analytics")
25-
};
26-
}
22+
BaseAddress = new Uri("https://subtitleedit.ivandrofly.com")
23+
};
2724

2825
/// <summary>
2926
/// Sends the provided analytics data asynchronously to the specified API endpoint.
@@ -34,11 +31,9 @@ public async Task SendAsync(Data data)
3431
{
3532
try
3633
{
37-
using (var jsonContent = new StringContent(data.ToString(), Encoding.UTF8, "application/json"))
38-
{
39-
_ = await _httpClient.PostAsync("/api/analytics", jsonContent)
40-
.ConfigureAwait(false);
41-
}
34+
using var jsonContent = new StringContent(data.ToString(), Encoding.UTF8, "application/json");
35+
_ = await _httpClient.PostAsync("/api/analytics", jsonContent)
36+
.ConfigureAwait(false);
4237
}
4338
catch (Exception e)
4439
{
@@ -51,19 +46,46 @@ public async Task SendAsync(Data data)
5146

5247
public class Data
5348
{
49+
public string UserId { get; } = GenerateId();
50+
5451
/// <summary>
5552
/// Gets or sets the operating system version of the device or system sending analytics data.
5653
/// </summary>
57-
public string OsVersion { get; set; }
54+
public string OsVersion { get; } = Environment.OSVersion.VersionString;
5855

5956
/// <summary>
6057
/// Gets or sets the timestamp of the last recorded activity.
6158
/// </summary>
62-
public DateTimeOffset LastActive { get; set; }
59+
public DateTimeOffset LastActive { get; } = DateTimeOffset.Now;
6360

6461
public override string ToString()
6562
{
6663
// convert properties to json string
67-
return $"{{\"osVersion\": \"{JsonUtils.EscapeJsonString(OsVersion)}\", \"lastActive\": \"{LastActive}\"}}";
64+
return $"{{\"userId\": \"{UserId}\", \"osVersion\": \"{OsVersion}\", \"lastActive\": \"{LastActive}\"}}";
65+
}
66+
67+
private static string GenerateId()
68+
{
69+
try
70+
{
71+
var mac = string.Empty;
72+
foreach (var nic in NetworkInterface.GetAllNetworkInterfaces())
73+
{
74+
if (nic.OperationalStatus == OperationalStatus.Up &&
75+
nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
76+
{
77+
mac += nic.GetPhysicalAddress().ToString();
78+
}
79+
}
80+
81+
using var md5 = MD5.Create();
82+
var inputBytes = Encoding.ASCII.GetBytes(mac);
83+
var hashBytes = md5.ComputeHash(inputBytes);
84+
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
85+
}
86+
catch (Exception e)
87+
{
88+
return string.Empty;
89+
}
6890
}
6991
}

source/HI2UC/Services/ReportService.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,15 @@ private void ThrowIfNullOrEmpty(string value, string name)
8181
/// <returns>The hash of the given input string as a lowercase hexadecimal string.</returns>
8282
private string ComputeHash(string fileName)
8383
{
84-
using (var sha256 = SHA256.Create())
85-
{
86-
// Convert the fileName to a byte array
87-
var fileNameBytes = Encoding.UTF8.GetBytes(fileName);
84+
using var sha256 = SHA256.Create();
85+
// Convert the fileName to a byte array
86+
var fileNameBytes = Encoding.UTF8.GetBytes(fileName);
8887

89-
// Compute the hash
90-
var hashBytes = sha256.ComputeHash(fileNameBytes);
88+
// Compute the hash
89+
var hashBytes = sha256.ComputeHash(fileNameBytes);
9190

92-
// Convert the hash bytes to a hexadecimal string
93-
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
94-
}
91+
// Convert the hash bytes to a hexadecimal string
92+
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
9593
}
9694

9795
/// <summary>

0 commit comments

Comments
 (0)