Add SearchBar feature
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
e447943261
commit
4321756733
@ -0,0 +1,79 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||
xmlns:local="clr-namespace:Linaris"
|
||||
xmlns:model="clr-namespace:Model;assembly=Model"
|
||||
x:Class="Linaris.SearchBarView">
|
||||
|
||||
<VerticalStackLayout>
|
||||
<SearchBar Style="{StaticResource SearchBar}" TextChanged="SearchBar_TextChanged" Text=""/>
|
||||
<VerticalStackLayout Margin="40,-10,40,10">
|
||||
|
||||
<FlexLayout Direction="Column" BindableLayout.ItemsSource="{Binding Albums}">
|
||||
<BindableLayout.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Frame Padding="10" BackgroundColor="white" BorderColor="black" HeightRequest="40">
|
||||
<Label Text="{Binding Name}" TextColor="black"/>
|
||||
<Frame.GestureRecognizers>
|
||||
<TapGestureRecognizer Tapped="SearchResult_Tapped" />
|
||||
</Frame.GestureRecognizers>
|
||||
</Frame>
|
||||
</DataTemplate>
|
||||
</BindableLayout.ItemTemplate>
|
||||
</FlexLayout>
|
||||
|
||||
<FlexLayout Direction="Column" BindableLayout.ItemsSource="{Binding Artists}">
|
||||
<BindableLayout.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Frame Padding="10" BackgroundColor="white" BorderColor="black" HeightRequest="40">
|
||||
<Label Text="{Binding Name}" TextColor="black"/>
|
||||
<Frame.GestureRecognizers>
|
||||
<TapGestureRecognizer Tapped="SearchResult_Tapped" />
|
||||
</Frame.GestureRecognizers>
|
||||
</Frame>
|
||||
</DataTemplate>
|
||||
</BindableLayout.ItemTemplate>
|
||||
</FlexLayout>
|
||||
|
||||
<FlexLayout Direction="Column" BindableLayout.ItemsSource="{Binding InfoTitles}">
|
||||
<BindableLayout.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Frame Padding="10" BackgroundColor="white" BorderColor="black" HeightRequest="40">
|
||||
<Label Text="{Binding Name}" TextColor="black"/>
|
||||
<Frame.GestureRecognizers>
|
||||
<TapGestureRecognizer Tapped="SearchResult_Tapped" />
|
||||
</Frame.GestureRecognizers>
|
||||
</Frame>
|
||||
</DataTemplate>
|
||||
</BindableLayout.ItemTemplate>
|
||||
</FlexLayout>
|
||||
|
||||
<FlexLayout Direction="Column" BindableLayout.ItemsSource="{Binding CustomTitles}">
|
||||
<BindableLayout.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Frame Padding="10" BackgroundColor="white" BorderColor="black" HeightRequest="40">
|
||||
<Label Text="{Binding Name}" TextColor="black"/>
|
||||
<Frame.GestureRecognizers>
|
||||
<TapGestureRecognizer Tapped="SearchResult_Tapped" />
|
||||
</Frame.GestureRecognizers>
|
||||
</Frame>
|
||||
</DataTemplate>
|
||||
</BindableLayout.ItemTemplate>
|
||||
</FlexLayout>
|
||||
|
||||
<FlexLayout Direction="Column" BindableLayout.ItemsSource="{Binding Playlists}">
|
||||
<BindableLayout.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Frame Padding="10" BackgroundColor="white" BorderColor="black" HeightRequest="40">
|
||||
<Label Text="{Binding Name}" TextColor="black"/>
|
||||
<Frame.GestureRecognizers>
|
||||
<TapGestureRecognizer Tapped="SearchResult_Tapped" />
|
||||
</Frame.GestureRecognizers>
|
||||
</Frame>
|
||||
</DataTemplate>
|
||||
</BindableLayout.ItemTemplate>
|
||||
</FlexLayout>
|
||||
|
||||
</VerticalStackLayout>
|
||||
</VerticalStackLayout>
|
||||
</ContentView>
|
@ -0,0 +1,127 @@
|
||||
using Microsoft.Maui.Controls;
|
||||
using Model;
|
||||
using Model.Stub;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Linaris;
|
||||
|
||||
public partial class SearchBarView : ContentView
|
||||
{
|
||||
Manager Manager { get; set; } = (Application.Current as App).Manager;
|
||||
|
||||
private ObservableCollection<Album> albums = new ObservableCollection<Album>();
|
||||
|
||||
public ObservableCollection<Album> Albums
|
||||
{
|
||||
get => albums;
|
||||
set
|
||||
{
|
||||
albums = value;
|
||||
}
|
||||
}
|
||||
|
||||
private ObservableCollection<InfoTitle> infoTitles = new ObservableCollection<InfoTitle>();
|
||||
|
||||
public ObservableCollection<InfoTitle> InfoTitles
|
||||
{
|
||||
get => infoTitles;
|
||||
set
|
||||
{
|
||||
infoTitles = value;
|
||||
}
|
||||
}
|
||||
|
||||
private ObservableCollection<Playlist> playlists = new ObservableCollection<Playlist>();
|
||||
|
||||
public ObservableCollection<Playlist> Playlists
|
||||
{
|
||||
get => playlists;
|
||||
set
|
||||
{
|
||||
playlists = value;
|
||||
}
|
||||
}
|
||||
|
||||
public SearchBarView()
|
||||
{
|
||||
InitializeComponent();
|
||||
BindingContext = this;
|
||||
}
|
||||
|
||||
public void SearchBar_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
string filterText = ((SearchBar)sender).Text;
|
||||
if (filterText.Length == 0)
|
||||
{
|
||||
ResetCollections();
|
||||
return;
|
||||
}
|
||||
SearchAll(filterText);
|
||||
}
|
||||
|
||||
public void ResetCollections()
|
||||
{
|
||||
Albums.Clear();
|
||||
InfoTitles.Clear();
|
||||
Playlists.Clear();
|
||||
}
|
||||
|
||||
public void SearchAll(string filterText)
|
||||
{
|
||||
Albums.Clear();
|
||||
foreach (var album in SearchAlbums(filterText))
|
||||
{
|
||||
Albums.Add(album);
|
||||
}
|
||||
|
||||
InfoTitles.Clear();
|
||||
foreach (var infoTitle in SearchInfoTitles(filterText))
|
||||
{
|
||||
InfoTitles.Add(infoTitle);
|
||||
}
|
||||
|
||||
Playlists.Clear();
|
||||
foreach (var playlist in SearchPlaylists(filterText))
|
||||
{
|
||||
Playlists.Add(playlist);
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<Album> SearchAlbums(string filterText)
|
||||
{
|
||||
return new ObservableCollection<Album>(Manager.GetAlbums().Where(x => x.Name.Contains(filterText, StringComparison.OrdinalIgnoreCase)));
|
||||
}
|
||||
|
||||
public ObservableCollection<InfoTitle> SearchInfoTitles(string filterText)
|
||||
{
|
||||
return new ObservableCollection<InfoTitle>(Manager.GetInfoTitles().Where(x => x.Name.Contains(filterText, StringComparison.OrdinalIgnoreCase)));
|
||||
}
|
||||
|
||||
public ObservableCollection<Playlist> SearchPlaylists(string filterText)
|
||||
{
|
||||
return new ObservableCollection<Playlist>(Manager.GetPlaylists().Where(x => x.Name.Contains(filterText, StringComparison.OrdinalIgnoreCase)));
|
||||
}
|
||||
|
||||
private async void SearchResult_Tapped(object sender, TappedEventArgs e)
|
||||
{
|
||||
if (((Frame)sender).BindingContext is Album album)
|
||||
{
|
||||
Manager.CurrentAlbum = album;
|
||||
await Navigation.PushAsync(new AlbumPage());
|
||||
}
|
||||
|
||||
if (((Frame)sender).BindingContext is InfoTitle infoTitle)
|
||||
{
|
||||
Manager.CurrentInfoTitle = infoTitle;
|
||||
await Navigation.PushAsync(new InfoTitlePage());
|
||||
}
|
||||
|
||||
if (((Frame)sender).BindingContext is Playlist playlist)
|
||||
{
|
||||
Manager.CurrentPlaylist = playlist;
|
||||
await Navigation.PushAsync(new PlaylistPage());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue