Skip to content

Commit 7a2c496

Browse files
committed
Cleanup
Mainly on Language because that was a *mess* (Still is but not something that will hunt my dreams...)
1 parent 35fcfc3 commit 7a2c496

30 files changed

+251
-221
lines changed

src/MultiRPC/Assets/Language/en-gb.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,8 @@
257257
"FailedToGetOldProfiles": "We failed to get your profiles from V5!",
258258
"FailedToGetOldData": "We failed to get your configuration and profiles from V5!",
259259
"AcrylicEffect": "Acrylic effect",
260-
"ButtonWarn": "We see that you added buttons to your rich presence (nice choice btw ;P)\r\nHere's your notice that in discord's desktop client, your own buttons will not be clickable but others users will be able to just fine :D"
260+
"ButtonWarn": "We see that you added buttons to your rich presence (nice choice btw ;P)\r\nHere's your notice that in discord's desktop client, your own buttons will not be clickable but others users will be able to just fine :D",
261+
"UnknownValidationReason": "Unknown validation reason",
262+
"Profile": "Profile",
263+
"MultiRPCThemes": "MultiRPC Themes"
261264
}

src/MultiRPC/Commands/TrayCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
namespace MultiRPC.Commands;
88

9-
class TrayCommand : ICommand
9+
internal class TrayCommand : ICommand
1010
{
1111
public bool CanExecute(object? _) => true;
1212

1313
public void Execute(object? _)
1414
{
15-
var mainWin = ((App)Application.Current).DesktopLifetime!.MainWindow!;
15+
var mainWin = App.MainWindow;
1616
switch (mainWin.WindowState)
1717
{
1818
case WindowState.Normal:

src/MultiRPC/ControlValidation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public string Result
5050
return;
5151
}
5252

53-
var error = check?.ReasonWhy ?? "Unknown validation reason";
53+
var error = check?.ReasonWhy ?? Language.GetText(LanguageText.UnknownValidationReason);
5454
_logging.Error(error);
5555
throw new DataValidationException(error);
5656
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
22
using System.Globalization;
33
using System.Text.Json;
4-
using Avalonia.Media;
54
using System.Text.Json.Serialization;
5+
using Avalonia.Media;
66

7-
namespace MultiRPC.Theming.JsonConverter;
7+
namespace MultiRPC.Converters;
88

99
public class ColourJsonConverter : JsonConverter<Color>
1010
{
@@ -13,14 +13,14 @@ public override Color Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe
1313
var s = reader.GetString();
1414
if (string.IsNullOrWhiteSpace(s))
1515
{
16-
throw new Exception();
16+
throw new JsonException("We should of been given an string when reading the colour!");
1717
}
1818

1919
return Color.FromArgb(byte.Parse(s[1..3], NumberStyles.HexNumber), byte.Parse(s[3..5], NumberStyles.HexNumber), byte.Parse(s[5..7], NumberStyles.HexNumber), byte.Parse(s[7..9], NumberStyles.HexNumber));
2020
}
2121

2222
public override void Write(Utf8JsonWriter writer, Color value, JsonSerializerOptions options)
2323
{
24-
writer.WriteStringValue($"#{value.ToUint32():x8}");
24+
writer.WriteStringValue("#" + value.ToUint32().ToString(":x8"));
2525
}
2626
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
using System.Text.Json;
4+
using System.Text.Json.Serialization;
5+
using MultiRPC.Discord;
6+
7+
namespace MultiRPC.Converters;
8+
9+
public class StatusConverter : JsonConverter<DiscordStatus>
10+
{
11+
public override DiscordStatus Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
12+
{
13+
var status = reader.GetString();
14+
return status switch
15+
{
16+
"operational" => DiscordStatus.Operational,
17+
"degraded_performance" => DiscordStatus.Degraded,
18+
"partial_outage" => DiscordStatus.PartialOutage,
19+
"major_outage" => DiscordStatus.MajorOutage,
20+
_ => throw new InvalidDataContractException()
21+
};
22+
}
23+
24+
public override void Write(Utf8JsonWriter writer, DiscordStatus value, JsonSerializerOptions options)
25+
{
26+
throw new NotImplementedException();
27+
}
28+
}

src/MultiRPC/Theming/JsonConverter/VersionJsonConverter.cs renamed to src/MultiRPC/Converters/VersionJsonConverter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Text.Json.Serialization;
55
using SemVersion;
66

7-
namespace MultiRPC.Theming.JsonConverter;
7+
namespace MultiRPC.Converters;
88

99
public class VersionJsonConverter : JsonConverter<SemanticVersion>
1010
{
@@ -15,10 +15,10 @@ public class VersionJsonConverter : JsonConverter<SemanticVersion>
1515
var versionS = reader.GetString();
1616
if (string.IsNullOrWhiteSpace(versionS))
1717
{
18-
throw new Exception();
18+
throw new JsonException("We was given an empty string when reading the version!");
1919
}
2020

21-
//This allows us to use Version strings nicely
21+
//This allows us to use Version strings in a way that work for us
2222
var lastDot = versionS.LastIndexOf('.');
2323
if (versionS.Count(x => x == '.') == 3)
2424
{
@@ -29,12 +29,12 @@ public class VersionJsonConverter : JsonConverter<SemanticVersion>
2929
{
3030
return version;
3131
}
32-
throw new Exception();
32+
throw new JsonException("Wasn't able to make an SemanticVersion out of the string given! (" + versionS + ")");
3333
}
3434

3535
if (reader.TokenType != JsonTokenType.StartObject)
3636
{
37-
throw new Exception();
37+
throw new JsonException("We haven't been given an old styled version!");
3838
}
3939
reader.Read();
4040

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System.Net.Http;
2-
using System.Net.Http.Json;
1+
using System.Net.Http.Json;
32
using System.Threading.Tasks;
4-
using MultiRPC.Discord.Status;
53
using MultiRPC.Extensions;
64
using MultiRPC.UI;
75

@@ -19,22 +17,13 @@ public static class DiscordStatusChecker
1917
{
2018
public static async Task<DiscordStatus> GetStatus()
2119
{
22-
var response = await App.HttpClient.GetResponseMessage("https://discordstatus.com/api/v2/components.json");
20+
var response = await App.HttpClient.GetResponseMessageAsync("https://discordstatus.com/api/v2/components.json");
2321
if (response is null || !response.IsSuccessStatusCode)
2422
{
2523
return DiscordStatus.MajorOutage;
2624
}
2725

28-
var data = await response.Content.ReadFromJsonAsync(DataContext.Default.Data);
29-
var status = data?.Components[0].Status switch
30-
{
31-
"operational" => DiscordStatus.Operational,
32-
"degraded_performance" => DiscordStatus.Degraded,
33-
"partial_outage" => DiscordStatus.PartialOutage,
34-
"major_outage" => DiscordStatus.MajorOutage,
35-
_ => DiscordStatus.MajorOutage
36-
};
37-
38-
return status;
26+
var data = await response.Content.ReadFromJsonAsync(StatusContext.Default.Status);
27+
return data?.Components[0].Status ?? DiscordStatus.MajorOutage;
3928
}
4029
}

src/MultiRPC/Discord/IDChecker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static class IDChecker
1919
public static async Task<(bool Successful, string? ResultMessage)> Check(long id)
2020
{
2121
var responseMessage =
22-
await App.HttpClient.GetResponseMessage($"https://discordapp.com/api/v6/oauth2/applications/{id}/rpc");
22+
await App.HttpClient.GetResponseMessageAsync($"https://discordapp.com/api/v6/oauth2/applications/{id}/rpc");
2323

2424
if (responseMessage is null or { IsSuccessStatusCode: false })
2525
{

src/MultiRPC/Discord/ProfileAssetsManager.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Net.Http;
54
using System.Net.Http.Json;
65
using System.Text.Json;
76
using System.Text.Json.Serialization;
@@ -43,18 +42,9 @@ public static ProfileAssetsManager GetOrAddManager(long id)
4342

4443
public DiscordAsset[]? GetAssets()
4544
{
46-
HttpResponseMessage response;
47-
try
48-
{
49-
response = App.HttpClient.Send(new HttpRequestMessage(HttpMethod.Get, _baseUrl));
50-
if (response is not { IsSuccessStatusCode: true })
51-
{
52-
return null;
53-
}
54-
}
55-
catch (Exception e)
45+
var response = App.HttpClient.GetResponseMessage(_baseUrl);
46+
if (response is not { IsSuccessStatusCode: true })
5647
{
57-
//TODO: Log
5848
return null;
5949
}
6050

@@ -69,7 +59,7 @@ public static ProfileAssetsManager GetOrAddManager(long id)
6959
return _lazyAssets.Value;
7060
}
7161

72-
var response = await App.HttpClient.GetResponseMessage(_baseUrl);
62+
var response = await App.HttpClient.GetResponseMessageAsync(_baseUrl);
7363
if (response is not { IsSuccessStatusCode: true })
7464
{
7565
return null;

src/MultiRPC/Discord/Status/Component.cs renamed to src/MultiRPC/Discord/Status.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// ReSharper disable UnusedAutoPropertyAccessor.Global
22
#pragma warning disable CS8618
3-
43
using System.Text.Json.Serialization;
5-
namespace MultiRPC.Discord.Status;
4+
using MultiRPC.Converters;
5+
6+
namespace MultiRPC.Discord;
67

78
/*public class Page
89
{
@@ -30,8 +31,8 @@ public class Component
3031
[JsonPropertyName("name")]
3132
public string Name { get; init; }*/
3233

33-
[JsonPropertyName("status")]
34-
public string Status { get; set; }
34+
[JsonPropertyName("status"), JsonConverter(typeof(StatusConverter))]
35+
public DiscordStatus Status { get; set; }
3536

3637
/*[JsonPropertyName("created_at")]
3738
public DateTime CreatedAt { get; init; }
@@ -67,7 +68,7 @@ public class Component
6768
public string[] Components { get; init; }*/
6869
}
6970

70-
public class Data
71+
public class Status
7172
{
7273
//[JsonPropertyName("page")]
7374
//public Page Page { get; init; }
@@ -76,5 +77,5 @@ public class Data
7677
public Component[] Components { get; set; }
7778
}
7879

79-
[JsonSerializable(typeof(Data))]
80-
public partial class DataContext : JsonSerializerContext { }
80+
[JsonSerializable(typeof(Status))]
81+
public partial class StatusContext : JsonSerializerContext { }

0 commit comments

Comments
 (0)