Skip to content

Commit

Permalink
Improved Local Playlist Screen
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Nov 30, 2024
1 parent f03f20c commit 837ff62
Show file tree
Hide file tree
Showing 27 changed files with 2,093 additions and 87 deletions.

Large diffs are not rendered by default.

971 changes: 971 additions & 0 deletions app/schemas/com.maxrave.simpmusic.data.db.MusicDatabase/12.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.maxrave.simpmusic.data.db

import androidx.room.DeleteColumn
import androidx.room.DeleteTable
import androidx.room.migration.AutoMigrationSpec
import androidx.sqlite.db.SupportSQLiteDatabase
Expand All @@ -14,4 +15,12 @@ class AutoMigration7_8 : AutoMigrationSpec {
super.onPostMigrate(db)
db.execSQL("DROP TABLE IF EXISTS `format`")
}
}
}

@DeleteColumn.Entries(
DeleteColumn(
tableName = "local_playlist",
columnName = "synced_with_youtube_playlist"
)
)
class AutoMigration11_12: AutoMigrationSpec
Original file line number Diff line number Diff line change
Expand Up @@ -348,19 +348,13 @@ interface DatabaseDao {
youtubePlaylistId: String?,
)

@Query("UPDATE local_playlist SET synced_with_youtube_playlist = :synced WHERE id = :id")
suspend fun updateLocalPlaylistYouTubePlaylistSynced(
id: Long,
synced: Int,
)

@Query("UPDATE local_playlist SET youtube_sync_state = :state WHERE id = :id")
suspend fun updateLocalPlaylistYouTubePlaylistSyncState(
id: Long,
state: Int,
)

@Query("UPDATE local_playlist SET synced_with_youtube_playlist = 0, youtube_sync_state = 0, youtubePlaylistId = NULL WHERE id = :id")
@Query("UPDATE local_playlist SET youtube_sync_state = 0, youtubePlaylistId = NULL WHERE id = :id")
suspend fun unsyncLocalPlaylist(id: Long)

@Query("SELECT downloadState FROM local_playlist WHERE id = :id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,6 @@ class LocalDataSource(
ytId: String?,
) = databaseDao.updateLocalPlaylistYouTubePlaylistId(id, ytId)

suspend fun updateLocalPlaylistYouTubePlaylistSynced(
id: Long,
synced: Int,
) = databaseDao.updateLocalPlaylistYouTubePlaylistSynced(id, synced)

suspend fun updateLocalPlaylistYouTubePlaylistSyncState(
id: Long,
syncState: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import com.maxrave.simpmusic.data.db.entities.SongInfoEntity
SetVideoIdEntity::class, PairSongLocalPlaylist::class, GoogleAccountEntity::class, FollowedArtistSingleAndAlbum::class,
NotificationEntity::class,
],
version = 11,
version = 12,
exportSchema = true,
autoMigrations = [
AutoMigration(from = 2, to = 3), AutoMigration(
Expand All @@ -42,6 +42,7 @@ import com.maxrave.simpmusic.data.db.entities.SongInfoEntity
spec = AutoMigration7_8::class,
), AutoMigration(8, 9),
AutoMigration(9, 10),
AutoMigration(from = 11, to = 12, spec = AutoMigration11_12::class)
],
)
@TypeConverters(Converters::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ data class LocalPlaylistEntity(
val thumbnail: String? = null,
val inLibrary: LocalDateTime = LocalDateTime.now(),
val downloadState: Int = DownloadState.STATE_NOT_DOWNLOADED,
@ColumnInfo(name = "synced_with_youtube_playlist", defaultValue = "0")
val syncedWithYouTubePlaylist: Int = 0,
val youtubePlaylistId: String? = null,
@ColumnInfo(name = "youtube_sync_state", defaultValue = "0")
val syncState: Int = YouTubeSyncState.NotSynced,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.maxrave.kotlinytmusicscraper.YouTube
import com.maxrave.kotlinytmusicscraper.extension.verifyYouTubePlaylistId
import com.maxrave.kotlinytmusicscraper.models.MusicShelfRenderer
import com.maxrave.simpmusic.R
import com.maxrave.simpmusic.common.DownloadState
Expand Down Expand Up @@ -170,7 +171,6 @@ class LocalPlaylistManager(
title = playlist.title,
thumbnail = playlist.thumbnails.lastOrNull()?.url,
youtubePlaylistId = playlist.id,
syncedWithYouTubePlaylist = 1,
tracks = playlist.tracks.toListVideoId(),
downloadState = DownloadState.STATE_NOT_DOWNLOADED,
syncState = Syncing,
Expand All @@ -195,7 +195,7 @@ class LocalPlaylistManager(
}
}
val ytPlaylistId = playlist.id
val id = if (ytPlaylistId.startsWith("VL")) ytPlaylistId else "VL$ytPlaylistId"
val id = ytPlaylistId.verifyYouTubePlaylistId()
YouTube
.customQuery(browseId = id, setLogin = true)
.onSuccess { res ->
Expand Down Expand Up @@ -255,15 +255,14 @@ class LocalPlaylistManager(
parsed.forEach { setVideoId ->
localDataSource.insertSetVideoId(setVideoId)
}
localDataSource.updateLocalPlaylistYouTubePlaylistSyncState(
localPlaylistId,
Synced,
)
emit(LocalResource.Success(getString(R.string.synced)))
}.onFailure {
emit(LocalResource.Error("Can't get setVideoId"))
}
localDataSource.updateLocalPlaylistYouTubePlaylistSyncState(
localPlaylistId,
Synced,
)
emit(LocalResource.Success(getString(R.string.synced)))
}.flowOn(
Dispatchers.IO,
)
Expand All @@ -274,15 +273,43 @@ class LocalPlaylistManager(
* @param playlistId
* @return Flow<LocalResource<String>>
*/
suspend fun syncLocalPlaylistToYouTubePlaylist(playlistId: Long) =
wrapResultResource {
fun syncLocalPlaylistToYouTubePlaylist(playlistId: Long) =
flow<LocalResource<String>> {
emit(LocalResource.Loading())
val playlist = localDataSource.getLocalPlaylist(playlistId)
YouTube
.createPlaylist(
val res = YouTube.createPlaylist(
playlist.title,
playlist.tracks,
).map {
it.playlistId
)
val value = res.getOrNull()
if (res.isSuccess && value != null) {
val ytId = value.playlistId
Log.d(tag, "syncLocalPlaylistToYouTubePlaylist: $ytId")
YouTube.getYouTubePlaylistFullTracksWithSetVideoId(ytId).onSuccess { list ->
Log.d(tag, "syncLocalPlaylistToYouTubePlaylist: onSuccess song ${list.map { it.first.title }}")
Log.d(tag, "syncLocalPlaylistToYouTubePlaylist: onSuccess setVideoId ${list.map { it.second }}")
list.forEach { new ->
localDataSource.insertSong(new.first.toTrack().toSongEntity())
localDataSource.insertSetVideoId(
SetVideoIdEntity(
videoId = new.first.id,
setVideoId = new.second,
youtubePlaylistId = ytId
)
)
}
if (list.isEmpty()) Log.w(tag, "syncLocalPlaylistToYouTubePlaylist: SetVideoIds Empty list")
localDataSource.updateLocalPlaylistYouTubePlaylistId(playlistId, ytId)
localDataSource.updateLocalPlaylistYouTubePlaylistSyncState(playlistId, Synced)
Log.d(tag, "syncLocalPlaylistToYouTubePlaylist: $ytId")
emit(LocalResource.Success(ytId))
}.onFailure {
emit(LocalResource.Error(it.message ?: getString(R.string.error)))
}
} else {
val e = res.exceptionOrNull()
e?.printStackTrace()
emit(LocalResource.Error(e?.message ?: getString(R.string.error)))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.util.Log
import androidx.media3.common.util.UnstableApi
import com.maxrave.kotlinytmusicscraper.YouTube
import com.maxrave.kotlinytmusicscraper.extension.verifyYouTubePlaylistId
import com.maxrave.kotlinytmusicscraper.models.AccountInfo
import com.maxrave.kotlinytmusicscraper.models.MediaType
import com.maxrave.kotlinytmusicscraper.models.MusicShelfRenderer
Expand Down Expand Up @@ -494,13 +495,6 @@ class MainRepository(
localDataSource.updateLocalPlaylistYouTubePlaylistId(id, ytId)
}

suspend fun updateLocalPlaylistYouTubePlaylistSynced(
id: Long,
synced: Int,
) = withContext(Dispatchers.IO) {
localDataSource.updateLocalPlaylistYouTubePlaylistSynced(id, synced)
}

suspend fun updateLocalPlaylistYouTubePlaylistSyncState(
id: Long,
syncState: Int,
Expand Down Expand Up @@ -3058,7 +3052,7 @@ class MainRepository(
) = flow {
runCatching {
YouTube
.addPlaylistItem(youtubePlaylistId, videoId)
.addPlaylistItem(youtubePlaylistId.verifyYouTubePlaylistId(), videoId)
.onSuccess {
if (it.playlistEditResults.isNotEmpty()) {
for (playlistEditResult in it.playlistEditResults) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.google.gson.reflect.TypeToken
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.maxrave.simpmusic.common.DB_NAME
import com.maxrave.simpmusic.data.dataStore.DataStoreManager
import com.maxrave.simpmusic.data.db.Converters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ class SearchFragment : Fragment() {
if (playlist.tracks != null) {
tempTrack.addAll(playlist.tracks)
}
if (!tempTrack.contains(track.videoId) && playlist.syncedWithYouTubePlaylist == 1 && playlist.youtubePlaylistId != null) {
if (!tempTrack.contains(track.videoId) && playlist.syncState == LocalPlaylistEntity.YouTubeSyncState.Synced && playlist.youtubePlaylistId != null) {
viewModel.addToYouTubePlaylist(playlist.id, playlist.youtubePlaylistId, track.videoId)
}
if (!tempTrack.contains(track.videoId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class HomeFragment : Fragment() {
// if (playlist.tracks != null) {
// tempTrack.addAll(playlist.tracks)
// }
// if (!tempTrack.contains(track.videoId) && playlist.syncedWithYouTubePlaylist == 1 && playlist.youtubePlaylistId != null) {
// if (!tempTrack.contains(track.videoId) && playlist.syncState == LocalPlaylistEntity.YouTubeSyncState.Synced && playlist.youtubePlaylistId != null) {
// viewModel.addToYouTubePlaylist(
// playlist.id,
// playlist.youtubePlaylistId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import androidx.media3.exoplayer.offline.DownloadService
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import coil3.load
import coil3.request.crossfade
import coil3.request.placeholder
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.maxrave.simpmusic.R
import com.maxrave.simpmusic.adapter.artist.SeeArtistOfNowPlayingAdapter
Expand Down Expand Up @@ -263,7 +261,9 @@ class DownloadedFragment : Fragment() {
if (playlist.tracks != null) {
tempTrack.addAll(playlist.tracks)
}
if (!tempTrack.contains(song.videoId) && playlist.syncedWithYouTubePlaylist == 1 && playlist.youtubePlaylistId != null) {
if (!tempTrack.contains(song.videoId) &&
playlist.syncState == LocalPlaylistEntity.YouTubeSyncState.Synced &&
playlist.youtubePlaylistId != null) {
viewModel.addToYouTubePlaylist(playlist.id, playlist.youtubePlaylistId, song.videoId)
}
if (!tempTrack.contains(song.videoId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class FavoriteFragment : Fragment() {
tempTrack.addAll(playlist.tracks)
}
if (!tempTrack.contains(song.videoId) &&
playlist.syncedWithYouTubePlaylist == 1 &&
playlist.syncState == LocalPlaylistEntity.YouTubeSyncState.Synced &&
playlist.youtubePlaylistId != null
) {
viewModel.addToYouTubePlaylist(playlist.id, playlist.youtubePlaylistId, song.videoId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ class LibraryFragment : Fragment() {
// }
// if (!tempTrack.contains(
// song.videoId,
// ) && playlist.syncedWithYouTubePlaylist == 1 && playlist.youtubePlaylistId != null
// ) && playlist.syncState == LocalPlaylistEntity.YouTubeSyncState.Synced && playlist.youtubePlaylistId != null
// ) {
// viewModel.addToYouTubePlaylist(playlist.id, playlist.youtubePlaylistId, song.videoId)
// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class MostPlayedFragment: Fragment() {
if (playlist.tracks != null) {
tempTrack.addAll(playlist.tracks)
}
if (!tempTrack.contains(song.videoId) && playlist.syncedWithYouTubePlaylist == 1 && playlist.youtubePlaylistId != null) {
if (!tempTrack.contains(song.videoId) && playlist.syncState == LocalPlaylistEntity.YouTubeSyncState.Synced && playlist.youtubePlaylistId != null) {
viewModel.addToYouTubePlaylist(playlist.id, playlist.youtubePlaylistId, song.videoId)
}
if (!tempTrack.contains(song.videoId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ class AlbumFragment : Fragment() {
if (!tempTrack.contains(
song.videoId,
) &&
playlist.syncedWithYouTubePlaylist == 1 &&
playlist.syncState == LocalPlaylistEntity.YouTubeSyncState.Synced &&
playlist.youtubePlaylistId != null
) {
viewModel.addToYouTubePlaylist(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ class ArtistFragment : Fragment() {
tempTrack.addAll(playlist.tracks)
}
if (!tempTrack.contains(song.videoId) &&
playlist.syncedWithYouTubePlaylist == 1 &&
playlist.syncState == LocalPlaylistEntity.YouTubeSyncState.Synced &&
playlist.youtubePlaylistId != null
) {
viewModel.addToYouTubePlaylist(playlist.id, playlist.youtubePlaylistId, song.videoId)
Expand Down
Loading

0 comments on commit 837ff62

Please sign in to comment.