|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Avalonia.Threading; |
| 5 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 6 | + |
| 7 | +namespace SourceGit.ViewModels |
| 8 | +{ |
| 9 | + public class BlameCommandPalette : ObservableObject, IDisposable |
| 10 | + { |
| 11 | + public bool IsLoading |
| 12 | + { |
| 13 | + get => _isLoading; |
| 14 | + private set => SetProperty(ref _isLoading, value); |
| 15 | + } |
| 16 | + |
| 17 | + public List<string> VisibleFiles |
| 18 | + { |
| 19 | + get => _visibleFiles; |
| 20 | + private set => SetProperty(ref _visibleFiles, value); |
| 21 | + } |
| 22 | + |
| 23 | + public string Filter |
| 24 | + { |
| 25 | + get => _filter; |
| 26 | + set |
| 27 | + { |
| 28 | + if (SetProperty(ref _filter, value)) |
| 29 | + UpdateVisible(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public string SelectedFile |
| 34 | + { |
| 35 | + get => _selectedFile; |
| 36 | + set => SetProperty(ref _selectedFile, value); |
| 37 | + } |
| 38 | + |
| 39 | + public BlameCommandPalette(Launcher launcher, string repo) |
| 40 | + { |
| 41 | + _launcher = launcher; |
| 42 | + _repo = repo; |
| 43 | + _isLoading = true; |
| 44 | + |
| 45 | + Task.Run(async () => |
| 46 | + { |
| 47 | + var files = await new Commands.QueryRevisionFileNames(_repo, "HEAD") |
| 48 | + .GetResultAsync() |
| 49 | + .ConfigureAwait(false); |
| 50 | + |
| 51 | + var head = await new Commands.QuerySingleCommit(_repo, "HEAD") |
| 52 | + .GetResultAsync() |
| 53 | + .ConfigureAwait(false); |
| 54 | + |
| 55 | + Dispatcher.UIThread.Post(() => |
| 56 | + { |
| 57 | + IsLoading = false; |
| 58 | + _repoFiles = files; |
| 59 | + _head = head; |
| 60 | + UpdateVisible(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + public void Dispose() |
| 66 | + { |
| 67 | + _launcher = null; |
| 68 | + _repo = null; |
| 69 | + _head = null; |
| 70 | + _repoFiles.Clear(); |
| 71 | + _filter = null; |
| 72 | + _visibleFiles.Clear(); |
| 73 | + _selectedFile = null; |
| 74 | + } |
| 75 | + |
| 76 | + public void ClearFilter() |
| 77 | + { |
| 78 | + Filter = string.Empty; |
| 79 | + } |
| 80 | + |
| 81 | + public void Launch() |
| 82 | + { |
| 83 | + if (!string.IsNullOrEmpty(_selectedFile)) |
| 84 | + App.ShowWindow(new Blame(_repo, _selectedFile, _head)); |
| 85 | + _launcher.CancelCommandPalette(); |
| 86 | + } |
| 87 | + |
| 88 | + private void UpdateVisible() |
| 89 | + { |
| 90 | + if (_repoFiles is { Count: > 0 }) |
| 91 | + { |
| 92 | + if (string.IsNullOrEmpty(_filter)) |
| 93 | + { |
| 94 | + VisibleFiles = _repoFiles; |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + var visible = new List<string>(); |
| 99 | + foreach (var f in _repoFiles) |
| 100 | + { |
| 101 | + if (f.Contains(_filter, StringComparison.OrdinalIgnoreCase)) |
| 102 | + visible.Add(f); |
| 103 | + } |
| 104 | + VisibleFiles = visible; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private Launcher _launcher = null; |
| 110 | + private string _repo = null; |
| 111 | + private bool _isLoading = false; |
| 112 | + private Models.Commit _head = null; |
| 113 | + private List<string> _repoFiles = null; |
| 114 | + private string _filter = string.Empty; |
| 115 | + private List<string> _visibleFiles = []; |
| 116 | + private string _selectedFile = null; |
| 117 | + } |
| 118 | +} |
0 commit comments