added user filtering

This commit is contained in:
Nicolas Constant 2021-04-11 19:46:51 -04:00
parent 25221c33e0
commit 809a6b605f
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
3 changed files with 131 additions and 27 deletions

View file

@ -5,6 +5,8 @@ using System.Threading.Tasks;
using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Models; using BirdsiteLive.DAL.Models;
using BirdsiteLive.Moderation.Actions; using BirdsiteLive.Moderation.Actions;
using BSLManager.Domain;
using BSLManager.Tools;
using Terminal.Gui; using Terminal.Gui;
namespace BSLManager namespace BSLManager
@ -14,8 +16,7 @@ namespace BSLManager
private readonly IFollowersDal _followersDal; private readonly IFollowersDal _followersDal;
private readonly IRemoveFollowerAction _removeFollowerAction; private readonly IRemoveFollowerAction _removeFollowerAction;
private List<string> _displayableUserList = new List<string>(); private readonly FollowersListState _state = new FollowersListState();
private List<Follower> _sourceUserList = new List<Follower>();
#region Ctor #region Ctor
public App(IFollowersDal followersDal, IRemoveFollowerAction removeFollowerAction) public App(IFollowersDal followersDal, IRemoveFollowerAction removeFollowerAction)
@ -69,10 +70,10 @@ namespace BSLManager
RetrieveUserList(); RetrieveUserList();
var list = new ListView(_displayableUserList) var list = new ListView(_state.GetDisplayableList())
{ {
X = 1, X = 1,
Y = 2, Y = 3,
Width = Dim.Fill(), Width = Dim.Fill(),
Height = Dim.Fill() Height = Dim.Fill()
}; };
@ -96,7 +97,8 @@ namespace BSLManager
var dialog = new Dialog("Delete", 60, 18, cancel, ok); 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, X = 1,
Y = 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( win.Add(
new Label(1, 0, "Listing followers"), listingFollowersLabel,
filterLabel,
filterText,
list list
); );
@ -134,13 +156,11 @@ namespace BSLManager
{ {
Application.MainLoop.Invoke(async () => Application.MainLoop.Invoke(async () =>
{ {
var userToDelete = _sourceUserList[el]; var userToDelete = _state.GetElementAt(el);
await _removeFollowerAction.ProcessAsync(userToDelete); await _removeFollowerAction.ProcessAsync(userToDelete);
_state.RemoveAt(el);
_sourceUserList.RemoveAt(el); ConsoleGui.RefreshUI();
_displayableUserList.RemoveAt(el);
RefreshUI();
}); });
} }
@ -149,22 +169,9 @@ namespace BSLManager
Application.MainLoop.Invoke(async () => Application.MainLoop.Invoke(async () =>
{ {
var followers = await _followersDal.GetAllFollowersAsync(); var followers = await _followersDal.GetAllFollowersAsync();
_sourceUserList = followers.OrderByDescending(x => x.Followings.Count).ToList(); _state.Load(followers.ToList());
ConsoleGui.RefreshUI();
_displayableUserList.Clear();
foreach (var follower in _sourceUserList)
{
_displayableUserList.Add($"@{follower.Acct}@{follower.Host} {follower.Followings.Count}");
}
RefreshUI();
}); });
} }
private void RefreshUI()
{
typeof(Application).GetMethod("TerminalResized", BindingFlags.Static | BindingFlags.NonPublic)
.Invoke(null, null);
}
} }
} }

View file

@ -0,0 +1,82 @@
using System.Collections.Generic;
using System.Linq;
using BirdsiteLive.DAL.Models;
namespace BSLManager.Domain
{
public class FollowersListState
{
private List<string> _displayableUserList = new List<string>();
private List<Follower> _sourceUserList = new List<Follower>();
private List<string> _filteredDisplayableUserList = new List<string>();
private List<Follower> _filteredSourceUserList = new List<Follower>();
public void Load(List<Follower> 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<string> 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];
}
}
}

View file

@ -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);
}
}
}