Fixed Playlist save
This commit is contained in:
parent
9a32aa2805
commit
e9b7b5a203
5 changed files with 129 additions and 33 deletions
|
@ -21,13 +21,16 @@ import android.widget.Toast
|
|||
import androidx.core.os.bundleOf
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import code.name.monkey.appthemehelper.util.VersionUtils
|
||||
import code.name.monkey.retromusic.App
|
||||
import code.name.monkey.retromusic.EXTRA_PLAYLIST
|
||||
import code.name.monkey.retromusic.R
|
||||
import code.name.monkey.retromusic.db.PlaylistWithSongs
|
||||
import code.name.monkey.retromusic.extensions.colorButtons
|
||||
import code.name.monkey.retromusic.extensions.createNewFile
|
||||
import code.name.monkey.retromusic.extensions.extraNotNull
|
||||
import code.name.monkey.retromusic.extensions.materialDialog
|
||||
import code.name.monkey.retromusic.helper.M3UWriter
|
||||
import code.name.monkey.retromusic.util.PlaylistsUtil
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
@ -46,22 +49,59 @@ class SavePlaylistDialog : DialogFragment() {
|
|||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
val playlistWithSongs = extraNotNull<PlaylistWithSongs>(EXTRA_PLAYLIST).value
|
||||
val file = PlaylistsUtil.savePlaylistWithSongs(playlistWithSongs)
|
||||
MediaScannerConnection.scanFile(
|
||||
requireActivity(),
|
||||
arrayOf<String>(file.path),
|
||||
null
|
||||
) { _, _ ->
|
||||
val playlistWithSongs = extraNotNull<PlaylistWithSongs>(EXTRA_PLAYLIST).value
|
||||
|
||||
if (VersionUtils.hasR()) {
|
||||
createNewFile(
|
||||
"audio/mpegurl",
|
||||
playlistWithSongs.playlistEntity.playlistName
|
||||
) { outputStream, data ->
|
||||
try {
|
||||
if (outputStream != null) {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
M3UWriter.writeIO(
|
||||
outputStream,
|
||||
playlistWithSongs
|
||||
)
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(
|
||||
requireContext(),
|
||||
String.format(
|
||||
requireContext().getString(R.string.saved_playlist_to),
|
||||
data?.lastPathSegment
|
||||
),
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Toast.makeText(
|
||||
context,
|
||||
"Something went wrong : " + e.message,
|
||||
Toast.LENGTH_SHORT
|
||||
)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(
|
||||
requireContext(),
|
||||
String.format(App.getContext().getString(R.string.saved_playlist_to), file),
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
dismiss()
|
||||
} else {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
val file = PlaylistsUtil.savePlaylistWithSongs(playlistWithSongs)
|
||||
MediaScannerConnection.scanFile(
|
||||
requireActivity(),
|
||||
arrayOf<String>(file.path),
|
||||
null
|
||||
) { _, _ ->
|
||||
}
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(
|
||||
requireContext(),
|
||||
String.format(App.getContext().getString(R.string.saved_playlist_to), file),
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue