Skip to content

Commit

Permalink
Added navigation buttons "Next section" and "Random song" (#385)
Browse files Browse the repository at this point in the history
* * Added navigation button (Blue) "Next section" that selects the next letter with respect to the current song.

* Navigation button (Orange) is added to select a random song.

* Changes made based on conversation [#385]
  • Loading branch information
SamuelPalma authored May 28, 2023
1 parent 1ac6073 commit cca8bbb
Showing 1 changed file with 81 additions and 8 deletions.
89 changes: 81 additions & 8 deletions Assets/Script/UI/MusicLibrary/SongSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ private set {
private float _scrollTimer = 0f;
private bool searchBoxShouldBeEnabled = false;

private List<char> songsFirstLetter;

private void Awake() {
refreshFlag = true;
Instance = this;
Expand Down Expand Up @@ -113,9 +115,14 @@ private void OnEnable() {
Back();
}),
new NavigationScheme.Entry(MenuAction.Shortcut1, "Search Artist", () => {
if (_songs[SelectedIndex] is SongViewType view) {
searchField.text = $"artist:{view.SongEntry.Artist}";
}
SearchByArtist();
}),
new NavigationScheme.Entry(MenuAction.Shortcut2, "Random song", () => {
ClearSearchBox();
SelectRandomSong();
}),
new NavigationScheme.Entry(MenuAction.Shortcut3, "Next section", () => {
SelectNextSection();
})
}, false));

Expand Down Expand Up @@ -221,11 +228,7 @@ public void UpdateSearch() {
"RANDOM SONG",
"Icon/Random",
() => {
// Get how many non-song things there are
int skip = _songs.Count - SongContainer.Songs.Count;

// Select random between all of the songs
SelectedIndex = Random.Range(skip, SongContainer.Songs.Count);
SelectRandomSong();
}
));
} else {
Expand Down Expand Up @@ -334,10 +337,23 @@ public void UpdateSearch() {
SelectedIndex = Mathf.Max(1, index);
}

SetFirstLetters();
UpdateSongViews();
UpdateScrollbar();
}

private void SetFirstLetters(){
songsFirstLetter =
_songs
.OfType<SongViewType>()
.Select(song => song.SongEntry.NameNoParenthesis)
.Where(name => !string.IsNullOrEmpty(name))
.Select(name => Char.ToUpper(name[0]))
.Distinct()
.OrderBy(ch => ch)
.ToList();
}

private static string RemoveDiacritics(string text) {
if (text == null) {
return null;
Expand Down Expand Up @@ -459,5 +475,62 @@ private void ClearSearchBox() {
searchField.text = "";
searchField.ActivateInputField();
}

private void SelectRandomSong(){
// Get how many non-song things there are
int skip = _songs.Count - SongContainer.Songs.Count;
// Select random between all of the songs
SelectedIndex = Random.Range(skip, SongContainer.Songs.Count);
}

private void SearchByArtist(){
if (_songs[SelectedIndex] is SongViewType view) {
searchField.text = $"artist:{view.SongEntry.Artist}";
}
}

private void SelectNextSection(){
if (_songs[_selectedIndex] is not SongViewType song) {
return;
}

int skip = Mathf.Max(1, _songs.Count - SongContainer.Songs.Count);
var nameWithoutParenthesis = song.SongEntry.NameNoParenthesis;
string nextCharacter = GetNextLetterOrNumber(nameWithoutParenthesis);

// If an error occurs no change is made
if(string.IsNullOrEmpty(nextCharacter)){
return;
}

var index = _songs.FindIndex(skip, song =>
song is SongViewType songType &&
songType.SongEntry.NameNoParenthesis.Substring(0, 1) == nextCharacter
);

SelectedIndex = index;
}

private string GetNextLetterOrNumber(string input){
if(string.IsNullOrEmpty(input)){
return null;
}

char firstCharacter = Char.ToUpper(input[0]);

int indexOfActualLetter = songsFirstLetter.FindIndex(letter => {
return letter == firstCharacter;
});

bool isLast = indexOfActualLetter == (songsFirstLetter.Count - 1);

if(isLast){
var firstCharacterInList = Char.ToString(songsFirstLetter[0]);
return firstCharacterInList;
}

var nextCharacter = Char.ToString(songsFirstLetter[indexOfActualLetter + 1]);
return nextCharacter;
}
}
}

0 comments on commit cca8bbb

Please sign in to comment.