From 7daf387f8adfd6ab75551d9867d8d19d08923aab Mon Sep 17 00:00:00 2001 From: Adrien Allard Date: Tue, 19 Jun 2018 19:20:07 +0200 Subject: [PATCH] Add ConversionJobToEstimatedRemainingDuration --- .../FileConverter/FileConverter.csproj | 1 + ...nversionJobToEstimatedRemainingDuration.cs | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 Application/FileConverter/ValueConverters/ConversionJobToEstimatedRemainingDuration.cs diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index b0c0174..97a579a 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -168,6 +168,7 @@ + diff --git a/Application/FileConverter/ValueConverters/ConversionJobToEstimatedRemainingDuration.cs b/Application/FileConverter/ValueConverters/ConversionJobToEstimatedRemainingDuration.cs new file mode 100644 index 0000000..67308ff --- /dev/null +++ b/Application/FileConverter/ValueConverters/ConversionJobToEstimatedRemainingDuration.cs @@ -0,0 +1,48 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +namespace FileConverter.ValueConverters +{ + using System; + using System.Globalization; + using System.Windows.Data; + + using ConversionJobs; + + public class ConversionJobToEstimatedRemainingDuration : IMultiValueConverter + { + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) + { + if (values.Length != 3) + { + return string.Empty; + } + + ConversionJob.ConversionState state = (ConversionJob.ConversionState)values[0]; + DateTime startTime = (DateTime)values[1]; + float progress = (float)values[2]; + + if (state == ConversionJob.ConversionState.Unknown || + state == ConversionJob.ConversionState.Ready || + progress < 0.05f) + { + return "-"; + } + + if (progress >= 1f) + { + return "-"; + } + + TimeSpan elapsedTime = DateTime.Now - startTime; + + double remainingTimeInSeconds = (1 - progress) * elapsedTime.TotalSeconds / progress; + TimeSpan remainingTime = TimeSpan.FromSeconds(Math.Floor(remainingTimeInSeconds)); + return remainingTime.ToString("g"); + } + + public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +}