Download Location Change For Playlist-Tracks.

This commit is contained in:
shabinder 2020-08-07 09:36:58 +05:30
parent 72fd6a4c73
commit bee88f02c7
2 changed files with 11 additions and 15 deletions

View File

@ -36,7 +36,6 @@ import com.github.kiulian.downloader.model.formats.Format
import com.github.kiulian.downloader.model.quality.AudioQuality import com.github.kiulian.downloader.model.quality.AudioQuality
import com.shabinder.spotiflyer.models.DownloadObject import com.shabinder.spotiflyer.models.DownloadObject
import com.shabinder.spotiflyer.models.Track import com.shabinder.spotiflyer.models.Track
import com.shabinder.spotiflyer.ui.spotify.SpotifyFragment
import com.shabinder.spotiflyer.ui.spotify.SpotifyViewModel import com.shabinder.spotiflyer.ui.spotify.SpotifyViewModel
import com.shabinder.spotiflyer.worker.ForegroundService import com.shabinder.spotiflyer.worker.ForegroundService
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -71,13 +70,13 @@ object SpotifyDownloadHelper {
}else{ }else{
if(isBrowserLoading){//WebView Busy!! if(isBrowserLoading){//WebView Busy!!
if (listProcessed){//Previous List request progress check if (listProcessed){//Previous List request progress check
getYTLink(null,type,subFolder,ytDownloader,"${it.name} ${it.artists?.get(0)?.name ?:""}", it) getYTLink(type,subFolder,ytDownloader,"${it.name} ${it.artists?.get(0)?.name ?:""}", it)
listProcessed = false//Notifying A list Processing Started listProcessed = false//Notifying A list Processing Started
}else{//Adding Requests to a Queue }else{//Adding Requests to a Queue
youtubeList.add(YoutubeRequest(null,type,subFolder,ytDownloader,"${it.name} ${it.artists?.get(0)?.name ?:""}", it)) youtubeList.add(YoutubeRequest(type,subFolder,ytDownloader,"${it.name} ${it.artists?.get(0)?.name ?:""}", it))
} }
}else{ }else{
getYTLink(null,type,subFolder,ytDownloader,"${it.name} ${it.artists?.get(0)?.name ?:""}", it) getYTLink(type,subFolder,ytDownloader,"${it.name} ${it.artists?.get(0)?.name ?:""}", it)
} }
} }
updateStatusBar() updateStatusBar()
@ -90,18 +89,17 @@ object SpotifyDownloadHelper {
//TODO CleanUp here and there!! //TODO CleanUp here and there!!
@SuppressLint("SetJavaScriptEnabled") @SuppressLint("SetJavaScriptEnabled")
suspend fun getYTLink(spotifyFragment: SpotifyFragment? = null, suspend fun getYTLink(type:String,
type:String,
subFolder:String?, subFolder:String?,
ytDownloader: YoutubeDownloader?, ytDownloader: YoutubeDownloader?,
searchQuery: String, searchQuery: String,
track: Track){ track: Track){
isBrowserLoading = true // Notify Web View Started Loading
val searchText = searchQuery.replace("\\s".toRegex(), "+") val searchText = searchQuery.replace("\\s".toRegex(), "+")
val url = "https://www.youtube.com/results?sp=EgIQAQ%253D%253D&q=$searchText" val url = "https://www.youtube.com/results?sp=EgIQAQ%253D%253D&q=$searchText"
Log.i("DH YT LINK ",url) Log.i("DH YT LINK ",url)
applyWebViewSettings(webView!!) applyWebViewSettings(webView!!)
withContext(Dispatchers.Main){ withContext(Dispatchers.Main){
isBrowserLoading = true
webView!!.loadUrl(url) webView!!.loadUrl(url)
webView!!.webViewClient = object : WebViewClient() { webView!!.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) { override fun onPageFinished(view: WebView?, url: String?) {
@ -114,20 +112,19 @@ object SpotifyDownloadHelper {
val id = value!!.substringAfterLast("=", "error").replace("\"","") val id = value!!.substringAfterLast("=", "error").replace("\"","")
Log.i("YT-id",id) Log.i("YT-id",id)
if(id !="error"){//Link extracting error if(id !="error"){//Link extracting error
spotifyFragment?.showToast("Starting Download")
Processed++ Processed++
if(Processed == total)listProcessed = true //List Processesd
updateStatusBar() updateStatusBar()
downloadFile(subFolder, type, track,ytDownloader,id) downloadFile(subFolder, type, track,ytDownloader,id)
} }
if(youtubeList.isNotEmpty()){ if(youtubeList.isNotEmpty()){
val request = youtubeList[0] val request = youtubeList[0]
spotifyViewModel!!.uiScope.launch { spotifyViewModel!!.uiScope.launch {
getYTLink(request.spotifyFragment,request.type,request.subFolder,request.ytDownloader,request.searchQuery,request.track) getYTLink(request.type,request.subFolder,request.ytDownloader,request.searchQuery,request.track)
} }
youtubeList.remove(request) youtubeList.remove(request)
if(youtubeList.size == 0){//list processing completed , webView is free again! if(youtubeList.size == 0){//list processing completed , webView is free again!
isBrowserLoading = false isBrowserLoading = false
listProcessed = true
} }
} }
} }
@ -263,7 +260,6 @@ object SpotifyDownloadHelper {
} }
} }
data class YoutubeRequest( data class YoutubeRequest(
val spotifyFragment: SpotifyFragment? = null,
val type:String, val type:String,
val subFolder:String?, val subFolder:String?,
val ytDownloader: YoutubeDownloader?, val ytDownloader: YoutubeDownloader?,

View File

@ -27,9 +27,8 @@ import androidx.recyclerview.widget.RecyclerView
import com.shabinder.spotiflyer.R import com.shabinder.spotiflyer.R
import com.shabinder.spotiflyer.databinding.TrackListItemBinding import com.shabinder.spotiflyer.databinding.TrackListItemBinding
import com.shabinder.spotiflyer.downloadHelper.SpotifyDownloadHelper.context import com.shabinder.spotiflyer.downloadHelper.SpotifyDownloadHelper.context
import com.shabinder.spotiflyer.downloadHelper.SpotifyDownloadHelper.getYTLink import com.shabinder.spotiflyer.downloadHelper.SpotifyDownloadHelper.downloadAllTracks
import com.shabinder.spotiflyer.models.Track import com.shabinder.spotiflyer.models.Track
import com.shabinder.spotiflyer.ui.spotify.SpotifyFragment
import com.shabinder.spotiflyer.ui.spotify.SpotifyViewModel import com.shabinder.spotiflyer.ui.spotify.SpotifyViewModel
import com.shabinder.spotiflyer.utils.bindImage import com.shabinder.spotiflyer.utils.bindImage
import com.shabinder.spotiflyer.utils.rotateAnim import com.shabinder.spotiflyer.utils.rotateAnim
@ -40,7 +39,6 @@ class SpotifyTrackListAdapter: ListAdapter<Track,SpotifyTrackListAdapter.ViewHol
var spotifyViewModel = SpotifyViewModel() var spotifyViewModel = SpotifyViewModel()
var isAlbum:Boolean = false var isAlbum:Boolean = false
var spotifyFragment: SpotifyFragment? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
@ -81,7 +79,9 @@ class SpotifyTrackListAdapter: ListAdapter<Track,SpotifyTrackListAdapter.ViewHol
rotateAnim(it) rotateAnim(it)
item.downloaded = "Downloading" item.downloaded = "Downloading"
spotifyViewModel.uiScope.launch { spotifyViewModel.uiScope.launch {
getYTLink(spotifyFragment,"Tracks",null,spotifyViewModel.ytDownloader,"${item.name} ${item.artists?.get(0)!!.name?:""}",track = item) val itemList = mutableListOf<Track>()
itemList.add(item)
downloadAllTracks(spotifyViewModel.folderType,spotifyViewModel.subFolder,itemList,spotifyViewModel.ytDownloader)
} }
notifyItemChanged(position) notifyItemChanged(position)
} }