Skip to content

Commit

Permalink
Added user filtering for playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
rithik-b committed Mar 21, 2022
1 parent 0ea26a0 commit edf8333
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 37 deletions.
23 changes: 20 additions & 3 deletions MorePlaylists/BeatSaver/BeatSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Sprite Logo
}
}

public SearchTextPlaylistFilterOptions? CurrentFilters => filtersViewController.filterOptions;
public BeatSaverFilterModel CurrentFilters => filtersViewController.filterOptions;
public IListViewController ListViewController => listViewController;
public IDetailViewController DetailViewController { get; }

Expand Down Expand Up @@ -75,7 +75,24 @@ public void Dispose()
{
if (refreshRequested || page == null)
{
page = await beatSaverInstance.SearchPlaylists(CurrentFilters, token: token);
switch (CurrentFilters.FilterMode)
{
case FilterMode.Search:
page = await beatSaverInstance.SearchPlaylists(CurrentFilters.NullableSearchFilter, token: token);
break;

case FilterMode.User:
page = null;
if (CurrentFilters.UserName != null)
{
var user = await beatSaverInstance.User(CurrentFilters.UserName, token);
if (user != null)
{
page = await beatSaverInstance.UserPlaylists(user.ID, token: token);
}
}
break;
}
ExhaustedPlaylists = false;
}
else
Expand Down Expand Up @@ -107,7 +124,7 @@ private void RequestFilterView() =>

private void ClearFilters() => filtersViewController.ClearFilters();

private void OnFiltersSet(SearchTextPlaylistFilterOptions? filterOptions)
private void OnFiltersSet(BeatSaverFilterModel filterOptions)
{
listViewController.SetActiveFilter(filterOptions);
RequestFilterViewDismiss();
Expand Down
24 changes: 24 additions & 0 deletions MorePlaylists/BeatSaver/BeatSaverFilterModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using BeatSaverSharp;

namespace MorePlaylists.BeatSaver;

internal class BeatSaverFilterModel
{
public SearchTextPlaylistFilterOptions? NullableSearchFilter { get; private set; }
public SearchTextPlaylistFilterOptions SearchFilter => NullableSearchFilter ??= new SearchTextPlaylistFilterOptions();
public string? UserName { get; set; }
public FilterMode FilterMode { get; set; } = FilterMode.Search;

public void ClearFilters()
{
NullableSearchFilter = null;
UserName = null;
FilterMode = FilterMode.Search;
}
}

internal enum FilterMode
{
Search,
User
}
21 changes: 9 additions & 12 deletions MorePlaylists/BeatSaver/BeatSaverFiltersViewController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using BeatSaberMarkupLanguage.Attributes;
using BeatSaberMarkupLanguage.ViewControllers;
using BeatSaverSharp;
using HMUI;
using MorePlaylists.Utilities;
using UnityEngine;
Expand All @@ -11,17 +10,15 @@ namespace MorePlaylists.BeatSaver;

[HotReload(RelativePathToLayout = @".\BeatSaverFiltersView.bsml")]
[ViewDefinition("MorePlaylists.BeatSaver.BeatSaverFiltersView.bsml")]
public class BeatSaverFiltersViewController : BSMLAutomaticViewController
internal class BeatSaverFiltersViewController : BSMLAutomaticViewController
{
[Inject]
private readonly InputFieldGrabber inputFieldGrabber = null!;

private InputFieldView? inputFieldView;

public SearchTextPlaylistFilterOptions? filterOptions { get; private set; }
private SearchTextPlaylistFilterOptions FilterOptions => filterOptions ??= new SearchTextPlaylistFilterOptions();

public event Action<SearchTextPlaylistFilterOptions?>? FiltersSet;
public readonly BeatSaverFilterModel filterOptions = new();

public event Action<BeatSaverFilterModel>? FiltersSet;
public event Action? RequestDismiss;

[UIComponent("vertical")]
Expand All @@ -45,20 +42,20 @@ private void PostParse()
[UIAction("ok-click")]
private void OkClicked()
{
FilterOptions.IncludeEmpty = IncludeEmpty;
FilterOptions.IsCurated = CuratedOnly;
filterOptions.SearchFilter.IncludeEmpty = IncludeEmpty;
filterOptions.SearchFilter.IsCurated = CuratedOnly;

if (inputFieldView != null)
{
FilterOptions.Query = inputFieldView.text;
filterOptions.SearchFilter.Query = inputFieldView.text;
}

FiltersSet?.Invoke(FilterOptions);
FiltersSet?.Invoke(filterOptions);
}

public void ClearFilters()
{
filterOptions = null;
filterOptions.ClearFilters();
IncludeEmpty = false;
CuratedOnly = false;

Expand Down
54 changes: 32 additions & 22 deletions MorePlaylists/BeatSaver/BeatSaverListViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,36 +317,46 @@ private void ClearFilters()
}
}

public void SetActiveFilter(SearchTextPlaylistFilterOptions? filterOptions)
public void SetActiveFilter(BeatSaverFilterModel filterOptions)
{
if (filterOptions == null)
switch (filterOptions.FilterMode)
{
FilterString = string.Empty;
return;
}
case FilterMode.Search:
if (filterOptions.NullableSearchFilter == null)
{
FilterString = string.Empty;
return;
}

var sb = new StringBuilder();
var sb = new StringBuilder();

if (!string.IsNullOrWhiteSpace(filterOptions.Query))
{
sb.Append(filterOptions.Query + ", ");
}
if (!string.IsNullOrWhiteSpace(filterOptions.SearchFilter.Query))
{
sb.Append(filterOptions.SearchFilter.Query + ", ");
}

if (filterOptions.IncludeEmpty)
{
sb.Append("IncludeEmpty, ");
}
if (filterOptions.SearchFilter.IncludeEmpty)
{
sb.Append("IncludeEmpty, ");
}

if (filterOptions.IsCurated)
{
sb.Append("CuratedOnly, ");
}
if (filterOptions.SearchFilter.IsCurated)
{
sb.Append("CuratedOnly, ");
}

if (sb.Length >= 2)
{
sb.Remove(sb.Length - 2, 2);
if (sb.Length >= 2)
{
sb.Remove(sb.Length - 2, 2);
}

FilterString = sb.ToString();
break;

case FilterMode.User:
FilterString = $"user:{filterOptions.UserName}";
break;
}
FilterString = sb.ToString();
}

private string filterString = string.Empty;
Expand Down

0 comments on commit edf8333

Please sign in to comment.