Skip to content

Commit

Permalink
GUI - finished implementation of mass conversion with MassConvertProg…
Browse files Browse the repository at this point in the history
…ressPage.

Core - reworked IXUIHelperProgressable to be more lightweight, with work count being implemented into mass conversion directly.
  • Loading branch information
SGCSam committed Apr 9, 2024
1 parent b6c44e7 commit 71ef27d
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 38 deletions.
6 changes: 1 addition & 5 deletions XUIHelper.Core/API/IXUIHelperProgressable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ namespace XUIHelper.Core
{
public interface IXUIHelperProgressable
{
int TotalWorkCount { get; set; }
int SuccessfulWorkCount { get; set; }
int FailedWorkCount { get; set; }
int CompletedWorkCount { get { return SuccessfulWorkCount + FailedWorkCount; } }
float Progress { get { return (CompletedWorkCount / (float)TotalWorkCount) * 100.0f; } }
float Progress { get; set; }
bool IsIndeterminate { get; set; }
string Description { get; set; }
}
Expand Down
71 changes: 49 additions & 22 deletions XUIHelper.Core/API/XUIHelperAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ public enum XUIHelperSupportedFormats
XUI12
}

public class MassConversionResult
{
public bool Successful { get; private set; }
public int SuccessfulWorkCount { get; private set; }
public int FailedWorkCount { get; private set; }

public MassConversionResult(bool successful, int successfulWorkCount, int failedWorkCount)
{
Successful = successful;
SuccessfulWorkCount = successfulWorkCount;
FailedWorkCount = failedWorkCount;
}

public MassConversionResult(bool successful)
{
Successful = successful;
SuccessfulWorkCount = 0;
FailedWorkCount = 0;
}
}

public static bool AreIgnoredPropertiesActive { get; private set; } = true;
public static ILogger? Logger { get; private set; } = null;

Expand Down Expand Up @@ -73,26 +94,24 @@ public static void SetCurrentExtensionsGroup(string groupName)
#endregion

#region Conversion
public static async Task<bool> TryMassConvertDirectoryAsync(string directoryPath, XUIHelperSupportedFormats format, string outputDir, IXUIHelperProgressable? progressable)
public static async Task<MassConversionResult> TryMassConvertDirectoryAsync(string directoryPath, XUIHelperSupportedFormats format, string outputDir, IXUIHelperProgressable? progressable)
{
try
{
if(!XUIHelperCoreUtilities.IsStringValidPath(directoryPath))
{
Logger?.Error("The directory path {0} is invalid, returning false.", directoryPath);
return false;
return new MassConversionResult(false);
}

if (!XUIHelperCoreUtilities.IsStringValidPath(outputDir))
{
Logger?.Error("The output directory path {0} is invalid, returning false.", outputDir);
return false;
return new MassConversionResult(false);
}

if (progressable != null)
{
progressable.SuccessfulWorkCount = 0;
progressable.FailedWorkCount = 0;
progressable.IsIndeterminate = true;
progressable.Description = "Searching for convertible files, please wait...";
}
Expand All @@ -117,14 +136,20 @@ public static async Task<bool> TryMassConvertDirectoryAsync(string directoryPath
}
}

if(progressable != null)
Directory.CreateDirectory(outputDir);

int totalWorkCount = convertibleFilePaths.Count;
int completedWorkCount = 0;
int successfulWorkCount = 0;
int failedWorkCount = 0;

if (progressable != null)
{
progressable.TotalWorkCount = convertibleFilePaths.Count;
progressable.IsIndeterminate = false;
progressable.Description = string.Format("Converting files, converted 0 of {0}...", totalWorkCount);
}

Directory.CreateDirectory(outputDir);

foreach(string filePath in convertibleFilePaths)
foreach (string filePath in convertibleFilePaths)
{
string thisOutputPath = Path.Combine(outputDir, Path.GetFileNameWithoutExtension(filePath));
if(format == XUIHelperSupportedFormats.XUI12)
Expand All @@ -137,28 +162,30 @@ public static async Task<bool> TryMassConvertDirectoryAsync(string directoryPath
}

bool successful = await TryConvertAsync(filePath, format, thisOutputPath);
if (successful)
{
successfulWorkCount++;
}
else
{
failedWorkCount++;
}

completedWorkCount++;

if(progressable != null)
{
if(successful)
{
progressable.SuccessfulWorkCount++;
}
else
{
progressable.FailedWorkCount++;
}

progressable.Description = string.Format("Converting files, converted {0} of {1}...", progressable.CompletedWorkCount, progressable.TotalWorkCount);
progressable.Description = string.Format("Converting files, converted {0} of {1}...", completedWorkCount, totalWorkCount);
progressable.Progress = Convert.ToInt32((completedWorkCount / (float)totalWorkCount) * 100.0f);
}
}

return true;
return new MassConversionResult(true, successfulWorkCount, failedWorkCount);
}
catch (Exception ex)
{
Logger?.Error("Caught an exception when trying to mass convert files from {0} to {1}, returning false. The exception is: {2}", directoryPath, format, ex);
return false;
return new MassConversionResult(false);
}
}

Expand Down
1 change: 1 addition & 0 deletions XUIHelper.GUI/Helpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static Constants()
public static void Initialize(NXEHUD hud)
{
HUDManager = new NXEHUDManager(hud);
HUDManager.SetShouldManageDateTime(true);
}
}
}
85 changes: 85 additions & 0 deletions XUIHelper.GUI/ViewModels/HUD/MassConvertProgressPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using NXEControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XUIHelper.Core;

namespace XUIHelper.GUI
{
public class MassConvertProgressPageViewModel : NXEViewModelBase, IXUIHelperProgressable
{
private float _Progress;
private bool _IsIndeterminate;
private string _Description = string.Empty;

public float Progress
{
get
{
return _Progress;
}
set
{
_Progress = value;
NotifyPropertyChanged();
}
}

public bool IsIndeterminate
{
get
{
return _IsIndeterminate;
}
set
{
_IsIndeterminate = value;
NotifyPropertyChanged();
}
}

public string Description
{
get
{
return _Description;
}
set
{
_Description = value;
NotifyPropertyChanged();
}
}

private string _SourceDirectory;
private XUIHelperAPI.XUIHelperSupportedFormats _Format;
private string _DestinationDirectory;

public async Task ConvertAsync()
{
XUIHelperAPI.MassConversionResult result = await XUIHelperAPI.TryMassConvertDirectoryAsync(_SourceDirectory, _Format, _DestinationDirectory, this);
if (!result.Successful)
{
_ = Constants.HUDManager?.ShowMessageBox("The conversion has failed. Consider enabling logging and consult the log file for more information.", "Conversion Failed", System.Windows.MessageBoxButton.OK, NXEHUD.NXEHUDIconType.Error, true);
}
else
{
_ = Constants.HUDManager?.ShowMessageBox(string.Format("The conversion has completed!\n\nSuccessful Conversions: {0}\nFailed Conversions: {1}\nSuccess Rate: {2}%",
result.SuccessfulWorkCount,
result.FailedWorkCount,
Convert.ToInt32((result.SuccessfulWorkCount / (float)(result.SuccessfulWorkCount + result.FailedWorkCount)) * 100.0f)
),
"Conversion Completed", System.Windows.MessageBoxButton.OK, NXEHUD.NXEHUDIconType.Exclamation, true);
}
}

public MassConvertProgressPageViewModel(string sourceDir, XUIHelperAPI.XUIHelperSupportedFormats format, string destDir)
{
_SourceDirectory = sourceDir;
_Format = format;
_DestinationDirectory = destDir;
}
}
}
14 changes: 3 additions & 11 deletions XUIHelper.GUI/ViewModels/MassConvertPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace XUIHelper.GUI
{
public class MassConvertPageViewModel : NXEViewModelBase
{
private string _SourceDirectory;
private string _SourceDirectory = @"F:\Code Repos\XUIHelper\XUIHelper.Tests\Test Data\XUR\17559";
private ICommand _BrowseSourceDirectoryCommand;
private string _DestinationDirectory;
private string _DestinationDirectory = @"F:\XUIHelper Example\output";
private ICommand _BrowseDestinationDirectoryCommand;
private bool _IgnoreProperties = true;
private ObservableCollection<string> _OutputFileTypes = new ObservableCollection<string>() { "XUR v5", "XUR v8", "XUI v12" };
Expand Down Expand Up @@ -304,15 +304,7 @@ private async Task ConvertAsync()
return;
}

/*bool successful = await XUIHelperAPI.TryMassConvertDirectoryAsync(SourceDirectory, format.Value, DestinationDirectory);
if (!successful)
{
_ = Constants.HUDManager?.ShowMessageBox("The conversion has failed. Consider enabling logging and consult the log file for more information.", "Conversion Failed", System.Windows.MessageBoxButton.OK, NXEHUD.NXEHUDIconType.Error);
}
else
{
_ = Constants.HUDManager?.ShowMessageBox("The conversion was successful!", "Conversion Succeeded", System.Windows.MessageBoxButton.OK, NXEHUD.NXEHUDIconType.Exclamation);
}*/
_ = Constants.HUDManager?.NavigateForwardAsync(new MassConvertProgressPage(SourceDirectory, format.Value, DestinationDirectory));
}
catch (Exception ex)
{
Expand Down
22 changes: 22 additions & 0 deletions XUIHelper.GUI/Views/HUD/MassConvertProgressPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<nxe:NXEHUDUserControl x:Class="XUIHelper.GUI.MassConvertProgressPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:XUIHelper.GUI"
xmlns:nxe="clr-namespace:NXEControls;assembly=NXEControls"
mc:Ignorable="d"
d:DesignHeight="315" d:DesignWidth="550" PageSize="Half" Title="Conversion in Progress" IconType="Exclamation">
<Viewbox>
<Grid Width="550" Height="315" Margin="15">
<TextBlock Style="{StaticResource NXEHUDMessageBoxPage.TextBlock.MessageText}" Text="Please wait while files are converted. This may take some time." />
<nxe:NXELoading Variant="HUDInline" Width="38" Height="38" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBlock Style="{StaticResource NXEHUDMessageBoxPage.TextBlock.MessageText}" HorizontalAlignment="Stretch" VerticalAlignment="Center" Height="100" Margin="0, 150, 0, 0"
Text="{Binding Description, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, FallbackValue=Description Text}" />
<nxe:NXEHUDProgressBar HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Margin="20, 0"
IsIndeterminate="{Binding IsIndeterminate, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Value="{Binding Progress, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Viewbox>

</nxe:NXEHUDUserControl>
40 changes: 40 additions & 0 deletions XUIHelper.GUI/Views/HUD/MassConvertProgressPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using NXEControls;
using XUIHelper.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace XUIHelper.GUI
{
/// <summary>
/// Interaction logic for MassConvertProgressPage.xaml
/// </summary>
public partial class MassConvertProgressPage : NXEHUDUserControl
{
private MassConvertProgressPageViewModel _ViewModel;

public MassConvertProgressPage(string sourceDir, XUIHelperAPI.XUIHelperSupportedFormats format, string destDir)
{
_ViewModel = new MassConvertProgressPageViewModel(sourceDir, format, destDir);
DataContext = _ViewModel;
InitializeComponent();
Loaded += OnLoaded;
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
_ = _ViewModel.ConvertAsync();
}
}
}

0 comments on commit 71ef27d

Please sign in to comment.