Skip to content

Commit

Permalink
Fixed setVideoId caused by YouTube (change API data), Fixed #204 Wron…
Browse files Browse the repository at this point in the history
…g Songs count and Position in Local Playlist, Fixed #219 Like checkbox wrong, Fixed #198
  • Loading branch information
maxrave-dev committed Jan 18, 2024
1 parent 90c0c08 commit 7ed20c7
Show file tree
Hide file tree
Showing 25 changed files with 198 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ interface DatabaseDao {
suspend fun updateLiked(liked: Int, videoId: String)

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insertSong(song: SongEntity)
suspend fun insertSong(song: SongEntity): Long

@Query("SELECT * FROM song WHERE totalPlayTime > 1 ORDER BY totalPlayTime DESC LIMIT 20")
suspend fun getMostPlayedSongs(): List<SongEntity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ fun parsePlaylistData(
fun parseSetVideoId(listContent: List<MusicShelfRenderer.Content>): ArrayList<SetVideoIdEntity> {
val listSetVideoId: ArrayList<SetVideoIdEntity> = arrayListOf()
for (content in listContent) {
val videoId =
content.musicResponsiveListItemRenderer?.flexColumns?.firstOrNull()?.musicResponsiveListItemFlexColumnRenderer?.text?.runs?.get(
0
)?.navigationEndpoint?.watchEndpoint?.videoId
val videoId = content.musicResponsiveListItemRenderer?.playlistItemData?.videoId
val setVideoId =
content.musicResponsiveListItemRenderer?.menu?.menuRenderer?.items?.find { it.menuServiceItemRenderer?.icon?.iconType == "REMOVE_FROM_PLAYLIST" }?.menuServiceItemRenderer?.serviceEndpoint?.playlistEditEndpoint?.actions?.get(
0
Expand All @@ -149,6 +146,7 @@ fun parseSetVideoId(listContent: List<MusicShelfRenderer.Content>): ArrayList<Se
Log.d("PlaylistParser", "videoId or setVideoId is null")
}
}
Log.w("PlaylistParser", "listSetVideoId: $listSetVideoId")
return listSetVideoId
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ class MainRepository @Inject constructor(private val localDataSource: LocalDataS
suspend fun getSongById(id: String): Flow<SongEntity?> =
flow { emit(localDataSource.getSong(id)) }.flowOn(Dispatchers.IO)

suspend fun insertSong(songEntity: SongEntity) =
withContext(Dispatchers.IO) { localDataSource.insertSong(songEntity) }
suspend fun insertSong(songEntity: SongEntity): Flow<Long> =
flow<Long> { emit(localDataSource.insertSong(songEntity)) }.flowOn(Dispatchers.IO)

suspend fun updateListenCount(videoId: String) =
withContext(Dispatchers.IO) { localDataSource.updateListenCount(videoId) }
Expand Down Expand Up @@ -1586,8 +1586,6 @@ class MainRepository @Inject constructor(private val localDataSource: LocalDataS
}

suspend fun getYouTubeSetVideoId(youtubePlaylistId: String): Flow<ArrayList<SetVideoIdEntity>?> = flow {
YouTube.playlist(youtubePlaylistId).onSuccess {
flow {
runCatching {
var id = ""
if (!youtubePlaylistId.startsWith("VL")) {
Expand Down Expand Up @@ -1625,6 +1623,7 @@ class MainRepository @Inject constructor(private val localDataSource: LocalDataS
}
Log.d("Repository", "playlist final data: ${listContent.size}")
parseSetVideoId(listContent).let { playlist ->
Log.d("Repository", "playlist final data setVideoId: $playlist")
playlist.forEach { item ->
insertSetVideoId(item)
}
Expand All @@ -1636,8 +1635,6 @@ class MainRepository @Inject constructor(private val localDataSource: LocalDataS
}
}
}.flowOn(Dispatchers.IO)
}
}

suspend fun createYouTubePlaylist(playlist: LocalPlaylistEntity): Flow<String?> = flow {
runCatching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,11 @@ class SearchFragment : Fragment() {
if (!tempTrack.contains(track.videoId)) {
viewModel.insertPairSongLocalPlaylist(
PairSongLocalPlaylist(
playlistId = playlist.id, songId = track.videoId, position = tempTrack.size, inPlaylist = LocalDateTime.now()
)
playlistId = playlist.id,
songId = track.videoId,
position = playlist.tracks?.size ?: 0,
inPlaylist = LocalDateTime.now()
)
)
tempTrack.add(track.videoId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ class DownloadedFragment : Fragment() {
if (!tempTrack.contains(song.videoId)) {
viewModel.insertPairSongLocalPlaylist(
PairSongLocalPlaylist(
playlistId = playlist.id, songId = song.videoId, position = tempTrack.size, inPlaylist = LocalDateTime.now()
playlistId = playlist.id,
songId = song.videoId,
position = playlist.tracks?.size ?: 0,
inPlaylist = LocalDateTime.now()
)
)
tempTrack.add(song.videoId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ class FavoriteFragment : Fragment() {
if (!tempTrack.contains(song.videoId)) {
viewModel.insertPairSongLocalPlaylist(
PairSongLocalPlaylist(
playlistId = playlist.id, songId = song.videoId, position = tempTrack.size, inPlaylist = LocalDateTime.now()
playlistId = playlist.id,
songId = song.videoId,
position = playlist.tracks?.size ?: 0,
inPlaylist = LocalDateTime.now()
)
)
tempTrack.add(song.videoId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,10 @@ class LibraryFragment : Fragment() {
if (!tempTrack.contains(song.videoId)) {
viewModel.insertPairSongLocalPlaylist(
PairSongLocalPlaylist(
playlistId = playlist.id, songId = song.videoId, position = tempTrack.size, inPlaylist = LocalDateTime.now()
playlistId = playlist.id,
songId = song.videoId,
position = playlist.tracks?.size ?: 0,
inPlaylist = LocalDateTime.now()
)
)
tempTrack.add(song.videoId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ class MostPlayedFragment: Fragment() {
if (!tempTrack.contains(song.videoId)) {
viewModel.insertPairSongLocalPlaylist(
PairSongLocalPlaylist(
playlistId = playlist.id, songId = song.videoId, position = tempTrack.size, inPlaylist = LocalDateTime.now()
playlistId = playlist.id,
songId = song.videoId,
position = playlist.tracks?.size ?: 0,
inPlaylist = LocalDateTime.now()
)
)
tempTrack.add(song.videoId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ class ArtistFragment: Fragment(){
if (!tempTrack.contains(song.videoId)) {
viewModel.insertPairSongLocalPlaylist(
PairSongLocalPlaylist(
playlistId = playlist.id, songId = song.videoId, position = tempTrack.size, inPlaylist = LocalDateTime.now()
playlistId = playlist.id,
songId = song.videoId,
position = playlist.tracks?.size ?: 0,
inPlaylist = LocalDateTime.now()
)
)
tempTrack.add(song.videoId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ class LocalPlaylistFragment : Fragment() {
}
else
{
binding.topAppBar.background = null
requireActivity().window.statusBarColor = ContextCompat.getColor(requireContext(), R.color.colorPrimaryDark)
Log.d("ArtistFragment", "Expanded")
}
Expand Down Expand Up @@ -579,7 +578,9 @@ class LocalPlaylistFragment : Fragment() {
{
binding.collapsingToolbarLayout.isTitleEnabled = false
binding.topAppBar.background = null
requireActivity().window.statusBarColor = ContextCompat.getColor(requireContext(), R.color.colorPrimaryDark)
binding.topAppBarLayout.background = viewModel.gradientDrawable.value
requireActivity().window.statusBarColor =
ContextCompat.getColor(requireContext(), R.color.colorPrimaryDark)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,10 @@ class PlaylistFragment: Fragment() {
if (!tempTrack.contains(song.videoId)) {
viewModel.insertPairSongLocalPlaylist(
PairSongLocalPlaylist(
playlistId = playlist.id, songId = song.videoId, position = tempTrack.size, inPlaylist = LocalDateTime.now()
playlistId = playlist.id,
songId = song.videoId,
position = playlist.tracks?.size ?: 0,
inPlaylist = LocalDateTime.now()
)
)
tempTrack.add(song.videoId)
Expand Down Expand Up @@ -534,11 +537,11 @@ class PlaylistFragment: Fragment() {
}
}
}
else
{
else {
binding.collapsingToolbarLayout.isTitleEnabled = false
binding.topAppBar.background = null
requireActivity().window.statusBarColor = ContextCompat.getColor(requireContext(), R.color.colorPrimaryDark)
binding.topAppBar.background = viewModel.gradientDrawable.value
requireActivity().window.statusBarColor =
ContextCompat.getColor(requireContext(), R.color.colorPrimaryDark)
}
}
binding.btShuffle.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ class FullscreenFragment : Fragment() {
PairSongLocalPlaylist(
playlistId = playlist.id,
songId = song.videoId,
position = tempTrack.size,
position = playlist.tracks?.size ?: 0,
inPlaylist = LocalDateTime.now()
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,17 +964,17 @@ class NowPlayingFragment : Fragment() {
}
binding.cbFavorite.setOnCheckedChangeListener { _, isChecked ->
if (!isChecked) {
viewModel.getCurrentMediaItem()?.let { nowPlayingSong ->
viewModel.songDB.value?.let { nowPlayingSong ->
viewModel.updateLikeStatus(
nowPlayingSong.mediaId,
nowPlayingSong.videoId,
false
)
viewModel.updateLikeInNotification(false)
}
} else {
viewModel.getCurrentMediaItem()?.let { nowPlayingSong ->
viewModel.songDB.value?.let { nowPlayingSong ->
viewModel.updateLikeStatus(
nowPlayingSong.mediaId,
nowPlayingSong.videoId,
true
)
viewModel.updateLikeInNotification(true)
Expand Down Expand Up @@ -1172,7 +1172,10 @@ class NowPlayingFragment : Fragment() {
if (!tempTrack.contains(song.videoId)) {
viewModel.insertPairSongLocalPlaylist(
PairSongLocalPlaylist(
playlistId = playlist.id, songId = song.videoId, position = tempTrack.size, inPlaylist = LocalDateTime.now()
playlistId = playlist.id,
songId = song.videoId,
position = playlist.tracks?.size ?: 0,
inPlaylist = LocalDateTime.now()
)
)
tempTrack.add(song.videoId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ class AlbumViewModel @Inject constructor(

fun insertSong(songEntity: SongEntity) {
viewModelScope.launch {
mainRepository.insertSong(songEntity)
mainRepository.insertSong(songEntity).collect {
println("Insert Song $it")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ class ArtistViewModel @Inject constructor(private val application: Application,

fun getSongEntity(song: SongEntity) {
viewModelScope.launch {
mainRepository.insertSong(song)
mainRepository.insertSong(song).first().let {
println("Insert Song $it")
}
mainRepository.getSongById(song.videoId).collect { values ->
_songEntity.value = values
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class LibraryViewModel @Inject constructor(private val mainRepository: MainRepos
fun getYouTubePlaylist() {
viewModelScope.launch {
mainRepository.getLibraryPlaylist().collect { data ->
_listYouTubePlaylist.postValue(data)
_listYouTubePlaylist.postValue(data?.reversed())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.maxrave.simpmusic.service.test.download.DownloadUtils
import com.maxrave.simpmusic.utils.Resource
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -273,6 +274,7 @@ class LocalPlaylistViewModel @Inject constructor(
)
}
mainRepository.deletePairSongLocalPlaylist(id, song.videoId)
delay(500)
// song.videoId.let {
//
// for (i in tempList.indexOf(song) until tempList.size) {
Expand Down Expand Up @@ -431,13 +433,17 @@ class LocalPlaylistViewModel @Inject constructor(
yt.data.tracks.forEach { track ->
if (!list.contains(track.videoId)) {
listTrack.add(track.videoId)
mainRepository.insertSong(track.toSongEntity())
mainRepository.insertPairSongLocalPlaylist(PairSongLocalPlaylist(
playlistId = id,
songId = track.videoId,
position = yt.data.tracks.indexOf(track),
inPlaylist = LocalDateTime.now()
))
mainRepository.insertSong(track.toSongEntity()).first().let {
println("Insert song $it")
}
mainRepository.insertPairSongLocalPlaylist(
PairSongLocalPlaylist(
playlistId = id,
songId = track.videoId,
position = yt.data.tracks.indexOf(track),
inPlaylist = LocalDateTime.now()
)
)
}
}
mainRepository.updateLocalPlaylistTracks(listTrack, id)
Expand Down Expand Up @@ -513,7 +519,9 @@ class LocalPlaylistViewModel @Inject constructor(

fun insertSong(song: Track) {
viewModelScope.launch {
mainRepository.insertSong(song.toSongEntity())
mainRepository.insertSong(song.toSongEntity()).collect {
println("Insert Song $it")
}
}
}

Expand Down
Loading

0 comments on commit 7ed20c7

Please sign in to comment.