Skip to content

Commit

Permalink
Implemented share RiMusic local playlist cached, preferites, download…
Browse files Browse the repository at this point in the history
…ed via export/import. Exportable selected or all songs #784
  • Loading branch information
fast4x committed Feb 27, 2024
1 parent 77b8775 commit 75fc6f7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package it.vfsfitvnm.vimusic.ui.screens.builtinplaylist

import android.annotation.SuppressLint
import android.content.ActivityNotFoundException
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.LinearEasing
Expand Down Expand Up @@ -70,6 +73,7 @@ import androidx.media3.common.MediaItem
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.offline.Download
import coil.compose.AsyncImage
import com.github.doyaaaaaken.kotlincsv.dsl.csvWriter
import it.vfsfitvnm.compose.persist.persist
import it.vfsfitvnm.compose.persist.persistList
import it.vfsfitvnm.compose.reordering.reorder
Expand Down Expand Up @@ -137,6 +141,9 @@ import it.vfsfitvnm.vimusic.ui.components.themed.NowPlayingShow
import it.vfsfitvnm.vimusic.ui.components.themed.PlaylistsItemMenu
import it.vfsfitvnm.vimusic.ui.components.themed.SortMenu
import it.vfsfitvnm.vimusic.ui.styling.favoritesIcon
import it.vfsfitvnm.vimusic.utils.toast
import java.text.SimpleDateFormat
import java.util.Date

@ExperimentalTextApi
@SuppressLint("SuspiciousIndentation", "StateFlowValueCalledInComposition")
Expand Down Expand Up @@ -286,13 +293,51 @@ fun BuiltInPlaylistSongs(
mutableIntStateOf(0)
}

val exportLauncher =
rememberLauncherForActivityResult(ActivityResultContracts.CreateDocument("text/csv")) { uri ->
if (uri == null) return@rememberLauncherForActivityResult

context.applicationContext.contentResolver.openOutputStream(uri)
?.use { outputStream ->
csvWriter().open(outputStream){
writeRow("PlaylistBrowseId", "PlaylistName", "MediaId", "Title", "Artists", "Duration", "ThumbnailUrl")
if (listMediaItems.isEmpty()) {
songs.forEach {
writeRow(
"",
when (builtInPlaylist) {
BuiltInPlaylist.Favorites -> context.resources.getString(R.string.favorites)
BuiltInPlaylist.Downloaded -> context.resources.getString(R.string.downloaded)
BuiltInPlaylist.Offline -> context.resources.getString(R.string.cached)
},
it.id,
it.title,
it.artistsText,
it.durationText,
it.thumbnailUrl
)
}
} else {
listMediaItems.forEach {
writeRow(
"",
when (builtInPlaylist) {
BuiltInPlaylist.Favorites -> context.resources.getString(R.string.favorites)
BuiltInPlaylist.Downloaded -> context.resources.getString(R.string.downloaded)
BuiltInPlaylist.Offline -> context.resources.getString(R.string.cached)
},
it.mediaId,
it.mediaMetadata.title,
it.mediaMetadata.artist,
"",
it.mediaMetadata.artworkUri
)
}
}
}
}

/*
var showSortTypeSelectDialog by remember {
mutableStateOf(false)
}
*/

}


Box {
Expand Down Expand Up @@ -602,6 +647,21 @@ fun BuiltInPlaylistSongs(
selectItems = false
}
},
onExport = {
try {
@SuppressLint("SimpleDateFormat")
val dateFormat = SimpleDateFormat("yyyyMMddHHmmss")
exportLauncher.launch("RMPlaylist_${when (builtInPlaylist) {
BuiltInPlaylist.Favorites -> context.resources.getString(R.string.favorites)
BuiltInPlaylist.Downloaded -> context.resources.getString(R.string.downloaded)
BuiltInPlaylist.Offline -> context.resources.getString(R.string.cached)
}.take(20)}_${dateFormat.format(
Date()
)}")
} catch (e: ActivityNotFoundException) {
context.toast("Couldn't find an application to create documents")
}
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,11 @@ import it.vfsfitvnm.vimusic.enums.RecommendationsNumber
import it.vfsfitvnm.vimusic.enums.SortOrder
import it.vfsfitvnm.vimusic.enums.ThumbnailRoundness
import it.vfsfitvnm.vimusic.enums.UiType
import it.vfsfitvnm.vimusic.internal
import it.vfsfitvnm.vimusic.models.Playlist
import it.vfsfitvnm.vimusic.models.PlaylistPreview
import it.vfsfitvnm.vimusic.models.Song
import it.vfsfitvnm.vimusic.models.SongPlaylistMap
import it.vfsfitvnm.vimusic.path
import it.vfsfitvnm.vimusic.query
import it.vfsfitvnm.vimusic.service.MyDownloadService
import it.vfsfitvnm.vimusic.service.PlayerService
import it.vfsfitvnm.vimusic.service.isLocal
import it.vfsfitvnm.vimusic.transaction
import it.vfsfitvnm.vimusic.ui.components.LocalMenuState
Expand Down Expand Up @@ -142,7 +138,6 @@ import it.vfsfitvnm.vimusic.utils.forcePlayAtIndex
import it.vfsfitvnm.vimusic.utils.forcePlayFromBeginning
import it.vfsfitvnm.vimusic.utils.formatAsTime
import it.vfsfitvnm.vimusic.utils.getDownloadState
import it.vfsfitvnm.vimusic.utils.intent
import it.vfsfitvnm.vimusic.utils.isRecommendationEnabledKey
import it.vfsfitvnm.vimusic.utils.manageDownload
import it.vfsfitvnm.vimusic.utils.playlistSongSortByKey
Expand Down Expand Up @@ -405,14 +400,37 @@ fun LocalPlaylistSongs(
?.use { outputStream ->
csvWriter().open(outputStream){
writeRow("PlaylistBrowseId", "PlaylistName", "MediaId", "Title", "Artists", "Duration", "ThumbnailUrl")
playlistSongs.forEach{
writeRow(playlistPreview?.playlist?.browseId,playlistPreview?.playlist?.name,it.id,it.title,it.artistsText,it.durationText,it.thumbnailUrl)
if (listMediaItems.isEmpty()) {
playlistSongs.forEach {
writeRow(
playlistPreview?.playlist?.browseId,
playlistPreview?.playlist?.name,
it.id,
it.title,
it.artistsText,
it.durationText,
it.thumbnailUrl
)
}
} else {
listMediaItems.forEach {
writeRow(
playlistPreview?.playlist?.browseId,
playlistPreview?.playlist?.name,
it.mediaId,
it.mediaMetadata.title,
it.mediaMetadata.artist,
"",
it.mediaMetadata.artworkUri
)
}
}
}
}

}


val importLauncher =
rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri ->
if (uri == null) return@rememberLauncherForActivityResult
Expand Down Expand Up @@ -818,8 +836,7 @@ fun LocalPlaylistSongs(
try {
@SuppressLint("SimpleDateFormat")
val dateFormat = SimpleDateFormat("yyyyMMddHHmmss")
exportLauncher.launch("RiMusicPlaylist_${playlistPreview.playlist.name.take(20)}_${dateFormat.format(Date())}")
context.toast("Export completed")
exportLauncher.launch("RMPlaylist_${playlistPreview.playlist.name.take(20)}_${dateFormat.format(Date())}")
} catch (e: ActivityNotFoundException) {
context.toast("Couldn't find an application to create documents")
}
Expand Down

0 comments on commit 75fc6f7

Please sign in to comment.