33import static org .schabi .newpipe .database .playlist .model .PlaylistEntity .DEFAULT_THUMBNAIL_ID ;
44
55import android .os .Bundle ;
6+ import android .text .Editable ;
7+ import android .text .TextWatcher ;
68import android .view .LayoutInflater ;
79import android .view .View ;
810import android .view .ViewGroup ;
11+ import android .widget .EditText ;
912import android .widget .TextView ;
1013import android .widget .Toast ;
1114
2124import org .schabi .newpipe .local .LocalItemListAdapter ;
2225import org .schabi .newpipe .local .playlist .LocalPlaylistManager ;
2326
27+ import java .util .ArrayList ;
2428import java .util .List ;
29+ import java .util .Locale ;
30+ import java .util .stream .Collectors ;
2531
2632import io .reactivex .rxjava3 .android .schedulers .AndroidSchedulers ;
2733import io .reactivex .rxjava3 .disposables .CompositeDisposable ;
@@ -32,7 +38,10 @@ public final class PlaylistAppendDialog extends PlaylistDialog {
3238 private RecyclerView playlistRecyclerView ;
3339 private LocalItemListAdapter playlistAdapter ;
3440 private TextView playlistDuplicateIndicator ;
41+ private EditText playlistSearchEditText ;
42+ private View playlistSearchClear ;
3543
44+ private List <PlaylistDuplicatesEntry > allPlaylists = new ArrayList <>();
3645 private final CompositeDisposable playlistDisposables = new CompositeDisposable ();
3746
3847 /**
@@ -82,6 +91,11 @@ public void onViewCreated(@NonNull final View view, @Nullable final Bundle saved
8291 final View newPlaylistButton = view .findViewById (R .id .newPlaylist );
8392 newPlaylistButton .setOnClickListener (ignored -> openCreatePlaylistDialog ());
8493
94+ // Setup search functionality
95+ playlistSearchEditText = view .findViewById (R .id .playlist_search_edit_text );
96+ playlistSearchClear = view .findViewById (R .id .playlist_search_clear );
97+ setupSearch ();
98+
8599 playlistDisposables .add (playlistManager
86100 .getPlaylistDuplicates (getStreamEntities ().get (0 ).getUrl ())
87101 .observeOn (AndroidSchedulers .mainThread ())
@@ -103,12 +117,66 @@ public void onDestroyView() {
103117 playlistDisposables .clear ();
104118 playlistRecyclerView = null ;
105119 playlistAdapter = null ;
120+ playlistSearchEditText = null ;
121+ playlistSearchClear = null ;
122+ allPlaylists .clear ();
106123 }
107124
108125 /*//////////////////////////////////////////////////////////////////////////
109126 // Helper
110127 //////////////////////////////////////////////////////////////////////////*/
111128
129+ private void setupSearch () {
130+ if (playlistSearchEditText == null || playlistSearchClear == null ) {
131+ return ;
132+ }
133+
134+ playlistSearchEditText .addTextChangedListener (new TextWatcher () {
135+ @ Override
136+ public void beforeTextChanged (final CharSequence s , final int start ,
137+ final int count , final int after ) {
138+ }
139+
140+ @ Override
141+ public void onTextChanged (final CharSequence s , final int start ,
142+ final int before , final int count ) {
143+ }
144+
145+ @ Override
146+ public void afterTextChanged (final Editable s ) {
147+ final String query = s .toString ();
148+ playlistSearchClear .setVisibility (
149+ query .isEmpty () ? View .GONE : View .VISIBLE );
150+ filterPlaylists (query );
151+ }
152+ });
153+
154+ playlistSearchClear .setOnClickListener (v -> {
155+ playlistSearchEditText .setText ("" );
156+ playlistSearchClear .setVisibility (View .GONE );
157+ });
158+ }
159+
160+ private void filterPlaylists (final String query ) {
161+ if (playlistAdapter == null || allPlaylists .isEmpty ()) {
162+ return ;
163+ }
164+
165+ if (query .isEmpty ()) {
166+ playlistAdapter .clearStreamItemList ();
167+ playlistAdapter .addItems (allPlaylists );
168+ } else {
169+ final String lowerCaseQuery = query .toLowerCase (Locale .getDefault ());
170+ final List <PlaylistDuplicatesEntry > filteredPlaylists = allPlaylists .stream ()
171+ .filter (playlist -> playlist .name .toLowerCase (Locale .getDefault ())
172+ .contains (lowerCaseQuery ))
173+ .collect (Collectors .toList ());
174+
175+ playlistAdapter .clearStreamItemList ();
176+ playlistAdapter .addItems (filteredPlaylists );
177+ }
178+ }
179+
112180 /** Display create playlist dialog. */
113181 public void openCreatePlaylistDialog () {
114182 if (getStreamEntities () == null || !isAdded ()) {
@@ -129,6 +197,7 @@ private void onPlaylistsReceived(@NonNull final List<PlaylistDuplicatesEntry> pl
129197 if (playlistAdapter != null
130198 && playlistRecyclerView != null
131199 && playlistDuplicateIndicator != null ) {
200+ allPlaylists = playlists ;
132201 playlistAdapter .clearStreamItemList ();
133202 playlistAdapter .addItems (playlists );
134203 playlistRecyclerView .setVisibility (View .VISIBLE );
0 commit comments