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

View file

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