sourcegit/src/ViewModels/LFSLocks.cs
Mat 6e35ac9eb0 Now uses correct username for lock filtering.
WIP: needs an error popup if user is misconfigured
2024-09-26 08:26:34 +02:00

106 lines
2.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Collections;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class LFSLocks : ObservableObject
{
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();
}
}
private AvaloniaList<Models.LFSLock> Locks
{
get;
}
public IEnumerable<Models.LFSLock> FilteredLocks
{
get
{
if (string.IsNullOrEmpty(_userName))
{
//todo: add an error popup
return Locks;
}
return ShowOnlyMyLocks ? Locks.Where(@lock => @lock.User == _userName) : Locks;
}
}
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);
Task.Run(() =>
{
var collect = new Commands.LFS(_repo).Locks(_remote);
Dispatcher.UIThread.Invoke(() =>
{
if (collect.Count > 0)
Locks.AddRange(collect);
IsLoading = false;
IsEmpty = collect.Count == 0;
});
});
}
public void Unlock(Models.LFSLock lfsLock, bool force)
{
if (_isLoading)
return;
IsLoading = true;
Task.Run(() =>
{
var succ = new Commands.LFS(_repo).Unlock(_remote, lfsLock.ID, force);
Dispatcher.UIThread.Invoke(() =>
{
if (succ)
Locks.Remove(lfsLock);
IsLoading = false;
IsEmpty = Locks.Count == 0;
});
});
}
private string _repo;
private string _remote;
private bool _isLoading = true;
private bool _isEmpty = false;
private bool _showOnlyMyLocks = false;
private string _userName;
}
}