implementing Search at the Playlists tab

This commit is contained in:
tibbi 2020-10-20 15:03:22 +02:00
parent 2200a8b1d5
commit ea8653c55a
5 changed files with 40 additions and 17 deletions

View file

@ -58,7 +58,7 @@ android {
}
dependencies {
implementation 'com.simplemobiletools:commons:5.31.5'
implementation 'com.simplemobiletools:commons:5.31.6'
implementation 'org.greenrobot:eventbus:3.2.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'androidx.media:media:1.2.0'

View file

@ -28,13 +28,15 @@ import com.simplemobiletools.musicplayer.dialogs.SleepTimerCustomDialog
import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.queueDAO
import com.simplemobiletools.musicplayer.extensions.sendIntent
import com.simplemobiletools.musicplayer.fragments.MyViewPagerFragment
import com.simplemobiletools.musicplayer.helpers.*
import com.simplemobiletools.musicplayer.models.Events
import com.simplemobiletools.musicplayer.services.MusicService
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_albums.*
import kotlinx.android.synthetic.main.fragment_artists.*
import kotlinx.android.synthetic.main.fragment_old_songs.*
import kotlinx.android.synthetic.main.fragment_playlists.*
import kotlinx.android.synthetic.main.fragment_tracks.*
import kotlinx.android.synthetic.main.view_current_track_bar.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
@ -70,18 +72,12 @@ class MainActivity : SimpleActivity() {
override fun onResume() {
super.onResume()
updateTextColors(main_holder)
getCurrentFragment()?.onResume()
sleep_timer_holder.background = ColorDrawable(config.backgroundColor)
sleep_timer_stop.applyColorFilter(config.textColor)
updateCurrentTrackBar()
invalidateOptionsMenu()
}
override fun onPause() {
super.onPause()
getCurrentFragment()?.onPause()
}
override fun onStop() {
super.onStop()
searchMenuItem?.collapseActionView()
@ -134,7 +130,7 @@ class MainActivity : SimpleActivity() {
override fun onQueryTextChange(newText: String): Boolean {
if (isSearchOpen) {
getCurrentFragment()?.searchQueryChanged(newText)
getCurrentFragment().onSearchQueryChanged(newText)
}
return true
}
@ -143,13 +139,13 @@ class MainActivity : SimpleActivity() {
MenuItemCompat.setOnActionExpandListener(searchMenuItem, object : MenuItemCompat.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
getCurrentFragment()?.searchOpened()
getCurrentFragment().onSearchOpened()
isSearchOpen = true
return true
}
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
getCurrentFragment()?.searchClosed()
getCurrentFragment().onSearchClosed()
isSearchOpen = false
return true
}
@ -218,7 +214,14 @@ class MainActivity : SimpleActivity() {
viewpager.currentItem = config.lastUsedViewPagerPage
}
private fun getCurrentFragment() = songs_fragment_holder
private fun getCurrentFragment(): MyViewPagerFragment {
return when (viewpager.currentItem) {
0 -> playlists_fragment_holder
1 -> artists_fragment_holder
2 -> albums_fragment_holder
else -> tracks_fragment_holder
}
}
private fun showSortingDialog() {
ChangeSortingDialog(this) {

View file

@ -54,9 +54,6 @@ class OldSongAdapter(activity: SimpleActivity, var songs: ArrayList<Track>, val
private var navigationViewHolder: NavigationViewHolder? = null
private var navigationViewHeight = 0
var isThirdPartyIntent = false
private var adjustedPrimaryColor = activity.getAdjustedPrimaryColor()
init {
setupDragListener(true)
positionOffset = LIST_HEADERS_COUNT

View file

@ -5,7 +5,9 @@ import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.deleteFiles
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.extensions.getFilenameFromPath
import com.simplemobiletools.commons.extensions.highlightTextPart
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.FastScroller
@ -23,9 +25,11 @@ import com.simplemobiletools.musicplayer.models.Playlist
import kotlinx.android.synthetic.main.item_playlist.view.*
import java.util.*
class PlaylistsAdapter(activity: SimpleActivity, val playlists: ArrayList<Playlist>, recyclerView: MyRecyclerView, fastScroller: FastScroller,
class PlaylistsAdapter(activity: SimpleActivity, var playlists: ArrayList<Playlist>, recyclerView: MyRecyclerView, fastScroller: FastScroller,
itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) {
private var textToHighlight = ""
init {
setupDragListener(true)
}
@ -133,6 +137,19 @@ class PlaylistsAdapter(activity: SimpleActivity, val playlists: ArrayList<Playli
private fun getItemWithKey(key: Int): Playlist? = playlists.firstOrNull { it.id == key }
fun updateItems(newItems: ArrayList<Playlist>, highlightText: String = "") {
if (newItems.hashCode() != playlists.hashCode()) {
playlists = newItems.clone() as ArrayList<Playlist>
textToHighlight = highlightText
notifyDataSetChanged()
finishActMode()
} else if (textToHighlight != highlightText) {
textToHighlight = highlightText
notifyDataSetChanged()
}
fastScroller?.measureRecyclerView()
}
private fun showRenameDialog() {
NewPlaylistDialog(activity, playlists[getItemKeyPosition(selectedKeys.first())]) {
activity.runOnUiThread {
@ -144,7 +161,7 @@ class PlaylistsAdapter(activity: SimpleActivity, val playlists: ArrayList<Playli
private fun setupView(view: View, playlist: Playlist) {
view.apply {
playlist_frame?.isSelected = selectedKeys.contains(playlist.id)
playlist_title.text = playlist.title
playlist_title.text = if (textToHighlight.isEmpty()) playlist.title else playlist.title.highlightTextPart(textToHighlight, adjustedPrimaryColor)
playlist_title.setTextColor(textColor)
val tracks = resources.getQuantityString(R.plurals.tracks_plural, playlist.trackCnt, playlist.trackCnt)

View file

@ -16,6 +16,8 @@ import com.simplemobiletools.musicplayer.models.Playlist
import kotlinx.android.synthetic.main.fragment_playlists.view.*
class PlaylistsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) {
var playlistsIgnoringSearch = ArrayList<Playlist>()
override fun setupFragment(activity: SimpleActivity) {
ensureBackgroundThread {
var playlists = activity.playlistDAO.getAll() as ArrayList<Playlist>
@ -50,11 +52,15 @@ class PlaylistsFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
}
override fun onSearchQueryChanged(text: String) {
val filtered = playlistsIgnoringSearch.filter { it.title.contains(text, true) }.toMutableList() as ArrayList<Playlist>
(playlists_list.adapter as? PlaylistsAdapter)?.updateItems(filtered, text)
}
override fun onSearchOpened() {
playlistsIgnoringSearch = (playlists_list?.adapter as? PlaylistsAdapter)?.playlists ?: ArrayList()
}
override fun onSearchClosed() {
(playlists_list.adapter as? PlaylistsAdapter)?.updateItems(playlistsIgnoringSearch)
}
}