implement album searching
This commit is contained in:
parent
33cddf93da
commit
1c9c62f1ac
2 changed files with 28 additions and 2 deletions
|
@ -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<Album>, recyclerView: MyRecyclerView, fastScroller: FastScroller, itemClick: (Any) -> Unit) :
|
||||
class AlbumsAdapter(activity: SimpleActivity, var albums: ArrayList<Album>, 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<Album>, recy
|
|||
|
||||
private fun getSelectedAlbums(): List<Album> = albums.filter { selectedKeys.contains(it.id) }.toList()
|
||||
|
||||
fun updateItems(newItems: ArrayList<Album>, highlightText: String = "") {
|
||||
if (newItems.hashCode() != albums.hashCode()) {
|
||||
albums = newItems.clone() as ArrayList<Album>
|
||||
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()
|
||||
|
|
|
@ -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<Album>()
|
||||
|
||||
override fun setupFragment(activity: SimpleActivity) {
|
||||
ensureBackgroundThread {
|
||||
val albums = ArrayList<Album>()
|
||||
|
@ -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<Album>
|
||||
(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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue