code_review: PR #515

* remove Linq expressions due to AOT limitations. See https://learn.microsoft.com/zh-cn/dotnet/core/deploying/native-aot/?tabs=windows%2Cnet8#limitations-of-native-aot-deployment
* rename `FilteredLocks` to `VisibleLocks`
* use `Commands.Config.Get` instead of `Commands.Config.ListAll`
* disable checkbox if user name is not valid
This commit is contained in:
leo 2024-09-26 15:36:20 +08:00
parent 21498f7009
commit 5d2a442144
No known key found for this signature in database
3 changed files with 67 additions and 52 deletions

View file

@ -10,6 +10,9 @@ namespace SourceGit.Converters
public static readonly FuncValueConverter<IList, string> ToCount = public static readonly FuncValueConverter<IList, string> ToCount =
new FuncValueConverter<IList, string>(v => v == null ? " (0)" : $" ({v.Count})"); new FuncValueConverter<IList, string>(v => v == null ? " (0)" : $" ({v.Count})");
public static readonly FuncValueConverter<IList, bool> IsNullOrEmpty =
new FuncValueConverter<IList, bool>(v => v == null || v.Count == 0);
public static readonly FuncValueConverter<IList, bool> IsNotNullOrEmpty = public static readonly FuncValueConverter<IList, bool> IsNotNullOrEmpty =
new FuncValueConverter<IList, bool>(v => v != null && v.Count > 0); new FuncValueConverter<IList, bool>(v => v != null && v.Count > 0);

View file

@ -1,7 +1,6 @@
using System.Linq; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Collections;
using Avalonia.Threading; using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
@ -10,69 +9,49 @@ namespace SourceGit.ViewModels
{ {
public class LFSLocks : ObservableObject public class LFSLocks : ObservableObject
{ {
public bool HasValidUserName
{
get;
private set;
} = false;
public bool IsLoading public bool IsLoading
{ {
get => _isLoading; get => _isLoading;
private set => SetProperty(ref _isLoading, value); private set => SetProperty(ref _isLoading, value);
} }
public bool IsEmpty
{
get => _isEmpty;
private set => SetProperty(ref _isEmpty, value);
}
public bool ShowOnlyMyLocks public bool ShowOnlyMyLocks
{ {
get => _showOnlyMyLocks; get => _showOnlyMyLocks;
set set
{ {
if (!SetProperty(ref _showOnlyMyLocks, value)) if (SetProperty(ref _showOnlyMyLocks, value))
return; UpdateVisibleLocks();
OnPropertyChanged(nameof(FilteredLocks));
IsEmpty = !FilteredLocks.Any();
} }
} }
private AvaloniaList<Models.LFSLock> Locks public List<Models.LFSLock> VisibleLocks
{ {
get; get => _visibleLocks;
} private set => SetProperty(ref _visibleLocks, value);
public AvaloniaList<Models.LFSLock> FilteredLocks
{
get
{
if (string.IsNullOrEmpty(_userName))
{
App.RaiseException(_repo, "Username is empty");
return Locks;
}
return _showOnlyMyLocks ?
new AvaloniaList<Models.LFSLock>(Locks.Where(@lock => @lock.User == _userName)) :
Locks;
}
} }
public LFSLocks(string repo, string remote) public LFSLocks(string repo, string remote)
{ {
_repo = repo; _repo = repo;
_remote = remote; _remote = remote;
Locks = new AvaloniaList<Models.LFSLock>(); _userName = new Commands.Config(repo).Get("user.name");
new Commands.Config(repo).ListAll().TryGetValue("user.name", out _userName);
HasValidUserName = !string.IsNullOrEmpty(_userName);
Task.Run(() => Task.Run(() =>
{ {
var collect = new Commands.LFS(_repo).Locks(_remote); _cachedLocks = new Commands.LFS(_repo).Locks(_remote);
Dispatcher.UIThread.Invoke(() => Dispatcher.UIThread.Invoke(() =>
{ {
if (collect.Count > 0) UpdateVisibleLocks();
Locks.AddRange(collect);
IsLoading = false; IsLoading = false;
IsEmpty = collect.Count == 0;
}); });
}); });
} }
@ -89,18 +68,40 @@ namespace SourceGit.ViewModels
Dispatcher.UIThread.Invoke(() => Dispatcher.UIThread.Invoke(() =>
{ {
if (succ) if (succ)
Locks.Remove(lfsLock); {
_cachedLocks.Remove(lfsLock);
UpdateVisibleLocks();
}
IsLoading = false; IsLoading = false;
IsEmpty = Locks.Count == 0;
}); });
}); });
} }
private void UpdateVisibleLocks()
{
if (!_showOnlyMyLocks)
{
VisibleLocks = _cachedLocks;
}
else
{
var visible = new List<Models.LFSLock>();
foreach (var lfsLock in _cachedLocks)
{
if (lfsLock.User == _userName)
visible.Add(lfsLock);
}
VisibleLocks = visible;
}
}
private string _repo; private string _repo;
private string _remote; private string _remote;
private bool _isLoading = true; private bool _isLoading = true;
private bool _isEmpty = false; private List<Models.LFSLock> _cachedLocks = [];
private List<Models.LFSLock> _visibleLocks = [];
private bool _showOnlyMyLocks = false; private bool _showOnlyMyLocks = false;
private string _userName; private string _userName;
} }

View file

@ -5,6 +5,7 @@
xmlns:m="using:SourceGit.Models" xmlns:m="using:SourceGit.Models"
xmlns:vm="using:SourceGit.ViewModels" xmlns:vm="using:SourceGit.ViewModels"
xmlns:v="using:SourceGit.Views" xmlns:v="using:SourceGit.Views"
xmlns:c="using:SourceGit.Converters"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.LFSLocks" x:Class="SourceGit.Views.LFSLocks"
x:DataType="vm:LFSLocks" x:DataType="vm:LFSLocks"
@ -44,19 +45,24 @@
</Grid> </Grid>
<!-- Filter and Unlock All --> <!-- Filter and Unlock All -->
<Grid Grid.Row="1" ColumnDefinitions="Auto,*,Auto" Margin="8,0,0,0"> <CheckBox Grid.Row="1"
<CheckBox Grid.Column="0" Margin="8,0,0,0"
Content="Show only my locks" Content="Show only my locks"
IsChecked="{Binding ShowOnlyMyLocks}" IsChecked="{Binding ShowOnlyMyLocks, Mode=TwoWay}"
IsEnabled="{Binding !IsLoading}" VerticalAlignment="Center">
VerticalAlignment="Center" /> <CheckBox.IsEnabled>
</Grid> <MultiBinding Converter="{x:Static BoolConverters.And}">
<Binding Path="HasValidUserName"/>
<Binding Path="!IsLoading"/>
</MultiBinding>
</CheckBox.IsEnabled>
</CheckBox>
<!-- Locked Files --> <!-- Locked Files -->
<Grid Grid.Row="2"> <Grid Grid.Row="2">
<ListBox Margin="8,0,8,8" <ListBox Margin="8,0,8,8"
Background="{DynamicResource Brush.Contents}" Background="{DynamicResource Brush.Contents}"
ItemsSource="{Binding FilteredLocks}" ItemsSource="{Binding VisibleLocks}"
SelectionMode="Single" SelectionMode="Single"
BorderThickness="1" BorderThickness="1"
BorderBrush="{DynamicResource Brush.Border2}" BorderBrush="{DynamicResource Brush.Border2}"
@ -98,9 +104,14 @@
</ListBox> </ListBox>
<!-- Empty --> <!-- Empty -->
<StackPanel Orientation="Vertical" <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
HorizontalAlignment="Center" VerticalAlignment="Center" <StackPanel.IsVisible>
IsVisible="{Binding IsEmpty}"> <MultiBinding Converter="{x:Static BoolConverters.And}">
<Binding Path="!IsLoading"/>
<Binding Path="VisibleLocks" Converter="{x:Static c:ListConverters.IsNullOrEmpty}"/>
</MultiBinding>
</StackPanel.IsVisible>
<Path Width="48" Height="48" Data="{StaticResource Icons.Empty}" Fill="{DynamicResource Brush.FG2}"/> <Path Width="48" Height="48" Data="{StaticResource Icons.Empty}" Fill="{DynamicResource Brush.FG2}"/>
<TextBlock Margin="0,16,0,0" Text="{DynamicResource Text.GitLFS.Locks.Empty}" Foreground="{DynamicResource Brush.FG2}"/> <TextBlock Margin="0,16,0,0" Text="{DynamicResource Text.GitLFS.Locks.Empty}" Foreground="{DynamicResource Brush.FG2}"/>
</StackPanel> </StackPanel>