From 923e28617a6e820336c0716a548cd195fb17408a Mon Sep 17 00:00:00 2001 From: Shabinder Date: Fri, 6 Nov 2020 17:07:42 +0530 Subject: [PATCH] 100 Max Limit Issue Fixed. --- .../downloadrecord/DownloadRecordFragment.kt | 8 ++--- .../downloadrecord/DownloadRecordViewModel.kt | 3 +- .../ui/mainfragment/MainFragment.kt | 32 +++++++++---------- .../spotiflyer/ui/spotify/SpotifyFragment.kt | 23 +++++++------ .../spotiflyer/ui/spotify/SpotifyViewModel.kt | 24 ++++++++++---- .../spotiflyer/utils/SpotifyInterface.kt | 12 ++++--- app/src/main/res/layout/spotify_fragment.xml | 1 + 7 files changed, 60 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/com/shabinder/spotiflyer/ui/downloadrecord/DownloadRecordFragment.kt b/app/src/main/java/com/shabinder/spotiflyer/ui/downloadrecord/DownloadRecordFragment.kt index b9271092..e6e60b89 100755 --- a/app/src/main/java/com/shabinder/spotiflyer/ui/downloadrecord/DownloadRecordFragment.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/ui/downloadrecord/DownloadRecordFragment.kt @@ -23,7 +23,6 @@ import android.view.View import android.view.ViewGroup import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment -import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider import com.google.android.material.tabs.TabLayout import com.shabinder.spotiflyer.R @@ -46,7 +45,8 @@ class DownloadRecordFragment : Fragment() { downloadRecordViewModel = ViewModelProvider(this).get(DownloadRecordViewModel::class.java) adapter = DownloadRecordAdapter() binding.downloadRecordList.adapter = adapter - downloadRecordViewModel.downloadRecordList.observe(viewLifecycleOwner, Observer { + + downloadRecordViewModel.downloadRecordList.observe(viewLifecycleOwner, { if(it.isNotEmpty()){ downloadRecordViewModel.spotifyList = mutableListOf() downloadRecordViewModel.ytList = mutableListOf() @@ -56,7 +56,6 @@ class DownloadRecordFragment : Fragment() { } if(binding.tabLayout.selectedTabPosition == 0) adapter.submitList(downloadRecordViewModel.spotifyList) else adapter.submitList(downloadRecordViewModel.ytList) -// adapter.notifyDataSetChanged() } }) @@ -67,7 +66,6 @@ class DownloadRecordFragment : Fragment() { if(tab?.text == "Spotify"){ adapter.submitList(downloadRecordViewModel.spotifyList) } else adapter.submitList(downloadRecordViewModel.ytList) -// adapter.notifyDataSetChanged() } override fun onTabReselected(tab: TabLayout.Tab?) { @@ -75,7 +73,7 @@ class DownloadRecordFragment : Fragment() { } override fun onTabUnselected(tab: TabLayout.Tab?) { - // Handle tab unselect + // Handle tab unselected } }) diff --git a/app/src/main/java/com/shabinder/spotiflyer/ui/downloadrecord/DownloadRecordViewModel.kt b/app/src/main/java/com/shabinder/spotiflyer/ui/downloadrecord/DownloadRecordViewModel.kt index 0c98d044..7f9cc1a7 100755 --- a/app/src/main/java/com/shabinder/spotiflyer/ui/downloadrecord/DownloadRecordViewModel.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/ui/downloadrecord/DownloadRecordViewModel.kt @@ -30,12 +30,13 @@ import kotlinx.coroutines.launch class DownloadRecordViewModel @ViewModelInject constructor(val databaseDAO: DatabaseDAO) : ViewModel(){ private var viewModelJob = Job() - private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob) + private val uiScope = CoroutineScope(Dispatchers.Default + viewModelJob) var spotifyList = mutableListOf() var ytList = mutableListOf() val downloadRecordList = MutableLiveData>().apply { value = mutableListOf() } + init { getDownloadRecordList() } diff --git a/app/src/main/java/com/shabinder/spotiflyer/ui/mainfragment/MainFragment.kt b/app/src/main/java/com/shabinder/spotiflyer/ui/mainfragment/MainFragment.kt index 95727e31..b2accaf3 100755 --- a/app/src/main/java/com/shabinder/spotiflyer/ui/mainfragment/MainFragment.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/ui/mainfragment/MainFragment.kt @@ -35,6 +35,9 @@ import com.shabinder.spotiflyer.SharedViewModel import com.shabinder.spotiflyer.databinding.MainFragmentBinding import com.shreyaspatil.easyupipayment.EasyUpiPayment import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import javax.inject.Inject @AndroidEntryPoint @@ -92,25 +95,20 @@ class MainFragment : Fragment() { **/ private fun handleIntent() { sharedViewModel.intentString.observe(viewLifecycleOwner,{ - //Waiting for Authentication to Finish with Spotify()Access Token Observe - if(sharedViewModel.accessToken.value != ""){ - if(it != ""){ - binding.linkSearch.setText(sharedViewModel.intentString.value) - binding.btnSearch.performClick() - sharedViewModel.intentString.value = "" + if(it != ""){ + sharedViewModel.uiScope.launch(Dispatchers.IO) { + while (sharedViewModel.accessToken.value == "") { + //Waiting for Authentication to Finish + Thread.sleep(1000) + } + withContext(Dispatchers.Main){ + binding.linkSearch.setText(sharedViewModel.intentString.value) + binding.btnSearch.performClick() + sharedViewModel.intentString.value = "" + } } } - })/* - sharedViewModel.accessToken.observe(viewLifecycleOwner, { - //Waiting for Authentication to Finish with Spotify()Access Token Observe - if (it != ""){ - if(sharedViewModel.intentString != ""){ - binding.linkSearch.setText(sharedViewModel.intentString) - binding.btnSearch.performClick() - sharedViewModel.intentString = "" - } - } - })*/ + }) } /** diff --git a/app/src/main/java/com/shabinder/spotiflyer/ui/spotify/SpotifyFragment.kt b/app/src/main/java/com/shabinder/spotiflyer/ui/spotify/SpotifyFragment.kt index b49496ab..9e2835b3 100755 --- a/app/src/main/java/com/shabinder/spotiflyer/ui/spotify/SpotifyFragment.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/ui/spotify/SpotifyFragment.kt @@ -108,6 +108,7 @@ class SpotifyFragment : Fragment() { else{ spotifyViewModel.spotifySearch(type,link) if(type=="album")adapterSpotify.isAlbum = true + binding.btnDownloadAllSpotify.setOnClickListener { for (track in spotifyViewModel.trackList.value!!){ if(track.downloaded != "Downloaded"){ @@ -116,6 +117,8 @@ class SpotifyFragment : Fragment() { } binding.btnDownloadAllSpotify.visibility = View.GONE binding.downloadingFabSpotify.visibility = View.VISIBLE + + rotateAnim(binding.downloadingFabSpotify) for (track in spotifyViewModel.trackList.value!!){ if(track.downloaded != "Downloaded"){ @@ -123,7 +126,7 @@ class SpotifyFragment : Fragment() { } } showToast("Starting Download in Few Seconds") - loadAllImages(spotifyViewModel.trackList.value!!) + spotifyViewModel.uiScope.launch(Dispatchers.Default){loadAllImages(spotifyViewModel.trackList.value!!)} spotifyViewModel.uiScope.launch { SpotifyDownloadHelper.downloadAllTracks( spotifyViewModel.folderType, @@ -212,15 +215,15 @@ class SpotifyFragment : Fragment() { } private fun checkIfAllDownloaded() { - var allDownloaded = true - for (track in spotifyViewModel.trackList.value!!){ - if (track.downloaded != "Downloaded")allDownloaded = false - } - if(allDownloaded){ - binding.downloadingFabSpotify.setImageResource(R.drawable.ic_tick) + if(!spotifyViewModel.trackList.value!!.any { it.downloaded != "Downloaded" }){ + //All Tracks Downloaded binding.btnDownloadAllSpotify.visibility = View.GONE - binding.downloadingFabSpotify.visibility = View.VISIBLE - binding.downloadingFabSpotify.clearAnimation() + binding.downloadingFabSpotify.apply{ + setImageResource(R.drawable.ic_tick) + visibility = View.VISIBLE + clearAnimation() + keepScreenOn = false + } } } @@ -245,7 +248,7 @@ class SpotifyFragment : Fragment() { /** * Function to fetch all Images for using in mp3 tag. **/ - private fun loadAllImages(trackList: List) { + private suspend fun loadAllImages(trackList: List) { trackList.forEach { val imgUrl = it.album?.images?.get(0)?.url imgUrl?.let { diff --git a/app/src/main/java/com/shabinder/spotiflyer/ui/spotify/SpotifyViewModel.kt b/app/src/main/java/com/shabinder/spotiflyer/ui/spotify/SpotifyViewModel.kt index bce98562..6a3a9082 100755 --- a/app/src/main/java/com/shabinder/spotiflyer/ui/spotify/SpotifyViewModel.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/ui/spotify/SpotifyViewModel.kt @@ -23,10 +23,7 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.shabinder.spotiflyer.database.DatabaseDAO import com.shabinder.spotiflyer.database.DownloadRecord -import com.shabinder.spotiflyer.models.Album -import com.shabinder.spotiflyer.models.Image -import com.shabinder.spotiflyer.models.Playlist -import com.shabinder.spotiflyer.models.Track +import com.shabinder.spotiflyer.models.* import com.shabinder.spotiflyer.utils.SpotifyService import com.shabinder.spotiflyer.utils.finalOutputDir import kotlinx.coroutines.* @@ -114,12 +111,23 @@ class SpotifyViewModel @ViewModelInject constructor(val databaseDAO: DatabaseDAO Log.i("Tracks Fetched",playlistObject?.tracks?.items?.size.toString()) playlistObject?.tracks?.items?.forEach { it.track?.let { - it1 -> if(File(finalOutputDir(it1.name!!,folderType,subFolder)).exists()){//Download Already Present!! + it1 -> if(File(finalOutputDir(it1.name!!,folderType,subFolder)).exists()){//Download Already Present!! it1.downloaded = "Downloaded" - } + } tempTrackList.add(it1) } } + var moreTracksAvailable = !playlistObject?.tracks?.next.isNullOrBlank() + + while(moreTracksAvailable){ + //Check For More Tracks If available + val moreTracks = getPlaylistTrackDetails(link,offset = tempTrackList.size) + moreTracks?.items?.forEach{ + it.track?.let { it1 -> tempTrackList.add(it1) } + } + moreTracksAvailable = !moreTracks?.next.isNullOrBlank() + } + Log.i("Total Tracks Fetched",tempTrackList.size.toString()) trackList.value = tempTrackList title.value = playlistObject?.name coverUrl.value = playlistObject?.images?.get(0)!!.url!! @@ -155,6 +163,10 @@ class SpotifyViewModel @ViewModelInject constructor(val databaseDAO: DatabaseDAO Log.i("Requesting","https://api.spotify.com/v1/playlists/$link") return spotifyService?.getPlaylist(link) } + private suspend fun getPlaylistTrackDetails(link:String,offset:Int = 0,limit:Int = 100): PagingObjectPlaylistTrack?{ + Log.i("Requesting","https://api.spotify.com/v1/playlists/$link/tracks?offset=$offset&limit=$limit") + return spotifyService?.getPlaylistTracks(link, offset, limit) + } override fun onCleared() { super.onCleared() diff --git a/app/src/main/java/com/shabinder/spotiflyer/utils/SpotifyInterface.kt b/app/src/main/java/com/shabinder/spotiflyer/utils/SpotifyInterface.kt index 704c1cdc..4758f80c 100755 --- a/app/src/main/java/com/shabinder/spotiflyer/utils/SpotifyInterface.kt +++ b/app/src/main/java/com/shabinder/spotiflyer/utils/SpotifyInterface.kt @@ -17,10 +17,7 @@ package com.shabinder.spotiflyer.utils -import com.shabinder.spotiflyer.models.Album -import com.shabinder.spotiflyer.models.Playlist -import com.shabinder.spotiflyer.models.Token -import com.shabinder.spotiflyer.models.Track +import com.shabinder.spotiflyer.models.* import retrofit2.http.* /* @@ -46,6 +43,13 @@ interface SpotifyService { @GET("playlists/{playlist_id}") suspend fun getPlaylist(@Path("playlist_id") playlistId: String?): Playlist + @GET("playlists/{playlist_id}/tracks") + suspend fun getPlaylistTracks( + @Path("playlist_id") playlistId: String?, + @Query("offset") offset: Int = 0, + @Query("limit") limit: Int = 100 + ): PagingObjectPlaylistTrack + @GET("tracks/{id}") suspend fun getTrack(@Path("id") trackId: String?): Track diff --git a/app/src/main/res/layout/spotify_fragment.xml b/app/src/main/res/layout/spotify_fragment.xml index 0ce6166e..2270feaf 100755 --- a/app/src/main/res/layout/spotify_fragment.xml +++ b/app/src/main/res/layout/spotify_fragment.xml @@ -45,6 +45,7 @@