Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

refactor: internal refactors #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions WSLDiskShrinker/Common/BooleanToInverseVisibilityConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ namespace WSLDiskShrinker.Common;

class BooleanToInverseVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool v = (bool) value;
return v ? Visibility.Collapsed : Visibility.Visible;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Collapsed : Visibility.Visible;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed; // Not used.
}
Comment on lines +13 to +16
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation of ConvertBack is not correct. What ConvertBack should do is converting from Visibility to bool, which is useless for our case. (FYI: It is useful in two-way bindings)

}

18 changes: 8 additions & 10 deletions WSLDiskShrinker/Common/BooleanToVisibilityConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ namespace WSLDiskShrinker.Common;

class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool v = (bool)value;
return v ? Visibility.Visible : Visibility.Collapsed;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Collapsed : Visibility.Visible; // Not used.
}
Comment on lines +13 to +16
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same issue here. Wrong implementation here. And definitely no need to implement it.

}

26 changes: 18 additions & 8 deletions WSLDiskShrinker/Common/CommandFailedException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
namespace WSLDiskShrinker.Common;
using System.Runtime.Serialization;

public class CommandFailedException : Exception
namespace WSLDiskShrinker.Common;

[Serializable]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the point of making it Serializable. Can you explain it?

public class CommandFailedException : Exception /* ISerializable */
{
public Process FailedProcess { get; set; }
public CommandFailedException(Process p)
{
this.FailedProcess = p;
}
}
public Process? FailedProcess { get; }
public CommandFailedException(Process p)
{
this.FailedProcess = p;
}

// Implement exception constructors (not used).
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why adding implementations that are not used 😂?

public CommandFailedException() : base() { }
public CommandFailedException(string? message) : base(message) { }
public CommandFailedException(string? message, Exception? innerException) : base(message, innerException) { }

// ISerializable should be implemented (not used).
protected CommandFailedException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
18 changes: 8 additions & 10 deletions WSLDiskShrinker/Common/NotConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ namespace WSLDiskShrinker.Common;

class NotConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value;
}

return !(bool)value;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value; // Not used.
}
}

36 changes: 17 additions & 19 deletions WSLDiskShrinker/Common/ProcessExtension.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
using System.Threading;
#if !NET5_0_OR_GREATER

using System.Threading;

namespace WSLDiskShrinker.Common;

// https://stackoverflow.com/questions/470256/process-waitforexit-asynchronously
static class ProcessExtension
{
/// <summary>
/// Waits asynchronously for the process to exit.
/// </summary>
/// <param name="process">The process to wait for cancellation.</param>
/// <param name="cancellationToken">A cancellation token. If invoked, the task will return
/// immediately as canceled.</param>
/// <returns>A Task representing waiting for the process to end.</returns>
public static Task CustomWaitForExitAsync(this Process process,
CancellationToken cancellationToken = default)
{
if (process.HasExited) return Task.CompletedTask;
/// <summary>Waits asynchronously for the process to exit.</summary>
/// <param name="process">The process to wait for cancellation.</param>
/// <param name="cancellationToken">A cancellation token. If invoked, the task will return immediately as canceled.</param>
/// <returns>A Task representing waiting for the process to end.</returns>
public static Task CustomWaitForExitAsync(this Process process, CancellationToken cancellationToken = default)
{
if (process.HasExited) return Task.CompletedTask;

var tcs = new TaskCompletionSource<object>();
process.EnableRaisingEvents = true;
process.Exited += (sender, args) => tcs.TrySetResult(null);
if (cancellationToken != default)
cancellationToken.Register(() => tcs.SetCanceled());
var tcs = new TaskCompletionSource<object>();
process.EnableRaisingEvents = true;
process.Exited += (sender, args) => tcs.TrySetResult(process.ExitCode);
if (cancellationToken != default) cancellationToken.Register(() => tcs.SetCanceled());

return process.HasExited ? Task.CompletedTask : tcs.Task;
}
return process.HasExited ? Task.CompletedTask : tcs.Task;
}
}

#endif
Loading