Skip to content

Commit

Permalink
Fixed #142
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Oct 29, 2023
1 parent f7236ed commit 01fa384
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlin.math.abs
import kotlin.random.Random

@AndroidEntryPoint
@UnstableApi
Expand Down Expand Up @@ -142,6 +143,47 @@ class AlbumFragment: Fragment() {
viewModel.albumEntity.value?.let { album -> viewModel.updateAlbumLiked(true, album.browseId) }
}
}
binding.btShuffle.setOnClickListener {
if (viewModel.albumBrowse.value is Resource.Success && viewModel.albumBrowse.value?.data != null){
val args = Bundle()
val index = Random.nextInt(viewModel.albumBrowse.value?.data!!.tracks.size)
args.putString("type", Config.ALBUM_CLICK)
args.putString("videoId", viewModel.albumBrowse.value?.data!!.tracks[index].videoId)
args.putString("from", "Album \"${viewModel.albumBrowse.value?.data!!.title}\"")
if (viewModel.albumEntity.value?.downloadState == DownloadState.STATE_DOWNLOADED) {
args.putInt("downloaded", 1)
}
Queue.clear()
Queue.setNowPlaying(viewModel.albumBrowse.value?.data!!.tracks[index])
val shuffleList: ArrayList<Track> = arrayListOf()
shuffleList.addAll(viewModel.albumBrowse.value?.data!!.tracks)
shuffleList.remove(viewModel.albumBrowse.value?.data!!.tracks[index])
shuffleList.shuffle()
Queue.addAll(shuffleList)
findNavController().navigateSafe(R.id.action_global_nowPlayingFragment, args)
}
else if (viewModel.albumEntity.value != null && viewModel.albumEntity.value?.downloadState == DownloadState.STATE_DOWNLOADED){
val args = Bundle()
val index = Random.nextInt(viewModel.albumEntity.value?.tracks?.size!!)
args.putString("type", Config.ALBUM_CLICK)
args.putString("videoId", viewModel.albumEntity.value?.tracks?.get(index))
args.putString("from", "Album \"${viewModel.albumEntity.value?.title}\"")
if (viewModel.albumEntity.value?.downloadState == DownloadState.STATE_DOWNLOADED) {
args.putInt("downloaded", 1)
}
Queue.clear()
Queue.setNowPlaying(viewModel.listTrack.value?.get(index)!!.toTrack())
val shuffleList: ArrayList<Track> = arrayListOf()
shuffleList.addAll(viewModel.listTrack.value.toArrayListTrack())
shuffleList.remove(viewModel.listTrack.value?.get(index)!!.toTrack())
shuffleList.shuffle()
Queue.addAll(shuffleList)
findNavController().navigateSafe(R.id.action_global_nowPlayingFragment, args)
}
else {
Snackbar.make(requireView(), "Error", Snackbar.LENGTH_SHORT).show()
}
}
binding.btPlayPause.setOnClickListener {
if (viewModel.albumBrowse.value is Resource.Success && viewModel.albumBrowse.value?.data != null){
val args = Bundle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import java.time.format.DateTimeFormatter
import kotlin.math.abs
import kotlin.random.Random

@AndroidEntryPoint
class LocalPlaylistFragment : Fragment() {
Expand Down Expand Up @@ -218,6 +219,32 @@ class LocalPlaylistFragment : Fragment() {
Snackbar.make(requireView(), getString(R.string.playlist_is_empty), Snackbar.LENGTH_SHORT).show()
}
}
binding.btShuffle.setOnClickListener {
if (listTrack.isNotEmpty()) {
val args = Bundle()
val index = Random.nextInt(listTrack.size)
args.putString("type", Config.ALBUM_CLICK)
args.putString("videoId", (listTrack[index] as SongEntity).videoId)
args.putString("from", "Playlist \"${(viewModel.localPlaylist.value)?.title}\"")
if (viewModel.localPlaylist.value?.downloadState == DownloadState.STATE_DOWNLOADED) {
args.putInt("downloaded", 1)
}
Queue.clear()
Queue.setNowPlaying((listTrack[index] as SongEntity).toTrack())
val tempList: ArrayList<Track> = arrayListOf()
for (i in listTrack) {
if (i != listTrack[index]) {
tempList.add((i as SongEntity).toTrack())
}
}
tempList.shuffle()
Queue.addAll(tempList)
findNavController().navigateSafe(R.id.action_global_nowPlayingFragment, args)
}
else {
Snackbar.make(requireView(), getString(R.string.playlist_is_empty), Snackbar.LENGTH_SHORT).show()
}
}

binding.btDownload.setOnClickListener {
if (viewModel.localPlaylist.value?.downloadState == DownloadState.STATE_NOT_DOWNLOADED) {
Expand Down Expand Up @@ -311,7 +338,9 @@ class LocalPlaylistFragment : Fragment() {
}
}
if (localPlaylist != null) {
binding.topAppBar.title = localPlaylist.title
binding.collapsingToolbarLayout.title = localPlaylist.title
binding.tvTitle.text = localPlaylist.title
binding.tvTitle.isSelected = true
if (localPlaylist.syncedWithYouTubePlaylist == 1 && localPlaylist.youtubePlaylistId != null) {
if (!localPlaylist.tracks.isNullOrEmpty()) {
viewModel.getSetVideoId(localPlaylist.youtubePlaylistId)
Expand Down Expand Up @@ -425,6 +454,23 @@ class LocalPlaylistFragment : Fragment() {
moreDialog.setContentView(moreDialogView.root)
moreDialog.show()
}
binding.topAppBarLayout.addOnOffsetChangedListener { it, verticalOffset ->
if(abs(it.totalScrollRange) == abs(verticalOffset)) {
binding.topAppBar.background = viewModel.gradientDrawable.value
binding.collapsingToolbarLayout.isTitleEnabled = true
if (viewModel.gradientDrawable.value != null ){
if (viewModel.gradientDrawable.value?.colors != null){
requireActivity().window.statusBarColor = viewModel.gradientDrawable.value?.colors!!.first()
}
}
}
else
{
binding.collapsingToolbarLayout.isTitleEnabled = false
binding.topAppBar.background = null
requireActivity().window.statusBarColor = ContextCompat.getColor(requireContext(), R.color.colorPrimaryDark)
}
}

lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
Expand Down Expand Up @@ -482,7 +528,9 @@ class LocalPlaylistFragment : Fragment() {
}
}
if (localPlaylist != null) {
binding.topAppBar.title = localPlaylist.title
binding.collapsingToolbarLayout.title = localPlaylist.title
binding.tvTitle.text = localPlaylist.title
binding.tvTitle.isSelected = true
if (localPlaylist.syncedWithYouTubePlaylist == 1 && localPlaylist.youtubePlaylistId != null) {
if (!localPlaylist.tracks.isNullOrEmpty()) {
viewModel.getSetVideoId(localPlaylist.youtubePlaylistId)
Expand Down Expand Up @@ -527,7 +575,9 @@ class LocalPlaylistFragment : Fragment() {
private fun fetchDataFromViewModel() {
Log.d("Check", "fetchDataFromViewModel: ${viewModel.localPlaylist.value}")
val localPlaylist = viewModel.localPlaylist.value!!
binding.topAppBar.title = localPlaylist.title
binding.collapsingToolbarLayout.title = localPlaylist.title
binding.tvTitle.text = localPlaylist.title
binding.tvTitle.isSelected = true
binding.tvTrackCountAndTimeCreated.text = getString(R.string.album_length, localPlaylist.tracks?.size.toString(), localPlaylist.inLibrary.format(
DateTimeFormatter.ofPattern("HH:mm:ss dd/MM/yyyy")
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlin.math.abs
import kotlin.random.Random

@AndroidEntryPoint
class PlaylistFragment: Fragment() {
Expand Down Expand Up @@ -495,6 +496,55 @@ class PlaylistFragment: Fragment() {
requireActivity().window.statusBarColor = ContextCompat.getColor(requireContext(), R.color.colorPrimaryDark)
}
}
binding.btShuffle.setOnClickListener {
if (viewModel.playlistBrowse.value is Resource.Success && viewModel.playlistBrowse.value?.data != null){
val args = Bundle()
args.putString("type", Config.PLAYLIST_CLICK)
val index = Random.nextInt(0, viewModel.playlistBrowse.value?.data!!.tracks.size - 1)
args.putString("videoId", viewModel.playlistBrowse.value?.data?.tracks?.get(index)?.videoId)
args.putString("from", "Playlist \"${viewModel.playlistBrowse.value?.data?.title}\"")
if (viewModel.playlistEntity.value?.downloadState == DownloadState.STATE_DOWNLOADED) {
args.putInt("downloaded", 1)
}
Queue.clear()
Queue.setNowPlaying(viewModel.playlistBrowse.value?.data!!.tracks[index])
val shuffleList: ArrayList<Track> = arrayListOf()
viewModel.playlistBrowse.value?.data?.tracks?.let {
shuffleList.addAll(it)
}
shuffleList.remove(viewModel.playlistBrowse.value?.data?.tracks?.get(index))
shuffleList.shuffle()
Queue.addAll(shuffleList)
Log.d("PlaylistFragment", "Queue: ${Queue.getQueue().size}")
findNavController().navigateSafe(R.id.action_global_nowPlayingFragment, args)
}
else if (viewModel.playlistEntity.value != null && viewModel.playlistEntity.value?.downloadState == DownloadState.STATE_DOWNLOADED){
val args = Bundle()
args.putString("type", Config.PLAYLIST_CLICK)
val index = Random.nextInt(0,
viewModel.playlistEntity.value?.tracks?.size?.minus(1) ?: 0
)
args.putString("videoId", viewModel.playlistEntity.value?.tracks?.get(index))
args.putString("from", "Playlist \"${viewModel.playlistEntity.value?.title}\"")
if (viewModel.playlistEntity.value?.downloadState == DownloadState.STATE_DOWNLOADED) {
args.putInt("downloaded", 1)
}
Queue.clear()
Queue.setNowPlaying(viewModel.listTrack.value?.get(index)!!.toTrack())
val shuffleList: ArrayList<Track> = arrayListOf()
viewModel.listTrack.value?.toArrayListTrack()
?.let { it1 -> shuffleList.addAll(it1) }
viewModel.listTrack.value?.get(index)?.let { shuffleList.remove(it.toTrack()) }
shuffleList.shuffle()
Queue.addAll(shuffleList)
Log.d("PlaylistFragment", "Queue: ${Queue.getQueue().size}")
findNavController().navigateSafe(R.id.action_global_nowPlayingFragment, args)
}
else {
Snackbar.make(requireView(),
getString(R.string.playlist_is_empty), Snackbar.LENGTH_SHORT).show()
}
}
binding.btDownload.setOnClickListener {
if (viewModel.playlistEntity.value?.downloadState == DownloadState.STATE_NOT_DOWNLOADED) {
// if (!viewModel.prevPlaylistDownloading.value){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,7 @@ class NowPlayingFragment : Fragment() {
launch {
viewModel.simpleMediaServiceHandler?.nowPlaying?.collectLatest { song ->
if (song != null) {
viewModel.resetLyrics()
viewModel.getFormat(song.mediaId)
// viewModel.getFormat(song.mediaId)
Log.i("Now Playing Fragment", "song ${song.mediaMetadata.title}")
videoId = viewModel.videoId.value
binding.ivArt.visibility = View.GONE
Expand Down Expand Up @@ -671,12 +670,6 @@ class NowPlayingFragment : Fragment() {
binding.tvUploader.text = format.uploader
binding.ivAuthor.load(format.uploaderThumbnail)
binding.tvSubCount.text = format.uploaderSubCount
viewModel.resetLyrics()
Log.w("Check Youtube Captions URL", format.youtubeCaptionsUrl.toString())
Log.w("Check CPN", format.cpn.toString())
format.lengthSeconds?.let {
viewModel.getLyricsFromFormat(format.videoId, it)
}
}
else {
binding.uploaderLayout.visibility = View.GONE
Expand Down Expand Up @@ -1272,7 +1265,7 @@ class NowPlayingFragment : Fragment() {
Log.d("CHECK QUEUE", "updateUIfromQueueNowPlaying: ${Queue.getQueue()}")
val nowPlaying = Queue.getNowPlaying()
if (nowPlaying != null) {
viewModel.getFormat(nowPlaying.videoId)
// viewModel.getFormat(nowPlaying.videoId)
binding.ivArt.visibility = View.GONE
binding.loadingArt.visibility = View.VISIBLE
Log.d("Update UI", "current: ${nowPlaying.title}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class LibraryViewModel @Inject constructor(private val mainRepository: MainRepos
val temp: MutableList<Any> = mutableListOf<Any>()
mainRepository.getAllRecentData().collect {data ->
temp.addAll(data)
temp.find {
it is PlaylistEntity && (it.id.contains("RDEM") || it.id.contains("RDAMVM"))
}.let {
temp.remove(it)
}
_listRecentlyAdded.postValue(temp)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ class LocalPlaylistViewModel @Inject constructor(
fun getListTrack(list: List<String>) {
viewModelScope.launch {
mainRepository.getSongsByListVideoId(list).collect {
_listTrack.postValue(it)
val temp: ArrayList<SongEntity> = arrayListOf()
var count = 0
it.forEach { track ->
temp.add(track)
if (track.downloadState == DownloadState.STATE_DOWNLOADED) {
count++
}
}
_listTrack.postValue(temp)
if (count == it.size && localPlaylist.value?.downloadState != DownloadState.STATE_DOWNLOADED) {
updatePlaylistDownloadState(id.value!!, DownloadState.STATE_DOWNLOADED)
getLocalPlaylist(id.value!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,6 @@ class SharedViewModel @Inject constructor(private var dataStoreManager: DataStor
_duration.value = mediaState.duration
calculateProgressValues(simpleMediaServiceHandler!!.getProgress())
_uiState.value = UIState.Ready
if (_format.value == null || _format.value?.videoId != simpleMediaServiceHandler!!.nowPlaying.first()?.mediaId) {
getFormat(simpleMediaServiceHandler!!.nowPlaying.first()?.mediaId)
}
}
}
}
Expand Down Expand Up @@ -271,6 +268,13 @@ class SharedViewModel @Inject constructor(private var dataStoreManager: DataStor
}
}
}
val job8 = launch {
duration.collect {
if (it > 0) {
getFormat(simpleMediaServiceHandler!!.nowPlaying.first()?.mediaId)
}
}
}
val job7 = launch {
format.collect {formatTemp ->
if (dataStoreManager.sendBackToGoogle.first() == TRUE) {
Expand All @@ -279,6 +283,12 @@ class SharedViewModel @Inject constructor(private var dataStoreManager: DataStor
initPlayback(formatTemp.playbackTrackingVideostatsPlaybackUrl, formatTemp.playbackTrackingAtrUrl, formatTemp.playbackTrackingVideostatsWatchtimeUrl, formatTemp.cpn)
}
}
resetLyrics()
Log.w("Check Youtube Captions URL", formatTemp?.youtubeCaptionsUrl.toString())
Log.w("Check CPN", formatTemp?.cpn.toString())
formatTemp?.lengthSeconds?.let {
getLyricsFromFormat(formatTemp.videoId, it)
}
}
}

Expand All @@ -288,6 +298,7 @@ class SharedViewModel @Inject constructor(private var dataStoreManager: DataStor
job4.join()
job6.join()
job7.join()
job8.join()
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/layout/fragment_album.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@
android:visibility="gone">

</com.airbnb.lottie.LottieAnimationView>
<ImageButton
android:id="@+id/btShuffle"
android:layout_width="35sp"
android:layout_height="35sp"
android:layout_centerVertical="true"
android:background="@android:color/transparent"
android:scaleType="centerCrop"
android:src="@drawable/baseline_shuffle_24"
android:layout_alignParentEnd="true"
android:layout_toStartOf="@+id/btMore"
android:layout_marginEnd="5dp">

</ImageButton>

<ImageButton
android:id="@+id/btMore"
Expand Down
Loading

0 comments on commit 01fa384

Please sign in to comment.