diff --git a/src/BSLManager/App.cs b/src/BSLManager/App.cs index 12b2970..39c1f33 100644 --- a/src/BSLManager/App.cs +++ b/src/BSLManager/App.cs @@ -5,6 +5,8 @@ using System.Threading.Tasks; using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Models; using BirdsiteLive.Moderation.Actions; +using BSLManager.Domain; +using BSLManager.Tools; using Terminal.Gui; namespace BSLManager @@ -14,8 +16,7 @@ namespace BSLManager private readonly IFollowersDal _followersDal; private readonly IRemoveFollowerAction _removeFollowerAction; - private List _displayableUserList = new List(); - private List _sourceUserList = new List(); + private readonly FollowersListState _state = new FollowersListState(); #region Ctor public App(IFollowersDal followersDal, IRemoveFollowerAction removeFollowerAction) @@ -69,10 +70,10 @@ namespace BSLManager RetrieveUserList(); - var list = new ListView(_displayableUserList) + var list = new ListView(_state.GetDisplayableList()) { X = 1, - Y = 2, + Y = 3, Width = Dim.Fill(), Height = Dim.Fill() }; @@ -96,7 +97,8 @@ namespace BSLManager var dialog = new Dialog("Delete", 60, 18, cancel, ok); - var name = new Label($"User: {_displayableUserList[el]}") + var follower = _state.GetElementAt(el); + var name = new Label($"User: @{follower.Acct}@{follower.Host}") { X = 1, Y = 1, @@ -121,9 +123,29 @@ namespace BSLManager } }; - // Add some controls, + var listingFollowersLabel = new Label(1, 0, "Listing followers"); + var filterLabel = new Label("Filter: ") { X = 1, Y = 1 }; + var filterText = new TextField("") + { + X = Pos.Right(filterLabel), + Y = 1, + Width = 40 + }; + + filterText.KeyDown += _ => + { + var text = filterText.Text.ToString(); + if (_.KeyEvent.Key == Key.Enter && !string.IsNullOrWhiteSpace(text)) + { + _state.FilterBy(text); + ConsoleGui.RefreshUI(); + } + }; + win.Add( - new Label(1, 0, "Listing followers"), + listingFollowersLabel, + filterLabel, + filterText, list ); @@ -134,13 +156,11 @@ namespace BSLManager { Application.MainLoop.Invoke(async () => { - var userToDelete = _sourceUserList[el]; - + var userToDelete = _state.GetElementAt(el); await _removeFollowerAction.ProcessAsync(userToDelete); + _state.RemoveAt(el); - _sourceUserList.RemoveAt(el); - _displayableUserList.RemoveAt(el); - RefreshUI(); + ConsoleGui.RefreshUI(); }); } @@ -149,22 +169,9 @@ namespace BSLManager Application.MainLoop.Invoke(async () => { var followers = await _followersDal.GetAllFollowersAsync(); - _sourceUserList = followers.OrderByDescending(x => x.Followings.Count).ToList(); - - _displayableUserList.Clear(); - foreach (var follower in _sourceUserList) - { - _displayableUserList.Add($"@{follower.Acct}@{follower.Host} {follower.Followings.Count}"); - } - - RefreshUI(); + _state.Load(followers.ToList()); + ConsoleGui.RefreshUI(); }); } - - private void RefreshUI() - { - typeof(Application).GetMethod("TerminalResized", BindingFlags.Static | BindingFlags.NonPublic) - .Invoke(null, null); - } } } \ No newline at end of file diff --git a/src/BSLManager/Domain/FollowersListState.cs b/src/BSLManager/Domain/FollowersListState.cs new file mode 100644 index 0000000..15b9b2a --- /dev/null +++ b/src/BSLManager/Domain/FollowersListState.cs @@ -0,0 +1,82 @@ +using System.Collections.Generic; +using System.Linq; +using BirdsiteLive.DAL.Models; + +namespace BSLManager.Domain +{ + public class FollowersListState + { + private List _displayableUserList = new List(); + private List _sourceUserList = new List(); + + private List _filteredDisplayableUserList = new List(); + private List _filteredSourceUserList = new List(); + + public void Load(List followers) + { + _sourceUserList = followers.OrderByDescending(x => x.Followings.Count).ToList(); + + ResetLists(); + } + + private void ResetLists() + { + _filteredSourceUserList = _sourceUserList.ToList(); + + _displayableUserList.Clear(); + _filteredDisplayableUserList.Clear(); + + foreach (var follower in _sourceUserList) + { + var displayedUser = $"{GetFullHandle(follower)} ({follower.Followings.Count})"; + _displayableUserList.Add(displayedUser); + _filteredDisplayableUserList.Add(displayedUser); + } + } + + public List GetDisplayableList() + { + return _filteredDisplayableUserList; + } + + public void FilterBy(string pattern) + { + ResetLists(); + + if (!string.IsNullOrWhiteSpace(pattern)) + { + var elToRemove = _filteredSourceUserList + .Where(x => !GetFullHandle(x).Contains(pattern)) + .Select(x => x) + .ToList(); + + foreach (var el in elToRemove) + { + _filteredSourceUserList.Remove(el); + + var dElToRemove = _filteredDisplayableUserList.First(x => x.Contains(GetFullHandle(el))); + _filteredDisplayableUserList.Remove(dElToRemove); + } + } + } + + private string GetFullHandle(Follower follower) + { + return $"@{follower.Acct}@{follower.Host}"; + } + + public void RemoveAt(int index) + { + var displayableUser = _filteredDisplayableUserList[index]; + var sourceUser = _filteredSourceUserList[index]; + + _displayableUserList.Remove(displayableUser); + _sourceUserList.Remove(sourceUser); + } + + public Follower GetElementAt(int index) + { + return _filteredSourceUserList[index]; + } + } +} \ No newline at end of file diff --git a/src/BSLManager/Tools/ConsoleGui.cs b/src/BSLManager/Tools/ConsoleGui.cs new file mode 100644 index 0000000..b6f7b6e --- /dev/null +++ b/src/BSLManager/Tools/ConsoleGui.cs @@ -0,0 +1,15 @@ +using System.Reflection; +using Terminal.Gui; + +namespace BSLManager.Tools +{ + public static class ConsoleGui + { + public static void RefreshUI() + { + typeof(Application) + .GetMethod("TerminalResized", BindingFlags.Static | BindingFlags.NonPublic) + .Invoke(null, null); + } + } +} \ No newline at end of file