Added searching functionality #44 #53

This commit is contained in:
amazing-username
2019-06-14 20:10:55 -07:00
parent 1706bec261
commit 277e20ef10
10 changed files with 92 additions and 38 deletions
+18 -5
View File
@@ -18,6 +18,7 @@ namespace Mear.ViewModels
private ObservableCollection<Album> _albumItems;
private Command _refreshAlbums;
private Command _searchAlbumCommand;
private List<Album> _albums;
#endregion
@@ -52,19 +53,31 @@ namespace Mear.ViewModels
#region Methods
public async void SearchAlbums(string text)
{
if (string.IsNullOrEmpty(text))
{
_albumItems.Clear();
_albums.ToList().ForEach(_albumItems.Add);
}
else
{
var albumItems = _albums.Where(a => a.Title.Contains(text)).ToList();
_albumItems.Clear();
albumItems.ToList().ForEach(_albumItems.Add);
}
}
private async Task PopulateAlbumsAsync()
{
try
{
var albumRepo = new RemoteAlbumRepository();
var albums = albumRepo.RetrieveAlbums().OrderBy(a => a.Title).ToList();
_albums = albumRepo.RetrieveAlbums().OrderBy(a => a.Title).ToList();
_albumItems.Clear();
foreach (var album in albums)
{
_albumItems.Add(album);
}
_albums.ToList().ForEach(_albumItems.Add);
}
catch (Exception ex)
{
+17 -5
View File
@@ -18,6 +18,7 @@ namespace Mear.ViewModels
private ObservableCollection<Artist> _artistItems;
private Command _refreshArtists;
private Command _searchArtistCommand;
private List<Artist> _artists;
#endregion
@@ -52,19 +53,30 @@ namespace Mear.ViewModels
#region Methods
public async void SearchArtists(string text)
{
if (string.IsNullOrEmpty(text))
{
_artistItems.Clear();
_artists.ToList().ForEach(_artistItems.Add);
}
else
{
var artistItems = _artists.Where(a => a.Name.Contains(text)).ToList();
_artistItems.Clear();
artistItems.ToList().ForEach(_artistItems.Add);
}
}
private async Task PopulateArtistsAsync()
{
try
{
var artistRepo = new RemoteArtistRepository();
var artists = artistRepo.RetrieveArtists().OrderBy(a => a.Name).ToList();
_artists = artistRepo.RetrieveArtists().OrderBy(a => a.Name).ToList();
_artistItems.Clear();
foreach (var artist in artists)
{
_artistItems.Add(artist);
}
_artists.ToList().ForEach(_artistItems.Add);
}
catch (Exception ex)
{
+20 -10
View File
@@ -19,6 +19,7 @@ namespace Mear.ViewModels
private ObservableCollection<Song> _songItems;
private Command _refreshSongs;
private Command _searchSongsCommand;
private List<Song> _songs;
#endregion
@@ -50,7 +51,7 @@ namespace Mear.ViewModels
_refreshSongs = new Command(async () => await PopulateSongsAsync());
_searchSongsCommand = new Command(async () => await SearchSongText());
PopulateSongs();
PopulateSongsAsync();
}
#endregion
@@ -73,9 +74,22 @@ namespace Mear.ViewModels
songs = rmtSongs;
}
foreach (var song in songs)
songs.ToList().ForEach(_songItems.Add);
}
public async void FilterSongs(string text)
{
_songItems.Add(song);
if (string.IsNullOrEmpty(text))
{
_songItems.Clear();
_songs.ToList().ForEach(_songItems.Add);
}
else
{
var songItems = _songs.Where(s => s.Title.Contains(text) ||
s.Artist.Contains(text)).ToList();
_songItems.Clear();
songItems.ToList().ForEach(_songItems.Add);
}
}
@@ -107,23 +121,19 @@ namespace Mear.ViewModels
var localSongRepo = new LocalSongRepository();
var lclSongs = localSongRepo.RetrieveSongs();
var rmtSongs = songRepo.RetrieveSongs().OrderBy(s => s.Title).ToList();
var songs = new List<Song>();
if (lclSongs != null)
{
lclSongs = lclSongs.OrderBy(s => s.Title).ToList();
songs = MergeSongs(lclSongs, rmtSongs);
_songs = MergeSongs(lclSongs, rmtSongs);
}
else
{
songs = rmtSongs;
_songs = rmtSongs;
}
_songItems.Clear();
foreach (var song in songs)
{
_songItems.Add(song);
}
_songs.ToList().ForEach(_songItems.Add);
}
catch (Exception ex)
{
+2 -1
View File
@@ -16,7 +16,8 @@
Placeholder="Album"
SearchCommand="{Binding SearchAlbumCommand}"
SearchCommandParameter="{Binding SearchedText, Source=x:Reference Search}"
Text="{Binding SearchedText, Mode=TwoWay}" />
Text="{Binding SearchedText, Mode=TwoWay}"
TextChanged="SearchAlbum_TextChanged" />
<ListView x:Name="AlbumListView"
ItemsSource="{Binding AlbumItems}"
VerticalOptions="FillAndExpand"
+6
View File
@@ -39,6 +39,12 @@ namespace Mear.Views
{
var albumItem = (sender as ListView).SelectedItem = null;
}
private async void SearchAlbum_TextChanged(object sender, TextChangedEventArgs e)
{
var text = e.NewTextValue;
_viewModel.SearchAlbums(text);
}
#endregion
#endregion
}
+2 -1
View File
@@ -17,7 +17,8 @@
Placeholder="Artist"
SearchCommand="{Binding SearchArtistCommand}"
SearchCommandParameter="{Binding SearchedText, Source=x:Reference Search}"
Text="{Binding SearchedText, Mode=TwoWay}" />
Text="{Binding SearchedText, Mode=TwoWay}"
TextChanged="SearchArtist_TextChanged" />
<ListView x:Name="ArtistListView"
ItemsSource="{Binding ArtistItems}"
VerticalOptions="FillAndExpand"
+6
View File
@@ -39,6 +39,12 @@ namespace Mear.Views
{
var artistItem = (sender as ListView).SelectedItem = null;
}
private async void SearchArtist_TextChanged(object sender, TextChangedEventArgs e)
{
var text = e.NewTextValue.ToString();
_viewModel.SearchArtists(text);
}
#endregion
#endregion
}
+4 -5
View File
@@ -225,11 +225,11 @@ namespace Mear.Views
await MearPlayer.SeekTo(progVal);
}
private void Download_Clicked(object sender, EventArgs e)
private async void Download_Clicked(object sender, EventArgs e)
{
RemoveSyncToolbar();
MearPlayer.DownloadSongToFS();
await MearPlayer.DownloadSongToFS();
RemoveSyncToolbar();
ToolbarItems.Add(RemoveOption());
_song.Downloaded = true;
@@ -237,11 +237,10 @@ namespace Mear.Views
private async void Remove_Clicked(object sender, EventArgs e)
{
RemoveSyncToolbar();
MearPlayer.RemoveSongFromFS();
await MearPlayer.RemoveSongFromFS();
_song.Downloaded = false;
ToolbarItems.Add(DownloadOption());
}
private async void PlayCount_Clicked(object sender, EventArgs e)
+2 -1
View File
@@ -17,7 +17,8 @@
Placeholder="Title, Artist"
SearchCommand="{Binding SearchSongsCommand}"
SearchCommandParameter="{Binding SearchedText, Source=x:Reference Search}"
Text="{Binding SerachedText, Mode=TwoWay}">
Text="{Binding SerachedText, Mode=TwoWay}"
TextChanged="SearchSong_TextChanged" >
</SearchBar>
<ListView x:Name="SongListView"
ItemsSource="{Binding SongItems}"
+6 -1
View File
@@ -43,7 +43,6 @@ namespace Mear.Views
#region Events
private async void SongListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var i = "here";
if (SongListView.SelectedItem == null)
{
SongListView.SelectedItem = null;
@@ -72,6 +71,12 @@ namespace Mear.Views
SongListView.SelectedItem = null;
}
private void SearchSong_TextChanged(object sender, TextChangedEventArgs e)
{
var text = e.NewTextValue.ToString();
_viewModel.FilterSongs(text);
}
private async void SongOptions_Clicked(object sender, EventArgs e)
{
try