Skip to content

Commit de70b80

Browse files
committed
Fix favcat and reapply previous fixes.
1 parent c446208 commit de70b80

File tree

2 files changed

+105
-62
lines changed

2 files changed

+105
-62
lines changed

FavCat/FavCatMod.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.IO;
34
using System.Linq;
45
using System.Reflection;
@@ -33,11 +34,32 @@ internal partial class FavCatMod : MelonMod
3334
internal PlayersModule? PlayerModule;
3435

3536
internal static PageUserInfo PageUserInfo;
36-
37+
private static readonly Func<VRCUiManager> ourGetUiManager;
38+
39+
static FavCatMod()
40+
{
41+
ourGetUiManager = (Func<VRCUiManager>)Delegate.CreateDelegate(typeof(Func<VRCUiManager>), typeof(VRCUiManager)
42+
.GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly)
43+
.First(it => it.PropertyType == typeof(VRCUiManager)).GetMethod);
44+
}
45+
46+
internal static VRCUiManager GetUiManager() => ourGetUiManager();
47+
48+
private static void DoAfterUiManagerInit(Action code)
49+
{
50+
MelonCoroutines.Start(OnUiManagerInitCoro(code));
51+
}
52+
53+
private static IEnumerator OnUiManagerInitCoro(Action code)
54+
{
55+
while (GetUiManager() == null)
56+
yield return null;
57+
code();
58+
}
59+
3760
public override void OnApplicationStart()
3861
{
3962
Instance = this;
40-
if (!CheckWasSuccessful || !MustStayTrue || MustStayFalse) return;
4163

4264
Directory.CreateDirectory("./UserData/FavCatImport");
4365

FavCat/Modules/ExtendedFavoritesModuleBase.cs

Lines changed: 81 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,34 @@ public abstract class ExtendedFavoritesModuleBase<T> where T: class, INamedStore
2929
private readonly bool myHasUpdateAndCreationDates;
3030
protected readonly DatabaseFavoriteHandler<T> Favorites;
3131
private readonly ExpandedMenu myExpandedMenu;
32-
32+
internal static bool isLocalSearch = false;
3333
protected string LastSearchRequest = "";
34-
35-
private List<T>? mySearchResult;
34+
protected abstract bool FavButtonsOnLists { get; }
35+
public static List<T> mySearchResult;
3636
private StoredCategory myCurrentlySelectedCategory;
3737
private CustomPickerList myCurrentlySelectedList;
3838

3939
private UISoundCollection mySoundCollection;
40+
private static ScrollRect? _AvatarPageScrollRect;
41+
protected internal abstract void RefreshFavButtons();
42+
43+
protected abstract void OnFavButtonClicked(StoredCategory storedCategory);
44+
private static ScrollRect? AvatarPageScrollRect
45+
{
4046

47+
get
48+
{
49+
if (_AvatarPageScrollRect == null)
50+
{
51+
var path = GameObject.Find("UserInterface/MenuContent/Screens/Avatar/");
52+
if (path != null)
53+
{
54+
return _AvatarPageScrollRect = path.GetComponent<ScrollRect>();
55+
}
56+
}
57+
return _AvatarPageScrollRect;
58+
}
59+
}
4160
protected void PlaySound()
4261
{
4362
if (!FavCatSettings.MakeClickSounds.Value) return;
@@ -53,43 +72,32 @@ protected void PlaySound()
5372
protected abstract IPickerElement WrapModel(StoredFavorite? favorite, T model);
5473
protected abstract void SearchButtonClicked();
5574

56-
protected bool CanPerformAdditiveActions { get; }
57-
protected bool CanShowExistingLists { get; }
5875

59-
public virtual void ShowAnnoyingMessage()
60-
{
61-
}
6276

6377

64-
protected ExtendedFavoritesModuleBase(ExpandedMenu expandedMenu, DatabaseFavoriteHandler<T> favoriteHandler, Transform listsParent, bool canPerformAdditiveActions, bool canShowExistingLists, bool hasUpdateAndCreationDates = true)
78+
protected ExtendedFavoritesModuleBase(ExpandedMenu expandedMenu, DatabaseFavoriteHandler<T> favoriteHandler, Transform listsParent, bool hasUpdateAndCreationDates = true)
6579
{
6680
myExpandedMenu = expandedMenu;
6781
Favorites = favoriteHandler;
68-
CanPerformAdditiveActions = canPerformAdditiveActions;
69-
CanShowExistingLists = canShowExistingLists;
70-
7182
ExpansionKitApi.GetExpandedMenu(myExpandedMenu).AddSimpleButton("Local Search", SearchButtonClicked);
72-
if (CanPerformAdditiveActions) ExpansionKitApi.GetExpandedMenu(myExpandedMenu).AddSimpleButton("New Category", CreateCategory);
83+
ExpansionKitApi.GetExpandedMenu(myExpandedMenu).AddSimpleButton("New Category", CreateCategory);
7384
ExpansionKitApi.GetExpandedMenu(myExpandedMenu).AddSimpleButton("More FavCat...", ShowExtraOptionsMenu);
7485

7586
this.listsParent = listsParent;
7687
myHasUpdateAndCreationDates = hasUpdateAndCreationDates;
7788

78-
if (CanShowExistingLists)
89+
var knownCategories = Favorites.GetCategories().ToList();
90+
if (knownCategories.Count == 0)
7991
{
80-
var knownCategories = Favorites.GetCategories().ToList();
81-
if (knownCategories.Count == 0)
82-
{
83-
var newCategory = new StoredCategory {CategoryName = "Local Favorites", SortType = "!added"};
84-
Favorites.UpdateCategory(newCategory);
85-
CreateList(newCategory);
86-
}
87-
else
88-
foreach (var categoryName in knownCategories)
89-
if (categoryName.CategoryName != SearchCategoryName)
90-
CreateList(categoryName);
92+
var newCategory = new StoredCategory { CategoryName = "Local Favorites", SortType = "!added" };
93+
Favorites.UpdateCategory(newCategory);
94+
CreateList(newCategory);
9195
}
92-
96+
else
97+
foreach (var categoryName in knownCategories)
98+
if (categoryName.CategoryName != SearchCategoryName)
99+
CreateList(categoryName);
100+
93101
var searchCategory = Favorites.GetCategory(SearchCategoryName) ??
94102
new StoredCategory {CategoryName = SearchCategoryName, SortType = hasUpdateAndCreationDates ? "!updated" : "name"};
95103
Favorites.UpdateCategory(searchCategory);
@@ -130,7 +138,7 @@ protected ExtendedFavoritesModuleBase(ExpandedMenu expandedMenu, DatabaseFavorit
130138
SearchList.SetList(Enumerable.Empty<IPickerElement>(), true);
131139
SearchList.SetVisibleRows(searchCategory.VisibleRows);
132140
SearchList.OnModelClick += OnPickerSelected;
133-
SearchList.transform.SetSiblingIndex(this.listsParent.transform.childCount);
141+
SearchList.transform.SetAsFirstSibling();
134142

135143
// assign these to something random by default -
136144
myCurrentlySelectedCategory = searchCategory;
@@ -208,12 +216,6 @@ private IEnumerator ReorderListsAfterDelay()
208216

209217
private void CreateCategory()
210218
{
211-
if (!CanPerformAdditiveActions)
212-
{
213-
ShowAnnoyingMessage();
214-
return;
215-
}
216-
217219
BuiltinUiUtils.ShowInputPopup("Enter category name", "", InputField.InputType.Standard, false, "Create",
218220
(s, _, __) =>
219221
{
@@ -285,8 +287,25 @@ internal void ReorderLists()
285287
foreach (var listToHideName in storedOrderFull.DefaultListsToHide)
286288
if (knownLists.TryGetValue(listToHideName ?? "", out var listToHide) && !listToHide.IsCustom)
287289
listToHide.ListTransform.gameObject.SetActive(false);
290+
291+
292+
if (AvatarPageScrollRect != null)
293+
{
294+
AvatarPageScrollRect.SetVerticalNormalizedPosition(2);
295+
}
296+
if (!isLocalSearch)
297+
{
298+
if (SearchList != null)
299+
{
300+
if (SearchList.transform != null)
301+
{
302+
SearchList.transform.SetSiblingIndex2(0);
303+
}
304+
}
305+
}
288306
}
289307

308+
290309
protected void ShowListSettingsMenu(StoredCategory category)
291310
{
292311
var listSettingsMenu = ExpansionKitApi.CreateCustomFullMenuPopup(LayoutDescription.QuickMenu3Columns);
@@ -300,10 +319,7 @@ protected void ShowListSettingsMenu(StoredCategory category)
300319
listSettingsMenu.AddSpacer();
301320

302321
if (category.CategoryName == SearchCategoryName)
303-
if (CanPerformAdditiveActions)
304-
listSettingsMenu.AddSimpleButton("Save as new category", () => SaveSearchAsCategory());
305-
else
306-
listSettingsMenu.AddSpacer();
322+
listSettingsMenu.AddSimpleButton("Save as new category", () => SaveSearchAsCategory());
307323
else
308324
listSettingsMenu.AddSimpleButton("Delete", DeleteSelectedList);
309325

@@ -419,7 +435,8 @@ internal void CreateList(StoredCategory storedCategory)
419435
list.SetVisibleRows(storedCategory.VisibleRows);
420436
list.OnModelClick += OnPickerSelected;
421437
list.Category = storedCategory;
422-
list.SetFavButtonVisible(false);
438+
list.OnFavClick += () => OnFavButtonClicked(storedCategory);
439+
list.SetFavButtonVisible(FavButtonsOnLists);
423440
list.OnSettingsClick += () =>
424441
{
425442
myCurrentlySelectedList = list;
@@ -571,7 +588,7 @@ protected List<StoredCategory> GetCategoriesInSortedOrder()
571588
return categories;
572589
}
573590

574-
protected void AcceptSearchResult(IEnumerable<T> result)
591+
public static void AcceptSearchResult(IEnumerable<T> result)
575592
{
576593
mySearchResult = result.ToList();
577594
}
@@ -586,18 +603,25 @@ protected void ProcessSearchResults()
586603

587604
SortModelList(SearchList.Category.SortType, SearchCategoryName, results);
588605
SearchList.SetList(results.Select(it => WrapModel(null, it.it)), true);
589-
590-
SetSearchListHeaderAndScrollToIt($"Search results ({LastSearchRequest})");
606+
607+
SetSearchListHeader($"Search results ({LastSearchRequest})");
608+
ScrollToIt(true);
591609
}
592610

593-
protected void SetSearchListHeaderAndScrollToIt(string text)
611+
public void SetSearchListHeader(string text, bool Scroll = true)
594612
{
595613
SearchList.HeaderString = text;
596-
597-
SearchList.SetVisibleRows(4);
598-
MelonCoroutines.Start(ScrollDownAfterDelay());
599614
}
600615

616+
public void ScrollToIt(bool Scroll = true)
617+
{
618+
if (Scroll && listsParent?.GetComponentInParent<ScrollRect>() != null)
619+
{
620+
SearchList.SetVisibleRows(4);
621+
MelonCoroutines.Start(ScrollDownAfterDelay());
622+
}
623+
624+
}
601625
protected IEnumerator ScrollDownAfterDelay()
602626
{
603627
yield return new WaitForSeconds(0.5f);
@@ -616,35 +640,32 @@ private void UpdateSelectedListSort(string sort, IShowableMenu menuToHide)
616640
private void ShowExtraOptionsMenu()
617641
{
618642
var customMenu = ExpansionKitApi.CreateCustomFullMenuPopup(LayoutDescription.WideSlimList);
619-
643+
620644
customMenu.AddSimpleButton("Hide default lists...", () =>
621645
{
622646
customMenu.Hide();
623647
ShowListHideMenu();
624648
});
625-
649+
626650
customMenu.AddSpacer();
627-
651+
628652
if (ImportFolderProcessor.ImportRunning)
629653
customMenu.AddLabel(ImportFolderProcessor.ImportStatusOuter + "\n" + ImportFolderProcessor.ImportStatusInner);
630654
else
631-
customMenu.AddSimpleButton(CanPerformAdditiveActions ? "Import databases and text files" : "Import databases", () =>
655+
customMenu.AddSimpleButton("Import databases and text files", () =>
632656
{
633657
customMenu.Hide();
634658
ImportFolderProcessor.ProcessImportsFolder().NoAwait();
635659
});
636-
637-
if (Favorites.EntityType == DatabaseEntity.Avatar)
638-
customMenu.AddSimpleButton("Show avatar favorites deprecation message", ShowAnnoyingMessage);
639-
else
640-
customMenu.AddSpacer();
641-
660+
661+
customMenu.AddSpacer();
662+
642663
customMenu.AddSimpleButton("Open documentation in browser", () =>
643664
{
644665
customMenu.Hide();
645-
Process.Start("https://github.com/knah/VRCMods#favcat");
666+
Process.Start("https://github.com/xAstroBoy/VRCMods-Unchained#favcat");
646667
});
647-
668+
648669
if (ReFetchFavoritesProcessor.ImportRunning)
649670
customMenu.AddLabel(ReFetchFavoritesProcessor.ImportStatusOuter + " " + ReFetchFavoritesProcessor.ImportStatusInner);
650671
else
@@ -653,7 +674,7 @@ private void ShowExtraOptionsMenu()
653674
customMenu.Hide();
654675
ReFetchFavoritesProcessor.ReFetchFavorites().NoAwait();
655676
});
656-
677+
657678
if (ExportProcessor.IsExportingFavorites)
658679
customMenu.AddLabel($"Exporting: {ExportProcessor.ProcessedCategories} / {ExportProcessor.TotalCategories}");
659680
else
@@ -663,9 +684,9 @@ private void ShowExtraOptionsMenu()
663684
customMenu.Hide();
664685
ExportProcessor.DoExportFavorites(Favorites).NoAwait();
665686
});
666-
687+
667688
customMenu.AddSimpleButton("Close", customMenu.Hide);
668-
689+
669690
customMenu.Show();
670691
}
671692

0 commit comments

Comments
 (0)