100 Max Limit Issue Fixed.

This commit is contained in:
Shabinder 2020-11-06 17:07:42 +05:30
parent 172c8d1f55
commit 923e28617a
7 changed files with 60 additions and 43 deletions

View File

@ -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
}
})

View File

@ -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<DownloadRecord>()
var ytList = mutableListOf<DownloadRecord>()
val downloadRecordList = MutableLiveData<MutableList<DownloadRecord>>().apply {
value = mutableListOf()
}
init {
getDownloadRecordList()
}

View File

@ -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 = ""
}
}
})*/
})
}
/**

View File

@ -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<Track>) {
private suspend fun loadAllImages(trackList: List<Track>) {
trackList.forEach {
val imgUrl = it.album?.images?.get(0)?.url
imgUrl?.let {

View File

@ -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()

View File

@ -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

View File

@ -45,6 +45,7 @@
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/downloading_fab_spotify"
android:layout_width="wrap_content"
android:keepScreenOn="true"
android:layout_height="wrap_content"
android:backgroundTint="@color/black"
android:scaleType="fitCenter"