mirror of
https://github.com/Shabinder/SpotiFlyer.git
synced 2024-11-25 02:14:32 +01:00
Progress Showing Improvements.
This commit is contained in:
parent
e1d88da580
commit
2e40db5452
@ -35,10 +35,8 @@ import com.shabinder.spotiflyer.models.TrackDetails
|
|||||||
import com.shabinder.spotiflyer.models.spotify.Source
|
import com.shabinder.spotiflyer.models.spotify.Source
|
||||||
import com.shabinder.spotiflyer.recyclerView.TrackListAdapter
|
import com.shabinder.spotiflyer.recyclerView.TrackListAdapter
|
||||||
import com.shabinder.spotiflyer.ui.base.BaseFragment
|
import com.shabinder.spotiflyer.ui.base.BaseFragment
|
||||||
|
import com.shabinder.spotiflyer.utils.*
|
||||||
import com.shabinder.spotiflyer.utils.Provider.mainActivity
|
import com.shabinder.spotiflyer.utils.Provider.mainActivity
|
||||||
import com.shabinder.spotiflyer.utils.bindImage
|
|
||||||
import com.shabinder.spotiflyer.utils.isOnline
|
|
||||||
import com.shabinder.spotiflyer.utils.showNoConnectionAlert
|
|
||||||
import com.tonyodev.fetch2.Status
|
import com.tonyodev.fetch2.Status
|
||||||
|
|
||||||
abstract class TrackListFragment<VM : TrackListViewModel, args: NavArgs> : BaseFragment<TrackListFragmentBinding,VM>() {
|
abstract class TrackListFragment<VM : TrackListViewModel, args: NavArgs> : BaseFragment<TrackListFragmentBinding,VM>() {
|
||||||
@ -48,6 +46,7 @@ abstract class TrackListFragment<VM : TrackListViewModel, args: NavArgs> : BaseF
|
|||||||
protected abstract var source: Source
|
protected abstract var source: Source
|
||||||
private var intentFilter: IntentFilter? = null
|
private var intentFilter: IntentFilter? = null
|
||||||
private var updateUIReceiver: BroadcastReceiver? = null
|
private var updateUIReceiver: BroadcastReceiver? = null
|
||||||
|
private var queryReceiver: BroadcastReceiver? = null
|
||||||
protected abstract val args:NavArgs
|
protected abstract val args:NavArgs
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
@ -87,7 +86,7 @@ abstract class TrackListFragment<VM : TrackListViewModel, args: NavArgs> : BaseF
|
|||||||
if (!it.isNullOrEmpty()){
|
if (!it.isNullOrEmpty()){
|
||||||
Log.i("GaanaFragment","TrackList Updated")
|
Log.i("GaanaFragment","TrackList Updated")
|
||||||
adapter.submitList(it, source)
|
adapter.submitList(it, source)
|
||||||
checkIfAllDownloaded()
|
updateTracksStatus()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -101,14 +100,14 @@ abstract class TrackListFragment<VM : TrackListViewModel, args: NavArgs> : BaseF
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initializeBroadcast() {
|
private fun initializeBroadcast() {
|
||||||
intentFilter = IntentFilter()
|
intentFilter = IntentFilter().apply {
|
||||||
intentFilter?.addAction(Status.QUEUED.name)
|
addAction(Status.QUEUED.name)
|
||||||
intentFilter?.addAction(Status.FAILED.name)
|
addAction(Status.FAILED.name)
|
||||||
intentFilter?.addAction(Status.DOWNLOADING.name)
|
addAction(Status.DOWNLOADING.name)
|
||||||
intentFilter?.addAction("Progress")
|
addAction("Progress")
|
||||||
intentFilter?.addAction("Converting")
|
addAction("Converting")
|
||||||
intentFilter?.addAction("track_download_completed")
|
addAction("track_download_completed")
|
||||||
|
}
|
||||||
updateUIReceiver = object : BroadcastReceiver() {
|
updateUIReceiver = object : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context?, intent: Intent?) {
|
override fun onReceive(context: Context?, intent: Intent?) {
|
||||||
//UI update here
|
//UI update here
|
||||||
@ -145,7 +144,28 @@ abstract class TrackListFragment<VM : TrackListViewModel, args: NavArgs> : BaseF
|
|||||||
}
|
}
|
||||||
viewModel.trackList.value?.set(position, it)
|
viewModel.trackList.value?.set(position, it)
|
||||||
adapter.notifyItemChanged(position)
|
adapter.notifyItemChanged(position)
|
||||||
checkIfAllDownloaded()
|
updateTracksStatus()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val queryFilter = IntentFilter().apply { addAction("query_result") }
|
||||||
|
queryReceiver = object : BroadcastReceiver() {
|
||||||
|
override fun onReceive(context: Context?, intent: Intent?) {
|
||||||
|
//UI update here
|
||||||
|
if (intent != null){
|
||||||
|
val trackList = intent.getParcelableArrayListExtra<TrackDetails?>("tracks") ?: listOf()
|
||||||
|
Log.i("Service Response", "${trackList.size} Tracks Active")
|
||||||
|
for (trackDetails in trackList) {
|
||||||
|
trackDetails?.let { it ->
|
||||||
|
val position: Int = viewModel.trackList.value?.map { it.title }?.indexOf(trackDetails.title) ?: -1
|
||||||
|
Log.i("BroadCast Received","$position, ${it.downloaded} , ${trackDetails.title}")
|
||||||
|
if(position != -1) {
|
||||||
|
viewModel.trackList.value?.set(position,it)
|
||||||
|
adapter.notifyItemChanged(position)
|
||||||
|
updateTracksStatus()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -153,6 +173,7 @@ abstract class TrackListFragment<VM : TrackListViewModel, args: NavArgs> : BaseF
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
requireActivity().registerReceiver(updateUIReceiver, intentFilter)
|
requireActivity().registerReceiver(updateUIReceiver, intentFilter)
|
||||||
|
requireActivity().registerReceiver(queryReceiver, queryFilter)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
@ -163,10 +184,25 @@ abstract class TrackListFragment<VM : TrackListViewModel, args: NavArgs> : BaseF
|
|||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
super.onPause()
|
super.onPause()
|
||||||
requireActivity().unregisterReceiver(updateUIReceiver)
|
requireActivity().unregisterReceiver(updateUIReceiver)
|
||||||
|
requireActivity().unregisterReceiver(queryReceiver)
|
||||||
}
|
}
|
||||||
private fun checkIfAllDownloaded() {
|
|
||||||
if(!viewModel.trackList.value!!.any { it.downloaded == DownloadStatus.NotDownloaded || it.downloaded == DownloadStatus.Queued || it.downloaded == DownloadStatus.Converting }){
|
private fun updateTracksStatus() {
|
||||||
//All Tracks Downloaded
|
var allDownloaded = true
|
||||||
|
var allProcessing = true
|
||||||
|
for (track in viewModel.trackList.value!!){
|
||||||
|
if(track.downloaded != DownloadStatus.Downloaded)allDownloaded = false
|
||||||
|
if(track.downloaded == DownloadStatus.NotDownloaded)allProcessing = false
|
||||||
|
}
|
||||||
|
if(allProcessing){
|
||||||
|
binding.btnDownloadAll.visibility = View.GONE
|
||||||
|
binding.downloadingFab.apply{
|
||||||
|
setImageResource(R.drawable.ic_refresh)
|
||||||
|
visible()
|
||||||
|
rotate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(allDownloaded){
|
||||||
binding.btnDownloadAll.visibility = View.GONE
|
binding.btnDownloadAll.visibility = View.GONE
|
||||||
binding.downloadingFab.apply{
|
binding.downloadingFab.apply{
|
||||||
setImageResource(R.drawable.ic_tick)
|
setImageResource(R.drawable.ic_tick)
|
||||||
|
@ -29,6 +29,7 @@ import com.shabinder.spotiflyer.networking.GaanaInterface
|
|||||||
import com.shabinder.spotiflyer.ui.base.tracklistbase.TrackListViewModel
|
import com.shabinder.spotiflyer.ui.base.tracklistbase.TrackListViewModel
|
||||||
import com.shabinder.spotiflyer.utils.Provider
|
import com.shabinder.spotiflyer.utils.Provider
|
||||||
import com.shabinder.spotiflyer.utils.finalOutputDir
|
import com.shabinder.spotiflyer.utils.finalOutputDir
|
||||||
|
import com.shabinder.spotiflyer.utils.queryActiveTracks
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@ -45,18 +46,25 @@ class GaanaViewModel @ViewModelInject constructor(
|
|||||||
private val gaanaPlaceholderImageUrl = "https://a10.gaanacdn.com/images/social/gaana_social.jpg"
|
private val gaanaPlaceholderImageUrl = "https://a10.gaanacdn.com/images/social/gaana_social.jpg"
|
||||||
|
|
||||||
fun gaanaSearch(type:String,link:String){
|
fun gaanaSearch(type:String,link:String){
|
||||||
when(type){
|
|
||||||
"song" -> {
|
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
|
when (type) {
|
||||||
|
"song" -> {
|
||||||
gaanaInterface.getGaanaSong(seokey = link).value?.tracks?.firstOrNull()?.also {
|
gaanaInterface.getGaanaSong(seokey = link).value?.tracks?.firstOrNull()?.also {
|
||||||
folderType = "Tracks"
|
folderType = "Tracks"
|
||||||
if(File(finalOutputDir(it.track_title,folderType,subFolder)).exists()){//Download Already Present!!
|
if (File(
|
||||||
|
finalOutputDir(
|
||||||
|
it.track_title,
|
||||||
|
folderType,
|
||||||
|
subFolder
|
||||||
|
)
|
||||||
|
).exists()
|
||||||
|
) {//Download Already Present!!
|
||||||
it.downloaded = DownloadStatus.Downloaded
|
it.downloaded = DownloadStatus.Downloaded
|
||||||
}
|
}
|
||||||
trackList.value = listOf(it).toTrackDetailsList()
|
trackList.value = listOf(it).toTrackDetailsList()
|
||||||
title.value = it.track_title
|
title.value = it.track_title
|
||||||
coverUrl.value = it.artworkLink
|
coverUrl.value = it.artworkLink
|
||||||
withContext(Dispatchers.IO){
|
withContext(Dispatchers.IO) {
|
||||||
databaseDAO.insert(
|
databaseDAO.insert(
|
||||||
DownloadRecord(
|
DownloadRecord(
|
||||||
type = "Track",
|
type = "Track",
|
||||||
@ -65,47 +73,71 @@ class GaanaViewModel @ViewModelInject constructor(
|
|||||||
coverUrl = coverUrl.value!!,
|
coverUrl = coverUrl.value!!,
|
||||||
totalFiles = 1,
|
totalFiles = 1,
|
||||||
downloaded = it.downloaded == DownloadStatus.Downloaded,
|
downloaded = it.downloaded == DownloadStatus.Downloaded,
|
||||||
directory = finalOutputDir(it.track_title,folderType,subFolder)
|
directory = finalOutputDir(
|
||||||
|
it.track_title,
|
||||||
|
folderType,
|
||||||
|
subFolder
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"album" -> {
|
"album" -> {
|
||||||
viewModelScope.launch {
|
|
||||||
gaanaInterface.getGaanaAlbum(seokey = link).value?.also {
|
gaanaInterface.getGaanaAlbum(seokey = link).value?.also {
|
||||||
folderType = "Albums"
|
folderType = "Albums"
|
||||||
subFolder = link
|
subFolder = link
|
||||||
it.tracks.forEach { track ->
|
it.tracks.forEach { track ->
|
||||||
if(File(finalOutputDir(track.track_title,folderType,subFolder)).exists()){//Download Already Present!!
|
if (File(
|
||||||
|
finalOutputDir(
|
||||||
|
track.track_title,
|
||||||
|
folderType,
|
||||||
|
subFolder
|
||||||
|
)
|
||||||
|
).exists()
|
||||||
|
) {//Download Already Present!!
|
||||||
track.downloaded = DownloadStatus.Downloaded
|
track.downloaded = DownloadStatus.Downloaded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
trackList.value = it.tracks.toTrackDetailsList()
|
trackList.value = it.tracks.toTrackDetailsList()
|
||||||
title.value = link
|
title.value = link
|
||||||
coverUrl.value = it.custom_artworks.size_480p
|
coverUrl.value = it.custom_artworks.size_480p
|
||||||
withContext(Dispatchers.IO){
|
withContext(Dispatchers.IO) {
|
||||||
databaseDAO.insert(DownloadRecord(
|
databaseDAO.insert(
|
||||||
|
DownloadRecord(
|
||||||
type = "Album",
|
type = "Album",
|
||||||
name = title.value!!,
|
name = title.value!!,
|
||||||
link = "https://gaana.com/$type/$link",
|
link = "https://gaana.com/$type/$link",
|
||||||
coverUrl = coverUrl.value.toString(),
|
coverUrl = coverUrl.value.toString(),
|
||||||
totalFiles = trackList.value?.size ?: 0,
|
totalFiles = trackList.value?.size ?: 0,
|
||||||
downloaded = File(finalOutputDir(type = folderType,subFolder = subFolder)).listFiles()?.size == trackList.value?.size,
|
downloaded = File(
|
||||||
directory = finalOutputDir(type = folderType,subFolder = subFolder)
|
finalOutputDir(
|
||||||
))
|
type = folderType,
|
||||||
}
|
subFolder = subFolder
|
||||||
|
)
|
||||||
|
).listFiles()?.size == trackList.value?.size,
|
||||||
|
directory = finalOutputDir(
|
||||||
|
type = folderType,
|
||||||
|
subFolder = subFolder
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"playlist" -> {
|
"playlist" -> {
|
||||||
viewModelScope.launch {
|
|
||||||
gaanaInterface.getGaanaPlaylist(seokey = link).value?.also {
|
gaanaInterface.getGaanaPlaylist(seokey = link).value?.also {
|
||||||
folderType = "Playlists"
|
folderType = "Playlists"
|
||||||
subFolder = link
|
subFolder = link
|
||||||
it.tracks.forEach {track ->
|
it.tracks.forEach { track ->
|
||||||
if(File(finalOutputDir(track.track_title,folderType,subFolder)).exists()){//Download Already Present!!
|
if (File(
|
||||||
|
finalOutputDir(
|
||||||
|
track.track_title,
|
||||||
|
folderType,
|
||||||
|
subFolder
|
||||||
|
)
|
||||||
|
).exists()
|
||||||
|
) {//Download Already Present!!
|
||||||
track.downloaded = DownloadStatus.Downloaded
|
track.downloaded = DownloadStatus.Downloaded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -113,49 +145,77 @@ class GaanaViewModel @ViewModelInject constructor(
|
|||||||
title.value = link
|
title.value = link
|
||||||
//coverUrl.value = "TODO"
|
//coverUrl.value = "TODO"
|
||||||
coverUrl.value = gaanaPlaceholderImageUrl
|
coverUrl.value = gaanaPlaceholderImageUrl
|
||||||
withContext(Dispatchers.IO){
|
withContext(Dispatchers.IO) {
|
||||||
databaseDAO.insert(DownloadRecord(
|
databaseDAO.insert(
|
||||||
|
DownloadRecord(
|
||||||
type = "Playlist",
|
type = "Playlist",
|
||||||
name = title.value.toString(),
|
name = title.value.toString(),
|
||||||
link = "https://gaana.com/$type/$link",
|
link = "https://gaana.com/$type/$link",
|
||||||
coverUrl = coverUrl.value.toString(),
|
coverUrl = coverUrl.value.toString(),
|
||||||
totalFiles = it.tracks.size,
|
totalFiles = it.tracks.size,
|
||||||
downloaded = File(finalOutputDir(type = folderType,subFolder = subFolder)).listFiles()?.size == trackList.value?.size,
|
downloaded = File(
|
||||||
directory = finalOutputDir(type = folderType,subFolder = subFolder)
|
finalOutputDir(
|
||||||
))
|
type = folderType,
|
||||||
}
|
subFolder = subFolder
|
||||||
|
)
|
||||||
|
).listFiles()?.size == trackList.value?.size,
|
||||||
|
directory = finalOutputDir(
|
||||||
|
type = folderType,
|
||||||
|
subFolder = subFolder
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"artist" -> {
|
"artist" -> {
|
||||||
viewModelScope.launch {
|
|
||||||
folderType = "Artist"
|
folderType = "Artist"
|
||||||
subFolder = link
|
subFolder = link
|
||||||
val artistDetails = gaanaInterface.getGaanaArtistDetails(seokey = link).value?.artist?.firstOrNull()?.also {
|
val artistDetails =
|
||||||
|
gaanaInterface.getGaanaArtistDetails(seokey = link).value?.artist?.firstOrNull()
|
||||||
|
?.also {
|
||||||
title.value = it.name
|
title.value = it.name
|
||||||
coverUrl.value = it.artworkLink
|
coverUrl.value = it.artworkLink
|
||||||
}
|
}
|
||||||
gaanaInterface.getGaanaArtistTracks(seokey = link).value?.also {
|
gaanaInterface.getGaanaArtistTracks(seokey = link).value?.also {
|
||||||
it.tracks.forEach {track ->
|
it.tracks.forEach { track ->
|
||||||
if(File(finalOutputDir(track.track_title,folderType,subFolder)).exists()){//Download Already Present!!
|
if (File(
|
||||||
|
finalOutputDir(
|
||||||
|
track.track_title,
|
||||||
|
folderType,
|
||||||
|
subFolder
|
||||||
|
)
|
||||||
|
).exists()
|
||||||
|
) {//Download Already Present!!
|
||||||
track.downloaded = DownloadStatus.Downloaded
|
track.downloaded = DownloadStatus.Downloaded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
trackList.value = it.tracks.toTrackDetailsList()
|
trackList.value = it.tracks.toTrackDetailsList()
|
||||||
withContext(Dispatchers.IO){
|
withContext(Dispatchers.IO) {
|
||||||
databaseDAO.insert(DownloadRecord(
|
databaseDAO.insert(
|
||||||
|
DownloadRecord(
|
||||||
type = "Artist",
|
type = "Artist",
|
||||||
name = artistDetails?.name ?: link,
|
name = artistDetails?.name ?: link,
|
||||||
link = "https://gaana.com/$type/$link",
|
link = "https://gaana.com/$type/$link",
|
||||||
coverUrl = coverUrl.value.toString(),
|
coverUrl = coverUrl.value.toString(),
|
||||||
totalFiles = trackList.value?.size ?: 0,
|
totalFiles = trackList.value?.size ?: 0,
|
||||||
downloaded = File(finalOutputDir(type = folderType,subFolder = subFolder)).listFiles()?.size == trackList.value?.size,
|
downloaded = File(
|
||||||
directory = finalOutputDir(type = folderType,subFolder = subFolder)
|
finalOutputDir(
|
||||||
))
|
type = folderType,
|
||||||
|
subFolder = subFolder
|
||||||
|
)
|
||||||
|
).listFiles()?.size == trackList.value?.size,
|
||||||
|
directory = finalOutputDir(
|
||||||
|
type = folderType,
|
||||||
|
subFolder = subFolder
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
queryActiveTracks()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,6 +33,7 @@ import com.shabinder.spotiflyer.networking.SpotifyService
|
|||||||
import com.shabinder.spotiflyer.ui.base.tracklistbase.TrackListViewModel
|
import com.shabinder.spotiflyer.ui.base.tracklistbase.TrackListViewModel
|
||||||
import com.shabinder.spotiflyer.utils.Provider.imageDir
|
import com.shabinder.spotiflyer.utils.Provider.imageDir
|
||||||
import com.shabinder.spotiflyer.utils.finalOutputDir
|
import com.shabinder.spotiflyer.utils.finalOutputDir
|
||||||
|
import com.shabinder.spotiflyer.utils.queryActiveTracks
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@ -199,6 +200,7 @@ class SpotifyViewModel @ViewModelInject constructor(
|
|||||||
"show" -> {//TODO
|
"show" -> {//TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
queryActiveTracks()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,11 +28,8 @@ import com.shabinder.spotiflyer.models.DownloadStatus
|
|||||||
import com.shabinder.spotiflyer.models.TrackDetails
|
import com.shabinder.spotiflyer.models.TrackDetails
|
||||||
import com.shabinder.spotiflyer.models.spotify.Source
|
import com.shabinder.spotiflyer.models.spotify.Source
|
||||||
import com.shabinder.spotiflyer.ui.base.tracklistbase.TrackListViewModel
|
import com.shabinder.spotiflyer.ui.base.tracklistbase.TrackListViewModel
|
||||||
|
import com.shabinder.spotiflyer.utils.*
|
||||||
import com.shabinder.spotiflyer.utils.Provider.imageDir
|
import com.shabinder.spotiflyer.utils.Provider.imageDir
|
||||||
import com.shabinder.spotiflyer.utils.finalOutputDir
|
|
||||||
import com.shabinder.spotiflyer.utils.isOnline
|
|
||||||
import com.shabinder.spotiflyer.utils.removeIllegalChars
|
|
||||||
import com.shabinder.spotiflyer.utils.showMessage
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@ -100,6 +97,7 @@ class YoutubeViewModel @ViewModelInject constructor(
|
|||||||
downloaded = File(finalOutputDir(itemName = removeIllegalChars(name),type = folderType,subFolder = subFolder)).exists()
|
downloaded = File(finalOutputDir(itemName = removeIllegalChars(name),type = folderType,subFolder = subFolder)).exists()
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
queryActiveTracks()
|
||||||
}
|
}
|
||||||
}catch (e:com.github.kiulian.downloader.YoutubeException.BadPageException){
|
}catch (e:com.github.kiulian.downloader.YoutubeException.BadPageException){
|
||||||
showMessage("An Error Occurred While Processing!")
|
showMessage("An Error Occurred While Processing!")
|
||||||
@ -145,6 +143,7 @@ class YoutubeViewModel @ViewModelInject constructor(
|
|||||||
directory = finalOutputDir(type = "YT_Downloads")
|
directory = finalOutputDir(type = "YT_Downloads")
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
queryActiveTracks()
|
||||||
}
|
}
|
||||||
} catch (e:com.github.kiulian.downloader.YoutubeException){
|
} catch (e:com.github.kiulian.downloader.YoutubeException){
|
||||||
showMessage("An Error Occurred While Processing!")
|
showMessage("An Error Occurred While Processing!")
|
||||||
|
@ -52,11 +52,17 @@ fun loadAllImages(context: Context?, images:List<String>? = null,source:Source)
|
|||||||
context?.let { ContextCompat.startForegroundService(it, serviceIntent) }
|
context?.let { ContextCompat.startForegroundService(it, serviceIntent) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startService(context:Context?,objects:ArrayList<DownloadObject>? = null ) {
|
fun startService(context:Context? = mainActivity,objects:ArrayList<DownloadObject>? = null ) {
|
||||||
val serviceIntent = Intent(context, ForegroundService::class.java)
|
val serviceIntent = Intent(context, ForegroundService::class.java)
|
||||||
objects?.let { serviceIntent.putParcelableArrayListExtra("object",it) }
|
objects?.let { serviceIntent.putParcelableArrayListExtra("object",it) }
|
||||||
context?.let { ContextCompat.startForegroundService(it, serviceIntent) }
|
context?.let { ContextCompat.startForegroundService(it, serviceIntent) }
|
||||||
}
|
}
|
||||||
|
fun queryActiveTracks(context:Context? = mainActivity) {
|
||||||
|
val serviceIntent = Intent(context, ForegroundService::class.java).apply {
|
||||||
|
action = "query"
|
||||||
|
}
|
||||||
|
context?.let { ContextCompat.startForegroundService(it, serviceIntent) }
|
||||||
|
}
|
||||||
|
|
||||||
fun finalOutputDir(itemName:String? = null,type:String, subFolder:String?=null,extension:String? = ".mp3"): String{
|
fun finalOutputDir(itemName:String? = null,type:String, subFolder:String?=null,extension:String? = ".mp3"): String{
|
||||||
return defaultDir + removeIllegalChars(type) + File.separator +
|
return defaultDir + removeIllegalChars(type) + File.separator +
|
||||||
|
@ -47,6 +47,7 @@ import com.mpatric.mp3agic.ID3v24Tag
|
|||||||
import com.mpatric.mp3agic.Mp3File
|
import com.mpatric.mp3agic.Mp3File
|
||||||
import com.shabinder.spotiflyer.R
|
import com.shabinder.spotiflyer.R
|
||||||
import com.shabinder.spotiflyer.models.DownloadObject
|
import com.shabinder.spotiflyer.models.DownloadObject
|
||||||
|
import com.shabinder.spotiflyer.models.DownloadStatus
|
||||||
import com.shabinder.spotiflyer.models.TrackDetails
|
import com.shabinder.spotiflyer.models.TrackDetails
|
||||||
import com.shabinder.spotiflyer.models.spotify.Source
|
import com.shabinder.spotiflyer.models.spotify.Source
|
||||||
import com.shabinder.spotiflyer.utils.Provider
|
import com.shabinder.spotiflyer.utils.Provider
|
||||||
@ -73,11 +74,12 @@ class ForegroundService : Service(){
|
|||||||
private var serviceJob = Job()
|
private var serviceJob = Job()
|
||||||
private val serviceScope = CoroutineScope(Dispatchers.IO + serviceJob)
|
private val serviceScope = CoroutineScope(Dispatchers.IO + serviceJob)
|
||||||
private val requestMap = mutableMapOf<Request, TrackDetails>()
|
private val requestMap = mutableMapOf<Request, TrackDetails>()
|
||||||
|
private val allTracksDetails = mutableListOf<TrackDetails>()
|
||||||
private var defaultDir = Provider.defaultDir
|
private var defaultDir = Provider.defaultDir
|
||||||
private var wakeLock: PowerManager.WakeLock? = null
|
private var wakeLock: PowerManager.WakeLock? = null
|
||||||
private var isServiceStarted = false
|
private var isServiceStarted = false
|
||||||
var notificationLine = 0
|
private var notificationLine = 0
|
||||||
var messageList = mutableListOf("", "", "", "")
|
private var messageList = mutableListOf("", "", "", "")
|
||||||
private var cancelIntent:PendingIntent? = null
|
private var cancelIntent:PendingIntent? = null
|
||||||
|
|
||||||
override fun onBind(intent: Intent): IBinder? = null
|
override fun onBind(intent: Intent): IBinder? = null
|
||||||
@ -103,6 +105,14 @@ class ForegroundService : Service(){
|
|||||||
|
|
||||||
if(intent.action == "kill") killService()
|
if(intent.action == "kill") killService()
|
||||||
|
|
||||||
|
if(intent.action == "query"){
|
||||||
|
val response = Intent().apply {
|
||||||
|
action = "query_result"
|
||||||
|
putParcelableArrayListExtra("tracks", allTracksDetails as ArrayList<TrackDetails> )
|
||||||
|
}
|
||||||
|
sendBroadcast(response)
|
||||||
|
}
|
||||||
|
|
||||||
val downloadObjects: ArrayList<DownloadObject>? = (intent.getParcelableArrayListExtra("object") ?: intent.extras?.getParcelableArrayList(
|
val downloadObjects: ArrayList<DownloadObject>? = (intent.getParcelableArrayListExtra("object") ?: intent.extras?.getParcelableArrayList(
|
||||||
"object"
|
"object"
|
||||||
))
|
))
|
||||||
@ -119,6 +129,9 @@ class ForegroundService : Service(){
|
|||||||
downloadObjects?.let {
|
downloadObjects?.let {
|
||||||
total += downloadObjects.size
|
total += downloadObjects.size
|
||||||
updateNotification()
|
updateNotification()
|
||||||
|
it.forEach { it1 ->
|
||||||
|
allTracksDetails.add(it1.trackDetails.apply { downloaded = DownloadStatus.Queued })
|
||||||
|
}
|
||||||
downloadAllTracks(downloadObjects)
|
downloadAllTracks(downloadObjects)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,7 +220,6 @@ class ForegroundService : Service(){
|
|||||||
download: Download,
|
download: Download,
|
||||||
waitingOnNetwork: Boolean
|
waitingOnNetwork: Boolean
|
||||||
) {
|
) {
|
||||||
//Notify Download Completed
|
|
||||||
val intent = Intent()
|
val intent = Intent()
|
||||||
.setAction(Status.QUEUED.name)
|
.setAction(Status.QUEUED.name)
|
||||||
.putExtra("track", requestMap[download.request])
|
.putExtra("track", requestMap[download.request])
|
||||||
@ -247,10 +259,14 @@ class ForegroundService : Service(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log.i(tag, "${track?.title} Download Started")
|
Log.i(tag, "${track?.title} Download Started")
|
||||||
|
track?.let{
|
||||||
|
allTracksDetails[allTracksDetails.map{ trackDetails -> trackDetails.title}.indexOf(it.title)] =
|
||||||
|
it.apply { downloaded = DownloadStatus.Downloading }
|
||||||
|
}
|
||||||
updateNotification()
|
updateNotification()
|
||||||
val intent = Intent()
|
val intent = Intent()
|
||||||
.setAction(Status.DOWNLOADING.name)
|
.setAction(Status.DOWNLOADING.name)
|
||||||
.putExtra("track", requestMap[download.request])
|
.putExtra("track", track)
|
||||||
this@ForegroundService.sendBroadcast(intent)
|
this@ForegroundService.sendBroadcast(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -277,7 +293,11 @@ class ForegroundService : Service(){
|
|||||||
|
|
||||||
serviceScope.launch {
|
serviceScope.launch {
|
||||||
try{
|
try{
|
||||||
track?.let { convertToMp3(download.file, it) }
|
track?.let {
|
||||||
|
convertToMp3(download.file, it)
|
||||||
|
allTracksDetails[allTracksDetails.map{ trackDetails -> trackDetails.title}.indexOf(it.title)] =
|
||||||
|
it.apply { downloaded = DownloadStatus.Converting }
|
||||||
|
}
|
||||||
Log.i(tag, "${track?.title} Download Completed")
|
Log.i(tag, "${track?.title} Download Completed")
|
||||||
}catch (
|
}catch (
|
||||||
e: KotlinNullPointerException
|
e: KotlinNullPointerException
|
||||||
@ -360,6 +380,8 @@ class ForegroundService : Service(){
|
|||||||
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
|
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
|
||||||
//Checking if the received broadcast is for our enqueued download by matching download id
|
//Checking if the received broadcast is for our enqueued download by matching download id
|
||||||
if (downloadID == id) {
|
if (downloadID == id) {
|
||||||
|
allTracksDetails[allTracksDetails.map{ trackDetails -> trackDetails.title}.indexOf(track.title)] =
|
||||||
|
track.apply { downloaded = DownloadStatus.Converting }
|
||||||
convertToMp3(outputDir, track)
|
convertToMp3(outputDir, track)
|
||||||
converted++
|
converted++
|
||||||
//Unregister this broadcast Receiver
|
//Unregister this broadcast Receiver
|
||||||
@ -419,7 +441,7 @@ class ForegroundService : Service(){
|
|||||||
newFile.renameTo(file)
|
newFile.renameTo(file)
|
||||||
converted++
|
converted++
|
||||||
updateNotification()
|
updateNotification()
|
||||||
|
allTracksDetails.removeAt(allTracksDetails.map{ trackDetails -> trackDetails.title}.indexOf(track.title))
|
||||||
//Notify Download Completed
|
//Notify Download Completed
|
||||||
val intent = Intent()
|
val intent = Intent()
|
||||||
.setAction("track_download_completed")
|
.setAction("track_download_completed")
|
||||||
|
Loading…
Reference in New Issue
Block a user