diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/AlbumsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/AlbumsAdapter.kt index 3b774e43..198a91c1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/AlbumsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/AlbumsAdapter.kt @@ -9,6 +9,7 @@ import com.bumptech.glide.load.resource.bitmap.RoundedCorners import com.bumptech.glide.request.RequestOptions import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor +import com.simplemobiletools.commons.extensions.highlightTextPart import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.views.FastScroller import com.simplemobiletools.commons.views.MyRecyclerView @@ -18,13 +19,15 @@ import com.simplemobiletools.musicplayer.extensions.addTracksToPlaylist import com.simplemobiletools.musicplayer.extensions.addTracksToQueue import com.simplemobiletools.musicplayer.extensions.getAlbumTracksSync import com.simplemobiletools.musicplayer.models.Album +import com.simplemobiletools.musicplayer.models.Artist import com.simplemobiletools.musicplayer.models.Track import kotlinx.android.synthetic.main.item_album.view.* import java.util.* -class AlbumsAdapter(activity: SimpleActivity, val albums: ArrayList, recyclerView: MyRecyclerView, fastScroller: FastScroller, itemClick: (Any) -> Unit) : +class AlbumsAdapter(activity: SimpleActivity, var albums: ArrayList, recyclerView: MyRecyclerView, fastScroller: FastScroller, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) { + private var textToHighlight = "" private val placeholderBig = resources.getColoredDrawableWithColor(R.drawable.ic_headset, textColor) init { @@ -100,10 +103,23 @@ class AlbumsAdapter(activity: SimpleActivity, val albums: ArrayList, recy private fun getSelectedAlbums(): List = albums.filter { selectedKeys.contains(it.id) }.toList() + fun updateItems(newItems: ArrayList, highlightText: String = "") { + if (newItems.hashCode() != albums.hashCode()) { + albums = newItems.clone() as ArrayList + textToHighlight = highlightText + notifyDataSetChanged() + finishActMode() + } else if (textToHighlight != highlightText) { + textToHighlight = highlightText + notifyDataSetChanged() + } + fastScroller?.measureRecyclerView() + } + private fun setupView(view: View, album: Album) { view.apply { album_frame?.isSelected = selectedKeys.contains(album.id) - album_title.text = album.title + album_title.text = if (textToHighlight.isEmpty()) album.title else album.title.highlightTextPart(textToHighlight, adjustedPrimaryColor) album_title.setTextColor(textColor) val options = RequestOptions() diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/fragments/AlbumsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/fragments/AlbumsFragment.kt index 377f8f2e..03a90df8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/fragments/AlbumsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/fragments/AlbumsFragment.kt @@ -5,6 +5,8 @@ import android.content.Intent import android.util.AttributeSet import com.google.gson.Gson import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter +import com.simplemobiletools.commons.extensions.beGone +import com.simplemobiletools.commons.extensions.beVisibleIf import com.simplemobiletools.commons.helpers.AlphanumericComparator import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.musicplayer.activities.SimpleActivity @@ -18,6 +20,8 @@ import kotlinx.android.synthetic.main.fragment_albums.view.* // Artists -> Albums -> Tracks class AlbumsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) { + var albumsIgnoringSearch = ArrayList() + override fun setupFragment(activity: SimpleActivity) { ensureBackgroundThread { val albums = ArrayList() @@ -55,11 +59,17 @@ class AlbumsFragment(context: Context, attributeSet: AttributeSet) : MyViewPager } override fun onSearchQueryChanged(text: String) { + val filtered = albumsIgnoringSearch.filter { it.title.contains(text, true) }.toMutableList() as ArrayList + (albums_list.adapter as? AlbumsAdapter)?.updateItems(filtered, text) + albums_placeholder.beVisibleIf(filtered.isEmpty()) } override fun onSearchOpened() { + albumsIgnoringSearch = (albums_list?.adapter as? AlbumsAdapter)?.albums ?: ArrayList() } override fun onSearchClosed() { + (albums_list.adapter as? AlbumsAdapter)?.updateItems(albumsIgnoringSearch) + albums_placeholder.beGone() } }