Skip to content

Commit

Permalink
using local ValueConverters instead of community toolkit ones
Browse files Browse the repository at this point in the history
  • Loading branch information
BeepBeepBopBop committed Nov 21, 2024
1 parent 2159125 commit 89ee546
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 55 deletions.
24 changes: 0 additions & 24 deletions LM-Kit-Maestro/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,6 @@
<mct:VariableMultiValueConverter x:Key="AllFalseConverter" ConditionType="None"/>
<mct:VariableMultiValueConverter x:Key="AnyTrueConverter" ConditionType="Any"/>

<x:Int32 x:Key="1">1</x:Int32>
<x:Int32 x:Key="0">0</x:Int32>

<mct:CompareConverter
x:Key="GreaterThanOneConverter"
ComparingValue="{StaticResource 1}"
ComparisonOperator="Greater"
TrueObject="True"
FalseObject="False" />

<mct:CompareConverter
x:Key="GreaterThanZeroConverter"
ComparingValue="{StaticResource 0}"
ComparisonOperator="Greater"
TrueObject="True"
FalseObject="False" />

<mct:CompareConverter
x:Key="EqualToZeroConverter"
ComparingValue="{StaticResource 0}"
ComparisonOperator="Equal"
TrueObject="True"
FalseObject="False" />

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="colors" Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
Expand Down
21 changes: 21 additions & 0 deletions LM-Kit-Maestro/Converters/EqualToZeroConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Globalization;

namespace LMKitMaestro.Converters;

internal sealed class EqualToZeroConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value != null && value is int integer)
{
return integer == 0;
}

return false;
}

public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
2 changes: 1 addition & 1 deletion LM-Kit-Maestro/Converters/FileNameConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace LMKitMaestro.Converters;

class FileNameConverter : IValueConverter
internal sealed class FileNameConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
Expand Down
51 changes: 25 additions & 26 deletions LM-Kit-Maestro/Converters/FileSizeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
using System.Globalization;

namespace LMKitMaestro.Converters
namespace LMKitMaestro.Converters;

internal sealed class FileSizeConverter : IValueConverter
{
class FileSizeConverter : IValueConverter
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
if (value != null)
{
if (value != null)
{
Type type = value.GetType();
Type type = value.GetType();

if (value is long bytes)
{
return FormatFileSize(bytes);
}
if (value is long bytes)
{
return FormatFileSize(bytes);
}

return string.Empty;
}

public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

private static string FormatFileSize(long bytes)
{
var unit = 1024;
return string.Empty;
}

if (bytes < unit)
{
return $"{bytes} B";
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

var exp = (int)(Math.Log(bytes) / Math.Log(unit));
private static string FormatFileSize(long bytes)
{
var unit = 1024;

return $"{bytes / Math.Pow(unit, exp):F2} {("KMGTPE")[exp - 1]}B";
if (bytes < unit)
{
return $"{bytes} B";
}

var exp = (int)(Math.Log(bytes) / Math.Log(unit));

return $"{bytes / Math.Pow(unit, exp):F2} {("KMGTPE")[exp - 1]}B";
}
}
21 changes: 21 additions & 0 deletions LM-Kit-Maestro/Converters/GreaterThanZeroConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Globalization;

namespace LMKitMaestro.Converters;

internal sealed class GreaterThanZeroConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value != null && value is int integer)
{
return integer > 0;
}

return false;
}

public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
4 changes: 2 additions & 2 deletions LM-Kit-Maestro/UI/Pages/ModelsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@
TextColor="{StaticResource Error}"
Text="{Binding FileManager.UnsortedModels.Count, StringFormat='{}You have {0} unsorted model files'}"
Command="{Binding Source={x:Reference modelsPage}, Path=ShowUnsortedModelFilesPopupCommand}"
IsVisible="{Binding FileManager.UnsortedModels.Count, Converter={StaticResource GreaterThanZeroConverter}}"/>
IsVisible="{Binding FileManager.UnsortedModels.Count, Converter={converters:GreaterThanZeroConverter}}"/>
</Grid>
</VerticalStackLayout>

Expand All @@ -353,7 +353,7 @@
</CollectionView.ItemsLayout>

<CollectionView.Triggers>
<DataTrigger TargetType="CollectionView" Value="True" Binding="{Binding Source={x:Reference modelsCollectionView}, Path=ItemsSource.Count, Converter={StaticResource EqualToZeroConverter}}">
<DataTrigger TargetType="CollectionView" Value="True" Binding="{Binding Source={x:Reference modelsCollectionView}, Path=ItemsSource.Count, Converter={converters:EqualToZeroConverter}}">
<Setter Property="VerticalOptions" Value="Center"/>
</DataTrigger>

Expand Down
4 changes: 2 additions & 2 deletions LM-Kit-Maestro/UI/Popups/ModelSelectionPopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
RowDefinitions="*,*"
RowSpacing="20"
Margin="0, 0, 0, 8"
IsVisible="{Binding ModelListViewModel.UserModels.Count, Converter={StaticResource EqualToZeroConverter}}">
IsVisible="{Binding ModelListViewModel.UserModels.Count, Converter={converters:EqualToZeroConverter}}">

<Label
TextColor="{StaticResource OutlineVariant}"
Expand All @@ -62,7 +62,7 @@
Margin="0"
Spacing="8"
Grid.Row="1"
IsVisible="{Binding ModelListViewModel.UserModels.Count, Converter={StaticResource GreaterThanZeroConverter}}"
IsVisible="{Binding ModelListViewModel.UserModels.Count, Converter={converters:GreaterThanZeroConverter}}"
ItemsSource="{Binding ModelListViewModel.UserModels}"
SelectedItem="{Binding ModelListViewModel.SelectedModel, Mode=TwoWay}">

Expand Down

0 comments on commit 89ee546

Please sign in to comment.