Skip to content

Commit

Permalink
Use latest C# features
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry08 committed Jul 17, 2024
1 parent 21a39ca commit 787169e
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 129 deletions.
12 changes: 3 additions & 9 deletions Yosu.Core/Utils/DelegateEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@

namespace Yosu.Core.Utils;

public class DelegateEqualityComparer<T> : IEqualityComparer<T>
public class DelegateEqualityComparer<T>(Func<T, T, bool> equals, Func<T, int> getHashCode) : IEqualityComparer<T>
{
private readonly Func<T, T, bool> _equals;
private readonly Func<T, int> _getHashCode;

public DelegateEqualityComparer(Func<T, T, bool> equals, Func<T, int> getHashCode)
{
_equals = equals;
_getHashCode = getHashCode;
}
private readonly Func<T, T, bool> _equals = equals;
private readonly Func<T, int> _getHashCode = getHashCode;

public bool Equals(T? x, T? y)
{
Expand Down
6 changes: 2 additions & 4 deletions Yosu.Core/Utils/ThrottleLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

namespace Yosu.Core.Utils;

public class ThrottleLock : IDisposable
public class ThrottleLock(TimeSpan interval) : IDisposable
{
private readonly SemaphoreSlim _semaphore = new(1, 1);
private readonly TimeSpan _interval;
private readonly TimeSpan _interval = interval;
private DateTimeOffset _lastRequestInstant = DateTimeOffset.MinValue;

public ThrottleLock(TimeSpan interval) => _interval = interval;

public async Task WaitAsync(CancellationToken cancellationToken = default)
{
await _semaphore.WaitAsync(cancellationToken);
Expand Down
6 changes: 2 additions & 4 deletions Yosu.Soundcloud.Core/Tagging/MediaFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Yosu.Soundcloud.Core.Tagging;

internal partial class MediaFile : IDisposable
internal partial class MediaFile(TagFile file) : IDisposable
{
private readonly TagFile _file;

public MediaFile(TagFile file) => _file = file;
private readonly TagFile _file = file;

public void SetThumbnail(byte[] thumbnailData) =>
_file.Tag.Pictures = [new Picture(thumbnailData)];
Expand Down
6 changes: 2 additions & 4 deletions Yosu.Spotify.Core/Tagging/MediaFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@

namespace Yosu.Spotify.Core.Tagging;

internal partial class MediaFile : IDisposable
internal partial class MediaFile(TagFile file) : IDisposable
{
private readonly TagFile _file;

public MediaFile(TagFile file) => _file = file;
private readonly TagFile _file = file;

/*public void SetThumbnail(byte[] thumbnailData)
{
Expand Down
12 changes: 5 additions & 7 deletions Yosu.Youtube.Converter/ConversionFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ namespace Yosu.Youtube.Converter;
/// <summary>
/// Encapsulates conversion media format.
/// </summary>
/// <remarks>
/// Initializes an instance of <see cref="ConversionFormat" />.
/// </remarks>
[Obsolete("Use YoutubeExplode.Videos.Streams.Container instead"), ExcludeFromCodeCoverage]
public readonly struct ConversionFormat
public readonly struct ConversionFormat(string name)
{
/// <summary>
/// Format name.
/// </summary>
public string Name { get; }
public string Name { get; } = name;

/// <summary>
/// Whether this format is a known audio-only format.
/// </summary>
public bool IsAudioOnly => new Container(Name).IsAudioOnly();

/// <summary>
/// Initializes an instance of <see cref="ConversionFormat" />.
/// </summary>
public ConversionFormat(string name) => Name = name;

/// <inheritdoc />
public override string ToString() => Name;
}
34 changes: 13 additions & 21 deletions Yosu.Youtube.Converter/ConversionRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,30 @@ namespace Yosu.Youtube.Converter;
/// <summary>
/// Conversion options.
/// </summary>
public class ConversionRequest
/// <remarks>
/// Initializes an instance of <see cref="ConversionRequest" />.
/// </remarks>
public class ConversionRequest(
string ffmpegCliFilePath,
string outputFilePath,
Container container,
ConversionPreset preset
)
{
/// <summary>
/// Path to FFmpeg CLI.
/// </summary>
public string FFmpegCliFilePath { get; }
public string FFmpegCliFilePath { get; } = ffmpegCliFilePath;

/// <summary>
/// Output file path.
/// </summary>
public string OutputFilePath { get; }
public string OutputFilePath { get; } = outputFilePath;

/// <summary>
/// Output container.
/// </summary>
public Container Container { get; }
public Container Container { get; } = container;

/// <summary>
/// Output format.
Expand All @@ -33,23 +41,7 @@ public class ConversionRequest
/// <summary>
/// Encoder preset.
/// </summary>
public ConversionPreset Preset { get; }

/// <summary>
/// Initializes an instance of <see cref="ConversionRequest" />.
/// </summary>
public ConversionRequest(
string ffmpegCliFilePath,
string outputFilePath,
Container container,
ConversionPreset preset
)
{
FFmpegCliFilePath = ffmpegCliFilePath;
OutputFilePath = outputFilePath;
Container = container;
Preset = preset;
}
public ConversionPreset Preset { get; } = preset;

/// <summary>
/// Initializes an instance of <see cref="ConversionRequest" />.
Expand Down
12 changes: 5 additions & 7 deletions Yosu.Youtube.Converter/ConversionRequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ namespace Yosu.Youtube.Converter;
/// <summary>
/// Builder for <see cref="ConversionRequest" />.
/// </summary>
public partial class ConversionRequestBuilder
/// <remarks>
/// Initializes an instance of <see cref="ConversionRequestBuilder" />.
/// </remarks>
public partial class ConversionRequestBuilder(string outputFilePath)
{
private readonly string _outputFilePath;
private readonly string _outputFilePath = outputFilePath;

private string? _ffmpegCliFilePath;
private Container? _container;
private ConversionPreset _preset;

/// <summary>
/// Initializes an instance of <see cref="ConversionRequestBuilder" />.
/// </summary>
public ConversionRequestBuilder(string outputFilePath) => _outputFilePath = outputFilePath;

private Container GetDefaultContainer() =>
new(Path.GetExtension(_outputFilePath).TrimStart('.').NullIfWhiteSpace() ?? "mp4");

Expand Down
39 changes: 10 additions & 29 deletions Yosu.Youtube.Converter/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,11 @@

namespace Yosu.Youtube.Converter;

internal partial class Converter
internal partial class Converter(VideoClient videoClient, FFmpeg ffmpeg, ConversionPreset preset)
{
private readonly VideoClient _videoClient;
private readonly FFmpeg _ffmpeg;
private readonly ConversionPreset _preset;

public Converter(VideoClient videoClient, FFmpeg ffmpeg, ConversionPreset preset)
{
_videoClient = videoClient;
_ffmpeg = ffmpeg;
_preset = preset;
}
private readonly VideoClient _videoClient = videoClient;
private readonly FFmpeg _ffmpeg = ffmpeg;
private readonly ConversionPreset _preset = preset;

private async ValueTask ProcessAsync(

Check warning on line 22 in Yosu.Youtube.Converter/Converter.cs

View workflow job for this annotation

GitHub Actions / Android Build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
string filePath,
Expand Down Expand Up @@ -233,17 +226,11 @@ await ProcessAsync(

internal partial class Converter
{
private class StreamInput : IDisposable
private class StreamInput(IStreamInfo info, string filePath) : IDisposable
{
public IStreamInfo Info { get; }

public string FilePath { get; }
public IStreamInfo Info { get; } = info;

public StreamInput(IStreamInfo info, string filePath)
{
Info = info;
FilePath = filePath;
}
public string FilePath { get; } = filePath;

public void Dispose()
{
Expand All @@ -258,17 +245,11 @@ public void Dispose()
}
}

private class SubtitleInput : IDisposable
private class SubtitleInput(ClosedCaptionTrackInfo info, string filePath) : IDisposable
{
public ClosedCaptionTrackInfo Info { get; }

public string FilePath { get; }
public ClosedCaptionTrackInfo Info { get; } = info;

public SubtitleInput(ClosedCaptionTrackInfo info, string filePath)
{
Info = info;
FilePath = filePath;
}
public string FilePath { get; } = filePath;

public void Dispose()
{
Expand Down
6 changes: 2 additions & 4 deletions Yosu.Youtube.Converter/FFmpeg/FFmpeg.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

namespace Yosu.Youtube.Converter;

internal class FFmpeg
internal class FFmpeg(string filePath)
{
private readonly string _filePath;

public FFmpeg(string filePath) => _filePath = filePath;
private readonly string _filePath = filePath;

public void Execute(
string arguments,
Expand Down
6 changes: 2 additions & 4 deletions Yosu.Youtube.Converter/FFmpeg/FFmpeg.macios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

namespace Yosu.Youtube.Converter;

internal class FFmpeg
internal class FFmpeg(string filePath)
{
private readonly string _filePath;

public FFmpeg(string filePath) => _filePath = filePath;
private readonly string _filePath = filePath;

public void Execute(
string arguments,
Expand Down
12 changes: 4 additions & 8 deletions Yosu.Youtube.Converter/FFmpeg/FFmpeg.net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ namespace Yosu.Youtube.Converter;
// Ideally this should use named pipes and stream through stdout.
// However, named pipes aren't well supported on non-Windows OS and
// stdout streaming only works with some specific formats.
internal partial class FFmpeg
internal partial class FFmpeg(string filePath)
{
private readonly string _filePath;

public FFmpeg(string filePath) => _filePath = filePath;
private readonly string _filePath = filePath;

public async ValueTask ExecuteAsync(
string arguments,
Expand Down Expand Up @@ -58,16 +56,14 @@ public async ValueTask ExecuteAsync(

internal partial class FFmpeg
{
private class FFmpegProgressRouter : PipeTarget
private class FFmpegProgressRouter(IProgress<double> output) : PipeTarget
{
private readonly StringBuilder _buffer = new();
private readonly IProgress<double> _output;
private readonly IProgress<double> _output = output;

private TimeSpan? _totalDuration;
private TimeSpan? _lastOffset;

public FFmpegProgressRouter(IProgress<double> output) => _output = output;

private TimeSpan? TryParseTotalDuration(string data) =>
data.Pipe(s => Regex.Match(s, @"Duration:\s(\d\d:\d\d:\d\d.\d\d)").Groups[1].Value)
.NullIfWhiteSpace()
Expand Down
6 changes: 2 additions & 4 deletions Yosu.Youtube.Converter/FFmpeg/FFmpeg.windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

namespace Yosu.Youtube.Converter;

internal class FFmpeg
internal class FFmpeg(string filePath)
{
private readonly string _filePath;

public FFmpeg(string filePath) => _filePath = filePath;
private readonly string _filePath = filePath;

public async ValueTask ExecuteAsync(
string arguments,
Expand Down
6 changes: 2 additions & 4 deletions Yosu.Youtube.Converter/Utils/ProgressMuxer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@

namespace Yosu.Youtube.Converter.Utils;

internal class ProgressMuxer
internal class ProgressMuxer(IProgress<double> target)
{
private readonly IProgress<double> _target;
private readonly IProgress<double> _target = target;

private readonly object _lock = new();
private readonly Dictionary<int, double> _splitWeights = [];
private readonly Dictionary<int, double> _splitValues = [];

public ProgressMuxer(IProgress<double> target) => _target = target;

public IProgress<double> CreateInput(double weight = 1)
{
lock (_lock)
Expand Down
6 changes: 2 additions & 4 deletions Yosu.Youtube.Core/Tagging/MediaFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@

namespace Yosu.Youtube.Core.Tagging;

internal partial class MediaFile : IDisposable
internal partial class MediaFile(TagFile file) : IDisposable
{
private readonly TagFile _file;

public MediaFile(TagFile file) => _file = file;
private readonly TagFile _file = file;

/*public void SetThumbnail(byte[] thumbnailData)
{
Expand Down
5 changes: 1 addition & 4 deletions Yosu/Platforms/Android/MainApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
namespace Yosu;

[Application(UsesCleartextTraffic = true)]
public class MainApplication : MauiApplication
public class MainApplication(IntPtr handle, JniHandleOwnership ownership) : MauiApplication(handle, ownership)
{
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
: base(handle, ownership) { }

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
6 changes: 2 additions & 4 deletions Yosu/Utils/ResizableSemaphore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ public void Dispose()

internal partial class ResizableSemaphore
{
private class AcquiredAccess : IDisposable
private class AcquiredAccess(ResizableSemaphore semaphore) : IDisposable
{
private readonly ResizableSemaphore _semaphore;

public AcquiredAccess(ResizableSemaphore semaphore) => _semaphore = semaphore;
private readonly ResizableSemaphore _semaphore = semaphore;

public void Dispose() => _semaphore.Release();
}
Expand Down
10 changes: 2 additions & 8 deletions Yosu/ViewModels/Framework/ListGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

namespace Yosu.ViewModels.Framework;

public class ListGroup<T> : List<T>
public class ListGroup<T>(string name, List<T> items) : List<T>(items)
{
public string Name { get; }

public ListGroup(string name, List<T> items)
: base(items)
{
Name = name;
}
public string Name { get; } = name;
}

0 comments on commit 787169e

Please sign in to comment.