Ionite
1 year ago
3 changed files with 158 additions and 0 deletions
@ -0,0 +1,56 @@ |
|||||||
|
<UserControl |
||||||
|
HorizontalAlignment="Stretch" |
||||||
|
VerticalAlignment="Stretch" |
||||||
|
d:DataContext="{d:DesignInstance Type=viewModels:RefreshBadgeViewModel, |
||||||
|
IsDesignTimeCreatable=True}" |
||||||
|
d:DesignHeight="150" |
||||||
|
d:DesignWidth="150" |
||||||
|
mc:Ignorable="d" |
||||||
|
ui:Design.Background="{DynamicResource ApplicationBackgroundBrush}" |
||||||
|
ui:Design.Foreground="{DynamicResource TextFillColorPrimaryBrush}" |
||||||
|
x:Class="StabilityMatrix.Controls.RefreshBadge" |
||||||
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
||||||
|
xmlns:converters="clr-namespace:StabilityMatrix.Converters" |
||||||
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
||||||
|
xmlns:local="clr-namespace:StabilityMatrix.Controls" |
||||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" |
||||||
|
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" |
||||||
|
xmlns:viewModels="clr-namespace:StabilityMatrix.ViewModels" |
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> |
||||||
|
|
||||||
|
<UserControl.Resources> |
||||||
|
<BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" /> |
||||||
|
<converters:ValueConverterGroup x:Key="InvertAndVisibleOrHidden"> |
||||||
|
<converters:BoolNegationConverter /> |
||||||
|
<converters:BooleanToHiddenVisibleConverter /> |
||||||
|
</converters:ValueConverterGroup> |
||||||
|
</UserControl.Resources> |
||||||
|
|
||||||
|
<Grid> |
||||||
|
<ui:Button |
||||||
|
Appearance="Transparent" |
||||||
|
BorderThickness="0" |
||||||
|
Command="{Binding RefreshCommand}" |
||||||
|
FontSize="26" |
||||||
|
Foreground="{Binding ColorBrush}" |
||||||
|
HorizontalAlignment="Center" |
||||||
|
Icon="{Binding Icon, FallbackValue=CheckmarkCircle12}" |
||||||
|
Margin="4" |
||||||
|
Padding="2" |
||||||
|
ToolTip="{Binding CurrentToolTip}" |
||||||
|
VerticalAlignment="Center" |
||||||
|
Visibility="{Binding IsWorking, Converter={StaticResource InvertAndVisibleOrHidden}}" /> |
||||||
|
<ui:ProgressRing |
||||||
|
FontSize="14" |
||||||
|
Grid.Row="0" |
||||||
|
Height="20" |
||||||
|
HorizontalAlignment="Center" |
||||||
|
IsEnabled="{Binding IsWorking, Converter={StaticResource BoolToVisibilityConverter}}" |
||||||
|
IsIndeterminate="{Binding IsWorking}" |
||||||
|
ToolTip="{Binding CurrentToolTip}" |
||||||
|
VerticalAlignment="Center" |
||||||
|
Visibility="{Binding IsWorking, Converter={StaticResource BoolToVisibilityConverter}}" |
||||||
|
Width="20" /> |
||||||
|
</Grid> |
||||||
|
|
||||||
|
</UserControl> |
@ -0,0 +1,11 @@ |
|||||||
|
using System.Windows.Controls; |
||||||
|
|
||||||
|
namespace StabilityMatrix.Controls; |
||||||
|
|
||||||
|
public partial class RefreshBadge : UserControl |
||||||
|
{ |
||||||
|
public RefreshBadge() |
||||||
|
{ |
||||||
|
InitializeComponent(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,91 @@ |
|||||||
|
using System; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using System.Windows.Media; |
||||||
|
using CommunityToolkit.Mvvm.ComponentModel; |
||||||
|
using CommunityToolkit.Mvvm.Input; |
||||||
|
using NLog; |
||||||
|
using StabilityMatrix.Models; |
||||||
|
using Wpf.Ui.Common; |
||||||
|
using Wpf.Ui.Controls; |
||||||
|
using Wpf.Ui.Controls.IconElements; |
||||||
|
|
||||||
|
namespace StabilityMatrix.ViewModels; |
||||||
|
|
||||||
|
public partial class RefreshBadgeViewModel : ObservableObject |
||||||
|
{ |
||||||
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); |
||||||
|
|
||||||
|
public string WorkingToolTipText = "Loading..."; |
||||||
|
public string SuccessToolTipText = "Success"; |
||||||
|
public string FailToolTipText = "Failed"; |
||||||
|
|
||||||
|
public SymbolIcon InactiveIcon = new(SymbolRegular.Circle12); |
||||||
|
public SymbolIcon SuccessIcon = new(SymbolRegular.CheckmarkCircle12, true); |
||||||
|
public SymbolIcon FailIcon = new(SymbolRegular.ErrorCircle12); |
||||||
|
|
||||||
|
public Func<Task<bool>>? RefreshFunc { get; set; } |
||||||
|
|
||||||
|
[ObservableProperty] |
||||||
|
[NotifyPropertyChangedFor(nameof(IsWorking))] |
||||||
|
[NotifyPropertyChangedFor(nameof(Appearance))] |
||||||
|
[NotifyPropertyChangedFor(nameof(ColorBrush))] |
||||||
|
[NotifyPropertyChangedFor(nameof(CurrentToolTip))] |
||||||
|
[NotifyPropertyChangedFor(nameof(Icon))] |
||||||
|
private ProgressState state; |
||||||
|
|
||||||
|
public bool IsWorking => State == ProgressState.Working; |
||||||
|
|
||||||
|
public ControlAppearance Appearance => State switch |
||||||
|
{ |
||||||
|
ProgressState.Working => ControlAppearance.Info, |
||||||
|
ProgressState.Success => ControlAppearance.Success, |
||||||
|
ProgressState.Failed => ControlAppearance.Danger, |
||||||
|
_ => ControlAppearance.Secondary, |
||||||
|
}; |
||||||
|
|
||||||
|
public SolidColorBrush ColorBrush => State switch |
||||||
|
{ |
||||||
|
ProgressState.Success => (SolidColorBrush) new BrushConverter().ConvertFrom("#4caf50")!, |
||||||
|
ProgressState.Failed => (SolidColorBrush) new BrushConverter().ConvertFrom("#f44336")!, |
||||||
|
_ => Brushes.Gray, |
||||||
|
}; |
||||||
|
|
||||||
|
public string CurrentToolTip => State switch |
||||||
|
{ |
||||||
|
ProgressState.Working => WorkingToolTipText, |
||||||
|
ProgressState.Success => SuccessToolTipText, |
||||||
|
ProgressState.Failed => FailToolTipText, |
||||||
|
_ => "" |
||||||
|
}; |
||||||
|
|
||||||
|
public SymbolIcon Icon => State switch |
||||||
|
{ |
||||||
|
ProgressState.Success => SuccessIcon, |
||||||
|
ProgressState.Failed => FailIcon, |
||||||
|
_ => InactiveIcon |
||||||
|
}; |
||||||
|
|
||||||
|
public RefreshBadgeViewModel() |
||||||
|
{ |
||||||
|
Logger.Info("New RefreshVM instance!"); |
||||||
|
} |
||||||
|
|
||||||
|
[RelayCommand] |
||||||
|
private async Task Refresh() |
||||||
|
{ |
||||||
|
Logger.Info("Running refresh command..."); |
||||||
|
if (RefreshFunc == null) return; |
||||||
|
|
||||||
|
State = ProgressState.Working; |
||||||
|
try |
||||||
|
{ |
||||||
|
var result = await RefreshFunc.Invoke(); |
||||||
|
State = result ? ProgressState.Success : ProgressState.Failed; |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
State = ProgressState.Failed; |
||||||
|
Logger.Error(ex, "Refresh command failed: {Ex}", ex.Message); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue