From 5d2a4421442d078a42b16170d645e77776603c1a Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 26 Sep 2024 15:36:20 +0800 Subject: [PATCH] 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 --- src/Converters/ListConverters.cs | 3 ++ src/ViewModels/LFSLocks.cs | 83 ++++++++++++++++---------------- src/Views/LFSLocks.axaml | 33 ++++++++----- 3 files changed, 67 insertions(+), 52 deletions(-) diff --git a/src/Converters/ListConverters.cs b/src/Converters/ListConverters.cs index fef0f584..81cac8b7 100644 --- a/src/Converters/ListConverters.cs +++ b/src/Converters/ListConverters.cs @@ -10,6 +10,9 @@ namespace SourceGit.Converters public static readonly FuncValueConverter ToCount = new FuncValueConverter(v => v == null ? " (0)" : $" ({v.Count})"); + public static readonly FuncValueConverter IsNullOrEmpty = + new FuncValueConverter(v => v == null || v.Count == 0); + public static readonly FuncValueConverter IsNotNullOrEmpty = new FuncValueConverter(v => v != null && v.Count > 0); diff --git a/src/ViewModels/LFSLocks.cs b/src/ViewModels/LFSLocks.cs index 7f3f9c30..38dc2eb9 100644 --- a/src/ViewModels/LFSLocks.cs +++ b/src/ViewModels/LFSLocks.cs @@ -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 Locks + public List VisibleLocks { - get; - } - - public AvaloniaList FilteredLocks - { - get - { - if (string.IsNullOrEmpty(_userName)) - { - App.RaiseException(_repo, "Username is empty"); - return Locks; - } - - return _showOnlyMyLocks ? - new AvaloniaList(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(); - 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(); + 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 _cachedLocks = []; + private List _visibleLocks = []; private bool _showOnlyMyLocks = false; private string _userName; } diff --git a/src/Views/LFSLocks.axaml b/src/Views/LFSLocks.axaml index e275a0d8..b54f6be5 100644 --- a/src/Views/LFSLocks.axaml +++ b/src/Views/LFSLocks.axaml @@ -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 @@ - - - + + + + + + + + - + + + + + + + +