Skip to content

Commit

Permalink
Added a way to select which song will be downloaded in the song tab a…
Browse files Browse the repository at this point in the history
…nd fixed ui errors

This commit contains the addition of a recycler to select the exact song to be downloaded from a list and a couple of ui errors
  • Loading branch information
kostas214 committed May 15, 2023
1 parent afc51f8 commit 767c7ee
Show file tree
Hide file tree
Showing 23 changed files with 377 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.example.spotifydownloader.Fragments

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import androidx.activity.OnBackPressedCallback
import androidx.core.view.forEach
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.spotifydownloader.R
import com.example.spotifydownloader.databinding.FragmentSelectSongBinding
import com.example.spotifydownloader.parcels.DataSearch
import com.example.spotifydownloader.rvAdaptors.SongSearchRecyclerViewAdaptor
import com.google.android.material.bottomnavigation.BottomNavigationView


class selectSongFragment : Fragment(R.layout.fragment_select_song) {
private lateinit var binding :FragmentSelectSongBinding
private lateinit var navController: NavController
private val args by navArgs<selectSongFragmentArgs>()
private lateinit var bottomNav : BottomNavigationView






override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentSelectSongBinding.bind(view)
navController = Navigation.findNavController(view)
bottomNav = activity?.findViewById(R.id.bottomNav)!!

val rV = binding.rvSongSearch

val from_top : Animation by lazy { AnimationUtils.loadAnimation((context as Activity),
R.anim.from_top
) }
val from_bottom : Animation by lazy { AnimationUtils.loadAnimation((context as Activity),
R.anim.from_bottom
) }


val callback = object : OnBackPressedCallback(true){
override fun handleOnBackPressed() {
navController.popBackStack()
bottomNav.startAnimation(from_bottom)
bottomNav.menu.forEach {
it.isEnabled = true
}


}
}
requireActivity().onBackPressedDispatcher.addCallback(callback)


bottomNav.startAnimation(from_top)
bottomNav.menu.forEach {
it.isEnabled = false
}


val adapter = SongSearchRecyclerViewAdaptor(args.ItemListData.Items)
rV.adapter = adapter
rV.layoutManager = LinearLayoutManager(context)
adapter.setOnItemClickListener(object :SongSearchRecyclerViewAdaptor.onItemClickListener{
override fun onItemClick(position: Int) {
val regex = Regex("[^A-Za-z0-9]")
val songData = args.ItemListData.Items[position]


val data = DataSearch(
songName = songData.name,
imgUrl = songData.album.images[0].url,
artistName = songData.artists[0].name,
filename = regex.replace(songData.name,""),
albumName = songData.album.name,
albumArtistName = songData.album.artists[0].name,
releaseDate = songData.album.release_date,
folderUri = args.ItemListData.folderUri
)
val action = selectSongFragmentDirections.actionSelectSongFragmentToSongDownloadFragment2(data)
navController.navigate(action)



}

})





















}




}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import androidx.navigation.fragment.navArgs
import coil.load
import com.example.spotifydownloader.R
import com.example.spotifydownloader.databinding.FragmentSongDownloadBinding
import com.example.spotifydownloader.model.songItemData
import com.example.spotifydownloader.rvData.songItemData
import com.example.spotifydownloader.mp3agic.Mp3File
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.yausername.youtubedl_android.YoutubeDL
Expand All @@ -42,12 +42,13 @@ import java.net.URL
class SongDownloadFragment : Fragment(R.layout.fragment_song_download) {
private var fragmentSongDownload: FragmentSongDownloadBinding? = null
private val tag = "MainActivity"
private lateinit var bottomNav : BottomNavigationView
private lateinit var navController: NavController
private val args by navArgs<SongDownloadFragmentArgs>()
private var stop = false
private var songItems = mutableListOf<songItemData>()
private lateinit var binding:FragmentSongDownloadBinding
private lateinit var bottomNav : BottomNavigationView




Expand All @@ -63,17 +64,10 @@ class SongDownloadFragment : Fragment(R.layout.fragment_song_download) {
bottomNav = activity?.findViewById(R.id.bottomNav)!!


val from_top : Animation by lazy { AnimationUtils.loadAnimation((context as Activity),
R.anim.from_top
) }

val from_bottom : Animation by lazy { AnimationUtils.loadAnimation((context as Activity),
R.anim.from_bottom
) }
bottomNav.startAnimation(from_top)
bottomNav.menu.forEach {
it.isEnabled = false
}

val callback = object : OnBackPressedCallback(true){
override fun handleOnBackPressed() {
Toast.makeText((context as Activity),"Stopping...",Toast.LENGTH_SHORT).show()
Expand All @@ -100,6 +94,7 @@ class SongDownloadFragment : Fragment(R.layout.fragment_song_download) {
Toast.LENGTH_SHORT
).show()
navController.popBackStack()
navController.popBackStack()
bottomNav.startAnimation(from_bottom)
bottomNav.menu.forEach {
it.isEnabled = true
Expand All @@ -117,6 +112,7 @@ class SongDownloadFragment : Fragment(R.layout.fragment_song_download) {
Toast.LENGTH_SHORT
).show()
navController.popBackStack()
navController.popBackStack()
bottomNav.startAnimation(from_bottom)
bottomNav.menu.forEach {
it.isEnabled = true
Expand All @@ -135,6 +131,7 @@ class SongDownloadFragment : Fragment(R.layout.fragment_song_download) {
Toast.LENGTH_SHORT
).show()
navController.popBackStack()
navController.popBackStack()
bottomNav.startAnimation(from_bottom)
bottomNav.menu.forEach {
it.isEnabled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ import androidx.navigation.NavController
import androidx.navigation.Navigation
import com.example.spotifydownloader.R
import com.example.spotifydownloader.SpotifyApi.SpotifyApi
import com.example.spotifydownloader.SpotifyApi.model.Search.Item
import com.example.spotifydownloader.SpotifyApi.util.Constants.Companion.CLIENT_ID
import com.example.spotifydownloader.SpotifyApi.util.Constants.Companion.CLIENT_SECRET
import com.example.spotifydownloader.databinding.FragmentSongBinding
import com.example.spotifydownloader.model.DataSearch
import com.example.spotifydownloader.parcels.ItemListData
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.io.IOException
import java.lang.NullPointerException



class SongFragment : Fragment(R.layout.fragment_song) {
Expand Down Expand Up @@ -91,36 +89,26 @@ class SongFragment : Fragment(R.layout.fragment_song) {
runOnUiThread {
enableDisableUI(false)
}
var songName =""
var imgUrl =""
var artistName = ""
var filename = ""
var albumName = ""
var albumArtistName = ""
var releaseDate = ""
var succesCode :Int
var succesCode = 0
val ItemListObject = mutableListOf<Item>()





try {


val textInBox = binding.songNameEditText.text.toString()
val textInBox = "${binding.songNameEditText.text.toString()} ${binding.artistNameEditText.text.toString()}"
val response = spotifyApi.getSearch(textInBox)



if (response.tracks.items.size !=0) {
val searchResult = response.tracks.items[0]
val regex = Regex("[^A-Za-z0-9]")
songName = searchResult.name
imgUrl = searchResult.album.images[0].url
artistName = searchResult.artists[0].name
filename = regex.replace(searchResult.name, "")
albumName = searchResult.album.name
albumArtistName = searchResult.album.artists[0].name
releaseDate = searchResult.album.release_date
response.tracks.items.forEach {
ItemListObject.add(it)
}

succesCode = 0
}
else{
Expand Down Expand Up @@ -149,6 +137,7 @@ class SongFragment : Fragment(R.layout.fragment_song) {


if (isDeviceOnline(context as Activity)&&data!=null&& succesCode== 0){
/*
val dataToBeSent = DataSearch(
songName = songName,
imgUrl = imgUrl,
Expand All @@ -159,7 +148,9 @@ class SongFragment : Fragment(R.layout.fragment_song) {
releaseDate = releaseDate,
folderUri = data!!.data
)
val action = SongFragmentDirections.actionSongFragmentToSongDownloadFragment(dataToBeSent)
*/
val action = SongFragmentDirections.actionSongFragmentToSelectSongFragment2(ItemListData(ItemListObject.toList(),data!!.data))
runOnUiThread {
enableDisableUI(true)
navController.navigate(action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.example.spotifydownloader.R
import com.example.spotifydownloader.SpotifyApi.SpotifyApi
import com.example.spotifydownloader.SpotifyApi.util.Constants
import com.example.spotifydownloader.databinding.FragmentAlbumBinding
import com.example.spotifydownloader.model.Data
import com.example.spotifydownloader.parcels.Data
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.spotifydownloader.R
import com.example.spotifydownloader.databinding.FragmentDownloadBinding
import com.example.spotifydownloader.model.recyclerViewAdaptor
import com.example.spotifydownloader.model.songItemData
import com.example.spotifydownloader.rvAdaptors.recyclerViewAdaptor
import com.example.spotifydownloader.rvData.songItemData
import com.example.spotifydownloader.mp3agic.Mp3File
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.yausername.youtubedl_android.YoutubeDL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.example.spotifydownloader.SpotifyApi.SpotifyApi
import com.example.spotifydownloader.SpotifyApi.util.Constants.Companion.CLIENT_ID
import com.example.spotifydownloader.SpotifyApi.util.Constants.Companion.CLIENT_SECRET
import com.example.spotifydownloader.databinding.FragmentPlayListBinding
import com.example.spotifydownloader.model.Data
import com.example.spotifydownloader.parcels.Data
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class SpotifyApi(private val clientId: String, private val clientSecret: String,
}


suspend fun getSearch(query:String):Search{
suspend fun getSearch(query:String ):Search{
if (authToken == null){
getAuthTokenOrRefresh(
clientId = clientId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ interface SpotifyAPI {
@HeaderMap headers: Map<String,String>,
@Query("q") searchQuery:String,
@Query("type")type:String = "track",
@Query("limit")limit:Int = 1


):Response<Search>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.example.spotifydownloader.SpotifyApi.model.Search

import android.os.Parcelable



data class Item(
val album: Album,
val artists: List<ArtistX>,
val album: Album,
val artists: List<ArtistX>,
val disc_number: Int,
val duration_ms: Int,
val explicit: Boolean,
val external_ids: ExternalIds,
val external_urls: ExternalUrlsXXX,
val external_ids: ExternalIds,
val external_urls: ExternalUrlsXXX,
val href: String,
val id: String,
val is_local: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.spotifydownloader.model
package com.example.spotifydownloader.parcels


import android.net.Uri
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.spotifydownloader.model
package com.example.spotifydownloader.parcels

import android.net.Uri
import android.os.Parcelable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.spotifydownloader.parcels

import android.net.Uri
import android.os.Parcelable
import com.example.spotifydownloader.SpotifyApi.model.Search.Item
import kotlinx.parcelize.Parcelize
import kotlinx.parcelize.RawValue

@Parcelize
data class ItemListData(
var Items : @RawValue List<Item>,
var folderUri: Uri?
):Parcelable
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.spotifydownloader.model
package com.example.spotifydownloader.rvAdaptors

import android.view.LayoutInflater
import android.view.View
Expand All @@ -9,6 +9,7 @@ import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.example.spotifydownloader.R
import com.example.spotifydownloader.rvData.songItemData

class recyclerViewAdaptor (private val songData: List<songItemData>)
:RecyclerView.Adapter<recyclerViewAdaptor.ViewHolder>() {
Expand Down Expand Up @@ -40,7 +41,7 @@ class recyclerViewAdaptor (private val songData: List<songItemData>)
holder.songCoverImage.load(recyclerViewAdaptor.imageUrl)


if (recyclerViewAdaptor.songName.length>19) {
if (recyclerViewAdaptor.songName.length>24) {
holder.songTitle.text = recyclerViewAdaptor.songName.take(17).plus("...")
}
else{
Expand Down
Loading

0 comments on commit 767c7ee

Please sign in to comment.