From d4823f0e1050cbe33a2dd9834fc554fae65a9a1d Mon Sep 17 00:00:00 2001 From: shabinder Date: Tue, 21 Jul 2020 17:02:52 +0530 Subject: [PATCH] Ui Revamp ,And Functionality Improved(Album,Playlist,RecyclerView) --- .idea/dictionaries/shabinder.xml | 1 + app/build.gradle | 2 +- .../musicForEveryone/MainActivity.kt | 1 + .../musicForEveryone/SharedViewModel.kt | 3 +- .../fragments/MainFragment.kt | 85 ++++----- .../recyclerView/TrackListAdapter.kt | 54 ++++++ .../{ => utils}/BindingAdapter.kt | 3 +- .../{ => utils}/SpotifyNewService.kt | 2 +- .../main/res/drawable/ic_musicplaceholder.xml | 12 ++ app/src/main/res/drawable/text_backdround.xml | 12 ++ app/src/main/res/layout/main_fragment.xml | 166 ++++++------------ app/src/main/res/layout/track_list_item.xml | 138 +++++++++++++++ app/src/main/res/values/colors.xml | 11 +- app/src/main/res/values/styles.xml | 34 +++- 14 files changed, 356 insertions(+), 168 deletions(-) create mode 100644 app/src/main/java/com/shabinder/musicForEveryone/recyclerView/TrackListAdapter.kt rename app/src/main/java/com/shabinder/musicForEveryone/{ => utils}/BindingAdapter.kt (86%) rename app/src/main/java/com/shabinder/musicForEveryone/{ => utils}/SpotifyNewService.kt (84%) create mode 100644 app/src/main/res/drawable/ic_musicplaceholder.xml create mode 100644 app/src/main/res/drawable/text_backdround.xml create mode 100644 app/src/main/res/layout/track_list_item.xml diff --git a/.idea/dictionaries/shabinder.xml b/.idea/dictionaries/shabinder.xml index 53539b4a..841adff3 100644 --- a/.idea/dictionaries/shabinder.xml +++ b/.idea/dictionaries/shabinder.xml @@ -2,6 +2,7 @@ musicforeveryone + musicplaceholder shabinder spotify spotifydownloader diff --git a/app/build.gradle b/app/build.gradle index 3682a22b..d142aded 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -71,7 +71,7 @@ dependencies { implementation 'com.google.code.gson:gson:2.8.6' implementation 'com.squareup.okhttp3:okhttp:4.8.0' - implementation 'com.github.sealedtx:java-youtube-downloader:2.2.1' + implementation 'com.github.sealedtx:java-youtube-downloader:2.2.2' implementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava' testImplementation 'junit:junit:4.13' diff --git a/app/src/main/java/com/shabinder/musicForEveryone/MainActivity.kt b/app/src/main/java/com/shabinder/musicForEveryone/MainActivity.kt index c9cf90ab..dd2075d8 100644 --- a/app/src/main/java/com/shabinder/musicForEveryone/MainActivity.kt +++ b/app/src/main/java/com/shabinder/musicForEveryone/MainActivity.kt @@ -13,6 +13,7 @@ import androidx.databinding.DataBindingUtil import androidx.lifecycle.ViewModelProvider import com.github.kiulian.downloader.YoutubeDownloader import com.shabinder.musicForEveryone.databinding.MainActivityBinding +import com.shabinder.musicForEveryone.utils.SpotifyNewService import com.shabinder.musicForEveryone.utils.YoutubeInterface import com.spotify.sdk.android.authentication.AuthenticationClient import com.spotify.sdk.android.authentication.AuthenticationRequest diff --git a/app/src/main/java/com/shabinder/musicForEveryone/SharedViewModel.kt b/app/src/main/java/com/shabinder/musicForEveryone/SharedViewModel.kt index 4f37d647..597cbf5b 100644 --- a/app/src/main/java/com/shabinder/musicForEveryone/SharedViewModel.kt +++ b/app/src/main/java/com/shabinder/musicForEveryone/SharedViewModel.kt @@ -4,6 +4,7 @@ import android.app.DownloadManager import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.github.kiulian.downloader.YoutubeDownloader +import com.shabinder.musicForEveryone.utils.SpotifyNewService import kaaes.spotify.webapi.android.SpotifyService import kaaes.spotify.webapi.android.models.Album import kaaes.spotify.webapi.android.models.Playlist @@ -13,7 +14,7 @@ class SharedViewModel : ViewModel() { var accessToken:String = "" var userName = MutableLiveData().apply { value = "Placeholder" } var spotify :SpotifyService? = null - var spotifyExtra :SpotifyNewService? = null + var spotifyExtra : SpotifyNewService? = null var ytDownloader : YoutubeDownloader? = null var downloadManager : DownloadManager? = null diff --git a/app/src/main/java/com/shabinder/musicForEveryone/fragments/MainFragment.kt b/app/src/main/java/com/shabinder/musicForEveryone/fragments/MainFragment.kt index 5cbd8d49..b63f0721 100644 --- a/app/src/main/java/com/shabinder/musicForEveryone/fragments/MainFragment.kt +++ b/app/src/main/java/com/shabinder/musicForEveryone/fragments/MainFragment.kt @@ -10,13 +10,14 @@ import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider -import com.google.android.material.snackbar.Snackbar import com.shabinder.musicForEveryone.R import com.shabinder.musicForEveryone.SharedViewModel -import com.shabinder.musicForEveryone.bindImage import com.shabinder.musicForEveryone.databinding.MainFragmentBinding import com.shabinder.musicForEveryone.downloadHelper.DownloadHelper +import com.shabinder.musicForEveryone.recyclerView.TrackListAdapter +import com.shabinder.musicForEveryone.utils.bindImage import kaaes.spotify.webapi.android.SpotifyService +import kaaes.spotify.webapi.android.models.Track class MainFragment : Fragment(),DownloadHelper { @@ -34,10 +35,10 @@ class MainFragment : Fragment(),DownloadHelper { spotify = sharedViewModel.spotify sharedViewModel.userName.observe(viewLifecycleOwner, Observer { binding.message.text = it - if(it!="Placeholder"){Snackbar.make(requireView(),"Hello, $it!", Snackbar.LENGTH_SHORT).show()} - }) +// if(it!="Placeholder"){Snackbar.make(requireView(),"Hello, $it!", Snackbar.LENGTH_SHORT).show()} + }) - binding.btnGetDetails.setOnClickListener { + binding.btnSearch.setOnClickListener { val spotifyLink = binding.spotifyLink.text.toString() val link = spotifyLink.substringAfterLast('/' , "Error").substringBefore('?') @@ -45,47 +46,55 @@ class MainFragment : Fragment(),DownloadHelper { Log.i("Fragment", "$type : $link") + val adapter = TrackListAdapter() + binding.trackList.adapter = adapter + adapter.sharedViewModel = sharedViewModel + when(type){ "track" -> { val trackObject = sharedViewModel.getTrackDetails(link) - Log.i("Fragment",trackObject?.name.toString()) - binding.name.text = trackObject?.name ?: "Error" - var artistNames = "" - trackObject?.artists?.forEach { artistNames = artistNames.plus("${it.name} ,") } - binding.artist.text = artistNames - binding.popularity.text = trackObject?.popularity.toString() - binding.duration.text = ((trackObject?.duration_ms!! /1000/60).toString() + "minutes") - binding.albumName.text = trackObject.album.name - bindImage(binding.imageUrl, trackObject.album.images[0].url) + + val trackList = mutableListOf() + trackList.add(trackObject!!) + adapter.totalItems = 1 + adapter.trackList = trackList + adapter.notifyDataSetChanged() + + Log.i("Adapter",trackList.size.toString()) } "album" -> { val albumObject = sharedViewModel.getAlbumDetails(link) - Log.i("Fragment",albumObject?.name.toString()) - binding.name.text = albumObject?.name ?: "Error" - var artistNames = "" - albumObject?.artists?.forEach { artistNames = artistNames.plus(", ${it.name}") } - binding.artist.text = artistNames - binding.popularity.text = albumObject?.popularity.toString() - binding.duration.visibility = View.GONE - binding.textView5.visibility = View.GONE - binding.albumName.text = albumObject?.name - bindImage(binding.imageUrl, albumObject?.images?.get(0)?.url) + + bindImage(binding.imageView,albumObject!!.images[1].url) + binding.titleView.text = albumObject.name + binding.titleView.visibility =View.VISIBLE + binding.imageView.visibility =View.VISIBLE + + val trackList = mutableListOf() + albumObject.tracks?.items?.forEach { trackList.add(it as Track) } + adapter.totalItems = trackList.size + adapter.trackList = trackList + adapter.notifyDataSetChanged() + + Log.i("Adapter",trackList.size.toString()) + } "playlist" -> { val playlistObject = sharedViewModel.getPlaylistDetails(link) - Log.i("Fragment",playlistObject?.name.toString()) - binding.name.text = playlistObject?.name ?: "Error" - binding.artist.visibility = View.GONE - binding.textView1.visibility = View.GONE - binding.popularity.text = playlistObject?.followers?.total.toString() - binding.textview3.text = "Followers" - binding.duration.visibility = View.GONE - binding.textView5.visibility = View.GONE - binding.textView7.text = "Total Tracks" - binding.albumName.text = playlistObject?.tracks?.items?.size.toString() - bindImage(binding.imageUrl, playlistObject?.images?.get(0)?.url) + + bindImage(binding.imageView,playlistObject!!.images[0].url) + binding.titleView.text = playlistObject.name + binding.titleView.visibility =View.VISIBLE + binding.imageView.visibility =View.VISIBLE + + val trackList = mutableListOf() + playlistObject.tracks?.items!!.forEach { trackList.add(it.track) } + adapter.trackList = trackList.toList() + adapter.totalItems = trackList.size + adapter.notifyDataSetChanged() + Log.i("Adapter",trackList.size.toString()) } "episode" -> {showToast("Implementation Pending")} @@ -94,12 +103,6 @@ class MainFragment : Fragment(),DownloadHelper { binding.spotifyLink.setText(link) } - - binding.btnDownload.setOnClickListener { - downloadTrack(sharedViewModel.ytDownloader,sharedViewModel.downloadManager,"${binding.name.text} ${if(binding.artist.text != "TextView" ){binding.artist.text}else{""}}") - } - - return binding.root } diff --git a/app/src/main/java/com/shabinder/musicForEveryone/recyclerView/TrackListAdapter.kt b/app/src/main/java/com/shabinder/musicForEveryone/recyclerView/TrackListAdapter.kt new file mode 100644 index 00000000..7e97cbfe --- /dev/null +++ b/app/src/main/java/com/shabinder/musicForEveryone/recyclerView/TrackListAdapter.kt @@ -0,0 +1,54 @@ +package com.shabinder.musicForEveryone.recyclerView + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.Button +import android.widget.ImageView +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import com.shabinder.musicForEveryone.R +import com.shabinder.musicForEveryone.SharedViewModel +import com.shabinder.musicForEveryone.downloadHelper.DownloadHelper +import com.shabinder.musicForEveryone.utils.bindImage +import kaaes.spotify.webapi.android.models.Track + +class TrackListAdapter:RecyclerView.Adapter(),DownloadHelper { + + var trackList = listOf() + var totalItems:Int = 0 + var sharedViewModel = SharedViewModel() + + override fun getItemCount():Int = totalItems + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + val layoutInflater = LayoutInflater.from(parent.context) + + val view = layoutInflater.inflate(R.layout.track_list_item,parent,false) + return ViewHolder(view) + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val item = trackList[position] + + bindImage(holder.coverImage,item.album.images[0].url) + + holder.trackName.text = "${if(item.name.length > 22){"${item.name.subSequence(0,19)}..."}else{item.name}}" + holder.artistName.text = "${item.artists[0]?.name?:""}..." + holder.popularity.text = item.popularity.toString() + holder.duration.text = "${item.duration_ms/1000/60} minutes, ${(item.duration_ms/1000)%60} seconds" + holder.downloadBtn.setOnClickListener{ + downloadTrack(sharedViewModel.ytDownloader,sharedViewModel.downloadManager,"${item.name} ${item.artists[0].name?:""}") + } + + } + class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){ + val trackName:TextView = itemView.findViewById(R.id.track_name) + val artistName:TextView = itemView.findViewById(R.id.artist) + val popularity:TextView = itemView.findViewById(R.id.popularity) + val duration:TextView = itemView.findViewById(R.id.duration) + val downloadBtn:Button = itemView.findViewById(R.id.btn_download) + val coverImage:ImageView = itemView.findViewById(R.id.imageUrl) + + } +} \ No newline at end of file diff --git a/app/src/main/java/com/shabinder/musicForEveryone/BindingAdapter.kt b/app/src/main/java/com/shabinder/musicForEveryone/utils/BindingAdapter.kt similarity index 86% rename from app/src/main/java/com/shabinder/musicForEveryone/BindingAdapter.kt rename to app/src/main/java/com/shabinder/musicForEveryone/utils/BindingAdapter.kt index e4bfb6d3..f17edd91 100644 --- a/app/src/main/java/com/shabinder/musicForEveryone/BindingAdapter.kt +++ b/app/src/main/java/com/shabinder/musicForEveryone/utils/BindingAdapter.kt @@ -1,10 +1,11 @@ -package com.shabinder.musicForEveryone +package com.shabinder.musicForEveryone.utils import android.widget.ImageView import androidx.core.net.toUri import androidx.databinding.BindingAdapter import com.bumptech.glide.Glide import com.bumptech.glide.request.RequestOptions +import com.shabinder.musicForEveryone.R @BindingAdapter("imageUrl") diff --git a/app/src/main/java/com/shabinder/musicForEveryone/SpotifyNewService.kt b/app/src/main/java/com/shabinder/musicForEveryone/utils/SpotifyNewService.kt similarity index 84% rename from app/src/main/java/com/shabinder/musicForEveryone/SpotifyNewService.kt rename to app/src/main/java/com/shabinder/musicForEveryone/utils/SpotifyNewService.kt index 8438c7d6..63f76103 100644 --- a/app/src/main/java/com/shabinder/musicForEveryone/SpotifyNewService.kt +++ b/app/src/main/java/com/shabinder/musicForEveryone/utils/SpotifyNewService.kt @@ -1,4 +1,4 @@ -package com.shabinder.musicForEveryone +package com.shabinder.musicForEveryone.utils import kaaes.spotify.webapi.android.models.Playlist import retrofit.http.GET diff --git a/app/src/main/res/drawable/ic_musicplaceholder.xml b/app/src/main/res/drawable/ic_musicplaceholder.xml new file mode 100644 index 00000000..83a828d0 --- /dev/null +++ b/app/src/main/res/drawable/ic_musicplaceholder.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable/text_backdround.xml b/app/src/main/res/drawable/text_backdround.xml new file mode 100644 index 00000000..677cbb52 --- /dev/null +++ b/app/src/main/res/drawable/text_backdround.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/main_fragment.xml b/app/src/main/res/layout/main_fragment.xml index 45fc6649..4ba11d8b 100644 --- a/app/src/main/res/layout/main_fragment.xml +++ b/app/src/main/res/layout/main_fragment.xml @@ -14,8 +14,13 @@ android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginBottom="16dp" + android:layout_marginBottom="8dp" android:text="MainFragment" + android:background="@drawable/text_backdround" + android:padding="5dp" + android:textColor="@color/colorPrimary" + android:textSize="12dp" + android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> @@ -24,146 +29,77 @@ android:id="@+id/spotifyLink" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginTop="24dp" + android:layout_marginTop="25dp" + android:background="@drawable/text_backdround" + android:hint="Link From Spotify" android:ems="10" + android:padding="5dp" android:inputType="text" - android:text="Song Link From Spotify" - app:layout_constraintEnd_toStartOf="@+id/btn_getDetails" + android:textAlignment="center" + android:textColor="@color/white" + android:textColorHint="@color/grey" + android:textSize="20dp" + app:layout_constraintEnd_toStartOf="@+id/btn_search" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />