RetroMusicPlayer / RetroMusicPlayer

Best Material You Design music player for Android
https://retromusic.app
GNU General Public License v3.0
4.02k stars 759 forks source link

Lagging "Artists" #1407

Open h6rd opened 2 years ago

h6rd commented 2 years ago

Very lag "Artist" if it has a lot of tracks, If you select Artist where there are few tracks there is no lag

Device info:

https://user-images.githubusercontent.com/51972490/181837017-b46ef70e-f126-4f4f-b707-e8ebbb96362f.mp4

lucasfunkedition commented 2 years ago

The App Freezes When An Artist Has A Lot Of Songs And There Comes A Time Where It Doesn't Respond

Device Info •Device: Motorola G9 Power •Android Version: 11 •App Version: 6.0.3Beta

https://user-images.githubusercontent.com/66811563/188331909-45c42e7b-9e83-4014-8f92-b30d15dfe9ec.mp4

devaeon commented 2 years ago

Use Pagination for loading artist songs like this:

AbsArtistDetailsFragment.kt:


  // Pagination
    private var songsList: List<Song> = arrayListOf()
    private var tempArrayList = ArrayList<Song>()
    private val tempList: List<Song> get() = tempArrayList.toList()
    private var start = 0
    private var end = 0
    private var range = 20

 detailsViewModel.getArtist().observe(viewLifecycleOwner) {
            songsList = it.songs
            end += range        // 0 + 20 = 20 (items)  (0-19 index)
            tempArrayList.clear()
            tempArrayList.addAll(songsList.take(end))
            songAdapter.swapDataSet(tempList, MusicPlayerRemote.currentSong.id)
        }

add this before setting adapter:

   addOnScrolledToEnd {
                start = end         // 20 = 20
                if (songsList.size > tempList.size) {
                    end += range        // 20 + 20 = 40 (items)  (0-39 index)

                    if (songsList.size > end) {         // 413 > 420
                        tempArrayList.addAll(songsList.subList(start, end).toList())
                        songAdapter.swapDataSet(tempList, MusicPlayerRemote.currentSong.id)
                    } else {
                        end = songsList.size
                        tempArrayList.addAll(songsList.subList(start, end).toList())
                        songAdapter.swapDataSet(tempList, MusicPlayerRemote.currentSong.id)
                    }
                }
            }

Create File for paginationa and paste this:


fun RecyclerView.addOnScrolledToEnd(onScrolledToEnd: () -> Unit) {

    this.addOnScrollListener(object : RecyclerView.OnScrollListener() {

        private val visibleThreshold = 5
        private var loading = true
        private var previousTotal = 0

        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {

            with(layoutManager as LinearLayoutManager) {

                val visibleItemCount = childCount
                val totalItemCount = itemCount
                val firstVisibleItem = findFirstVisibleItemPosition()

                if (loading && totalItemCount > previousTotal) {
                    loading = false
                    previousTotal = totalItemCount
                }

                if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
                    onScrolledToEnd()
                    loading = true
                }
            }
        }
    })
}
Shiva000K commented 1 year ago

Yeah, same issue on my app.