Skip to content

Commit

Permalink
Merge pull request #838 from b-editor/refactoring
Browse files Browse the repository at this point in the history
リファクタリング
  • Loading branch information
yuto-trd authored Dec 31, 2023
2 parents 69c8e72 + a11a4eb commit 1c0cf21
Show file tree
Hide file tree
Showing 343 changed files with 1,681 additions and 3,169 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ csharp_indent_switch_labels = true
csharp_indent_labels = one_less_than_current

# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
csharp_preferred_modifier_order = public,private,protected,internal,file,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async:suggestion

# avoid this. unless absolutely necessary
dotnet_style_qualification_for_field = false:suggestion
Expand Down
45 changes: 16 additions & 29 deletions build/generators/ResourcesGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ List<IResourceItem> GetOrCreate(string key, Dictionary<string, List<IResourceIte
}
else
{
value = new List<IResourceItem>();
value = [];
dict[key] = value;
return value;
}
Expand Down Expand Up @@ -128,12 +128,13 @@ void AddChild(Dictionary<string, HierarchizedList> hierarchizedLists, string key
match2.Key = $"Index{match2.Key}";
}

if (!current.Children.ContainsKey(curKey))
if (!current.Children.TryGetValue(curKey, out HierarchizedList? v))
{
current.Children[curKey] = new HierarchizedList();
v = new HierarchizedList();
current.Children[curKey] = v;
}

current = current.Children[curKey];
current = v;

if (i == splitted.Length - 1)
{
Expand Down Expand Up @@ -186,9 +187,9 @@ namespace ResourcesGenerator
{
public class HierarchizedList
{
public List<IResourceItem> Items { get; set; } = new();
public List<IResourceItem> Items { get; set; } = [];

public Dictionary<string, HierarchizedList> Children { get; } = new();
public Dictionary<string, HierarchizedList> Children { get; } = [];
}

public interface IResourceItem
Expand All @@ -204,20 +205,13 @@ public interface IResourceItem
void GenerateGetObservableCode(Span<char> indentStr, StringBuilder sb, string rootNamespace, Dictionary<string, string> redirects);
}

public class StaticResource : IResourceItem
public class StaticResource(string rawKey, string key, string target) : IResourceItem
{
public StaticResource(string rawKey, string key, string target)
{
RawKey = rawKey;
Key = key;
Target = target;
}

public string RawKey { get; set; }
public string RawKey { get; set; } = rawKey;

public string Key { get; set; }
public string Key { get; set; } = key;

public string Target { get; set; }
public string Target { get; set; } = target;

public void GenerateGetObservableCode(Span<char> indentStr, StringBuilder sb, string rootNamespace, Dictionary<string, string> redirects)
{
Expand Down Expand Up @@ -249,26 +243,19 @@ public string GetPath()
}
}

public class StringResource : IResourceItem
public class StringResource(string rawKey, string key, string value) : IResourceItem
{
public StringResource(string rawKey, string key, string value)
{
RawKey = rawKey;
Key = key;
Value = value;
}

public string RawKey { get; set; }
public string RawKey { get; set; } = rawKey;

public string Key { get; set; }
public string Key { get; set; } = key;

public string Value { get; set; }
public string Value { get; set; } = value;

public void GenerateGetObservableCode(Span<char> indentStr, StringBuilder sb, string rootNamespace, Dictionary<string, string> redirects)
{
sb.AppendLine($"{indentStr}private static IObservable<string>? _{Key}Observable;");

if (redirects.TryGetValue(RawKey, out var rawKey1))
if (redirects.TryGetValue(RawKey, out string? rawKey1))
{
sb.AppendLine($"{indentStr}public static IObservable<string> {Key}Observable => _{Key}Observable ??= \"{RawKey}\".GetStringObservable(global::{rootNamespace}.Resources.{rawKey1});");
}
Expand Down
14 changes: 7 additions & 7 deletions nukebuild/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ partial class Build : NukeBuild
}
AbsolutePath mainProj = SourceDirectory / "Beutl" / "Beutl.csproj";
var mainOutput = OutputDirectory / "Beutl";
AbsolutePath mainOutput = OutputDirectory / "Beutl";
DotNetPublish(s => s
.EnableNoRestore()
Expand All @@ -87,12 +87,12 @@ partial class Build : NukeBuild
.SetOutput(mainOutput)
.SetProperty("NukePublish", true));
string[] subProjects = new string[]
{
string[] subProjects =
[
"Beutl.ExceptionHandler",
"Beutl.PackageTools",
"Beutl.WaitingDialog",
};
];
foreach (string item in subProjects)
{
AbsolutePath output = OutputDirectory / item;
Expand All @@ -108,14 +108,14 @@ partial class Build : NukeBuild
.ForEach(t => CopyFile(t.Source, t.Target));
}
string[] asmsToCopy = new string[]
{
string[] asmsToCopy =
[
"FluentTextTable",
"Kokuban",
"Kurukuru",
"Sharprompt",
"DeviceId",
};
];
foreach (string asm in asmsToCopy)
{
foreach (string item in subProjects)
Expand Down
2 changes: 1 addition & 1 deletion src/Beutl.Api/BeutlApiApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class BeutlApiApplication
private const string BaseUrl = "https://beutl.beditor.net";
private readonly HttpClient _httpClient;
private readonly ReactivePropertySlim<AuthorizedUser?> _authorizedUser = new();
private readonly Dictionary<Type, Lazy<object>> _services = new();
private readonly Dictionary<Type, Lazy<object>> _services = [];

public BeutlApiApplication(HttpClient httpClient)
{
Expand Down
11 changes: 2 additions & 9 deletions src/Beutl.Api/MyAsyncLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,11 @@ public Task<IDisposable> LockAsync(CancellationToken cancellationToken = default
}
}

private sealed class Releaser : IDisposable
private sealed class Releaser(MyAsyncLock toRelease) : IDisposable
{
private readonly MyAsyncLock _mutex;

public Releaser(MyAsyncLock toRelease)
{
_mutex = toRelease;
}

public void Dispose()
{
_mutex._semaphore.Release();
toRelease._semaphore.Release();
}
}
}
Expand Down
36 changes: 12 additions & 24 deletions src/Beutl.Api/Objects/AuthorizedUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,44 @@

namespace Beutl.Api.Objects;

public class AuthorizedUser
public class AuthorizedUser(Profile profile, AuthResponse response, BeutlApiApplication clients, HttpClient httpClient)
{
private readonly BeutlApiApplication _clients;
private readonly HttpClient _httpClient;
private AuthResponse _response;
public Profile Profile { get; } = profile;

public AuthorizedUser(Profile profile, AuthResponse response, BeutlApiApplication clients, HttpClient httpClient)
{
Profile = profile;
_response = response;
_clients = clients;
_httpClient = httpClient;
}

public Profile Profile { get; }

public string Token => _response.Token;
public string Token => response.Token;

public string RefreshToken => _response.Refresh_token;
public string RefreshToken => response.Refresh_token;

public DateTimeOffset Expiration => _response.Expiration;
public DateTimeOffset Expiration => response.Expiration;

public bool IsExpired => Expiration < DateTimeOffset.UtcNow;

public MyAsyncLock Lock => _clients.Lock;
public MyAsyncLock Lock => clients.Lock;

public async ValueTask RefreshAsync(bool force = false)
{
using Activity? activity = _clients.ActivitySource.StartActivity("AuthorizedUser.Refresh", ActivityKind.Client);
using Activity? activity = clients.ActivitySource.StartActivity("AuthorizedUser.Refresh", ActivityKind.Client);

activity?.SetTag("force", force);
activity?.SetTag("is_expired", IsExpired);

if (force || IsExpired)
{
_response = await _clients.Account.RefreshAsync(new RefeshTokenRequest(RefreshToken, Token))
response = await clients.Account.RefreshAsync(new RefeshTokenRequest(RefreshToken, Token))
.ConfigureAwait(false);
activity?.AddEvent(new("Refreshed"));
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);

if (_clients.AuthorizedUser.Value == this)
if (clients.AuthorizedUser.Value == this)
{
_clients.SaveUser();
clients.SaveUser();
activity?.AddEvent(new("Saved"));
}
}
}

public async Task<StorageUsageResponse> StorageUsageAsync()
{
return await _clients.Account.StorageUsageAsync();
return await clients.Account.StorageUsageAsync();
}
}
2 changes: 1 addition & 1 deletion src/Beutl.Api/Services/AcceptedLicenseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Beutl.Api.Services;

public class AcceptedLicenseManager : IBeutlApiResource
{
private readonly Dictionary<PackageIdentity, LicenseMetadata> _accepted = new();
private readonly Dictionary<PackageIdentity, LicenseMetadata> _accepted = [];
private const string FileName = "accepted-licenses.json";

public AcceptedLicenseManager()
Expand Down
49 changes: 21 additions & 28 deletions src/Beutl.Api/Services/DiscoverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,35 @@

namespace Beutl.Api.Services;

public class DiscoverService : IBeutlApiResource
public class DiscoverService(BeutlApiApplication clients) : IBeutlApiResource
{
private readonly BeutlApiApplication _clients;

public DiscoverService(BeutlApiApplication clients)
{
_clients = clients;
}

public MyAsyncLock Lock => _clients.Lock;
public MyAsyncLock Lock => clients.Lock;

public async Task<Package> GetPackage(string name)
{
using Activity? activity = _clients.ActivitySource.StartActivity("DiscoverService.GetPackage", ActivityKind.Client);
using Activity? activity = clients.ActivitySource.StartActivity("DiscoverService.GetPackage", ActivityKind.Client);

PackageResponse package = await _clients.Packages.GetPackageAsync(name).ConfigureAwait(false);
PackageResponse package = await clients.Packages.GetPackageAsync(name).ConfigureAwait(false);
Profile owner = await GetProfile(package.Owner.Name).ConfigureAwait(false);

return new Package(owner, package, _clients);
return new Package(owner, package, clients);
}

public async Task<Profile> GetProfile(string name)
{
using Activity? activity = _clients.ActivitySource.StartActivity("DiscoverService.GetProfile", ActivityKind.Client);
using Activity? activity = clients.ActivitySource.StartActivity("DiscoverService.GetProfile", ActivityKind.Client);

ProfileResponse response = await _clients.Users.GetUserAsync(name).ConfigureAwait(false);
return new Profile(response, _clients);
ProfileResponse response = await clients.Users.GetUserAsync(name).ConfigureAwait(false);
return new Profile(response, clients);
}

public async Task<Package[]> GetDailyRanking(int start = 0, int count = 30)
{
using Activity? activity = _clients.ActivitySource.StartActivity("DiscoverService.GetDailyRanking", ActivityKind.Client);
using Activity? activity = clients.ActivitySource.StartActivity("DiscoverService.GetDailyRanking", ActivityKind.Client);
activity?.SetTag("start", start);
activity?.SetTag("count", count);

return await (await _clients.Discover.GetDailyAsync(start, count).ConfigureAwait(false))
return await (await clients.Discover.GetDailyAsync(start, count).ConfigureAwait(false))
.ToAsyncEnumerable()
.SelectAwait(async x => await GetPackage(x.Name).ConfigureAwait(false))
.ToArrayAsync()
Expand All @@ -48,11 +41,11 @@ public async Task<Package[]> GetDailyRanking(int start = 0, int count = 30)

public async Task<Package[]> GetWeeklyRanking(int start = 0, int count = 30)
{
using Activity? activity = _clients.ActivitySource.StartActivity("DiscoverService.GetWeeklyRanking", ActivityKind.Client);
using Activity? activity = clients.ActivitySource.StartActivity("DiscoverService.GetWeeklyRanking", ActivityKind.Client);
activity?.SetTag("start", start);
activity?.SetTag("count", count);

return await (await _clients.Discover.GetWeeklyAsync(start, count).ConfigureAwait(false))
return await (await clients.Discover.GetWeeklyAsync(start, count).ConfigureAwait(false))
.ToAsyncEnumerable()
.SelectAwait(async x => await GetPackage(x.Name).ConfigureAwait(false))
.ToArrayAsync()
Expand All @@ -61,11 +54,11 @@ public async Task<Package[]> GetWeeklyRanking(int start = 0, int count = 30)

public async Task<Package[]> GetOverallRanking(int start = 0, int count = 30)
{
using Activity? activity = _clients.ActivitySource.StartActivity("DiscoverService.GetOverallRanking", ActivityKind.Client);
using Activity? activity = clients.ActivitySource.StartActivity("DiscoverService.GetOverallRanking", ActivityKind.Client);
activity?.SetTag("start", start);
activity?.SetTag("count", count);

return await (await _clients.Discover.GetOverallAsync(start, count).ConfigureAwait(false))
return await (await clients.Discover.GetOverallAsync(start, count).ConfigureAwait(false))
.ToAsyncEnumerable()
.SelectAwait(async x => await GetPackage(x.Name).ConfigureAwait(false))
.ToArrayAsync()
Expand All @@ -74,11 +67,11 @@ public async Task<Package[]> GetOverallRanking(int start = 0, int count = 30)

public async Task<Package[]> GetRecentlyRanking(int start = 0, int count = 30)
{
using Activity? activity = _clients.ActivitySource.StartActivity("DiscoverService.GetRecentlyRanking", ActivityKind.Client);
using Activity? activity = clients.ActivitySource.StartActivity("DiscoverService.GetRecentlyRanking", ActivityKind.Client);
activity?.SetTag("start", start);
activity?.SetTag("count", count);

return await (await _clients.Discover.GetRecentlyAsync(start, count).ConfigureAwait(false))
return await (await clients.Discover.GetRecentlyAsync(start, count).ConfigureAwait(false))
.ToAsyncEnumerable()
.SelectAwait(async x => await GetPackage(x.Name).ConfigureAwait(false))
.ToArrayAsync()
Expand All @@ -87,11 +80,11 @@ public async Task<Package[]> GetRecentlyRanking(int start = 0, int count = 30)

public async Task<Package[]> SearchPackages(string query, int start = 0, int count = 30)
{
using Activity? activity = _clients.ActivitySource.StartActivity("DiscoverService.SearchPackages", ActivityKind.Client);
using Activity? activity = clients.ActivitySource.StartActivity("DiscoverService.SearchPackages", ActivityKind.Client);
activity?.SetTag("start", start);
activity?.SetTag("count", count);

return await (await _clients.Discover.SearchPackagesAsync(query, start, count).ConfigureAwait(false))
return await (await clients.Discover.SearchPackagesAsync(query, start, count).ConfigureAwait(false))
.ToAsyncEnumerable()
.SelectAwait(async x => await GetPackage(x.Name).ConfigureAwait(false))
.ToArrayAsync()
Expand All @@ -100,11 +93,11 @@ public async Task<Package[]> SearchPackages(string query, int start = 0, int cou

public async Task<Profile[]> SearchUsers(string query, int start = 0, int count = 30)
{
using Activity? activity = _clients.ActivitySource.StartActivity("DiscoverService.SearchUsers", ActivityKind.Client);
using Activity? activity = clients.ActivitySource.StartActivity("DiscoverService.SearchUsers", ActivityKind.Client);
activity?.SetTag("start", start);
activity?.SetTag("count", count);

return await (await _clients.Discover.SearchUsersAsync(query, start, count).ConfigureAwait(false))
return await (await clients.Discover.SearchUsersAsync(query, start, count).ConfigureAwait(false))
.ToAsyncEnumerable()
.SelectAwait(async x => await GetProfile(x.Name).ConfigureAwait(false))
.ToArrayAsync()
Expand All @@ -113,7 +106,7 @@ public async Task<Profile[]> SearchUsers(string query, int start = 0, int count

public async Task<Package[]> Search(string query, int start = 0, int count = 30)
{
return await (await _clients.Discover.SearchAsync(query, start, count).ConfigureAwait(false))
return await (await clients.Discover.SearchAsync(query, start, count).ConfigureAwait(false))
.ToAsyncEnumerable()
.SelectAwait(async x => await GetPackage(x.Name).ConfigureAwait(false))
.ToArrayAsync()
Expand Down
Loading

0 comments on commit 1c0cf21

Please sign in to comment.